Project

General

Profile

Download (31.7 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / mesh / MeshCubes.java @ 554ce72b

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 6c00149d Leszek Koltunski
package org.distorted.library.mesh;
21 6a06a912 Leszek Koltunski
22
import java.util.ArrayList;
23
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25 e0b6c593 Leszek Koltunski
/**
26
 * Create a 3D grid composed of Cubes.
27
 * <p>
28
 * Any subset of a MxNx1 cuboid is possible.
29
 */
30 715e7726 Leszek Koltunski
public class MeshCubes extends MeshBase
31 6a06a912 Leszek Koltunski
   {
32 06d71892 Leszek Koltunski
   private static final float R = 0.0f;//0.2f;
33 f08b268d Leszek Koltunski
34 6a06a912 Leszek Koltunski
   private static final int NORTH = 0;
35
   private static final int WEST  = 1;
36
   private static final int EAST  = 2;
37
   private static final int SOUTH = 3;
38 ce7f3833 Leszek Koltunski
39 6a06a912 Leszek Koltunski
   private static final float[] mNormalX = new float[4];
40
   private static final float[] mNormalY = new float[4];
41 ce7f3833 Leszek Koltunski
   private static final float[] mNormalZ = new float[4];
42
43 6a06a912 Leszek Koltunski
   private class Edge
44
     {
45
     final int side; 
46
     final int row;
47
     final int col;
48
     
49 06d71892 Leszek Koltunski
     Edge(int s, int r, int c)
50 6a06a912 Leszek Koltunski
       {
51
       side= s; 
52
       row = r;
53
       col = c;
54
       }
55 39cbf9dc Leszek Koltunski
     }
56 6a06a912 Leszek Koltunski
   
57 80cb15ab leszek
   private int mCols, mRows, mSlices;
58 d6994cc6 Leszek Koltunski
   private int[][] mCubes;
59 9d0df4c6 Leszek Koltunski
   private byte[][] mInflateX, mInflateY;
60 554ce72b Leszek Koltunski
   private float mInfCorrX, mInfCorrY, mInfCorrZ;
61 39cbf9dc Leszek Koltunski
   private ArrayList<Edge> mEdges = new ArrayList<>();
62 ce7f3833 Leszek Koltunski
63 15290f35 Leszek Koltunski
   private int currVert;
64 da681e7e Leszek Koltunski
   private int numVertices;
65 8d9da98a Leszek Koltunski
   private int mSideBends;
66
   private int mEdgeNum;
67 b62632fc Leszek Koltunski
   private int mSideWalls;
68 ce7f3833 Leszek Koltunski
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
// a Block is split into two triangles along the NE-SW line iff it is in the top-right
71
// or bottom-left quadrant of the grid.
72
73
   private boolean isNE(int row,int col)
74
     {
75 29dd01c6 Leszek Koltunski
     return ( (2*row<mRows)^(2*col<mCols) );
76 ce7f3833 Leszek Koltunski
     }
77
78 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
79 84ee2a6a Leszek Koltunski
// return the number of vertices our grid will contain
80 6a06a912 Leszek Koltunski
81 d1a396b2 leszek
   private int computeDataLength()
82 6a06a912 Leszek Koltunski
      {
83 b62632fc Leszek Koltunski
      int frontWalls=0, frontSegments=0, triangleShifts=0, windingShifts=0;
84 ce7f3833 Leszek Koltunski
      int shiftCol = (mCols-1)/2;
85
86
      boolean lastBlockIsNE=false;
87
      boolean thisBlockIsNE;        // the block we are currently looking at is split into
88
                                    // two triangles along the NE-SW line (rather than NW-SE)
89 29dd01c6 Leszek Koltunski
      for(int row=0; row<mRows; row++)
90 b62632fc Leszek Koltunski
        {
91
        if( mCols>=2 && (mCubes[row][shiftCol]%2 == 1) && (mCubes[row][shiftCol+1]%2 == 1) ) triangleShifts++;
92 ce7f3833 Leszek Koltunski
93 b62632fc Leszek Koltunski
        for(int col=0; col<mCols; col++)
94
          {
95
          if( mCubes[row][col]%2 == 1 )  // land
96 6a06a912 Leszek Koltunski
            {
97 b62632fc Leszek Koltunski
            thisBlockIsNE = isNE(row,col);
98
            if( thisBlockIsNE^lastBlockIsNE ) windingShifts++;
99
            lastBlockIsNE = thisBlockIsNE;
100
            frontWalls++;
101
            if( col==mCols-1 || mCubes[row][col+1]%2 == 0 ) frontSegments++;
102 6a06a912 Leszek Koltunski
            }
103 b62632fc Leszek Koltunski
          }
104
        }
105 ce7f3833 Leszek Koltunski
106 5f2853be Leszek Koltunski
      int frontVert       = 2*( frontWalls + 2*frontSegments - 1) +2*triangleShifts + windingShifts;
107
      int sideVertOneSlice= 2*( mSideWalls + mSideBends + mEdgeNum -1);
108
      int sideVert        = 2*(mSlices-1) + mSlices*sideVertOneSlice;
109
      int firstWinding    = (mSlices>0 && (frontVert+1)%2==1 ) ? 1:0;
110
      int dataL           = mSlices==0 ? frontVert : (frontVert+1) +firstWinding+ (1+sideVert+1) + (1+frontVert);
111 8d9da98a Leszek Koltunski
/*
112 b62632fc Leszek Koltunski
      android.util.Log.e("CUBES","triangleShifts="+triangleShifts+" windingShifts="+windingShifts+" winding1="+firstWinding+" frontVert="+frontVert+" sideVert="+sideVert);
113
      android.util.Log.e("CUBES", "frontW="+frontWalls+" fSegments="+frontSegments+" sWalls="+mSideWalls+" sSegments="+mEdgeNum+" sideBends="+mSideBends+" dataLen="+dataL );
114 8d9da98a Leszek Koltunski
*/
115 6a06a912 Leszek Koltunski
      return dataL<0 ? 0:dataL;
116
      }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
/*
120
   private static String debug(short[] val)
121
     {
122 ce7f3833 Leszek Koltunski
     String ret="";j
123 6a06a912 Leszek Koltunski
     
124
     for(int i=0; i<val.length; i++) ret+=(" "+val[i]); 
125
     
126
     return ret;
127
     }
128
*/
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130 e6904678 Leszek Koltunski
131 6a06a912 Leszek Koltunski
   private static String debug(float[] val, int stop)
132
     {
133
     String ret="";
134 e6904678 Leszek Koltunski
     float v;
135
     boolean neg;
136
     int mod;
137 6a06a912 Leszek Koltunski
138
     for(int i=0; i<val.length; i++) 
139
        {
140
        if( i%stop==0 ) ret+="\n";
141 e6904678 Leszek Koltunski
142
        mod = i%stop;
143
144
        if( mod==0 || mod==3 || mod==6 ) ret+=" (";
145
146
        v = val[i];
147
        if( v==-0.0f ) v=0.0f;
148
149
150
        neg = v<0;
151
        v = (v<0 ? -v:v);
152
153
        ret+=((neg? " -":" +")+v);
154
155
        if( mod==2 || mod==5 || mod==7 ) ret+=")";
156 6a06a912 Leszek Koltunski
        }
157
158
     return ret;
159
     }
160 e6904678 Leszek Koltunski
161 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
162
/*
163
   private static String debug(Edge e)
164
     {
165
     String d = "";
166
     
167
     switch(e.side)
168
       {
169
       case NORTH: d+="NORTH "; break;
170
       case SOUTH: d+="SOUTH "; break;
171
       case WEST : d+="WEST  "; break;
172
       case EAST : d+="EAST  "; break;
173
       }
174
     
175
     d+=("("+e.row+","+e.col+")");
176
     
177
     return d;
178
     }   
179 8d9da98a Leszek Koltunski
*/
180 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
181
182 80cb15ab leszek
   private void prepareDataStructures(int cols, String desc, int slices)
183 6a06a912 Leszek Koltunski
     {
184 e92785ba Leszek Koltunski
     mRows       =0;
185
     mCols       =0;
186
     mSlices     =slices;
187 da681e7e Leszek Koltunski
     numVertices =0;
188 554ce72b Leszek Koltunski
189 0729bc41 Leszek Koltunski
     if( cols>0 && desc.contains("1") )
190 6a06a912 Leszek Koltunski
       {
191 0729bc41 Leszek Koltunski
       mCols = cols;
192
       mRows = desc.length()/cols;
193 6a06a912 Leszek Koltunski
194 554ce72b Leszek Koltunski
       int max = mRows>mCols ? mRows:mCols;
195
       max = mSlices>max ? mSlices : max;
196
197
       mInfCorrX = (float)max/mCols;
198
       mInfCorrY = (float)max/mRows;
199
       mInfCorrZ = (float)max/mSlices;
200
201 9d0df4c6 Leszek Koltunski
       mCubes    = new int[mRows][mCols];
202
       mInflateX = new byte[mRows+1][mCols+1];
203
       mInflateY = new byte[mRows+1][mCols+1];
204
205 20156af7 Leszek Koltunski
       for(int col=0; col<mCols; col++)
206
         for(int row=0; row<mRows; row++)
207
           mCubes[row][col] = (desc.charAt(row * mCols + col) == '1' ? 1 : 0);
208 9d0df4c6 Leszek Koltunski
209 20156af7 Leszek Koltunski
       for(int col=0; col<mCols+1; col++)
210
         for(int row=0; row<mRows+1; row++)
211 9d0df4c6 Leszek Koltunski
           {
212 20156af7 Leszek Koltunski
           fillInflate(row,col);
213 9d0df4c6 Leszek Koltunski
           }
214 d6994cc6 Leszek Koltunski
215 0729bc41 Leszek Koltunski
       markRegions();
216 da681e7e Leszek Koltunski
       numVertices = computeDataLength();
217 15290f35 Leszek Koltunski
       currVert = 0;
218 6a06a912 Leszek Koltunski
       }
219
     }
220 665e2c45 Leszek Koltunski
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
// full grid
223
224 80cb15ab leszek
   private void prepareDataStructures(int cols, int rows, int slices)
225 665e2c45 Leszek Koltunski
     {
226 e92785ba Leszek Koltunski
     mRows        =rows;
227
     mCols        =cols;
228
     mSlices      =slices;
229 da681e7e Leszek Koltunski
     numVertices  =0;
230 665e2c45 Leszek Koltunski
231
     if( cols>0 && rows>0 )
232
       {
233 9d0df4c6 Leszek Koltunski
       mCubes    = new int[mRows][mCols];
234
       mInflateX = new byte[mRows+1][mCols+1];
235
       mInflateY = new byte[mRows+1][mCols+1];
236 665e2c45 Leszek Koltunski
237 554ce72b Leszek Koltunski
       int max = mRows>mCols ? mRows:mCols;
238
       max = mSlices>max ? mSlices : max;
239
240
       mInfCorrX = (float)max/mCols;
241
       mInfCorrY = (float)max/mRows;
242
       mInfCorrZ = (float)max/mSlices;
243
244 20156af7 Leszek Koltunski
       for(int col=0; col<mCols; col++)
245
         for(int row=0; row<mRows; row++)
246
           mCubes[row][col] = 1;
247 665e2c45 Leszek Koltunski
248 20156af7 Leszek Koltunski
       for(int col=0; col<mCols+1; col++)
249
         for(int row=0; row<mRows+1; row++)
250 9d0df4c6 Leszek Koltunski
           {
251 20156af7 Leszek Koltunski
           fillInflate(row,col);
252 9d0df4c6 Leszek Koltunski
           }
253
254 665e2c45 Leszek Koltunski
       markRegions();
255 da681e7e Leszek Koltunski
       numVertices = computeDataLength();
256 15290f35 Leszek Koltunski
       currVert = 0;
257 665e2c45 Leszek Koltunski
       }
258
     }
259
260 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
261
// Mark all the 'regions' of our grid  - i.e. separate pieces of 'land' (connected blocks that will 
262
// be rendered) and 'water' (connected holes in between) with integers. Each connected block of land
263
// gets a unique odd integer, each connected block of water a unique even integer.
264
//
265
// Water on the edges of the grid is also considered connected to itself!   
266
//   
267
// This function also creates a list of 'Edges'. Each Edge is a data structure from which later on we
268 2e96ee72 Leszek Koltunski
// will start building the side walls of each connected block of land (and sides of holes of water
269 8d9da98a Leszek Koltunski
// inside). Each Edge needs to point from Land to Water (thus the '(SOUTH,i-1,j)' below) - otherwise
270
// later on setting up normal vectors wouldn't work.
271 6a06a912 Leszek Koltunski
   
272 a51fe521 Leszek Koltunski
  private void markRegions()
273 6a06a912 Leszek Koltunski
     {
274
     int i, j, numWater=1, numLand=0;
275
     
276
     for(i=0; i<mRows;i++) if( mCubes[      i][      0]==0 ) markRegion((short)2,      i,       0);
277
     for(i=0; i<mRows;i++) if( mCubes[      i][mCols-1]==0 ) markRegion((short)2,      i, mCols-1);
278
     for(i=0; i<mCols;i++) if( mCubes[0      ][      i]==0 ) markRegion((short)2,      0,       i);
279
     for(i=0; i<mCols;i++) if( mCubes[mRows-1][      i]==0 ) markRegion((short)2,mRows-1,       i);
280
           
281
     for(i=0; i<mRows; i++)
282
        for(j=0; j<mCols; j++)
283
           {
284 8d9da98a Leszek Koltunski
           if( mCubes[i][j] == 0 ) { numWater++; markRegion( (short)(2*numWater ),i,j); mEdges.add(new Edge(SOUTH,i-1,j)); }
285
           if( mCubes[i][j] == 1 ) { numLand ++; markRegion( (short)(2*numLand+1),i,j); mEdges.add(new Edge(NORTH,i  ,j)); }
286 6a06a912 Leszek Koltunski
           }
287
     
288 8d9da98a Leszek Koltunski
     // now we potentially need to kick out some Edges . Otherwise the following does not work:
289 6a06a912 Leszek Koltunski
     //
290
     // 0 1 0
291
     // 1 0 1
292
     // 0 1 0
293
     
294 8d9da98a Leszek Koltunski
     mEdgeNum= mEdges.size();
295
     int initCol, initRow, initSide, lastSide;
296
     Edge e1,e2;
297 6a06a912 Leszek Koltunski
     
298 8d9da98a Leszek Koltunski
     for(i=0; i<mEdgeNum; i++)
299 6a06a912 Leszek Koltunski
       {
300 8d9da98a Leszek Koltunski
       e1 = mEdges.get(i);
301
       initRow= e1.row;
302
       initCol= e1.col;
303
       initSide=e1.side;
304
305
       do
306 6a06a912 Leszek Koltunski
         {
307 8d9da98a Leszek Koltunski
         //android.util.Log.d("CUBES", "checking edge "+debug(e1));
308
309 b62632fc Leszek Koltunski
         mSideWalls++;
310
311 8d9da98a Leszek Koltunski
         if( e1.side==NORTH || e1.side==SOUTH )
312 6a06a912 Leszek Koltunski
           {
313 8d9da98a Leszek Koltunski
           for(j=i+1;j<mEdgeNum;j++)
314 6a06a912 Leszek Koltunski
             {
315 8d9da98a Leszek Koltunski
             e2 = mEdges.get(j);
316
317
             if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col )
318
               {
319
               mEdges.remove(j);
320
               mEdgeNum--;
321
               j--;
322
323
               //android.util.Log.e("CUBES", "removing edge "+debug(e2));
324
               }
325 6a06a912 Leszek Koltunski
             }
326
           }
327 8d9da98a Leszek Koltunski
328
         lastSide = e1.side;
329
         e1 = getNextEdge(e1);
330
         if( e1.side!=lastSide ) mSideBends++;
331 6a06a912 Leszek Koltunski
         }
332 8d9da98a Leszek Koltunski
       while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide );
333 6a06a912 Leszek Koltunski
       }
334
     }
