Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshCubes.java @ 15ed35e6

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.mesh;
21

    
22
import java.util.ArrayList;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/**
26
 * Create a 3D grid composed of Cubes.
27
 * <p>
28
 * Any subset of a MxNx1 cuboid is possible.
29
 */
30
public class MeshCubes extends MeshBase
31
   {
32
   private static final float R = 0.0f;//0.2f;
33

    
34
   private static final int NORTH = 0;
35
   private static final int WEST  = 1;
36
   private static final int EAST  = 2;
37
   private static final int SOUTH = 3;
38

    
39
   private static final float[] mNormalX = new float[4];
40
   private static final float[] mNormalY = new float[4];
41
   private static final float[] mNormalZ = new float[4];
42

    
43
   private class Edge
44
     {
45
     final int side; 
46
     final int row;
47
     final int col;
48
     
49
     Edge(int s, int r, int c)
50
       {
51
       side= s; 
52
       row = r;
53
       col = c;
54
       }
55
     }
56
   
57
   private int mCols, mRows, mSlices;
58
   private int[][] mCubes;
59
   private byte[][] mInflateX, mInflateY;
60
   private float mInfCorr;
61
   private ArrayList<Edge> mEdges = new ArrayList<>();
62

    
63
   private int currVert;
64
   private int numVertices;
65
   private int mSideBends;
66
   private int mEdgeNum;
67
   private int mSideWalls;
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
// a Block is split into two triangles along the NE-SW line iff it is in the top-right
71
// or bottom-left quadrant of the grid.
72

    
73
   private boolean isNE(int row,int col)
74
     {
75
     return ( (2*row<mRows)^(2*col<mCols) );
76
     }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
// return the number of vertices our grid will contain
80

    
81
   private int computeDataLength()
82
      {
83
      int frontWalls=0, frontSegments=0, triangleShifts=0, windingShifts=0;
84
      int shiftCol = (mCols-1)/2;
85

    
86
      boolean lastBlockIsNE=false;
87
      boolean thisBlockIsNE;        // the block we are currently looking at is split into
88
                                    // two triangles along the NE-SW line (rather than NW-SE)
89
      for(int row=0; row<mRows; row++)
90
        {
91
        if( mCols>=2 && (mCubes[row][shiftCol]%2 == 1) && (mCubes[row][shiftCol+1]%2 == 1) ) triangleShifts++;
92

    
93
        for(int col=0; col<mCols; col++)
94
          {
95
          if( mCubes[row][col]%2 == 1 )  // land
96
            {
97
            thisBlockIsNE = isNE(row,col);
98
            if( thisBlockIsNE^lastBlockIsNE ) windingShifts++;
99
            lastBlockIsNE = thisBlockIsNE;
100
            frontWalls++;
101
            if( col==mCols-1 || mCubes[row][col+1]%2 == 0 ) frontSegments++;
102
            }
103
          }
104
        }
105

    
106
      int frontVert       = 2*( frontWalls + 2*frontSegments - 1) +2*triangleShifts + windingShifts;
107
      int sideVertOneSlice= 2*( mSideWalls + mSideBends + mEdgeNum -1);
108
      int sideVert        = 2*(mSlices-1) + mSlices*sideVertOneSlice;
109
      int firstWinding    = (mSlices>0 && (frontVert+1)%2==1 ) ? 1:0;
110
      int dataL           = mSlices==0 ? frontVert : (frontVert+1) +firstWinding+ (1+sideVert+1) + (1+frontVert);
111
/*
112
      android.util.Log.e("CUBES","triangleShifts="+triangleShifts+" windingShifts="+windingShifts+" winding1="+firstWinding+" frontVert="+frontVert+" sideVert="+sideVert);
113
      android.util.Log.e("CUBES", "frontW="+frontWalls+" fSegments="+frontSegments+" sWalls="+mSideWalls+" sSegments="+mEdgeNum+" sideBends="+mSideBends+" dataLen="+dataL );
114
*/
115
      return dataL<0 ? 0:dataL;
116
      }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
/*
120
   private static String debug(short[] val)
121
     {
122
     String ret="";j
123
     
124
     for(int i=0; i<val.length; i++) ret+=(" "+val[i]); 
125
     
126
     return ret;
127
     }
128
*/
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
   private static String debug(float[] val, int stop)
132
     {
133
     String ret="";
134
     float v;
135
     boolean neg;
136
     int mod;
137

    
138
     for(int i=0; i<val.length; i++) 
139
        {
140
        if( i%stop==0 ) ret+="\n";
141

    
142
        mod = i%stop;
143

    
144
        if( mod==0 || mod==3 || mod==6 ) ret+=" (";
145

    
146
        v = val[i];
147
        if( v==-0.0f ) v=0.0f;
148

    
149

    
150
        neg = v<0;
151
        v = (v<0 ? -v:v);
152

    
153
        ret+=((neg? " -":" +")+v);
154

    
155
        if( mod==2 || mod==5 || mod==7 ) ret+=")";
156
        }
157

    
158
     return ret;
159
     }
160

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

    
182
   private void prepareDataStructures(int cols, String desc, int slices)
183
     {
184
     mRows       =0;
185
     mCols       =0;
186
     mSlices     =slices;
187
     numVertices =0;
188

    
189
     if( cols>0 && desc.contains("1") )
190
       {
191
       mCols = cols;
192
       mRows = desc.length()/cols;
193

    
194
       mInfCorr = mRows>mCols ? mRows:mCols;
195
       mInfCorr = mSlices>mInfCorr ? mSlices : mInfCorr;
196

    
197
       mCubes    = new int[mRows][mCols];
198
       mInflateX = new byte[mRows+1][mCols+1];
199
       mInflateY = new byte[mRows+1][mCols+1];
200

    
201
       for(int col=0; col<mCols; col++)
202
         for(int row=0; row<mRows; row++)
203
           mCubes[row][col] = (desc.charAt(row * mCols + col) == '1' ? 1 : 0);
204

    
205
       for(int col=0; col<mCols+1; col++)
206
         for(int row=0; row<mRows+1; row++)
207
           {
208
           fillInflate(row,col);
209
           }
210

    
211
       markRegions();
212
       numVertices = computeDataLength();
213
       currVert = 0;
214
       }
215
     }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218
// full grid
219

    
220
   private void prepareDataStructures(int cols, int rows, int slices)
221
     {
222
     mRows        =rows;
223
     mCols        =cols;
224
     mSlices      =slices;
225
     numVertices  =0;
226

    
227
     if( cols>0 && rows>0 )
228
       {
229
       mCubes    = new int[mRows][mCols];
230
       mInflateX = new byte[mRows+1][mCols+1];
231
       mInflateY = new byte[mRows+1][mCols+1];
232

    
233
       mInfCorr = mRows>mCols ? mRows:mCols;
234
       mInfCorr = mSlices>mInfCorr ? mSlices : mInfCorr;
235

    
236
       for(int col=0; col<mCols; col++)
237
         for(int row=0; row<mRows; row++)
238
           mCubes[row][col] = 1;
239

    
240
       for(int col=0; col<mCols+1; col++)
241
         for(int row=0; row<mRows+1; row++)
242
           {
243
           fillInflate(row,col);
244
           }
245

    
246
       markRegions();
247
       numVertices = computeDataLength();
248
       currVert = 0;
249
       }
250
     }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253
// Mark all the 'regions' of our grid  - i.e. separate pieces of 'land' (connected blocks that will 
254
// be rendered) and 'water' (connected holes in between) with integers. Each connected block of land
255
// gets a unique odd integer, each connected block of water a unique even integer.
256
//
257
// Water on the edges of the grid is also considered connected to itself!   
258
//   
259
// This function also creates a list of 'Edges'. Each Edge is a data structure from which later on we
260
// will start building the side walls of each connected block of land (and sides of holes of water
261
// inside). Each Edge needs to point from Land to Water (thus the '(SOUTH,i-1,j)' below) - otherwise
262
// later on setting up normal vectors wouldn't work.
263
   
264
  private void markRegions()
265
     {
266
     int i, j, numWater=1, numLand=0;
267
     
268
     for(i=0; i<mRows;i++) if( mCubes[      i][      0]==0 ) markRegion((short)2,      i,       0);
269
     for(i=0; i<mRows;i++) if( mCubes[      i][mCols-1]==0 ) markRegion((short)2,      i, mCols-1);
270
     for(i=0; i<mCols;i++) if( mCubes[0      ][      i]==0 ) markRegion((short)2,      0,       i);
271
     for(i=0; i<mCols;i++) if( mCubes[mRows-1][      i]==0 ) markRegion((short)2,mRows-1,       i);
272
           
273
     for(i=0; i<mRows; i++)
274
        for(j=0; j<mCols; j++)
275
           {
276
           if( mCubes[i][j] == 0 ) { numWater++; markRegion( (short)(2*numWater ),i,j); mEdges.add(new Edge(SOUTH,i-1,j)); }
277
           if( mCubes[i][j] == 1 ) { numLand ++; markRegion( (short)(2*numLand+1),i,j); mEdges.add(new Edge(NORTH,i  ,j)); }
278
           }
279
     
280
     // now we potentially need to kick out some Edges . Otherwise the following does not work:
281
     //
282
     // 0 1 0
283
     // 1 0 1
284
     // 0 1 0
285
     
286
     mEdgeNum= mEdges.size();
287
     int initCol, initRow, initSide, lastSide;
288
     Edge e1,e2;
289
     
290
     for(i=0; i<mEdgeNum; i++)
291
       {
292
       e1 = mEdges.get(i);
293
       initRow= e1.row;
294
       initCol= e1.col;
295
       initSide=e1.side;
296

    
297
       do
298
         {
299
         //android.util.Log.d("CUBES", "checking edge "+debug(e1));
300

    
301
         mSideWalls++;
302

    
303
         if( e1.side==NORTH || e1.side==SOUTH )
304
           {
305
           for(j=i+1;j<mEdgeNum;j++)
306
             {
307
             e2 = mEdges.get(j);
308

    
309
             if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col )
310
               {
311
               mEdges.remove(j);
312
               mEdgeNum--;
313
               j--;
314

    
315
               //android.util.Log.e("CUBES", "removing edge "+debug(e2));
316
               }
317
             }
318
           }
319

    
320
         lastSide = e1.side;
321
         e1 = getNextEdge(e1);
322
         if( e1.side!=lastSide ) mSideBends++;
323
         }
324
       while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide );
325
       }
