Project

General

Profile

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

library / src / main / java / org / distorted / library / MeshCubes.java @ a51fe521

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.library;
21

    
22
import java.nio.ByteBuffer;
23
import java.nio.ByteOrder;
24
import java.util.ArrayList;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
/**
28
 * Create a 3D grid composed of Cubes.
29
 * <p>
30
 * Any subset of a MxNx1 cuboid is possible.
31
 */
32
public class MeshCubes extends MeshObject
33
   {
34
   private static final float R = 0.0f;//0.2f;
35
   private static final float FRONTZ = 0.5f;
36
   private static final float BACKZ  =-0.5f;
37
   
38
   private static final int NORTH = 0;
39
   private static final int WEST  = 1;
40
   private static final int EAST  = 2;
41
   private static final int SOUTH = 3;
42

    
43
   private static final boolean BACK  = true;
44
   private static final boolean FRONT = false;
45
   private static final boolean UPPER = false;
46
   private static final boolean LOWER = true;
47
   
48
   private static final float[] mNormalX = new float[4];
49
   private static final float[] mNormalY = new float[4];
50
   private static final float[] mNormalZ = new float[4];
51

    
52
   private class Edge
53
     {
54
     final int side; 
55
     final int row;
56
     final int col;
57
     
58
     Edge(int s, int r, int c)
59
       {
60
       side= s; 
61
       row = r;
62
       col = c;
63
       }
64
     }
65
   
66
   private int mCols, mRows;
67
   private int[][] mCubes;
68
   private ArrayList<Edge> mEdges = new ArrayList<>();
69

    
70
   private int remainingVert;
71
   private int mSideBends;
72
   private int mEdgeNum;
73
   private int mSideWalls;
74

    
75
   private float[] mBoundingVert;
76

    
77
   private static float[] mBoundingVert1 = new float[] { -0.5f,+0.5f, FRONTZ,
78
                                                         -0.5f,-0.5f, FRONTZ,
79
                                                         +0.5f,+0.5f, FRONTZ,
80
                                                         +0.5f,-0.5f, FRONTZ };
81

    
82
   private static float[] mBoundingVert2 = new float[] { -0.5f,+0.5f, FRONTZ,
83
                                                         -0.5f,-0.5f, FRONTZ,
84
                                                         +0.5f,+0.5f, FRONTZ,
85
                                                         +0.5f,-0.5f, FRONTZ,
86
                                                         -0.5f,+0.5f, BACKZ ,
87
                                                         -0.5f,-0.5f, BACKZ ,
88
                                                         +0.5f,+0.5f, BACKZ ,
89
                                                         +0.5f,-0.5f, BACKZ  };
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
// a Block is split into two triangles along the NE-SW line iff it is in the top-right
93
// or bottom-left quadrant of the grid.
94

    
95
   private boolean isNE(int row,int col)
96
     {
97
     return ( (2*row<mRows)^(2*col<mCols) );
98
     }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
// return the number of vertices our grid will contain
102

    
103
   private int computeDataLength(boolean frontOnly)
104
      {
105
      int frontWalls=0, frontSegments=0, triangleShifts=0, windingShifts=0;
106
      int shiftCol = (mCols-1)/2;
107

    
108
      boolean lastBlockIsNE=false;
109
      boolean thisBlockIsNE;        // the block we are currently looking at is split into
110
                                    // two triangles along the NE-SW line (rather than NW-SE)
111
      for(int row=0; row<mRows; row++)
112
        {
113
        if( mCols>=2 && (mCubes[row][shiftCol]%2 == 1) && (mCubes[row][shiftCol+1]%2 == 1) ) triangleShifts++;
114

    
115
        for(int col=0; col<mCols; col++)
116
          {
117
          if( mCubes[row][col]%2 == 1 )  // land
118
            {
119
            thisBlockIsNE = isNE(row,col);
120
            if( thisBlockIsNE^lastBlockIsNE ) windingShifts++;
121
            lastBlockIsNE = thisBlockIsNE;
122
            frontWalls++;
123
            if( col==mCols-1 || mCubes[row][col+1]%2 == 0 ) frontSegments++;
124
            }
125
          }
126
        }
127

    
128
      int frontVert = 2*( frontWalls + 2*frontSegments - 1) +2*triangleShifts + windingShifts;
129
      int sideVert  = 2*( mSideWalls + mSideBends + mEdgeNum -1);
130
      int firstWinding= (!frontOnly && (frontVert+1)%2==1 ) ? 1:0;
131
      int dataL = frontOnly ? frontVert : (frontVert+1) +firstWinding+ (1+sideVert+1) + (1+frontVert);
132
/*
133
      android.util.Log.e("CUBES","triangleShifts="+triangleShifts+" windingShifts="+windingShifts+" winding1="+firstWinding+" frontVert="+frontVert+" sideVert="+sideVert);
134
      android.util.Log.e("CUBES", "frontW="+frontWalls+" fSegments="+frontSegments+" sWalls="+mSideWalls+" sSegments="+mEdgeNum+" sideBends="+mSideBends+" dataLen="+dataL );
135
*/
136
      return dataL<0 ? 0:dataL;
137
      }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
/*
141
   private static String debug(short[] val)
142
     {
143
     String ret="";j
144
     
145
     for(int i=0; i<val.length; i++) ret+=(" "+val[i]); 
146
     
147
     return ret;
148
     }
149
*/
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
/*
152
   private static String debug(float[] val, int stop)
153
     {
154
     String ret="";
155

    
156
     for(int i=0; i<val.length; i++) 
157
        {
158
        if( i%stop==0 ) ret+="\n";
159
        ret+=(" "+val[i]);
160
        }
161

    
162
     return ret;
163
     }
164
*/
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
/*
167
   private static String debug(Edge e)
168
     {
169
     String d = "";
170
     
171
     switch(e.side)
172
       {
173
       case NORTH: d+="NORTH "; break;
174
       case SOUTH: d+="SOUTH "; break;
175
       case WEST : d+="WEST  "; break;
176
       case EAST : d+="EAST  "; break;
177
       }
178
     
179
     d+=("("+e.row+","+e.col+")");
180
     
181
     return d;
182
     }   
183
*/
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
   private void prepareDataStructures(int cols, String desc, boolean frontOnly)
187
     {
188
     mRows      =0;
189
     mCols      =0;
190
     numVertices=0;
191
     
192
     if( cols>0 && desc.contains("1") )
193
       {
194
       mCols = cols;
195
       mRows = desc.length()/cols;
196

    
197
       mCubes = new int[mRows][mCols];
198
       
199
       for(int j=0; j<mCols; j++)
200
         for(int i=0; i<mRows; i++)
201
           mCubes[i][j] = (desc.charAt(i*mCols+j) == '1' ? 1:0);
202

    
203
       buildBoundingVert(frontOnly);
204

    
205
//android.util.Log.d("cubes", "VERT STRING:"+debug(mBoundingVert,3));
206

    
207
       markRegions();
208
       numVertices = computeDataLength(frontOnly);
209

    
210
       remainingVert = numVertices;
211
       }
212
     }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
// full grid
216

    
217
   private void prepareDataStructures(int cols, int rows, boolean frontOnly)
218
     {
219
     mRows       =rows;
220
     mCols       =cols;
221
     numVertices =   0;
222

    
223
     if( cols>0 && rows>0 )
224
       {
225
       mCubes = new int[mRows][mCols];
226

    
227
       for(int j=0; j<mCols; j++)
228
         for(int i=0; i<mRows; i++)
229
           mCubes[i][j] = 1;
230

    
231
       mBoundingVert = frontOnly ? mBoundingVert1 : mBoundingVert2;
232

    
233
//android.util.Log.d("cubes", "VERT FULL:"+debug(mBoundingVert,3));
234

    
235
       markRegions();
236
       numVertices = computeDataLength(frontOnly);
237

    
238
       remainingVert = numVertices;
239
       }
240
     }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  private float retRightmost(int row)
245
    {
246
    if( row==0 )
247
      {
248
      for(int i=mCols-1; i>=0; i--)
249
        if( mCubes[0][i]==1 )
250
          return (float)(i+1);
251
      }
252
    else if(row==mRows)
253
      {
254
      for(int i=mCols-1; i>=0; i--)
255
        if( mCubes[mRows-1][i]==1 )
256
          return (float)(i+1);
257
      }
258
    else
259
      {
260
      for(int i=mCols-1; i>=0; i--)
261
        if( mCubes[row][i]==1 || mCubes[row-1][i]==1 )
262
          return (float)(i+1);
263
      }
264

    
265
    return 0.0f;
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  private float retLeftmost(int row)
271
    {
272
    if( row==0 )
273
      {
274
      for(int i=0; i<mCols; i++)
275
        if( mCubes[0][i]==1 )
276
          return (float)i;
277
      }
278
    else if(row==mRows)
279
      {
280
      for(int i=0; i<mCols; i++)
281
        if( mCubes[mRows-1][i]==1 )
282
          return (float)i;
283
      }
284
    else
285
      {
286
      for(int i=0; i<mCols; i++)
287
        if( mCubes[row][i]==1 || mCubes[row-1][i]==1 )
288
          return (float)i;
289
      }
290

    
291
    return (float)mCols;
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  private int addLeftmost(float[] temp, int row, int index)
297
    {
298
    float x2 = retLeftmost(row)/mCols - 0.5f;
299
    float y2 = 0.5f - (float)row/mRows;
300

    
301
    if( index>1 )
302
      {
303
      float x0 = temp[2*index-4];
304
      float y0 = temp[2*index-3];
305
      float x1 = temp[2*index-2];
306
      float y1 = temp[2*index-1];
307

    
308
      while( (x0-x2)*(y0-y1) >= (x0-x1)*(y0-y2) )
309
        {
310
        if( --index>1 )
311
          {
312
          x1 = x0;
313
          y1 = y0;
314
          x0 = temp[2*index-4];
315
          y0 = temp[2*index-3];
316
          }
317
        else break;
318
        }
319
      }
320

    
321
    temp[2*index  ] = x2;
322
    temp[2*index+1] = y2;
323
    return index+1;
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
  private int addRightmost(float[] temp, int row, int index)
329
    {
330
    float x2 = retRightmost(row)/mCols - 0.5f;
331
    float y2 = 0.5f - (float)row/mRows;
332

    
333
    if( index>1 )
334
      {
335
      float x0 = temp[2*index-4];
336
      float y0 = temp[2*index-3];
337
      float x1 = temp[2*index-2];
338
      float y1 = temp[2*index-1];
339

    
340
      while( (x0-x2)*(y0-y1) <= (x0-x1)*(y0-y2) )
341
        {
342
        if( --index>1 )
343
          {
344
          x1 = x0;
345
          y1 = y0;
346
          x0 = temp[2*index-4];
347
          y0 = temp[2*index-3];
348
          }
349
        else break;
350
        }
351
      }
352

    
353
    temp[2*index  ] = x2;
354
    temp[2*index+1] = y2;
355
    return index+1;
356
    }
357

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359
// fill up the mBoundingVert array with the smallest set of vertices which form the same convex hull
360
// like the whole thing.
361

    
362
  private void buildBoundingVert(boolean frontOnly)
363
    {
364
    int lVert=0, rVert=0;
365
    int firstRow=0, lastRow=mRows-1;
366

    
367
    for(int i=0; i<mRows; i++)
368
      for(int j=0; j<mCols; j++)
369
        if(mCubes[i][j] !=0 )
370
          {
371
          firstRow = i;
372
          i=mRows;
373
          j=mCols;
374
          }
375

    
376
    for(int i=mRows-1; i>=0; i--)
377
      for(int j=0; j<mCols; j++)
378
        if(mCubes[i][j] !=0 )
379
          {
380
          lastRow = i;
381
          i=-1;
382
          j=mCols;
383
          }
384

    
385
//android.util.Log.d("cubes", "first :"+firstRow+" last: "+lastRow);
386

    
387
    int numLines = lastRow-firstRow+2;
388

    
389
    float[] tempL = new float[2*numLines];
390
    float[] tempR = new float[2*numLines];
391

    
392
    for(int i=firstRow; i<=lastRow+1; i++)
393
      {
394
      lVert = addLeftmost (tempL,i,lVert);
395
      rVert = addRightmost(tempR,i,rVert);
396
      }
397

    
398
//android.util.Log.d("cubes", "LEFT :"+debug(tempL,2));
399
//android.util.Log.d("cubes", "RIGHT:"+debug(tempR,2));
400

    
401
    int numVert = lVert+rVert;
402

    
403
    mBoundingVert = new float[ numVert*(frontOnly ? 3:6) ];
404

    
405
    for(int i=0; i<lVert; i++)
406
      {
407
      mBoundingVert[3*i  ] = tempL[2*i  ];
408
      mBoundingVert[3*i+1] = tempL[2*i+1];
409
      mBoundingVert[3*i+2] = FRONTZ;
410
      }
411

    
412
    for(int i=0; i<rVert; i++)
413
      {
414
      mBoundingVert[3*(i+lVert)  ] = tempR[2*i  ];
415
      mBoundingVert[3*(i+lVert)+1] = tempR[2*i+1];
416
      mBoundingVert[3*(i+lVert)+2] = FRONTZ;
417
      }
418

    
419
    if( !frontOnly )
420
      {
421
      for(int i=0; i<numVert; i++)
422
        {
423
        mBoundingVert[3*(i+numVert)  ] = mBoundingVert[3*i  ];
424
        mBoundingVert[3*(i+numVert)+1] = mBoundingVert[3*i+1];
425
        mBoundingVert[3*(i+numVert)+2] = BACKZ;
426
        }
427
      }
428
    }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431
// Mark all the 'regions' of our grid  - i.e. separate pieces of 'land' (connected blocks that will 
432
// be rendered) and 'water' (connected holes in between) with integers. Each connected block of land
433
// gets a unique odd integer, each connected block of water a unique even integer.
434
//
435
// Water on the edges of the grid is also considered connected to itself!   
436
//   
437
// This function also creates a list of 'Edges'. Each Edge is a data structure from which later on we
438
// will start building the side walls of each connected block of land (and sides of holes of water
439
// inside). Each Edge needs to point from Land to Water (thus the '(SOUTH,i-1,j)' below) - otherwise
440
// later on setting up normal vectors wouldn't work.
441
   
442
  private void markRegions()
443
     {
444
     int i, j, numWater=1, numLand=0;
445
     
446
     for(i=0; i<mRows;i++) if( mCubes[      i][      0]==0 ) markRegion((short)2,      i,       0);
447
     for(i=0; i<mRows;i++) if( mCubes[      i][mCols-1]==0 ) markRegion((short)2,      i, mCols-1);
448
     for(i=0; i<mCols;i++) if( mCubes[0      ][      i]==0 ) markRegion((short)2,      0,       i);
449
     for(i=0; i<mCols;i++) if( mCubes[mRows-1][      i]==0 ) markRegion((short)2,mRows-1,       i);
450
           
451
     for(i=0; i<mRows; i++)
452
        for(j=0; j<mCols; j++)
453
           {
454
           if( mCubes[i][j] == 0 ) { numWater++; markRegion( (short)(2*numWater ),i,j); mEdges.add(new Edge(SOUTH,i-1,j)); }
455
           if( mCubes[i][j] == 1 ) { numLand ++; markRegion( (short)(2*numLand+1),i,j); mEdges.add(new Edge(NORTH,i  ,j)); }
456
           }
457
     
458
     // now we potentially need to kick out some Edges . Otherwise the following does not work:
459
     //
460
     // 0 1 0
461
     // 1 0 1
462
     // 0 1 0
463
     
464
     mEdgeNum= mEdges.size();
465
     int initCol, initRow, initSide, lastSide;
466
     Edge e1,e2;
467
     
468
     for(i=0; i<mEdgeNum; i++)
469
       {
470
       e1 = mEdges.get(i);
471
       initRow= e1.row;
472
       initCol= e1.col;
473
       initSide=e1.side;
474

    
475
       do
476
         {
477
         //android.util.Log.d("CUBES", "checking edge "+debug(e1));
478

    
479
         mSideWalls++;
480

    
481
         if( e1.side==NORTH || e1.side==SOUTH )
482
           {
483
           for(j=i+1;j<mEdgeNum;j++)
484
             {
485
             e2 = mEdges.get(j);
486

    
487
             if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col )
488
               {
489
               mEdges.remove(j);
490
               mEdgeNum--;
491
               j--;
492

    
493
               //android.util.Log.e("CUBES", "removing edge "+debug(e2));
494
               }
495
             }
496
           }
497

    
498
         lastSide = e1.side;
499
         e1 = getNextEdge(e1);
500
         if( e1.side!=lastSide ) mSideBends++;
501
         }
502
       while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide );
503
       }
504
     }
505

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507
// when calling, make sure that newVal != val
508
   
509
  private void markRegion(short newVal, int row, int col)
510
     {
511
     int val = mCubes[row][col];
512
     mCubes[row][col] = newVal;
513
     
514
     if( row>0       && mCubes[row-1][col  ]==val ) markRegion(newVal, row-1, col  );
515
     if( row<mRows-1 && mCubes[row+1][col  ]==val ) markRegion(newVal, row+1, col  );
516
     if( col>0       && mCubes[row  ][col-1]==val ) markRegion(newVal, row  , col-1);
517
     if( col<mCols-1 && mCubes[row  ][col+1]==val ) markRegion(newVal, row  , col+1);
518
     }
519
   
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521
   
522
  private void createNormals(boolean front, int row, int col)
523
     {
524
     int td,lr; 
525
      
526
     int nw = (col>0       && row>0      ) ? (mCubes[row-1][col-1]%2) : 0;
527
     int w  = (col>0                     ) ? (mCubes[row  ][col-1]%2) : 0;
528
     int n  = (               row>0      ) ? (mCubes[row-1][col  ]%2) : 0;
529
     int c  =                                (mCubes[row  ][col  ]%2);
530
     int sw = (col>0       && row<mRows-1) ? (mCubes[row+1][col-1]%2) : 0;
531
     int s  = (               row<mRows-1) ? (mCubes[row+1][col  ]%2) : 0;
532
     int ne = (col<mCols-1 && row>0      ) ? (mCubes[row-1][col+1]%2) : 0;
533
     int e  = (col<mCols-1               ) ? (mCubes[row  ][col+1]%2) : 0;
534
     int se = (col<mCols-1 && row<mRows-1) ? (mCubes[row+1][col+1]%2) : 0;
535

    
536
     if(front)
537
       {
538
       mNormalZ[0] = 1.0f;
539
       mNormalZ[1] = 1.0f;
540
       mNormalZ[2] = 1.0f;
541
       mNormalZ[3] = 1.0f;
542
       }
543
     else
544
       {
545
       mNormalZ[0] =-1.0f;
546
       mNormalZ[1] =-1.0f;
547
       mNormalZ[2] =-1.0f;
548
       mNormalZ[3] =-1.0f;
549
       }
550

    
551
     td = nw+n-w-c;
552
     lr = c+n-w-nw;
553
     if( td<0 ) td=-1;
554
     if( td>0 ) td= 1;
555
     if( lr<0 ) lr=-1;
556
     if( lr>0 ) lr= 1;
557
     mNormalX[0] = lr*R;
558
     mNormalY[0] = td*R;
559
     
560
     td = w+c-sw-s;
561
     lr = c+s-w-sw;
562
     if( td<0 ) td=-1;
563
     if( td>0 ) td= 1;
564
     if( lr<0 ) lr=-1;
565
     if( lr>0 ) lr= 1;
566
     mNormalX[1] = lr*R;
567
     mNormalY[1] = td*R;
568
     
569
     td = n+ne-c-e;
570
     lr = e+ne-c-n;
571
     if( td<0 ) td=-1;
572
     if( td>0 ) td= 1;
573
     if( lr<0 ) lr=-1;
574
     if( lr>0 ) lr= 1;
575
     mNormalX[2] = lr*R;
576
     mNormalY[2] = td*R;
577
     
578
     td = c+e-s-se;
579
     lr = e+se-c-s;
580
     if( td<0 ) td=-1;
581
     if( td>0 ) td= 1;
582
     if( lr<0 ) lr=-1;
583
     if( lr>0 ) lr= 1;
584
     mNormalX[3] = lr*R;
585
     mNormalY[3] = td*R;
586
     /*
587
     android.util.Log.d("CUBES", "row="+row+" col="+col);
588
     android.util.Log.d("CUBES", mNormalX[0]+" "+mNormalY[0]);
589
     android.util.Log.d("CUBES", mNormalX[1]+" "+mNormalY[1]);
590
     android.util.Log.d("CUBES", mNormalX[2]+" "+mNormalY[2]);
591
     android.util.Log.d("CUBES", mNormalX[3]+" "+mNormalY[3]);
592
     */
593
     }
594

    
595
///////////////////////////////////////////////////////////////////////////////////////////////////
596

    
597
  private int buildFrontBackGrid(boolean front, int vertex, float[] attribs)
598
     {
599
     int last, current;
600
     boolean seenLand=false;
601
     boolean lastBlockIsNE = false;
602
     boolean currentBlockIsNE;
603
     float vectZ = front?FRONTZ:BACKZ;
604

    
605
     //android.util.Log.d("CUBES", "buildFrontBack");
606

    
607
     for(int row=0; row<mRows; row++)
608
       {
609
       last =0;
610
         
611
       for(int col=0; col<mCols; col++)
612
         {
613
         current = mCubes[row][col];
614

    
615
         if( current%2 == 1 )
616
           {
617
           currentBlockIsNE = isNE(row,col);
618

    
619
           if( !seenLand && !front && ((vertex%2==1)^currentBlockIsNE) )
620
             {
621
             //android.util.Log.d("CUBES","repeating winding2 vertex");
622

    
623
             vertex = repeatLast(vertex,attribs);
624
             }
625

    
626
           createNormals(front,row,col);
627

    
628
           if( currentBlockIsNE )
629
             {
630
             if( (last!=current) || !lastBlockIsNE )
631
               {
632
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
633
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, attribs);
634
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
635
               if( !lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,attribs);
636
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, attribs);
637
               }
638
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row, attribs);
639
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, attribs);
640
             }
641
           else
642
             {
643
             if( (last!=current) || lastBlockIsNE )
644
               {
645
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
646
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, attribs);
647
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
648
               if( lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,attribs);
649
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, attribs);
650
               }
651
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, attribs);
652
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row  , attribs);
653
             }