335
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337
// when calling, make sure that newVal != val
338
   
339 a51fe521 Leszek Koltunski
  private void markRegion(short newVal, int row, int col)
340 6a06a912 Leszek Koltunski
     {
341 d6994cc6 Leszek Koltunski
     int val = mCubes[row][col];
342 6a06a912 Leszek Koltunski
     mCubes[row][col] = newVal;
343
     
344
     if( row>0       && mCubes[row-1][col  ]==val ) markRegion(newVal, row-1, col  );
345
     if( row<mRows-1 && mCubes[row+1][col  ]==val ) markRegion(newVal, row+1, col  );
346
     if( col>0       && mCubes[row  ][col-1]==val ) markRegion(newVal, row  , col-1);
347
     if( col<mCols-1 && mCubes[row  ][col+1]==val ) markRegion(newVal, row  , col+1);
348
     }
349
   
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351
   
352 a51fe521 Leszek Koltunski
  private void createNormals(boolean front, int row, int col)
353 6a06a912 Leszek Koltunski
     {
354
     int td,lr; 
355
      
356 2e96ee72 Leszek Koltunski
     int nw = (col>0       && row>0      ) ? (mCubes[row-1][col-1]%2) : 0;
357
     int w  = (col>0                     ) ? (mCubes[row  ][col-1]%2) : 0;
358
     int n  = (               row>0      ) ? (mCubes[row-1][col  ]%2) : 0;
359
     int c  =                                (mCubes[row  ][col  ]%2);
360
     int sw = (col>0       && row<mRows-1) ? (mCubes[row+1][col-1]%2) : 0;
361 6a06a912 Leszek Koltunski
     int s  = (               row<mRows-1) ? (mCubes[row+1][col  ]%2) : 0;
362
     int ne = (col<mCols-1 && row>0      ) ? (mCubes[row-1][col+1]%2) : 0;
363
     int e  = (col<mCols-1               ) ? (mCubes[row  ][col+1]%2) : 0;
364
     int se = (col<mCols-1 && row<mRows-1) ? (mCubes[row+1][col+1]%2) : 0;
365 ce7f3833 Leszek Koltunski
366
     if(front)
367
       {
368
       mNormalZ[0] = 1.0f;
369
       mNormalZ[1] = 1.0f;
370
       mNormalZ[2] = 1.0f;
371
       mNormalZ[3] = 1.0f;
372
       }
373
     else
374
       {
375
       mNormalZ[0] =-1.0f;
376
       mNormalZ[1] =-1.0f;
377
       mNormalZ[2] =-1.0f;
378
       mNormalZ[3] =-1.0f;
379
       }
380
381 6a06a912 Leszek Koltunski
     td = nw+n-w-c;
382
     lr = c+n-w-nw;
383
     if( td<0 ) td=-1;
384
     if( td>0 ) td= 1;
385
     if( lr<0 ) lr=-1;
386
     if( lr>0 ) lr= 1;
387
     mNormalX[0] = lr*R;
388
     mNormalY[0] = td*R;
389
     
390
     td = w+c-sw-s;
391
     lr = c+s-w-sw;
392
     if( td<0 ) td=-1;
393
     if( td>0 ) td= 1;
394
     if( lr<0 ) lr=-1;
395
     if( lr>0 ) lr= 1;
396
     mNormalX[1] = lr*R;
397
     mNormalY[1] = td*R;
398
     
399
     td = n+ne-c-e;
400
     lr = e+ne-c-n;
401
     if( td<0 ) td=-1;
402
     if( td>0 ) td= 1;
403
     if( lr<0 ) lr=-1;
404
     if( lr>0 ) lr= 1;
405
     mNormalX[2] = lr*R;
406
     mNormalY[2] = td*R;
407
     
408
     td = c+e-s-se;
409
     lr = e+se-c-s;
410
     if( td<0 ) td=-1;
411
     if( td>0 ) td= 1;
412
     if( lr<0 ) lr=-1;
413
     if( lr>0 ) lr= 1;
414
     mNormalX[3] = lr*R;
415
     mNormalY[3] = td*R;
416
     /*
417 2e96ee72 Leszek Koltunski
     android.util.Log.d("CUBES", "row="+row+" col="+col);
418
     android.util.Log.d("CUBES", mNormalX[0]+" "+mNormalY[0]);
419
     android.util.Log.d("CUBES", mNormalX[1]+" "+mNormalY[1]);
420
     android.util.Log.d("CUBES", mNormalX[2]+" "+mNormalY[2]);
421
     android.util.Log.d("CUBES", mNormalX[3]+" "+mNormalY[3]);
422 6a06a912 Leszek Koltunski
     */
423
     }