326
     }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329
// when calling, make sure that newVal != val
330
   
331
  private void markRegion(short newVal, int row, int col)
332
     {
333
     int val = mCubes[row][col];
334
     mCubes[row][col] = newVal;
335
     
336
     if( row>0       && mCubes[row-1][col  ]==val ) markRegion(newVal, row-1, col  );
337
     if( row<mRows-1 && mCubes[row+1][col  ]==val ) markRegion(newVal, row+1, col  );
338
     if( col>0       && mCubes[row  ][col-1]==val ) markRegion(newVal, row  , col-1);
339
     if( col<mCols-1 && mCubes[row  ][col+1]==val ) markRegion(newVal, row  , col+1);
340
     }
341
   
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343
   
344
  private void createNormals(boolean front, int row, int col)
345
     {
346
     int td,lr; 
347
      
348
     int nw = (col>0       && row>0      ) ? (mCubes[row-1][col-1]%2) : 0;
349
     int w  = (col>0                     ) ? (mCubes[row  ][col-1]%2) : 0;
350
     int n  = (               row>0      ) ? (mCubes[row-1][col  ]%2) : 0;
351
     int c  =                                (mCubes[row  ][col  ]%2);
352
     int sw = (col>0       && row<mRows-1) ? (mCubes[row+1][col-1]%2) : 0;
353
     int s  = (               row<mRows-1) ? (mCubes[row+1][col  ]%2) : 0;
354
     int ne = (col<mCols-1 && row>0      ) ? (mCubes[row-1][col+1]%2) : 0;
355
     int e  = (col<mCols-1               ) ? (mCubes[row  ][col+1]%2) : 0;
356
     int se = (col<mCols-1 && row<mRows-1) ? (mCubes[row+1][col+1]%2) : 0;
357

    
358
     if(front)
359
       {
360
       mNormalZ[0] = 1.0f;
361
       mNormalZ[1] = 1.0f;
362
       mNormalZ[2] = 1.0f;
363
       mNormalZ[3] = 1.0f;
364
       }
365
     else
366
       {
367
       mNormalZ[0] =-1.0f;
368
       mNormalZ[1] =-1.0f;
369
       mNormalZ[2] =-1.0f;
370
       mNormalZ[3] =-1.0f;
371
       }
372

    
373
     td = nw+n-w-c;
374
     lr = c+n-w-nw;
375
     if( td<0 ) td=-1;
376
     if( td>0 ) td= 1;
377
     if( lr<0 ) lr=-1;
378
     if( lr>0 ) lr= 1;
379
     mNormalX[0] = lr*R;
380
     mNormalY[0] = td*R;
381
     
382
     td = w+c-sw-s;
383
     lr = c+s-w-sw;
384
     if( td<0 ) td=-1;
385
     if( td>0 ) td= 1;
386
     if( lr<0 ) lr=-1;
387
     if( lr>0 ) lr= 1;
388
     mNormalX[1] = lr*R;
389
     mNormalY[1] = td*R;
390
     
391
     td = n+ne-c-e;
392
     lr = e+ne-c-n;
393
     if( td<0 ) td=-1;
394
     if( td>0 ) td= 1;
395
     if( lr<0 ) lr=-1;
396
     if( lr>0 ) lr= 1;
397
     mNormalX[2] = lr*R;
398
     mNormalY[2] = td*R;
399
     
400
     td = c+e-s-se;
401
     lr = e+se-c-s;
402
     if( td<0 ) td=-1;
403
     if( td>0 ) td= 1;
404
     if( lr<0 ) lr=-1;
405
     if( lr>0 ) lr= 1;
406
     mNormalX[3] = lr*R;
407
     mNormalY[3] = td*R;
408
     /*
409
     android.util.Log.d("CUBES", "row="+row+" col="+col);
410
     android.util.Log.d("CUBES", mNormalX[0]+" "+mNormalY[0]);
411
     android.util.Log.d("CUBES", mNormalX[1]+" "+mNormalY[1]);
412
     android.util.Log.d("CUBES", mNormalX[2]+" "+mNormalY[2]);
413
     android.util.Log.d("CUBES", mNormalX[3]+" "+mNormalY[3]);
414
     */
415
     }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
  private void buildFrontBackGrid(boolean front, float[] attribs)