654

    
655
           seenLand = true;
656
           lastBlockIsNE = currentBlockIsNE;
657
           }
658
            
659
         last = current;
660
         }
661
       }
662
     
663
     return vertex;
664
     }
665

    
666
///////////////////////////////////////////////////////////////////////////////////////////////////
667

    
668
  private int repeatLast(int vertex, float[] attribs)
669
     {
670
     //android.util.Log.e("CUBES", "repeating last vertex!");
671

    
672
     if( vertex>0 )
673
       {
674
       remainingVert--;
675

    
676
       attribs[8*vertex  ] = attribs[8*vertex-8];
677
       attribs[8*vertex+1] = attribs[8*vertex-7];
678
       attribs[8*vertex+2] = attribs[8*vertex-6];
679
       attribs[8*vertex+3] = attribs[8*vertex-5];
680
       attribs[8*vertex+4] = attribs[8*vertex-4];
681
       attribs[8*vertex+5] = attribs[8*vertex-3];
682
       attribs[8*vertex+6] = attribs[8*vertex-2];
683
       attribs[8*vertex+7] = attribs[8*vertex-1];
684
         
685
       vertex++;     
686
       }
687
     
688
     return vertex;
689
     }
690
   
691
///////////////////////////////////////////////////////////////////////////////////////////////////
692

    
693
  private int buildSideGrid(int vertex, float[] attribs)