424 ce7f3833 Leszek Koltunski
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426
427 15290f35 Leszek Koltunski
  private void buildFrontBackGrid(boolean front, float[] attribs)
428 6a06a912 Leszek Koltunski
     {
429 d6994cc6 Leszek Koltunski
     int last, current;
430 ce7f3833 Leszek Koltunski
     boolean seenLand=false;
431
     boolean lastBlockIsNE = false;
432
     boolean currentBlockIsNE;
433 f08b268d Leszek Koltunski
     float vectZ = (front ? 0.5f : -0.5f);
434 ce7f3833 Leszek Koltunski
435 84ee2a6a Leszek Koltunski
     //android.util.Log.d("CUBES", "buildFrontBack");
436
437 e5d9b235 Leszek Koltunski
     for(int row=0; row<mRows; row++)
438 6a06a912 Leszek Koltunski
       {
439
       last =0;
440
         
441 e5d9b235 Leszek Koltunski
       for(int col=0; col<mCols; col++)
442 6a06a912 Leszek Koltunski
         {
443 e5d9b235 Leszek Koltunski
         current = mCubes[row][col];
444 ce7f3833 Leszek Koltunski
445 6a06a912 Leszek Koltunski
         if( current%2 == 1 )
446
           {
447 e5d9b235 Leszek Koltunski
           currentBlockIsNE = isNE(row,col);
448 84ee2a6a Leszek Koltunski
449 15290f35 Leszek Koltunski
           if( !seenLand && !front && ((currVert%2==1)^currentBlockIsNE) )
450 84ee2a6a Leszek Koltunski
             {
451
             //android.util.Log.d("CUBES","repeating winding2 vertex");
452
453 15290f35 Leszek Koltunski
             repeatLast(attribs);
454 84ee2a6a Leszek Koltunski
             }
455
456 e5d9b235 Leszek Koltunski
           createNormals(front,row,col);
457 ce7f3833 Leszek Koltunski
458 e5d9b235 Leszek Koltunski
           if( currentBlockIsNE )
459 6a06a912 Leszek Koltunski
             {
460 e5d9b235 Leszek Koltunski
             if( (last!=current) || !lastBlockIsNE )
461
               {
462 15290f35 Leszek Koltunski
               if( seenLand  && (last != current) ) repeatLast(attribs);
463
               addFrontVertex( 0, vectZ, col, row, attribs);
464
               if( seenLand  && (last != current) ) repeatLast(attribs);
465
               if( !lastBlockIsNE || (!front && !seenLand) ) repeatLast(attribs);
466
               addFrontVertex( 1, vectZ, col, row+1, attribs);
467 e5d9b235 Leszek Koltunski
               }
468 15290f35 Leszek Koltunski
             addFrontVertex( 2, vectZ, col+1, row, attribs);
469
             addFrontVertex( 3, vectZ, col+1, row+1, attribs);
470 e5d9b235 Leszek Koltunski
             }
471
           else
472
             {
473
             if( (last!=current) || lastBlockIsNE )
474
               {
475 15290f35 Leszek Koltunski
               if( seenLand  && (last != current) ) repeatLast(attribs);
476
               addFrontVertex( 1, vectZ, col, row+1, attribs);
477
               if( seenLand  && (last != current) ) repeatLast(attribs);
478
               if( lastBlockIsNE || (!front && !seenLand) ) repeatLast(attribs);
479
               addFrontVertex( 0, vectZ, col, row, attribs);
480 e5d9b235 Leszek Koltunski
               }
481 15290f35 Leszek Koltunski
             addFrontVertex( 3, vectZ, col+1, row+1, attribs);
482
             addFrontVertex( 2, vectZ, col+1, row  , attribs);
483 6a06a912 Leszek Koltunski
             }
484 ce7f3833 Leszek Koltunski
485
           seenLand = true;
486
           lastBlockIsNE = currentBlockIsNE;
487 6a06a912 Leszek Koltunski
           }
488
            
489
         last = current;
490
         }
491
       }
492
     }