420
     {
421
     int last, current;
422
     boolean seenLand=false;
423
     boolean lastBlockIsNE = false;
424
     boolean currentBlockIsNE;
425
     float vectZ = (front ? 0.5f : -0.5f);
426

    
427
     //android.util.Log.d("CUBES", "buildFrontBack");
428

    
429
     for(int row=0; row<mRows; row++)
430
       {
431
       last =0;
432
         
433
       for(int col=0; col<mCols; col++)
434
         {
435
         current = mCubes[row][col];
436

    
437
         if( current%2 == 1 )
438
           {
439
           currentBlockIsNE = isNE(row,col);
440

    
441
           if( !seenLand && !front && ((currVert%2==1)^currentBlockIsNE) )
442
             {
443
             //android.util.Log.d("CUBES","repeating winding2 vertex");
444

    
445
             repeatLast(attribs);
446
             }
447

    
448
           createNormals(front,row,col);
449

    
450
           if( currentBlockIsNE )
451
             {
452
             if( (last!=current) || !lastBlockIsNE )
453
               {
454
               if( seenLand  && (last != current) ) repeatLast(attribs);
455
               addFrontVertex( 0, vectZ, col, row, attribs);
456
               if( seenLand  && (last != current) ) repeatLast(attribs);
457
               if( !lastBlockIsNE || (!front && !seenLand) ) repeatLast(attribs);
458
               addFrontVertex( 1, vectZ, col, row+1, attribs);
459
               }
460
             addFrontVertex( 2, vectZ, col+1, row, attribs);
461
             addFrontVertex( 3, vectZ, col+1, row+1, attribs);
462
             }
463
           else
464
             {
465
             if( (last!=current) || lastBlockIsNE )
466
               {
467
               if( seenLand  && (last != current) ) repeatLast(attribs);
468
               addFrontVertex( 1, vectZ, col, row+1, attribs);
469
               if( seenLand  && (last != current) ) repeatLast(attribs);
470
               if( lastBlockIsNE || (!front && !seenLand) ) repeatLast(attribs);
471
               addFrontVertex( 0, vectZ, col, row, attribs);
472
               }
473
             addFrontVertex( 3, vectZ, col+1, row+1, attribs);
474
             addFrontVertex( 2, vectZ, col+1, row  , attribs);
475
             }
476

    
477
           seenLand = true;
478
           lastBlockIsNE = currentBlockIsNE;
479
           }
480
            
481
         last = current;
482
         }
483
       }
484
     }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

    
