Project

General

Profile

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

library / src / main / java / org / distorted / library / MeshCubes.java @ 69ed1eb4

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 short[][] 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
     dataLength=0;
191
     
192
     if( cols>0 && desc.contains("1") )
193
       {
194
       mCols = cols;
195
       mRows = desc.length()/cols;
196

    
197
       mCubes = new short[mRows][mCols];
198
       
199
       for(int j=0; j<mCols; j++)
200
         for(int i=0; i<mRows; i++)
201
           mCubes[i][j] = (short)(desc.charAt(i*mCols+j) == '1' ? 1:0);
202
       
203
       markRegions();
204
       dataLength = computeDataLength(frontOnly);
205

    
206
       remainingVert = dataLength;
207
       buildBoundingVert(mCubes,frontOnly);
208
       }
209
     }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
// full grid
213

    
214
   private void prepareDataStructures(int cols, int rows, boolean frontOnly)
215
     {
216
     mRows     =rows;
217
     mCols     =cols;
218
     dataLength=   0;
219

    
220
     if( cols>0 && rows>0 )
221
       {
222
       mCubes = new short[mRows][mCols];
223

    
224
       for(int j=0; j<mCols; j++)
225
         for(int i=0; i<mRows; i++)
226
           mCubes[i][j] = (short)1;
227

    
228
       markRegions();
229
       dataLength = computeDataLength(frontOnly);
230

    
231
       remainingVert = dataLength;
232
       }
233

    
234
     mBoundingVert = frontOnly ? mBoundingVert1 : mBoundingVert2;
235
     }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
  private float retLeftmost(short[][] cubes, int row)