493
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495
496 15290f35 Leszek Koltunski
  private void buildSideGrid(float[] attribs)
497 6a06a912 Leszek Koltunski
     {
498 84ee2a6a Leszek Koltunski
     //android.util.Log.d("CUBES", "buildSide");
499 ce7f3833 Leszek Koltunski
500 8d9da98a Leszek Koltunski
     for(int i=0; i<mEdgeNum; i++)
501 6a06a912 Leszek Koltunski
       {
502 15290f35 Leszek Koltunski
       buildIthSide(mEdges.get(i), attribs);
503
       }
504 6a06a912 Leszek Koltunski
     }
505
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507
508 15290f35 Leszek Koltunski
  private void buildIthSide(Edge curr, float[] attribs)
509 6a06a912 Leszek Koltunski
     {
510 5f2853be Leszek Koltunski
     Edge prev, next;
511
     int col, row, side;
512
513 6a06a912 Leszek Koltunski
     if( curr.side==NORTH ) // water outside
514
       {
515
       prev = new Edge(WEST,curr.row,curr.col);
516
       }
517 d1a396b2 leszek
     else                   // land outside; we need to move forward one link because we are
518
       {                    // going in opposite direction and we need to start from a bend.
519 6a06a912 Leszek Koltunski
       prev = curr;
520
       curr = new Edge(EAST,curr.row+1,curr.col-1);
521
       }
522 5f2853be Leszek Koltunski
523 9d0df4c6 Leszek Koltunski
     for(int slice=0; slice<mSlices; slice++)
524 6a06a912 Leszek Koltunski
       {
525 5f2853be Leszek Koltunski
       col = curr.col;
526
       row = curr.row;
527
       side= curr.side;
528
       next = getNextEdge(curr);
529
     
530 9d0df4c6 Leszek Koltunski
       addSideVertex(curr,true,slice+1,prev.side,attribs);
531 5f2853be Leszek Koltunski
532
       do
533 6a06a912 Leszek Koltunski
         {
534 5f2853be Leszek Koltunski
         if( prev.side!=curr.side )
535
           {
536 9d0df4c6 Leszek Koltunski
           addSideVertex(curr,true,slice+1,prev.side,attribs);
537
           addSideVertex(curr,true,slice  ,prev.side,attribs);
538 5f2853be Leszek Koltunski
           }
539 6a06a912 Leszek Koltunski
       
540 9d0df4c6 Leszek Koltunski
         addSideVertex(curr,false,slice+1,next.side,attribs);
541
         addSideVertex(curr,false,slice  ,next.side,attribs);
542 6a06a912 Leszek Koltunski
       
543 5f2853be Leszek Koltunski
         prev = curr;
544
         curr = next;
545
         next = getNextEdge(curr);
546
         }
547
       while( curr.col!=col || curr.row!=row || curr.side!=side );
548 6a06a912 Leszek Koltunski
     
549 15290f35 Leszek Koltunski
       repeatLast(attribs);
550 5f2853be Leszek Koltunski
       }
551 6a06a912 Leszek Koltunski
     }