488
  private void buildSideGrid(float[] attribs)
489
     {
490
     //android.util.Log.d("CUBES", "buildSide");
491

    
492
     for(int i=0; i<mEdgeNum; i++)
493
       {
494
       buildIthSide(mEdges.get(i), attribs);
495
       }
496
     }
497

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499

    
500
  private void buildIthSide(Edge curr, float[] attribs)
501
     {
502
     Edge prev, next;
503
     int col, row, side;
504

    
505
     if( curr.side==NORTH ) // water outside
506
       {
507
       prev = new Edge(WEST,curr.row,curr.col);
508
       }
509
     else                   // land outside; we need to move forward one link because we are
510
       {                    // going in opposite direction and we need to start from a bend.
511
       prev = curr;
512
       curr = new Edge(EAST,curr.row+1,curr.col-1);
513
       }
514

    
515
     for(int slice=0; slice<mSlices; slice++)
516
       {
517
       col = curr.col;
518
       row = curr.row;
519
       side= curr.side;
520
       next = getNextEdge(curr);
521
     
522
       addSideVertex(curr,true,slice+1,prev.side,attribs);
523

    
524
       do
525
         {
526
         if( prev.side!=curr.side )
527
           {
528
           addSideVertex(curr,true,slice+1,prev.side,attribs);
529
           addSideVertex(curr,true,slice  ,prev.side,attribs);
530
           }
531
       
532
         addSideVertex(curr,false,slice+1,next.side,attribs);
533
         addSideVertex(curr,false,slice  ,next.side,attribs);
534
       
535
         prev = curr;
536
         curr = next;
537
         next = getNextEdge(curr);
538
         }
539
       while( curr.col!=col || curr.row!=row || curr.side!=side );
540
     
541
       repeatLast(attribs);
542
       }
543
     }