694
     {
695
     //android.util.Log.d("CUBES", "buildSide");
696

    
697
     for(int i=0; i<mEdgeNum; i++)
698
       {
699
       vertex = buildIthSide(mEdges.get(i), vertex, attribs);
700
       } 
701
      
702
     return vertex;
703
     }
704

    
705
///////////////////////////////////////////////////////////////////////////////////////////////////
706

    
707
  private int buildIthSide(Edge curr, int vertex, float[] attribs)
708
     {
709
     Edge prev; 
710
     
711
     if( curr.side==NORTH ) // water outside
712
       {
713
       prev = new Edge(WEST,curr.row,curr.col);
714
       }
715
     else                   // land outside; we need to move forward one link because we are going in opposite direction and we need to start from a bend.
716
       {
717
       prev = curr;
718
       curr = new Edge(EAST,curr.row+1,curr.col-1);
719
       }
720
     
721
     int col = curr.col;
722
     int row = curr.row;
723
     int side= curr.side;  
724
     Edge next = getNextEdge(curr);
725
     
726
     addSideVertex(curr,BACK,LOWER,prev.side,vertex,attribs);
727
     vertex++;
728
     
729
     do
730
       {
731
       if( prev.side!=curr.side )
732
         {
733
         addSideVertex(curr,BACK,LOWER,prev.side,vertex,attribs);
734
         vertex++;
735
         addSideVertex(curr,BACK,UPPER,prev.side,vertex,attribs);
736
         vertex++;
737
         }
738
       
739
       addSideVertex(curr,FRONT,LOWER,next.side,vertex,attribs);
740
       vertex++;
741
       addSideVertex(curr,FRONT,UPPER,next.side,vertex,attribs);
742
       vertex++;
743
       
744
       prev = curr;
745
       curr = next; 
746
       next = getNextEdge(curr);
747
       }
748
     while( curr.col!=col || curr.row!=row || curr.side!=side );
749
     
750
     vertex = repeatLast(vertex,attribs);
751
     
752
     return vertex;
753
     }