552
553
///////////////////////////////////////////////////////////////////////////////////////////////////
554
555 a51fe521 Leszek Koltunski
  private Edge getNextEdge(Edge curr)
556 6a06a912 Leszek Koltunski
     {
557
     int col = curr.col;
558
     int row = curr.row;
559
      
560
     //android.util.Log.e("CUBES", "row="+row+" col="+col+" mRows="+mRows+" mCols="+mCols);
561
                       
562
     switch(curr.side) 
563
       {
564
       case NORTH: if( col==mCols-1 ) 
565
                     return new Edge(EAST,row,col);
566
                   if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] )
567
                     return new Edge(WEST,row-1,col+1);
568
                   if( mCubes[row][col+1]==mCubes[row][col] )
569
                     return new Edge(NORTH,row,col+1);
570
                   else  
571
                     return new Edge(EAST,row,col);
572
                   
573
       case SOUTH: if( col==0 ) 
574
                     return new Edge(WEST,row,col);
575
                   if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] )
576
                     return new Edge(EAST,row+1,col-1); 
577
                   if( mCubes[row][col-1]==mCubes[row][col] )
578
                     return new Edge(SOUTH,row,col-1);
579
                   else
580
                     return new Edge(WEST,row,col); 
581
                     
582
       case EAST : if( row==mRows-1 ) 
