Project

General

Profile

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

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

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
     dataLength=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
       markRegions();
206
       dataLength = computeDataLength(frontOnly);
207

    
208
       remainingVert = dataLength;
209
       }
210
     }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
// full grid
214

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

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

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

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

    
232
       remainingVert = dataLength;
233
       }
234

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

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

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

    
261
    return 0.0f;
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

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

    
287
    return (float)mCols;
288
    }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
  private int addLeftmost(float[] temp, int row, int index)
293
    {
294
    float x2 = retLeftmost(row)/mCols - 0.5f;
295
    float y2 = 0.5f - (float)row/mRows;
296

    
297
    if( index>1 )
298
      {
299
      float x0 = temp[2*index-4];
300
      float y0 = temp[2*index-3];
301
      float x1 = temp[2*index-2];
302
      float y1 = temp[2*index-1];
303

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

    
317
    temp[2*index  ] = x2;
318
    temp[2*index+1] = y2;
319
    return index+1;
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
  private int addRightmost(float[] temp, int row, int index)
325
    {
326
    float x2 = retRightmost(row)/mCols - 0.5f;
327
    float y2 = 0.5f - (float)row/mRows;
328

    
329
    if( index>1 )
330
      {
331
      float x0 = temp[2*index-4];
332
      float y0 = temp[2*index-3];
333
      float x1 = temp[2*index-2];
334
      float y1 = temp[2*index-1];
335

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

    
349
    temp[2*index  ] = x2;
350
    temp[2*index+1] = y2;
351
    return index+1;
352
    }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355
// fill up the mBoundingVert array with the smallest set of vertices which form the same convex hull
356
// like the whole thing.
357

    
358
  private void buildBoundingVert(boolean frontOnly)
359
    {
360
    int lVert=0, rVert=0;
361
    int firstRow=0, lastRow=mRows-1;
362

    
363
    for(int i=0; i<mRows; i++)
364
      for(int j=0; j<mCols; j++)
365
        if(mCubes[i][j] !=0 )
366
          {
367
          firstRow = i;
368
          i=mRows;
369
          j=mCols;
370
          }
371

    
372
    for(int i=mRows-1; i>=0; i--)
373
      for(int j=0; j<mCols; j++)
374
        if(mCubes[i][j] !=0 )
375
          {
376
          lastRow = i;
377
          i=-1;
378
          j=mCols;
379
          }
380

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

    
383
    int numLines = lastRow-firstRow+2;
384

    
385
    float[] tempL = new float[2*numLines];
386
    float[] tempR = new float[2*numLines];
387

    
388
    for(int i=firstRow; i<=lastRow+1; i++)
389
      {
390
      lVert = addLeftmost (tempL,i,lVert);
391
      rVert = addRightmost(tempR,i,rVert);
392
      }
393

    
394
//android.util.Log.d("cubes", "LEFT :"+debug(tempL,2));
395
//android.util.Log.d("cubes", "RIGHT:"+debug(tempR,2));
396

    
397
    int numVert = lVert+rVert;
398

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

    
401
    for(int i=0; i<lVert; i++)
402
      {
403
      mBoundingVert[3*i  ] = tempL[2*i  ];
404
      mBoundingVert[3*i+1] = tempL[2*i+1];
405
      mBoundingVert[3*i+2] = FRONTZ;
406
      }
407

    
408
    for(int i=0; i<rVert; i++)
409
      {
410
      mBoundingVert[3*(i+lVert)  ] = tempR[2*i  ];
411
      mBoundingVert[3*(i+lVert)+1] = tempR[2*i+1];
412
      mBoundingVert[3*(i+lVert)+2] = FRONTZ;
413
      }
414

    
415
    if( !frontOnly )
416
      {
417
      for(int i=0; i<numVert; i++)
418
        {
419
        mBoundingVert[3*(i+numVert)  ] = mBoundingVert[3*i  ];
420
        mBoundingVert[3*(i+numVert)+1] = mBoundingVert[3*i+1];
421
        mBoundingVert[3*(i+numVert)+2] = BACKZ;
422
        }
423
      }
424

    
425
//android.util.Log.d("cubes", "VERT:"+debug(mBoundingVert,3));
426
    }
427

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

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

    
477
         mSideWalls++;
478

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

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

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

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

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

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

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

    
595
   private int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] position, float[] normal, float[] texture)
596
     {
597
     remainingVert--;
598

    
599
     float x = (float)col/mCols;
600
     float y = (float)row/mRows;
601

    
602
     position[3*vertex  ] = x-0.5f;
603
     position[3*vertex+1] = 0.5f-y;
604
     position[3*vertex+2] = vectZ;
605
     normal[3*vertex  ]   = mNormalX[index];
606
     normal[3*vertex+1]   = mNormalY[index];
607
     normal[3*vertex+2]   = mNormalZ[index];
608
     texture[2*vertex  ]  = x;
609
     texture[2*vertex+1]  = 1.0f-y;
610

    
611
     return vertex+1;
612
     }
613

    
614
///////////////////////////////////////////////////////////////////////////////////////////////////
615

    
616
   private int buildFrontBackGrid(boolean front, int vertex, float[] position, float[] normal, float[] texture)
617
     {
618
     int last, current;
619
     boolean seenLand=false;
620
     boolean lastBlockIsNE = false;
621
     boolean currentBlockIsNE;
622
     float vectZ = front?FRONTZ:BACKZ;
623

    
624
     //android.util.Log.d("CUBES", "buildFrontBack");
625

    
626
     for(int row=0; row<mRows; row++)
627
       {
628
       last =0;
629
         
630
       for(int col=0; col<mCols; col++)
631
         {
632
         current = mCubes[row][col];
633

    
634
         if( current%2 == 1 )
635
           {
636
           currentBlockIsNE = isNE(row,col);
637

    
638
           if( !seenLand && !front && ((vertex%2==1)^currentBlockIsNE) )
639
             {
640
             //android.util.Log.d("CUBES","repeating winding2 vertex");
641

    
642
             vertex = repeatLast(vertex,position,normal,texture);
643
             }
644

    
645
           createNormals(front,row,col);
646

    
647
           if( currentBlockIsNE )
648
             {
649
             if( (last!=current) || !lastBlockIsNE )
650
               {
651
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
652
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
653
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
654
               if( !lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
655
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
656
               }
657
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row, position, normal, texture);
658
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
659
             }
660
           else
661
             {
662
             if( (last!=current) || lastBlockIsNE )
663
               {
664
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
665
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
666
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
667
               if( lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
668
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
669
               }
670
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
671
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row  , position, normal, texture);
672
             }
673

    
674
           seenLand = true;
675
           lastBlockIsNE = currentBlockIsNE;
676
           }
677
            
678
         last = current;
679
         }
680
       }
681
     
682
     return vertex;
683
     }
684

    
685
///////////////////////////////////////////////////////////////////////////////////////////////////
686

    
687
   private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
688
     {
689
     //android.util.Log.e("CUBES", "repeating last vertex!");
690

    
691
     if( vertex>0 )
692
       {
693
       remainingVert--;
694

    
695
       position[3*vertex  ] = position[3*vertex-3];
696
       position[3*vertex+1] = position[3*vertex-2];
697
       position[3*vertex+2] = position[3*vertex-1];
698

    
699
       normal[3*vertex  ]   = normal[3*vertex-3];
700
       normal[3*vertex+1]   = normal[3*vertex-2];
701
       normal[3*vertex+2]   = normal[3*vertex-1];
702

    
703
       texture[2*vertex  ]  = texture[2*vertex-2];
704
       texture[2*vertex+1]  = texture[2*vertex-1];
705
         
706
       vertex++;     
707
       }
708
     
709
     return vertex;
710
     }
711
   
712
///////////////////////////////////////////////////////////////////////////////////////////////////
713

    
714
   private int buildSideGrid(int vertex, float[] position, float[] normal, float[] texture)
715
     {
716
     //android.util.Log.d("CUBES", "buildSide");
717

    
718
     for(int i=0; i<mEdgeNum; i++)
719
       {
720
       vertex = buildIthSide(mEdges.get(i), vertex, position, normal, texture);  
721
       } 
722
      
723
     return vertex;
724
     }
725

    
726
///////////////////////////////////////////////////////////////////////////////////////////////////
727

    
728
   private int buildIthSide(Edge curr, int vertex, float[] position, float[] normal, float[] texture)
729
     {
730
     Edge prev; 
731
     
732
     if( curr.side==NORTH ) // water outside
733
       {
734
       prev = new Edge(WEST,curr.row,curr.col);
735
       }
736
     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.
737
       {
738
       prev = curr;
739
       curr = new Edge(EAST,curr.row+1,curr.col-1);
740
       }
741
     
742
     int col = curr.col;
743
     int row = curr.row;
744
     int side= curr.side;  
745
     Edge next = getNextEdge(curr);
746
     
747
     addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
748
     vertex++;
749
     
750
     do
751
       {
752
       if( prev.side!=curr.side )
753
         {
754
         addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
755
         vertex++;
756
         addSideVertex(curr,BACK,UPPER,prev.side,vertex,position,normal,texture);
757
         vertex++;
758
         }
759
       
760
       addSideVertex(curr,FRONT,LOWER,next.side,vertex,position,normal,texture);
761
       vertex++;
762
       addSideVertex(curr,FRONT,UPPER,next.side,vertex,position,normal,texture);
763
       vertex++;
764
       
765
       prev = curr;
766
       curr = next; 
767
       next = getNextEdge(curr);
768
       }
769
     while( curr.col!=col || curr.row!=row || curr.side!=side );
770
     
771
     vertex = repeatLast(vertex,position,normal,texture);
772
     
773
     return vertex;
774
     }
775

    
776
///////////////////////////////////////////////////////////////////////////////////////////////////
777

    
778
   private Edge getNextEdge(Edge curr)
779
     {
780
     int col = curr.col;
781
     int row = curr.row;
782
      
783
     //android.util.Log.e("CUBES", "row="+row+" col="+col+" mRows="+mRows+" mCols="+mCols);
784
                       
785
     switch(curr.side) 
786
       {
787
       case NORTH: if( col==mCols-1 ) 
788
                     return new Edge(EAST,row,col);
789
                   if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] )
790
                     return new Edge(WEST,row-1,col+1);
791
                   if( mCubes[row][col+1]==mCubes[row][col] )
792
                     return new Edge(NORTH,row,col+1);
793
                   else  
794
                     return new Edge(EAST,row,col);
795
                   
796
       case SOUTH: if( col==0 ) 
797
                     return new Edge(WEST,row,col);
798
                   if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] )