754

    
755
///////////////////////////////////////////////////////////////////////////////////////////////////
756

    
757
  private Edge getNextEdge(Edge curr)
758
     {
759
     int col = curr.col;
760
     int row = curr.row;
761
      
762
     //android.util.Log.e("CUBES", "row="+row+" col="+col+" mRows="+mRows+" mCols="+mCols);
763
                       
764
     switch(curr.side) 
765
       {
766
       case NORTH: if( col==mCols-1 ) 
767
                     return new Edge(EAST,row,col);
768
                   if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] )
769
                     return new Edge(WEST,row-1,col+1);
770
                   if( mCubes[row][col+1]==mCubes[row][col] )
771
                     return new Edge(NORTH,row,col+1);
772
                   else  
773
                     return new Edge(EAST,row,col);
774
                   
775
       case SOUTH: if( col==0 ) 
776
                     return new Edge(WEST,row,col);
777
                   if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] )
778
                     return new Edge(EAST,row+1,col-1); 
779
                   if( mCubes[row][col-1]==mCubes[row][col] )
780
                     return new Edge(SOUTH,row,col-1);
781
                   else
782
                     return new Edge(WEST,row,col); 
783
                     
784
       case EAST : if( row==mRows-1 ) 
785
                     return new Edge(SOUTH,row,col);