583
                     return new Edge(SOUTH,row,col);
584
                   if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] )
585
                     return new Edge(NORTH,row+1,col+1);
586
                   if( mCubes[row+1][col]==mCubes[row][col] )
587
                     return new Edge(EAST,row+1,col);
588
                   else 
589
                     return new Edge(SOUTH,row,col);
590
                   
591 8d9da98a Leszek Koltunski
       default   : if( row==0 )
592 6a06a912 Leszek Koltunski
                     return new Edge(NORTH,row,col);
593
                   if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] )
594
                     return new Edge(SOUTH,row-1,col-1);
595
                   if( mCubes[row-1][col]==mCubes[row][col] )
596
                     return new Edge(WEST,row-1,col);
597
                   else
598
                     return new Edge(NORTH,row,col);     
599
       }
600
     }
601
602 9d0df4c6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
603
604 20156af7 Leszek Koltunski
  private void fillInflate(int row, int col)
605 9d0df4c6 Leszek Koltunski
    {
606
    int diff;
607
608 20156af7 Leszek Koltunski
         if( col==0     ) mInflateX[row][col] = -1;
609
    else if( col==mCols ) mInflateX[row][col] = +1;
610 9d0df4c6 Leszek Koltunski
    else
611
      {
612
      if( row==0 )
613
        {
614
        diff = mCubes[0][col-1]-mCubes[0][col];
615
        }
616
      else if( row==mRows )
617
        {
618
        diff = mCubes[mRows-1][col-1]-mCubes[mRows-1][col];
619
        }
620
      else
621
        {
622
        diff = (mCubes[row  ][col-1]-mCubes[row  ][col]) +
623
               (mCubes[row-1][col-1]-mCubes[row-1][col]) ;
624
625
        if( diff==-2 ) diff=-1;
626
        if( diff== 2 ) diff= 1;
627
        }
628
629 20156af7 Leszek Koltunski
      mInflateX[row][col] = (byte)diff;
630 9d0df4c6 Leszek Koltunski
      }
631
632 20156af7 Leszek Koltunski
         if( row==0     ) mInflateY[row][col] = +1;
633
    else if( row==mRows ) mInflateY[row][col] = -1;
634 9d0df4c6 Leszek Koltunski
    else
635
      {
636
      if( col==0 )
637
        {
638
        diff = mCubes[row][0]-mCubes[row-1][0];
639
        }
640
      else if( col==mCols )
641
        {
642
        diff = mCubes[row][mCols-1]-mCubes[row-1][mCols-1];
643
        }
644
      else
645
        {
646
        diff = (mCubes[row  ][col-1]+mCubes[row  ][col]) -
647
               (mCubes[row-1][col-1]+mCubes[row-1][col]) ;
648
649
        if( diff==-2 ) diff=-1;
650
        if( diff== 2 ) diff= 1;
651
        }
652
653 20156af7 Leszek Koltunski
      mInflateY[row][col] = (byte)diff;
654 9d0df4c6 Leszek Koltunski
      }
655
656
    //android.util.Log.e("mesh","col="+col+" row="+row+" inflateX="+mInflateX[col][row]+" InflateY="+mInflateY[col][row]);
657
    }
658
659 a51fe521 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
660
661 15290f35 Leszek Koltunski
  private void addFrontVertex(int index, float vectZ, int col, int row, float[] attribs)
662 a51fe521 Leszek Koltunski
     {
663
     float x = (float)col/mCols;
664
     float y = (float)row/mRows;
665
666 15290f35 Leszek Koltunski
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x-0.5f;
667
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f-y;
668
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = vectZ;
669 6f2d931d Leszek Koltunski
670 15290f35 Leszek Koltunski
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = mNormalX[index];
671
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = mNormalY[index];
672
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = mNormalZ[index];
673 6f2d931d Leszek Koltunski
674 554ce72b Leszek Koltunski
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInfCorrX*mInflateX[row][col]/2.0f;
675
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInfCorrY*mInflateY[row][col]/2.0f;
676
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = mInfCorrZ*vectZ;
677 6f2d931d Leszek Koltunski
678 15290f35 Leszek Koltunski
     attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = x;
679
     attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f-y;
680 a51fe521 Leszek Koltunski
681 15290f35 Leszek Koltunski
     currVert++;
682 a51fe521 Leszek Koltunski
     }