799
                     return new Edge(EAST,row+1,col-1); 
800
                   if( mCubes[row][col-1]==mCubes[row][col] )
801
                     return new Edge(SOUTH,row,col-1);
802
                   else
803
                     return new Edge(WEST,row,col); 
804
                     
805
       case EAST : if( row==mRows-1 ) 
806
                     return new Edge(SOUTH,row,col);
807
                   if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] )
808
                     return new Edge(NORTH,row+1,col+1);
809
                   if( mCubes[row+1][col]==mCubes[row][col] )
810
                     return new Edge(EAST,row+1,col);
811
                   else 
812
                     return new Edge(SOUTH,row,col);
813
                   
814
       default   : if( row==0 )
815
                     return new Edge(NORTH,row,col);
816
                   if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] )
817
                     return new Edge(SOUTH,row-1,col-1);
818
                   if( mCubes[row-1][col]==mCubes[row][col] )
819
                     return new Edge(WEST,row-1,col);
820
                   else
821
                     return new Edge(NORTH,row,col);     
822
       }
823
     }
824

    
825
///////////////////////////////////////////////////////////////////////////////////////////////////
826
   
827
   private void addSideVertex(Edge curr, boolean back, boolean lower,int side, int vertex, float[] position, float[] normal, float[] texture)
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
                   position[3*vertex  ] = x - 0.5f;