544

    
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546

    
547
  private Edge getNextEdge(Edge curr)
548
     {
549
     int col = curr.col;
550
     int row = curr.row;
551
      
552
     //android.util.Log.e("CUBES", "row="+row+" col="+col+" mRows="+mRows+" mCols="+mCols);
553
                       
554
     switch(curr.side) 
555
       {
556
       case NORTH: if( col==mCols-1 ) 
557
                     return new Edge(EAST,row,col);
558
                   if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] )
559
                     return new Edge(WEST,row-1,col+1);
560
                   if( mCubes[row][col+1]==mCubes[row][col] )
561
                     return new Edge(NORTH,row,col+1);
562
                   else  
563
                     return new Edge(EAST,row,col);
564
                   
565
       case SOUTH: if( col==0 ) 
566
                     return new Edge(WEST,row,col);
567
                   if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] )
568
                     return new Edge(EAST,row+1,col-1); 
569
                   if( mCubes[row][col-1]==mCubes[row][col] )
570
                     return new Edge(SOUTH,row,col-1);
571
                   else
572
                     return new Edge(WEST,row,col); 
573
                     
574
       case EAST : if( row==mRows-1 ) 
575
                     return new Edge(SOUTH,row,col);
576
                   if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] )
577
                     return new Edge(NORTH,row+1,col+1);