683
684 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
685
   
686 15290f35 Leszek Koltunski
  private void addSideVertex(Edge curr, boolean back, int slice, int side, float[] attribs)
687 6a06a912 Leszek Koltunski
     {
688 84ee2a6a Leszek Koltunski
     //android.util.Log.e("CUBES", "adding Side vertex!");
689 9d0df4c6 Leszek Koltunski
     float x, y, z;
690
     int row, col;
691 985ea9c5 Leszek Koltunski
692 6a06a912 Leszek Koltunski
     switch(curr.side)
693
       {
694 9d0df4c6 Leszek Koltunski
       case NORTH: row = curr.row;
695
                   col = (back ? (curr.col  ):(curr.col+1));
696
                   x = (float)col/mCols;
697
                   y = 0.5f - (float)row/mRows;
698
                   z = 0.5f - (float)slice/mSlices;
699 985ea9c5 Leszek Koltunski
700 15290f35 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x - 0.5f;
701 9d0df4c6 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = y;
702
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
703 6f2d931d Leszek Koltunski
704 15290f35 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = side==NORTH ? 0.0f : (side==WEST?-R:R);
705
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = 1.0f;
706
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
707 6f2d931d Leszek Koltunski
708 554ce72b Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInfCorrX*mInflateX[row][col]/2.0f;
709
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInfCorrY*mInflateY[row][col]/2.0f;
710
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = mInfCorrZ*z;
711 6f2d931d Leszek Koltunski
712 15290f35 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = x;
713 9d0df4c6 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f-(float)(row-slice)/mRows;
714 6a06a912 Leszek Koltunski
                   break;
715 9d0df4c6 Leszek Koltunski
       case SOUTH: row = curr.row+1;
716
                   col = (back ? (curr.col+1):(curr.col));
717
                   x = (float)col/mCols;
718
                   y = 0.5f - (float)row/mRows;
719
                   z = 0.5f - (float)slice/mSlices;
720 985ea9c5 Leszek Koltunski
721 15290f35 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x - 0.5f;
722 9d0df4c6 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = y;
723
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
724 6f2d931d Leszek Koltunski
725 15290f35 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = side==SOUTH ? 0.0f: (side==EAST?-R:R);
726
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] =-1.0f;
727
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
728 6f2d931d Leszek Koltunski
729 554ce72b Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInfCorrX*mInflateX[row][col]/2.0f;
730
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInfCorrY*mInflateY[row][col]/2.0f;
731
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = mInfCorrZ*z;
732 6f2d931d Leszek Koltunski
733 15290f35 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = x;
734 9d0df4c6 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f - (float)(row+slice)/mRows;
735 6a06a912 Leszek Koltunski
                   break;
736 9d0df4c6 Leszek Koltunski
       case WEST : row = (back  ? (curr.row+1):(curr.row));
737
                   col = curr.col;
738
                   x = (float)col/mCols -0.5f;
739
                   y = (float)row/mRows;
740
                   z = 0.5f - (float)slice/mSlices;
741 985ea9c5 Leszek Koltunski
742 9d0df4c6 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x;
743 15290f35 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f - y;
744 9d0df4c6 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
745 6f2d931d Leszek Koltunski
746 15290f35 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] =-1.0f;
747
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = side==WEST ? 0.0f : (side==NORTH?-R:R);
748
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
749 6f2d931d Leszek Koltunski
750 554ce72b Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInfCorrX*mInflateX[row][col]/2.0f;
751
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInfCorrY*mInflateY[row][col]/2.0f;
752
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = mInfCorrZ*z;
753 6f2d931d Leszek Koltunski
754 9d0df4c6 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = (float)(col-slice)/mCols;
755 15290f35 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f - y;
756 6a06a912 Leszek Koltunski
                   break;
757 9d0df4c6 Leszek Koltunski
       case EAST : row = (back  ? (curr.row):(curr.row+1));
758
                   col = (curr.col+1);
759
                   x = (float)col/mCols -0.5f;
760
                   y = (float)row/mRows;
761
                   z = 0.5f - (float)slice/mSlices;
762 985ea9c5 Leszek Koltunski
763 9d0df4c6 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x;
764 15290f35 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f - y;
765 9d0df4c6 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
766 6f2d931d Leszek Koltunski
767 15290f35 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = 1.0f;
768
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = side==EAST ? 0.0f : (side==SOUTH?-R:R);
769
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
770 6f2d931d Leszek Koltunski
771 554ce72b Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInfCorrX*mInflateX[row][col]/2.0f;
772
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInfCorrY*mInflateY[row][col]/2.0f;
773
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = mInfCorrZ*z;
774 6f2d931d Leszek Koltunski
775 9d0df4c6 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = (float)(col+slice)/mCols;
776 15290f35 Leszek Koltunski
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f - y;
777 6a06a912 Leszek Koltunski
                   break;
778
       }
779 6f2d931d Leszek Koltunski
780 15290f35 Leszek Koltunski
     float tex0 =  attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ];
781
     float tex1 =  attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1];
782
783
     if(tex0>1.0f) attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = 2.0f-tex0;
784
     if(tex0<0.0f) attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] =     -tex0;