240
    {
241

    
242
    return 0.0f;
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
 private float retRightmost(short[][] cubes, int row)
248
    {
249
    if( row==0 )
250
      {
251
      for(int i=0; i<mCols; i++)
252
        {
253

    
254
        }
255
      }
256
    else if(row==mRows)
257
      {
258
      for(int i=0; i<mCols; i++)
259
        {
260

    
261
        }
262
      }
263
    else
264
      {
265
      for(int i=0; i<mCols; i++)
266
        {
267

    
268
        }
269
      }
270

    
271
    return 0.0f;
272
    }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
  private int addLeftmost(float[] temp, short[][] cubes, int row, int index)
277
    {
278
    float x2 = retLeftmost(cubes,row);
279
    float y2 = row/mRows - 0.5f;
280

    
281
    if( index>1 )
282
      {
283
      float x0 = temp[2*index-4];
284
      float y0 = temp[2*index-3];
285
      float x1 = temp[2*index-2];
286
      float y1 = temp[2*index-1];
287

    
288
      if( (x0-x2)*(y0-y1) <= (x0-x1)*(y0-y2) )
289
        {
290
        temp[2*index-2] = x2;
291
        temp[2*index-1] = y2;
292
        return index;
293
        }
294
      }
295

    
296
    temp[2*index+0] = x2;
297
    temp[2*index+1] = y2;
298
    return index+1;
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
  private int addRightmost(float[] temp, short[][] cubes, int row, int index)
304
    {
305
    float x2 = retRightmost(cubes,row);
306
    float y2 = row/mRows - 0.5f;
307

    
308
    if( index>1 )
309
      {
310
      float x0 = temp[2*index-4];
311
      float y0 = temp[2*index-3];
312
      float x1 = temp[2*index-2];
313
      float y1 = temp[2*index-1];
314

    
315
      if( (x0-x2)*(y0-y1) >= (x0-x1)*(y0-y2) )
316
        {
317
        temp[2*index-2] = x2;
318
        temp[2*index-1] = y2;
319
        return index;
320
        }
321
      }
322

    
323
    temp[2*index+0] = x2;
324
    temp[2*index+1] = y2;
325
    return index+1;
326
    }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329
// fill up the mBoundingVert array with the smallest set of vertices which form the same convex hull
330
// like the whole thing.
331

    
332
  private void buildBoundingVert(short[][] cubes, boolean frontOnly)
333
    {
334
    int lVert=0, rVert=0;
335
    float[] tempL = new float[2*(mRows+1)];
336
    float[] tempR = new float[2*(mRows+1)];
337

    
338
    for(int i=0; i<=mRows; i++)
339
      {
340
      lVert = addLeftmost (tempL,cubes,i,lVert);
341
      rVert = addRightmost(tempR,cubes,i,rVert);
342
      }
343

    
344
    int numVert = lVert+rVert;
345

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

    
348
    for(int i=0; i<lVert; i++)
349
      {
350
      mBoundingVert[3*i  ] = tempL[2*i  ];
351
      mBoundingVert[3*i+1] = tempL[2*i+1];
352
      mBoundingVert[3*i+2] = FRONTZ;
353
      }
354

    
355
    for(int i=0; i<rVert; i++)
356
      {
357
      mBoundingVert[3*(i+lVert)  ] = tempR[2*i  ];
358
      mBoundingVert[3*(i+lVert)+1] = tempR[2*i+1];
359
      mBoundingVert[3*(i+lVert)+2] = FRONTZ;
360
      }
361

    
362
    if( !frontOnly )
363
      {
364
      for(int i=0; i<numVert; i++)
365
        {
366
        mBoundingVert[3*(i+numVert)  ] = mBoundingVert[3*i  ];
367
        mBoundingVert[3*(i+numVert)+1] = mBoundingVert[3*i+1];
368
        mBoundingVert[3*(i+numVert)+2] = BACKZ;
369
        }
370
      }
371
    }
372

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374
// Mark all the 'regions' of our grid  - i.e. separate pieces of 'land' (connected blocks that will 
375
// be rendered) and 'water' (connected holes in between) with integers. Each connected block of land
376
// gets a unique odd integer, each connected block of water a unique even integer.
377
//
378
// Water on the edges of the grid is also considered connected to itself!   
379
//   
380
// This function also creates a list of 'Edges'. Each Edge is a data structure from which later on we
381
// will start building the side walls of each connected block of land (and sides of holes of water
382
// inside). Each Edge needs to point from Land to Water (thus the '(SOUTH,i-1,j)' below) - otherwise
383
// later on setting up normal vectors wouldn't work.
384
   
385
   private void markRegions()
386
     {
387
     int i, j, numWater=1, numLand=0;
388
     
389
     for(i=0; i<mRows;i++) if( mCubes[      i][      0]==0 ) markRegion((short)2,      i,       0);
390
     for(i=0; i<mRows;i++) if( mCubes[      i][mCols-1]==0 ) markRegion((short)2,      i, mCols-1);
391
     for(i=0; i<mCols;i++) if( mCubes[0      ][      i]==0 ) markRegion((short)2,      0,       i);
392
     for(i=0; i<mCols;i++) if( mCubes[mRows-1][      i]==0 ) markRegion((short)2,mRows-1,       i);
393
           
394
     for(i=0; i<mRows; i++)
395
        for(j=0; j<mCols; j++)
396
           {
397
           if( mCubes[i][j] == 0 ) { numWater++; markRegion( (short)(2*numWater ),i,j); mEdges.add(new Edge(SOUTH,i-1,j)); }
398
           if( mCubes[i][j] == 1 ) { numLand ++; markRegion( (short)(2*numLand+1),i,j); mEdges.add(new Edge(NORTH,i  ,j)); }
399
           }
400
     
401
     // now we potentially need to kick out some Edges . Otherwise the following does not work:
402
     //
403
     // 0 1 0
404
     // 1 0 1
405
     // 0 1 0
406
     
407
     mEdgeNum= mEdges.size();
408
     int initCol, initRow, initSide, lastSide;
409
     Edge e1,e2;
410
     
411
     for(i=0; i<mEdgeNum; i++)
412
       {
413
       e1 = mEdges.get(i);
414
       initRow= e1.row;
415
       initCol= e1.col;
416
       initSide=e1.side;
417

    
418
       do
419
         {
420
         //android.util.Log.d("CUBES", "checking edge "+debug(e1));
421

    
422
         mSideWalls++;
423

    
424
         if( e1.side==NORTH || e1.side==SOUTH )
425
           {
426
           for(j=i+1;j<mEdgeNum;j++)
427
             {
428
             e2 = mEdges.get(j);
429

    
430
             if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col )
431
               {
432
               mEdges.remove(j);
433
               mEdgeNum--;
434
               j--;
435

    
436
               //android.util.Log.e("CUBES", "removing edge "+debug(e2));
437
               }
438
             }
439
           }
440

    
441
         lastSide = e1.side;
442
         e1 = getNextEdge(e1);
443
         if( e1.side!=lastSide ) mSideBends++;
444
         }
445
       while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide );
446
       }
447
     }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450