786
                   if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] )
787
                     return new Edge(NORTH,row+1,col+1);
788
                   if( mCubes[row+1][col]==mCubes[row][col] )
789
                     return new Edge(EAST,row+1,col);
790
                   else 
791
                     return new Edge(SOUTH,row,col);
792
                   
793
       default   : if( row==0 )
794
                     return new Edge(NORTH,row,col);
795
                   if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] )
796
                     return new Edge(SOUTH,row-1,col-1);
797
                   if( mCubes[row-1][col]==mCubes[row][col] )
798
                     return new Edge(WEST,row-1,col);
799
                   else
800
                     return new Edge(NORTH,row,col);     
801
       }
802
     }
803

    
804
///////////////////////////////////////////////////////////////////////////////////////////////////
805

    
806
  private int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] attribs)
807
     {
808
     remainingVert--;
809

    
810
     float x = (float)col/mCols;
811
     float y = (float)row/mRows;
812

    
813
     attribs[8*vertex  ] = x-0.5f;
814
     attribs[8*vertex+1] = 0.5f-y;
815
     attribs[8*vertex+2] = vectZ;
816
     attribs[8*vertex+3] = mNormalX[index];
817
     attribs[8*vertex+4] = mNormalY[index];
818
     attribs[8*vertex+5] = mNormalZ[index];
819
     attribs[8*vertex+6] = x;
820
     attribs[8*vertex+7] = 1.0f-y;
821

    
822
     return vertex+1;
823
     }