785
     if(tex1>1.0f) attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 2.0f-tex1;
786
     if(tex1<0.0f) attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] =     -tex1;
787 6f2d931d Leszek Koltunski
788 15290f35 Leszek Koltunski
     currVert++;
789 6f2d931d Leszek Koltunski
     }
790
791
///////////////////////////////////////////////////////////////////////////////////////////////////
792
793 15290f35 Leszek Koltunski
   private void repeatLast(float[] attribs)
794 6f2d931d Leszek Koltunski
     {
795
     //android.util.Log.e("CUBES", "repeating last vertex!");
796
797 15290f35 Leszek Koltunski
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + POS_ATTRIB  ];
798
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + POS_ATTRIB+1];
799
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = attribs[VERT_ATTRIBS*(currVert-1) + POS_ATTRIB+2];
800 6f2d931d Leszek Koltunski
801 15290f35 Leszek Koltunski
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + NOR_ATTRIB  ];
802
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + NOR_ATTRIB+1];
803
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = attribs[VERT_ATTRIBS*(currVert-1) + NOR_ATTRIB+2];
804 6f2d931d Leszek Koltunski
805 15290f35 Leszek Koltunski
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + INF_ATTRIB  ];
806
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + INF_ATTRIB+1];
807
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = attribs[VERT_ATTRIBS*(currVert-1) + INF_ATTRIB+2];
808 6f2d931d Leszek Koltunski
809 15290f35 Leszek Koltunski
     attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + TEX_ATTRIB  ];
810
     attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + TEX_ATTRIB+1];
811 6f2d931d Leszek Koltunski
812 15290f35 Leszek Koltunski
     currVert++;
813 6a06a912 Leszek Koltunski
     }
814
815 665e2c45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
816
817 d1a396b2 leszek
  private void build()
818 665e2c45 Leszek Koltunski
     {
819 6f2d931d Leszek Koltunski
     float[] attribs= new float[VERT_ATTRIBS*numVertices];
820 665e2c45 Leszek Koltunski
821 15290f35 Leszek Koltunski
     buildFrontBackGrid(true,attribs);
822 665e2c45 Leszek Koltunski
823 d1a396b2 leszek
     if( mSlices>0 )
824 665e2c45 Leszek Koltunski
       {
825 15290f35 Leszek Koltunski
       repeatLast(attribs);
826
       if( currVert%2==1 ) repeatLast(attribs);
827
       buildSideGrid(attribs);
828
       buildFrontBackGrid(false,attribs);
829 665e2c45 Leszek Koltunski
       }
830
831 8d9da98a Leszek Koltunski
     mEdges.clear();
832
     mEdges = null;
833
     mCubes = null;
834 9d0df4c6 Leszek Koltunski
     mInflateX = null;
835
     mInflateY = null;
836 8d9da98a Leszek Koltunski
837 15290f35 Leszek Koltunski
     if( currVert!=numVertices )
838
       android.util.Log.e("MeshCubes", "currVert " +currVert+" numVertices="+numVertices );
839 665e2c45 Leszek Koltunski
840 227b9bca Leszek Koltunski
     setAttribs(attribs);
841 665e2c45 Leszek Koltunski
     }
842
843 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
844
// PUBLIC API
845
///////////////////////////////////////////////////////////////////////////////////////////////////
846
/**
847 05403bba Leszek Koltunski
 * Creates the underlying mesh of vertices, normals, texture coords.
848 6a06a912 Leszek Koltunski
 *    
849 80cb15ab leszek
 * @param cols   Integer helping to parse the next parameter.
850
 * @param desc   String describing the subset of a MxNx1 cuboid that we want to create.
851
 *               Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
852
 *               <p></p>
853
 *               <p>
854
 *               <pre>
855
 *               For example, (cols=2, desc="111010") describes the following shape:
856 a56bc359 Leszek Koltunski
 *
857 80cb15ab leszek
 *               XX
858
 *               X
859
 *               X
860 a56bc359 Leszek Koltunski
 *
861 80cb15ab leszek
 *               whereas (cols=2,desc="110001") describes
862 a56bc359 Leszek Koltunski
 *
863 80cb15ab leszek
 *               XX
864 a56bc359 Leszek Koltunski
 *
865 80cb15ab leszek
 *                X
866
 *               </pre>
867
 *               </p>
868
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
869 6a06a912 Leszek Koltunski
 */
870 80cb15ab leszek
 public MeshCubes(int cols, String desc, int slices)
871 a51fe521 Leszek Koltunski
   {
872 80cb15ab leszek
   super( (float)slices/cols);
873
   prepareDataStructures(cols,desc,slices);
874 d1a396b2 leszek
   build();
875 a51fe521 Leszek Koltunski
   }
876 6a06a912 Leszek Koltunski
877 665e2c45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
878
/**
879 05403bba Leszek Koltunski
 * Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors.
880 665e2c45 Leszek Koltunski
 *
881 80cb15ab leszek
 * @param cols   Number of columns, i.e. 'width' of the Mesh.
882
 * @param rows   Number of rows, i.e. 'height' of the Mesh.
883
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
884 665e2c45 Leszek Koltunski
 */
885 80cb15ab leszek
 public MeshCubes(int cols, int rows, int slices)
886 a51fe521 Leszek Koltunski
   {
887 80cb15ab leszek
   super( (float)slices/cols);
888
   prepareDataStructures(cols,rows,slices);
889 d1a396b2 leszek
   build();
890 6a06a912 Leszek Koltunski
   }
891 a51fe521 Leszek Koltunski
 }