578
                   if( mCubes[row+1][col]==mCubes[row][col] )
579
                     return new Edge(EAST,row+1,col);
580
                   else 
581
                     return new Edge(SOUTH,row,col);
582
                   
583
       default   : if( row==0 )
584
                     return new Edge(NORTH,row,col);
585
                   if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] )
586
                     return new Edge(SOUTH,row-1,col-1);
587
                   if( mCubes[row-1][col]==mCubes[row][col] )
588
                     return new Edge(WEST,row-1,col);
589
                   else
590
                     return new Edge(NORTH,row,col);     
591
       }
592
     }
593

    
594
///////////////////////////////////////////////////////////////////////////////////////////////////
595

    
596
  private void fillInflate(int row, int col)
597
    {
598
    int diff;
599

    
600
         if( col==0     ) mInflateX[row][col] = -1;
601
    else if( col==mCols ) mInflateX[row][col] = +1;
602
    else
603
      {
604
      if( row==0 )
605
        {
606
        diff = mCubes[0][col-1]-mCubes[0][col];
607
        }
608
      else if( row==mRows )
609
        {
610
        diff = mCubes[mRows-1][col-1]-mCubes[mRows-1][col];
611
        }
612
      else
613
        {
614
        diff = (mCubes[row  ][col-1]-mCubes[row  ][col]) +
615
               (mCubes[row-1][col-1]-mCubes[row-1][col]) ;
616

    
617
        if( diff==-2 ) diff=-1;
618
        if( diff== 2 ) diff= 1;
619
        }
620

    
621
      mInflateX[row][col] = (byte)diff;
622
      }
623

    
624
         if( row==0     ) mInflateY[row][col] = +1;
625
    else if( row==mRows ) mInflateY[row][col] = -1;
626
    else
627
      {
628
      if( col==0 )
629
        {
630
        diff = mCubes[row][0]-mCubes[row-1][0];
631
        }
632
      else if( col==mCols )
633
        {
634
        diff = mCubes[row][mCols-1]-mCubes[row-1][mCols-1];
635
        }
636
      else
637
        {
638
        diff = (mCubes[row  ][col-1]+mCubes[row  ][col]) -
639
               (mCubes[row-1][col-1]+mCubes[row-1][col]) ;
640

    
641
        if( diff==-2 ) diff=-1;
642
        if( diff== 2 ) diff= 1;
643
        }
644

    
645
      mInflateY[row][col] = (byte)diff;
646
      }
647

    
648
    //android.util.Log.e("mesh","col="+col+" row="+row+" inflateX="+mInflateX[col][row]+" InflateY="+mInflateY[col][row]);
649
    }
650

    
651
///////////////////////////////////////////////////////////////////////////////////////////////////
652

    
653
  private void addFrontVertex(int index, float vectZ, int col, int row, float[] attribs)