824

    
825
///////////////////////////////////////////////////////////////////////////////////////////////////
826
   
827
  private void addSideVertex(Edge curr, boolean back, boolean lower,int side, int vertex, float[] attribs)
828
     {
829
     //android.util.Log.e("CUBES", "adding Side vertex!");
830

    
831
     remainingVert--;
832

    
833
     float x, y;
834

    
835
     switch(curr.side)
836
       {
837
       case NORTH: x = (float)(back ? (curr.col  ):(curr.col+1))/mCols;
838

    
839
                   attribs[8*vertex  ] = x - 0.5f;
840
                   attribs[8*vertex+1] = 0.5f - (float)curr.row/mRows;
841
                   attribs[8*vertex+2] = lower ? BACKZ : FRONTZ;
842
                   attribs[8*vertex+3] = side==NORTH ? 0.0f : (side==WEST?-R:R);
843
                   attribs[8*vertex+4] = 1.0f;
844
                   attribs[8*vertex+5] = lower ? -R:R;
845
                   attribs[8*vertex+6] = x;
846
                   attribs[8*vertex+7] = 1.0f-(float)(lower? (curr.row-1):(curr.row  ))/mRows;
847
                   break;
848
       case SOUTH: x = (float)(back ? (curr.col+1):(curr.col  ))/mCols;
849

    
850
                   attribs[8*vertex  ] = x - 0.5f;
851
                   attribs[8*vertex+1] = 0.5f - (float)(curr.row+1)/mRows;
852
                   attribs[8*vertex+2] = lower ? BACKZ : FRONTZ;
853
                   attribs[8*vertex+3] = side==SOUTH ? 0.0f: (side==EAST?-R:R);
854
                   attribs[8*vertex+4] =-1.0f;
855
                   attribs[8*vertex+5] = lower ? -R:R;
856
                   attribs[8*vertex+6] = x;
857
                   attribs[8*vertex+7] = 1.0f-(float)(lower? (curr.row+2):(curr.row+1))/mRows;
858
                   break;
859
       case WEST : y = (float)(back  ? (curr.row+1):(curr.row))/mRows;
860

    
861
                   attribs[8*vertex  ] = (float)curr.col/mCols -0.5f;
862
                   attribs[8*vertex+1] = 0.5f - y;
863
                   attribs[8*vertex+2] = lower ? BACKZ : FRONTZ;
864
                   attribs[8*vertex+3] =-1.0f;
865
                   attribs[8*vertex+4] = side==WEST ? 0.0f : (side==NORTH?-R:R);
866
                   attribs[8*vertex+5] = lower ? -R:R;
867
                   attribs[8*vertex+6] = (float)(lower ? (curr.col-1):(curr.col  ))/mCols;
868
                   attribs[8*vertex+7] = 1.0f - y;
869
                   break;
870
       case EAST : y = (float)(back  ? (curr.row):(curr.row+1))/mRows;
871

    
872
                   attribs[8*vertex  ] = (float)(curr.col+1)/mCols -0.5f;
873
                   attribs[8*vertex+1] = 0.5f - y;
874
                   attribs[8*vertex+2] = lower ? BACKZ : FRONTZ;
875
                   attribs[8*vertex+3] = 1.0f;
876
                   attribs[8*vertex+4] = side==EAST ? 0.0f : (side==SOUTH?-R:R);
877
                   attribs[8*vertex+5] = lower ? -R:R;
878
                   attribs[8*vertex+6] = (float)(lower ? (curr.col+2):(curr.col+1))/mCols;
879
                   attribs[8*vertex+7] = 1.0f - y;
880
                   break;
881
       }
882
     
883
     if(attribs[8*vertex+6]>1.0f) attribs[8*vertex+6] = 2.0f-attribs[8*vertex+6];
884
     if(attribs[8*vertex+6]<0.0f) attribs[8*vertex+6] =     -attribs[8*vertex+6];
885
     if(attribs[8*vertex+7]>1.0f) attribs[8*vertex+7] = 2.0f-attribs[8*vertex+7];
886
     if(attribs[8*vertex+7]<0.0f) attribs[8*vertex+7] =     -attribs[8*vertex+7];
887
     }