840
                   position[3*vertex+1] = 0.5f - (float)curr.row/mRows;
841
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
842

    
843
                   normal[3*vertex  ]   = side==NORTH ? 0.0f : (side==WEST?-R:R);
844
                   normal[3*vertex+1]   = 1.0f;
845
                   normal[3*vertex+2]   = lower ? -R:R;
846

    
847
                   texture[2*vertex  ]  = x;
848
                   texture[2*vertex+1]  = 1.0f-(float)(lower? (curr.row-1):(curr.row  ))/mRows;
849
                   break;
850
       case SOUTH: x = (float)(back ? (curr.col+1):(curr.col  ))/mCols;
851

    
852
                   position[3*vertex  ] = x - 0.5f;
853
                   position[3*vertex+1] = 0.5f - (float)(curr.row+1)/mRows;
854
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;  
855
            
856
                   normal[3*vertex  ]   = side==SOUTH ? 0.0f: (side==EAST?-R:R);
857
                   normal[3*vertex+1]   =-1.0f;
858
                   normal[3*vertex+2]   = lower ? -R:R;
859

    
860
                   texture[2*vertex  ]  = x;
861
                   texture[2*vertex+1]  = 1.0f-(float)(lower? (curr.row+2):(curr.row+1))/mRows;
862
                   break;
863
       case WEST : y = (float)(back  ? (curr.row+1):(curr.row))/mRows;
864

    
865
                   position[3*vertex  ] = (float)curr.col/mCols -0.5f;
866
                   position[3*vertex+1] = 0.5f - y;
867
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
868

    
869
                   normal[3*vertex  ]   =-1.0f;
870
                   normal[3*vertex+1]   = side==WEST ? 0.0f : (side==NORTH?-R:R);
871
                   normal[3*vertex+2]   = lower ? -R:R;
872
 
873
                   texture[2*vertex  ]  = (float)(lower ? (curr.col-1):(curr.col  ))/mCols;
874
                   texture[2*vertex+1]  = 1.0f - y;
875
                   break;
876
       case EAST : y = (float)(back  ? (curr.row):(curr.row+1))/mRows;
877

    
878
                   position[3*vertex  ] = (float)(curr.col+1)/mCols -0.5f;
879
                   position[3*vertex+1] = 0.5f - y;
880
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
881

    
882
                   normal[3*vertex  ]   = 1.0f;