// when calling, make sure that newVal != val
451
   
452
   private void markRegion(short newVal, int row, int col)
453
     {
454
     short val = mCubes[row][col];
455
     mCubes[row][col] = newVal;
456
     
457
     if( row>0       && mCubes[row-1][col  ]==val ) markRegion(newVal, row-1, col  );
458
     if( row<mRows-1 && mCubes[row+1][col  ]==val ) markRegion(newVal, row+1, col  );
459
     if( col>0       && mCubes[row  ][col-1]==val ) markRegion(newVal, row  , col-1);
460
     if( col<mCols-1 && mCubes[row  ][col+1]==val ) markRegion(newVal, row  , col+1);
461
     }
462
   
463
///////////////////////////////////////////////////////////////////////////////////////////////////
464
   
465
   private void createNormals(boolean front, int row, int col)
466
     {
467
     int td,lr; 
468
      
469
     int nw = (col>0       && row>0      ) ? (mCubes[row-1][col-1]%2) : 0;
470
     int w  = (col>0                     ) ? (mCubes[row  ][col-1]%2) : 0;
471
     int n  = (               row>0      ) ? (mCubes[row-1][col  ]%2) : 0;
472
     int c  =                                (mCubes[row  ][col  ]%2);
473
     int sw = (col>0       && row<mRows-1) ? (mCubes[row+1][col-1]%2) : 0;
474
     int s  = (               row<mRows-1) ? (mCubes[row+1][col  ]%2) : 0;
475
     int ne = (col<mCols-1 && row>0      ) ? (mCubes[row-1][col+1]%2) : 0;
476
     int e  = (col<mCols-1               ) ? (mCubes[row  ][col+1]%2) : 0;
477
     int se = (col<mCols-1 && row<mRows-1) ? (mCubes[row+1][col+1]%2) : 0;
478

    
479
     if(front)
480
       {
481
       mNormalZ[0] = 1.0f;
482
       mNormalZ[1] = 1.0f;
483
       mNormalZ[2] = 1.0f;
484
       mNormalZ[3] = 1.0f;
485
       }
486
     else
487
       {
488
       mNormalZ[0] =-1.0f;
489
       mNormalZ[1] =-1.0f;
490
       mNormalZ[2] =-1.0f;
491
       mNormalZ[3] =-1.0f;
492
       }
493

    
494
     td = nw+n-w-c;
495
     lr = c+n-w-nw;
496
     if( td<0 ) td=-1;
497
     if( td>0 ) td= 1;
498
     if( lr<0 ) lr=-1;
499
     if( lr>0 ) lr= 1;
500
     mNormalX[0] = lr*R;
501
     mNormalY[0] = td*R;
502
     
503
     td = w+c-sw-s;
504
     lr = c+s-w-sw;
505
     if( td<0 ) td=-1;
506
     if( td>0 ) td= 1;
507
     if( lr<0 ) lr=-1;
508
     if( lr>0 ) lr= 1;
509
     mNormalX[1] = lr*R;
510
     mNormalY[1] = td*R;
511
     
512
     td = n+ne-c-e;
513
     lr = e+ne-c-n;
514
     if( td<0 ) td=-1;
515
     if( td>0 ) td= 1;
516
     if( lr<0 ) lr=-1;
517
     if( lr>0 ) lr= 1;
518
     mNormalX[2] = lr*R;
519
     mNormalY[2] = td*R;
520
     
521
     td = c+e-s-se;
522
     lr = e+se-c-s;
523
     if( td<0 ) td=-1;
524
     if( td>0 ) td= 1;
525
     if( lr<0 ) lr=-1;
526
     if( lr>0 ) lr= 1;
527
     mNormalX[3] = lr*R;
528
     mNormalY[3] = td*R;
529
     /*
530
     android.util.Log.d("CUBES", "row="+row+" col="+col);
531
     android.util.Log.d("CUBES", mNormalX[0]+" "+mNormalY[0]);
532
     android.util.Log.d("CUBES", mNormalX[1]+" "+mNormalY[1]);
533
     android.util.Log.d("CUBES", mNormalX[2]+" "+mNormalY[2]);
534
     android.util.Log.d("CUBES", mNormalX[3]+" "+mNormalY[3]);
535
     */
536
     }