654
     {
655
     float x = (float)col/mCols;
656
     float y = (float)row/mRows;
657

    
658
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x-0.5f;
659
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f-y;
660
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = vectZ;
661

    
662
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = mNormalX[index];
663
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = mNormalY[index];
664
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = mNormalZ[index];
665

    
666
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInfCorr*mInflateX[row][col]/2.0f;
667
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInfCorr*mInflateY[row][col]/2.0f;
668
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = mInfCorr*vectZ;
669

    
670
     attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = x;
671
     attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f-y;
672

    
673
     currVert++;
674
     }
675

    
676
///////////////////////////////////////////////////////////////////////////////////////////////////
677

    
678
  private void addSideVertex(Edge curr, boolean back, int slice, int side, float[] attribs)
679
     {
680
     //android.util.Log.e("CUBES", "adding Side vertex!");
681
     float x, y, z;
682
     int row, col, texX, texY;
683

    
684
     switch(curr.side)
685
       {
686
       case NORTH: row = curr.row;
687
                   col = (back ? (curr.col  ):(curr.col+1));
688
                   x = (float)col/mCols;
689
                   y = 0.5f - (float)row/mRows;
690
                   z = 0.5f - (float)slice/mSlices;
691

    
692
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x - 0.5f;
693
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = y;
694
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
695

    
696
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = side==NORTH ? 0.0f : (side==WEST?-R:R);
697
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = 1.0f;
698
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
699

    
700
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInfCorr*mInflateX[row][col]/2.0f;
701
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInfCorr*mInflateY[row][col]/2.0f;
702
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = mInfCorr*z;
703

    
704
                   texY = (mRows-row+slice)%(2*mRows);
705
                   if( texY>mRows ) texY = 2*mRows-texY;
706

    
707
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = x;
708
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = (float)texY/mRows;
709

    
710
                   break;
711
       case SOUTH: row = curr.row+1;
712
                   col = (back ? (curr.col+1):(curr.col));
713
                   x = (float)col/mCols;
714
                   y = 0.5f - (float)row/mRows;
715
                   z = 0.5f - (float)slice/mSlices;
716

    
717
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x - 0.5f;
718
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = y;
719
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
720

    
721
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = side==SOUTH ? 0.0f: (side==EAST?-R:R);
722
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] =-1.0f;
723
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
724

    
725
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInfCorr*mInflateX[row][col]/2.0f;
726
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInfCorr*mInflateY[row][col]/2.0f;
727
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = mInfCorr*z;
728

    
729
                   texY = (mRows-row+slice)%(2*mRows);
730
                   if( texY>mRows ) texY = 2*mRows-texY;
731

    
732
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = x;
733
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = (float)texY/mRows;
734

    
735
                   break;
736
       case WEST : row = (back  ? (curr.row+1):(curr.row));
737
                   col = curr.col;
738
                   x = (float)col/mCols -0.5f;
739
                   y = (float)row/mRows;
740
                   z = 0.5f - (float)slice/mSlices;
741

    
742
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x;
743
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f - y;
744
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
745

    
746
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] =-1.0f;
747
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = side==WEST ? 0.0f : (side==NORTH?-R:R);
748
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
749

    
750
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInfCorr*mInflateX[row][col]/2.0f;
751
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInfCorr*mInflateY[row][col]/2.0f;
752
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = mInfCorr*z;
753

    
754
                   texX = (col+slice)%(2*mCols);
755
                   if( texX>mCols ) texX = 2*mCols-texX;
756

    
757
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = (float)texX/mCols;
758
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f-y;
759

    
760
                   break;
761
       case EAST : row = (back  ? (curr.row):(curr.row+1));
762
                   col = (curr.col+1);
763
                   x = (float)col/mCols -0.5f;
764
                   y = (float)row/mRows;
765
                   z = 0.5f - (float)slice/mSlices;
766

    
767
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x;
768
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f - y;
769
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
770

    
771
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = 1.0f;
772
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = side==EAST ? 0.0f : (side==SOUTH?-R:R);
773
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
774

    
775
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInfCorr*mInflateX[row][col]/2.0f;
776
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInfCorr*mInflateY[row][col]/2.0f;
777
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = mInfCorr*z;
778

    
779
                   texX = (col+slice)%(2*mCols);