883
                   normal[3*vertex+1]   = side==EAST ? 0.0f : (side==SOUTH?-R:R);
884
                   normal[3*vertex+2]   = lower ? -R:R; 
885

    
886
                   texture[2*vertex  ]  = (float)(lower ? (curr.col+2):(curr.col+1))/mCols;
887
                   texture[2*vertex+1]  = 1.0f - y;
888
                   break;
889
       }
890
     
891
     if(texture[2*vertex  ]>1.0f) texture[2*vertex  ] =2.0f-texture[2*vertex  ];
892
     if(texture[2*vertex  ]<0.0f) texture[2*vertex  ] =    -texture[2*vertex  ];
893
     if(texture[2*vertex+1]>1.0f) texture[2*vertex+1] =2.0f-texture[2*vertex+1];
894
     if(texture[2*vertex+1]<0.0f) texture[2*vertex+1] =    -texture[2*vertex+1];
895
     }
896

    
897
///////////////////////////////////////////////////////////////////////////////////////////////////
898

    
899
   private void build(boolean frontOnly)
900
     {
901
     int numVertices=0;
902
     float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
903
     float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
904
     float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
905

    
906
     //android.util.Log.d("CUBES","building front grid...");
907

    
908
     numVertices = buildFrontBackGrid(true, numVertices,positionData,normalData,textureData);
909

    
910
     if( !frontOnly )
911
       {
912
       numVertices = repeatLast(numVertices,positionData,normalData,textureData);
913
       if( numVertices%2==1 )
914
         {
915
         //android.util.Log.d("CUBES","repeating winding1 vertex");
916

    
917
         numVertices = repeatLast(numVertices,positionData,normalData,textureData);
918
         }
919

    
920
       //android.util.Log.d("CUBES","building side grid...");
921

    
922
       numVertices = buildSideGrid (numVertices,positionData,normalData,textureData);
923

    
924
       //android.util.Log.d("CUBES","building back grid...");
925

    
926
       numVertices = buildFrontBackGrid (false,numVertices,positionData,normalData,textureData);
927
       }
928

    
929
     /*
930
     android.util.Log.e("CUBES","dataLen="+dataLength+" vertex="+numVertices);
931
     android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
932
     android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
933
     android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
934
     */
935

    
936
     mEdges.clear();
937
     mEdges = null;
938
     mCubes = null;
939

    
940
     if( remainingVert!=0 )
941
       android.util.Log.d("CUBES", "remainingVert " +remainingVert );
942

    
943
     mMeshPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
944
     mMeshPositions.put(positionData).position(0);
945

    
946
     mMeshNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
947
     mMeshNormals.put(normalData).position(0);
948

    
949
     mMeshTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
950
     mMeshTexture.put(textureData).position(0);
951
     }
952

    
953
///////////////////////////////////////////////////////////////////////////////////////////////////
954

    
955
   float[] getBoundingVertices()
956
     {
957
     return mBoundingVert;
958
     }
959

    
960
///////////////////////////////////////////////////////////////////////////////////////////////////
961
// PUBLIC API
962
///////////////////////////////////////////////////////////////////////////////////////////////////
963
/**
964
 * Creates the underlying mesh of vertices, normals, texture coords.
965
 *    
966
 * @param cols      Integer helping to parse the next parameter.
967
 * @param desc      String describing the subset of a MxNx1 cuboid that we want to create.
968
 *                  Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
969
 *                  <p></p>
970
 *                  <p>
971
 *                  <pre>
972
 *                  For example, (cols=2, desc="111010") describes the following shape:
973
 *
974
 *                  XX
975
 *                  X
976
 *                  X
977
 *
978
 *                  whereas (cols=2,desc="110001") describes
979
 *
980
 *                  XX
981
 *
982
 *                   X
983
 *                  </pre>
984
 *                  </p>
985
 * @param frontOnly Only create the front wall or side and back as well?
986
 */
987
   public MeshCubes(int cols, String desc, boolean frontOnly)
988
      {
989
      super(frontOnly ? 0.0f:1.0f/cols);
990
      prepareDataStructures(cols,desc,frontOnly);
991
      build(frontOnly);
992
      }
993

    
994
///////////////////////////////////////////////////////////////////////////////////////////////////
995
/**
996
 * Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors.
997
 *
998
 * @param cols      Number of columns.
999
 * @param rows      Number of rows.
1000
 * @param frontOnly Only create the front wall or side and back as well?
1001
 */
1002
   public MeshCubes(int cols, int rows, boolean frontOnly)
1003
      {
1004
      super(frontOnly ? 0.0f:1.0f/cols);
1005
      prepareDataStructures(cols,rows,frontOnly);
1006
      build(frontOnly);
1007
      }
1008
   }
1009
///////////////////////////////////////////////////////////////////////////////////////////////////
1010

    
(14-14/16)