537
   
538
///////////////////////////////////////////////////////////////////////////////////////////////////
539

    
540
   private int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] position, float[] normal, float[] texture)
541
     {
542
     remainingVert--;
543

    
544
     float x = (float)col/mCols;
545
     float y = (float)row/mRows;
546

    
547
     position[3*vertex  ] = x-0.5f;
548
     position[3*vertex+1] = 0.5f-y;
549
     position[3*vertex+2] = vectZ;
550
     normal[3*vertex  ]   = mNormalX[index];
551
     normal[3*vertex+1]   = mNormalY[index];
552
     normal[3*vertex+2]   = mNormalZ[index];
553
     texture[2*vertex  ]  = x;
554
     texture[2*vertex+1]  = 1.0f-y;
555

    
556
     return vertex+1;
557
     }
558

    
559
///////////////////////////////////////////////////////////////////////////////////////////////////
560

    
561
   private int buildFrontBackGrid(boolean front, int vertex, float[] position, float[] normal, float[] texture)
562
     {
563
     short last, current;
564
     boolean seenLand=false;
565
     boolean lastBlockIsNE = false;
566
     boolean currentBlockIsNE;
567
     float vectZ = front?FRONTZ:BACKZ;
568

    
569
     //android.util.Log.d("CUBES", "buildFrontBack");
570

    
571
     for(int row=0; row<mRows; row++)
572
       {
573
       last =0;
574
         
575
       for(int col=0; col<mCols; col++)
576
         {
577
         current = mCubes[row][col];
578

    
579
         if( current%2 == 1 )
580
           {
581
           currentBlockIsNE = isNE(row,col);
582

    
583
           if( !seenLand && !front && ((vertex%2==1)^currentBlockIsNE) )
584
             {
585
             //android.util.Log.d("CUBES","repeating winding2 vertex");
586

    
587
             vertex = repeatLast(vertex,position,normal,texture);
588
             }
589

    
590
           createNormals(front,row,col);
591

    
592
           if( currentBlockIsNE )
593
             {
594
             if( (last!=current) || !lastBlockIsNE )
595
               {
596
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
597
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
598
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
599
               if( !lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
600
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
601
               }
602
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row, position, normal, texture);
603
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
604
             }
605
           else
606
             {
607
             if( (last!=current) || lastBlockIsNE )
608
               {
609
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
610
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
611
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
612
               if( lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
613
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
614
               }
615
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
616
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row  , position, normal, texture);
617
             }
618

    
619
           seenLand = true;
620
           lastBlockIsNE = currentBlockIsNE;
621
           }
622
            
623
         last = current;
624
         }
625
       }
626
     
627
     return vertex;
628
     }
629

    
630
///////////////////////////////////////////////////////////////////////////////////////////////////
631

    
632
   private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
633
     {
634
     //android.util.Log.e("CUBES", "repeating last vertex!");
635

    
636
     if( vertex>0 )
637
       {
638
       remainingVert--;
639

    
640
       position[3*vertex  ] = position[3*vertex-3];
641
       position[3*vertex+1] = position[3*vertex-2];
642
       position[3*vertex+2] = position[3*vertex-1];
643

    
644
       normal[3*vertex  ]   = normal[3*vertex-3];
645
       normal[3*vertex+1]   = normal[3*vertex-2];
646
       normal[3*vertex+2]   = normal[3*vertex-1];
647

    
648
       texture[2*vertex  ]  = texture[2*vertex-2];
649
       texture[2*vertex+1]  = texture[2*vertex-1];
650
         
651
       vertex++;     
652
       }
653
     
654
     return vertex;
655
     }