780
                   if( texX>mCols ) texX = 2*mCols-texX;
781

    
782
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = (float)texX/mCols;
783
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f-y;
784

    
785
                   break;
786
       }
787

    
788
     currVert++;
789
     }
790

    
791
///////////////////////////////////////////////////////////////////////////////////////////////////
792

    
793
   private void repeatLast(float[] attribs)
794
     {
795
     //android.util.Log.e("CUBES", "repeating last vertex!");
796

    
797
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + POS_ATTRIB  ];
798
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + POS_ATTRIB+1];
799
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = attribs[VERT_ATTRIBS*(currVert-1) + POS_ATTRIB+2];
800

    
801
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + NOR_ATTRIB  ];
802
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + NOR_ATTRIB+1];
803
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = attribs[VERT_ATTRIBS*(currVert-1) + NOR_ATTRIB+2];
804

    
805
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + INF_ATTRIB  ];
806
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + INF_ATTRIB+1];
807
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = attribs[VERT_ATTRIBS*(currVert-1) + INF_ATTRIB+2];
808

    
809
     attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + TEX_ATTRIB  ];
810
     attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + TEX_ATTRIB+1];
811

    
812
     currVert++;
813
     }
814

    
815
///////////////////////////////////////////////////////////////////////////////////////////////////
816

    
817
  private void build()
818
     {
819
     float[] attribs= new float[VERT_ATTRIBS*numVertices];
820

    
821
     buildFrontBackGrid(true,attribs);
822

    
823
     if( mSlices>0 )
824
       {
825
       repeatLast(attribs);
826
       if( currVert%2==1 ) repeatLast(attribs);
827
       buildSideGrid(attribs);
828
       buildFrontBackGrid(false,attribs);
829
       }
830

    
831
     mEdges.clear();
832
     mEdges = null;
833
     mCubes = null;
834
     mInflateX = null;
835
     mInflateY = null;
836

    
837
     if( currVert!=numVertices )
838
       android.util.Log.e("MeshCubes", "currVert " +currVert+" numVertices="+numVertices );
839

    
840
     setAttribs(attribs);
841
     }
842

    
843
///////////////////////////////////////////////////////////////////////////////////////////////////
844
// PUBLIC API
845
///////////////////////////////////////////////////////////////////////////////////////////////////
846
/**
847
 * Creates the underlying mesh of vertices, normals, texture coords.
848
 *    
849
 * @param cols   Integer helping to parse the next parameter.
850
 * @param desc   String describing the subset of a MxNx1 cuboid that we want to create.
851
 *               Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
852
 *               <p></p>
853
 *               <p>
854
 *               <pre>
855
 *               For example, (cols=2, desc="111010") describes the following shape:
856
 *
857
 *               XX
858
 *               X
859
 *               X
860
 *
861
 *               whereas (cols=2,desc="110001") describes
862
 *
863
 *               XX
864
 *
865
 *                X
866
 *               </pre>
867
 *               </p>
868
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
869
 */
870
 public MeshCubes(int cols, String desc, int slices)
871
   {
872
   super( (float)slices/cols);
873
   prepareDataStructures(cols,desc,slices);
874
   build();
875
   }
876

    
877
///////////////////////////////////////////////////////////////////////////////////////////////////
878
/**
879
 * Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors.
880
 *
881
 * @param cols   Number of columns, i.e. 'width' of the Mesh.
882
 * @param rows   Number of rows, i.e. 'height' of the Mesh.
883
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
884
 */
885
 public MeshCubes(int cols, int rows, int slices)
886
   {
887
   super( (float)slices/cols);
888
   prepareDataStructures(cols,rows,slices);
889
   build();
890
   }
891
 }
(2-2/3)