888

    
889
///////////////////////////////////////////////////////////////////////////////////////////////////
890

    
891
  private void build(boolean frontOnly)
892
     {
893
     int vertSoFar=0;
894
     float[] attribs= new float[(POSITION_DATA_SIZE+NORMAL_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
895

    
896
     //android.util.Log.d("MeshCubes","building front grid...");
897

    
898
     vertSoFar = buildFrontBackGrid(true, vertSoFar,attribs);
899

    
900
     if( !frontOnly )
901
       {
902
       vertSoFar = repeatLast(vertSoFar,attribs);
903
       if( vertSoFar%2==1 )
904
         {
905
         //android.util.Log.d("MeshCubes","repeating winding1 vertex");
906

    
907
         vertSoFar = repeatLast(vertSoFar,attribs);
908
         }
909

    
910
       //android.util.Log.d("MeshCubes","building side grid...");
911

    
912
       vertSoFar = buildSideGrid (vertSoFar,attribs);
913

    
914
       //android.util.Log.d("MeshCubes","building back grid...");
915

    
916
       buildFrontBackGrid (false,vertSoFar,attribs);
917
       }
918

    
919
     //android.util.Log.e("MeshCubes", "dataLen="+numVertices);
920
     //android.util.Log.d("MeshCubes", "attribs: "+debug(attribs,8) );
921

    
922
     mEdges.clear();
923
     mEdges = null;
924
     mCubes = null;
925

    
926
     if( remainingVert!=0 )
927
       android.util.Log.e("MeshCubes", "remainingVert " +remainingVert );
928

    
929
     mVertAttribs = ByteBuffer.allocateDirect(numVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
930
     mVertAttribs.put(attribs).position(0);
931
     }
932

    
933
///////////////////////////////////////////////////////////////////////////////////////////////////
934

    
935
  float[] getBoundingVertices()
936
     {
937
     return mBoundingVert;
938
     }
939

    
940
///////////////////////////////////////////////////////////////////////////////////////////////////
941
// PUBLIC API
942
///////////////////////////////////////////////////////////////////////////////////////////////////
943
/**
944
 * Creates the underlying mesh of vertices, normals, texture coords.
945
 *    
946
 * @param cols      Integer helping to parse the next parameter.
947
 * @param desc      String describing the subset of a MxNx1 cuboid that we want to create.
948
 *                  Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
949
 *                  <p></p>
950
 *                  <p>
951
 *                  <pre>
952
 *                  For example, (cols=2, desc="111010") describes the following shape:
953
 *
954
 *                  XX
955
 *                  X
956
 *                  X
957
 *
958
 *                  whereas (cols=2,desc="110001") describes
959
 *
960
 *                  XX
961
 *
962
 *                   X
963
 *                  </pre>
964
 *                  </p>
965
 * @param frontOnly Only create the front wall or side and back as well?
966
 */
967
 public MeshCubes(int cols, String desc, boolean frontOnly)
968
   {
969
   super(frontOnly ? 0.0f:1.0f/cols);
970
   prepareDataStructures(cols,desc,frontOnly);
971
   build(frontOnly);
972
   }
973

    
974
///////////////////////////////////////////////////////////////////////////////////////////////////
975
/**
976
 * Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors.
977
 *
978
 * @param cols      Number of columns.
979
 * @param rows      Number of rows.
980
 * @param frontOnly Only create the front wall or side and back as well?
981
 */
982
 public MeshCubes(int cols, int rows, boolean frontOnly)
983
   {
984
   super(frontOnly ? 0.0f:1.0f/cols);
985
   prepareDataStructures(cols,rows,frontOnly);
986
   build(frontOnly);
987
   }
988
 }
(24-24/26)