656
   
657
///////////////////////////////////////////////////////////////////////////////////////////////////
658

    
659
   private int buildSideGrid(int vertex, float[] position, float[] normal, float[] texture)
660
     {
661
     //android.util.Log.d("CUBES", "buildSide");
662

    
663
     for(int i=0; i<mEdgeNum; i++)
664
       {
665
       vertex = buildIthSide(mEdges.get(i), vertex, position, normal, texture);  
666
       } 
667
      
668
     return vertex;
669
     }
670

    
671
///////////////////////////////////////////////////////////////////////////////////////////////////
672

    
673
   private int buildIthSide(Edge curr, int vertex, float[] position, float[] normal, float[] texture)
674
     {
675
     Edge prev; 
676
     
677
     if( curr.side==NORTH ) // water outside
678
       {
679
       prev = new Edge(WEST,curr.row,curr.col);
680
       }
681
     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.
682
       {
683
       prev = curr;
684
       curr = new Edge(EAST,curr.row+1,curr.col-1);
685
       }
686
     
687
     int col = curr.col;
688
     int row = curr.row;
689
     int side= curr.side;  
690
     Edge next = getNextEdge(curr);
691
     
692
     addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
693
     vertex++;
694
     
695
     do
696
       {
697
       if( prev.side!=curr.side )
698
         {
699
         addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
700
         vertex++;
701
         addSideVertex(curr,BACK,UPPER,prev.side,vertex,position,normal,texture);
702
         vertex++;
703
         }
704
       
705
       addSideVertex(curr,FRONT,LOWER,next.side,vertex,position,normal,texture);
706
       vertex++;
707
       addSideVertex(curr,FRONT,UPPER,next.side,vertex,position,normal,texture);
708
       vertex++;
709
       
710
       prev = curr;
711
       curr = next; 
712
       next = getNextEdge(curr);
713
       }
714
     while( curr.col!=col || curr.row!=row || curr.side!=side );
715
     
716
     vertex = repeatLast(vertex,position,normal,texture);
717
     
718
     return vertex;
719
     }
720

    
721
///////////////////////////////////////////////////////////////////////////////////////////////////
722

    
723
   private Edge getNextEdge(Edge curr)
724
     {
725
     int col = curr.col;
726
     int row = curr.row;
727
      
728
     //android.util.Log.e("CUBES", "row="+row+" col="+col+" mRows="+mRows+" mCols="+mCols);
729
                       
730
     switch(curr.side) 
731
       {
732
       case NORTH: if( col==mCols-1 ) 
733
                     return new Edge(EAST,row,col);
734
                   if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] )
735
                     return new Edge(WEST,row-1,col+1);
736
                   if( mCubes[row][col+1]==mCubes[row][col] )
737
                     return new Edge(NORTH,row,col+1);
738
                   else  
739
                     return new Edge(EAST,row,col);
740
                   
741
       case SOUTH: if( col==0 ) 
742
                     return new Edge(WEST,row,col);
743
                   if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] )
744
                     return new Edge(EAST,row+1,col-1); 
745
                   if( mCubes[row][col-1]==mCubes[row][col] )
746
                     return new Edge(SOUTH,row,col-1);
747
                   else
748
                     return new Edge(WEST,row,col); 
749
                     
750
       case EAST : if( row==mRows-1 ) 
751
                     return new Edge(SOUTH,row,col);
752
                   if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] )
753
                     return new Edge(NORTH,row+1,col+1);
754
                   if( mCubes[row+1][col]==mCubes[row][col] )
755
                     return new Edge(EAST,row+1,col);
756
                   else 
757
                     return new Edge(SOUTH,row,col);
758
                   
759
       default   : if( row==0 )
760
                     return new Edge(NORTH,row,col);
761
                   if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] )
762
                     return new Edge(SOUTH,row-1,col-1);
763
                   if( mCubes[row-1][col]==mCubes[row][col] )
764
                     return new Edge(WEST,row-1,col);
765
                   else
766
                     return new Edge(NORTH,row,col);     
767
       }
768
     }
769

    
770
///////////////////////////////////////////////////////////////////////////////////////////////////
771
   
772
   private void addSideVertex(Edge curr, boolean back, boolean lower,int side, int vertex, float[] position, float[] normal, float[] texture)
773
     {
774
     //android.util.Log.e("CUBES", "adding Side vertex!");
775

    
776
     remainingVert--;
777

    
778
     float x, y;
779

    
780
     switch(curr.side)
781
       {
782
       case NORTH: x = (float)(back ? (curr.col  ):(curr.col+1))/mCols;
783

    
784
                   position[3*vertex  ] = x - 0.5f;
785
                   position[3*vertex+1] = 0.5f - (float)curr.row/mRows;
786
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
787

    
788
                   normal[3*vertex  ]   = side==NORTH ? 0.0f : (side==WEST?-R:R);
789
                   normal[3*vertex+1]   = 1.0f;
790
                   normal[3*vertex+2]   = lower ? -R:R;
791

    
792
                   texture[2*vertex  ]  = x;
793
                   texture[2*vertex+1]  = 1.0f-(float)(lower? (curr.row-1):(curr.row  ))/mRows;
794
                   break;
795
       case SOUTH: x = (float)(back ? (curr.col+1):(curr.col  ))/mCols;
796

    
797
                   position[3*vertex  ] = x - 0.5f;
798
                   position[3*vertex+1] = 0.5f - (float)(curr.row+1)/mRows;
799
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;  
800
            
801
                   normal[3*vertex  ]   = side==SOUTH ? 0.0f: (side==EAST?-R:R);
802
                   normal[3*vertex+1]   =-1.0f;
803
                   normal[3*vertex+2]   = lower ? -R:R;
804

    
805
                   texture[2*vertex  ]  = x;
806
                   texture[2*vertex+1]  = 1.0f-(float)(lower? (curr.row+2):(curr.row+1))/mRows;
807
                   break;
808
       case WEST : y = (float)(back  ? (curr.row+1):(curr.row))/mRows;
809

    
810
                   position[3*vertex  ] = (float)curr.col/mCols -0.5f;
811
                   position[3*vertex+1] = 0.5f - y;
812
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
813

    
814
                   normal[3*vertex  ]   =-1.0f;
815
                   normal[3*vertex+1]   = side==WEST ? 0.0f : (side==NORTH?-R:R);
816
                   normal[3*vertex+2]   = lower ? -R:R;
817
 
818
                   texture[2*vertex  ]  = (float)(lower ? (curr.col-1):(curr.col  ))/mCols;
819
                   texture[2*vertex+1]  = 1.0f - y;
820
                   break;
821
       case EAST : y = (float)(back  ? (curr.row):(curr.row+1))/mRows;
822

    
823
                   position[3*vertex  ] = (float)(curr.col+1)/mCols -0.5f;
824
                   position[3*vertex+1] = 0.5f - y;
825
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
826

    
827
                   normal[3*vertex  ]   = 1.0f;
828
                   normal[3*vertex+1]   = side==EAST ? 0.0f : (side==SOUTH?-R:R);
829
                   normal[3*vertex+2]   = lower ? -R:R; 
830

    
831
                   texture[2*vertex  ]  = (float)(lower ? (curr.col+2):(curr.col+1))/mCols;
832
                   texture[2*vertex+1]  = 1.0f - y;
833
                   break;
834
       }
835
     
836
     if(texture[2*vertex  ]>1.0f) texture[2*vertex  ] =2.0f-texture[2*vertex  ];
837
     if(texture[2*vertex  ]<0.0f) texture[2*vertex  ] =    -texture[2*vertex  ];
838
     if(texture[2*vertex+1]>1.0f) texture[2*vertex+1] =2.0f-texture[2*vertex+1];
839
     if(texture[2*vertex+1]<0.0f) texture[2*vertex+1] =    -texture[2*vertex+1];
840
     }
841

    
842
///////////////////////////////////////////////////////////////////////////////////////////////////
843

    
844
   private void build(boolean frontOnly)
845
     {
846
     int numVertices=0;
847
     float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
848
     float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
849
     float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
850

    
851
     //android.util.Log.d("CUBES","building front grid...");
852

    
853
     numVertices = buildFrontBackGrid(true, numVertices,positionData,normalData,textureData);
854

    
855
     if( !frontOnly )
856
       {
857
       numVertices = repeatLast(numVertices,positionData,normalData,textureData);
858
       if( numVertices%2==1 )
859
         {
860
         //android.util.Log.d("CUBES","repeating winding1 vertex");
861

    
862
         numVertices = repeatLast(numVertices,positionData,normalData,textureData);
863
         }
864

    
865
       //android.util.Log.d("CUBES","building side grid...");
866

    
867
       numVertices = buildSideGrid (numVertices,positionData,normalData,textureData);
868

    
869
       //android.util.Log.d("CUBES","building back grid...");
870

    
871
       numVertices = buildFrontBackGrid (false,numVertices,positionData,normalData,textureData);
872
       }
873

    
874
     /*
875
     android.util.Log.e("CUBES","dataLen="+dataLength+" vertex="+numVertices);
876
     android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
877
     android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
878
     android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
879
     */
880

    
881
     mEdges.clear();
882
     mEdges = null;
883
     mCubes = null;
884

    
885
     if( remainingVert!=0 )
886
       android.util.Log.d("CUBES", "remainingVert " +remainingVert );
887

    
888
     mMeshPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
889
     mMeshPositions.put(positionData).position(0);
890

    
891
     mMeshNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
892
     mMeshNormals.put(normalData).position(0);
893

    
894
     mMeshTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
895
     mMeshTexture.put(textureData).position(0);
896
     }
897

    
898
///////////////////////////////////////////////////////////////////////////////////////////////////
899

    
900
   float[] getBoundingVertices()
901
     {
902
     return mBoundingVert;
903
     }
904

    
905
///////////////////////////////////////////////////////////////////////////////////////////////////
906
// PUBLIC API
907
///////////////////////////////////////////////////////////////////////////////////////////////////
908
/**
909
 * Creates the underlying mesh of vertices, normals, texture coords.
910
 *    
911
 * @param cols      Integer helping to parse the next parameter.
912
 * @param desc      String describing the subset of a MxNx1 cuboid that we want to create.
913
 *                  Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
914
 *                  <p></p>
915
 *                  <p>
916
 *                  <pre>
917
 *                  For example, (cols=2, desc="111010") describes the following shape:
918
 *
919
 *                  XX
920
 *                  X
921
 *                  X
922
 *
923
 *                  whereas (cols=2,desc="110001") describes
924
 *
925
 *                  XX
926
 *
927
 *                   X
928
 *                  </pre>
929
 *                  </p>
930
 * @param frontOnly Only create the front wall or side and back as well?
931
 */
932
   public MeshCubes(int cols, String desc, boolean frontOnly)
933
      {
934
      super(frontOnly ? 0.0f:1.0f/cols);
935
      prepareDataStructures(cols,desc,frontOnly);
936
      build(frontOnly);
937
      }
938

    
939
///////////////////////////////////////////////////////////////////////////////////////////////////
940
/**
941
 * Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors.
942
 *
943
 * @param cols      Number of columns.
944
 * @param rows      Number of rows.
945
 * @param frontOnly Only create the front wall or side and back as well?
946
 */
947
   public MeshCubes(int cols, int rows, boolean frontOnly)
948
      {
949
      super(frontOnly ? 0.0f:1.0f/cols);
950
      prepareDataStructures(cols,rows,frontOnly);
951
      build(frontOnly);
952
      }
953
   }
954
///////////////////////////////////////////////////////////////////////////////////////////////////
955

    
(14-14/16)