Project

General

Profile

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

library / src / main / java / org / distorted / library / MeshCubes.java @ 5f2853be

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, mSlices;
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
///////////////////////////////////////////////////////////////////////////////////////////////////
76
// a Block is split into two triangles along the NE-SW line iff it is in the top-right
77
// or bottom-left quadrant of the grid.
78

    
79
   private boolean isNE(int row,int col)
80
     {
81
     return ( (2*row<mRows)^(2*col<mCols) );
82
     }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
// return the number of vertices our grid will contain
86

    
87
   private int computeDataLength()
88
      {
89
      int frontWalls=0, frontSegments=0, triangleShifts=0, windingShifts=0;
90
      int shiftCol = (mCols-1)/2;
91

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

    
99
        for(int col=0; col<mCols; col++)
100
          {
101
          if( mCubes[row][col]%2 == 1 )  // land
102
            {
103
            thisBlockIsNE = isNE(row,col);
104
            if( thisBlockIsNE^lastBlockIsNE ) windingShifts++;
105
            lastBlockIsNE = thisBlockIsNE;
106
            frontWalls++;
107
            if( col==mCols-1 || mCubes[row][col+1]%2 == 0 ) frontSegments++;
108
            }
109
          }
110
        }
111

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

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
/*
126
   private static String debug(short[] val)
127
     {
128
     String ret="";j
129
     
130
     for(int i=0; i<val.length; i++) ret+=(" "+val[i]); 
131
     
132
     return ret;
133
     }
134
*/
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
/*
137
   private static String debug(float[] val, int stop)
138
     {
139
     String ret="";
140

    
141
     for(int i=0; i<val.length; i++) 
142
        {
143
        if( i%stop==0 ) ret+="\n";
144
        ret+=(" "+val[i]);
145
        }
146

    
147
     return ret;
148
     }
149
*/
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
/*
152
   private static String debug(Edge e)
153
     {
154
     String d = "";
155
     
156
     switch(e.side)
157
       {
158
       case NORTH: d+="NORTH "; break;
159
       case SOUTH: d+="SOUTH "; break;
160
       case WEST : d+="WEST  "; break;
161
       case EAST : d+="EAST  "; break;
162
       }
163
     
164
     d+=("("+e.row+","+e.col+")");
165
     
166
     return d;
167
     }   
168
*/
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
   private void prepareDataStructures(int cols, String desc, int slices)
172
     {
173
     mRows      =0;
174
     mCols      =0;
175
     mSlices    =slices;
176
     numVertices=0;
177
     
178
     if( cols>0 && desc.contains("1") )
179
       {
180
       mCols = cols;
181
       mRows = desc.length()/cols;
182

    
183
       mCubes = new int[mRows][mCols];
184
       
185
       for(int j=0; j<mCols; j++)
186
         for(int i=0; i<mRows; i++)
187
           mCubes[i][j] = (desc.charAt(i*mCols+j) == '1' ? 1:0);
188

    
189
       markRegions();
190
       numVertices = computeDataLength();
191

    
192
       remainingVert = numVertices;
193
       }
194
     }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
// full grid
198

    
199
   private void prepareDataStructures(int cols, int rows, int slices)
200
     {
201
     mRows       =rows;
202
     mCols       =cols;
203
     mSlices     =slices;
204
     numVertices =   0;
205

    
206
     if( cols>0 && rows>0 )
207
       {
208
       mCubes = new int[mRows][mCols];
209

    
210
       for(int j=0; j<mCols; j++)
211
         for(int i=0; i<mRows; i++)
212
           mCubes[i][j] = 1;
213

    
214
       markRegions();
215
       numVertices = computeDataLength();
216

    
217
       remainingVert = numVertices;
218
       }
219
     }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
// Mark all the 'regions' of our grid  - i.e. separate pieces of 'land' (connected blocks that will 
223
// be rendered) and 'water' (connected holes in between) with integers. Each connected block of land
224
// gets a unique odd integer, each connected block of water a unique even integer.
225
//
226
// Water on the edges of the grid is also considered connected to itself!   
227
//   
228
// This function also creates a list of 'Edges'. Each Edge is a data structure from which later on we
229
// will start building the side walls of each connected block of land (and sides of holes of water
230
// inside). Each Edge needs to point from Land to Water (thus the '(SOUTH,i-1,j)' below) - otherwise
231
// later on setting up normal vectors wouldn't work.
232
   
233
  private void markRegions()
234
     {
235
     int i, j, numWater=1, numLand=0;
236
     
237
     for(i=0; i<mRows;i++) if( mCubes[      i][      0]==0 ) markRegion((short)2,      i,       0);
238
     for(i=0; i<mRows;i++) if( mCubes[      i][mCols-1]==0 ) markRegion((short)2,      i, mCols-1);
239
     for(i=0; i<mCols;i++) if( mCubes[0      ][      i]==0 ) markRegion((short)2,      0,       i);
240
     for(i=0; i<mCols;i++) if( mCubes[mRows-1][      i]==0 ) markRegion((short)2,mRows-1,       i);
241
           
242
     for(i=0; i<mRows; i++)
243
        for(j=0; j<mCols; j++)
244
           {
245
           if( mCubes[i][j] == 0 ) { numWater++; markRegion( (short)(2*numWater ),i,j); mEdges.add(new Edge(SOUTH,i-1,j)); }
246
           if( mCubes[i][j] == 1 ) { numLand ++; markRegion( (short)(2*numLand+1),i,j); mEdges.add(new Edge(NORTH,i  ,j)); }
247
           }
248
     
249
     // now we potentially need to kick out some Edges . Otherwise the following does not work:
250
     //
251
     // 0 1 0
252
     // 1 0 1
253
     // 0 1 0
254
     
255
     mEdgeNum= mEdges.size();
256
     int initCol, initRow, initSide, lastSide;
257
     Edge e1,e2;
258
     
259
     for(i=0; i<mEdgeNum; i++)
260
       {
261
       e1 = mEdges.get(i);
262
       initRow= e1.row;
263
       initCol= e1.col;
264
       initSide=e1.side;
265

    
266
       do
267
         {
268
         //android.util.Log.d("CUBES", "checking edge "+debug(e1));
269

    
270
         mSideWalls++;
271

    
272
         if( e1.side==NORTH || e1.side==SOUTH )
273
           {
274
           for(j=i+1;j<mEdgeNum;j++)
275
             {
276
             e2 = mEdges.get(j);
277

    
278
             if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col )
279
               {
280
               mEdges.remove(j);
281
               mEdgeNum--;
282
               j--;
283

    
284
               //android.util.Log.e("CUBES", "removing edge "+debug(e2));
285
               }
286
             }
287
           }
288

    
289
         lastSide = e1.side;
290
         e1 = getNextEdge(e1);
291
         if( e1.side!=lastSide ) mSideBends++;
292
         }
293
       while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide );
294
       }
295
     }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298
// when calling, make sure that newVal != val
299
   
300
  private void markRegion(short newVal, int row, int col)
301
     {
302
     int val = mCubes[row][col];
303
     mCubes[row][col] = newVal;
304
     
305
     if( row>0       && mCubes[row-1][col  ]==val ) markRegion(newVal, row-1, col  );
306
     if( row<mRows-1 && mCubes[row+1][col  ]==val ) markRegion(newVal, row+1, col  );
307
     if( col>0       && mCubes[row  ][col-1]==val ) markRegion(newVal, row  , col-1);
308
     if( col<mCols-1 && mCubes[row  ][col+1]==val ) markRegion(newVal, row  , col+1);
309
     }
310
   
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312
   
313
  private void createNormals(boolean front, int row, int col)
314
     {
315
     int td,lr; 
316
      
317
     int nw = (col>0       && row>0      ) ? (mCubes[row-1][col-1]%2) : 0;
318
     int w  = (col>0                     ) ? (mCubes[row  ][col-1]%2) : 0;
319
     int n  = (               row>0      ) ? (mCubes[row-1][col  ]%2) : 0;
320
     int c  =                                (mCubes[row  ][col  ]%2);
321
     int sw = (col>0       && row<mRows-1) ? (mCubes[row+1][col-1]%2) : 0;
322
     int s  = (               row<mRows-1) ? (mCubes[row+1][col  ]%2) : 0;
323
     int ne = (col<mCols-1 && row>0      ) ? (mCubes[row-1][col+1]%2) : 0;
324
     int e  = (col<mCols-1               ) ? (mCubes[row  ][col+1]%2) : 0;
325
     int se = (col<mCols-1 && row<mRows-1) ? (mCubes[row+1][col+1]%2) : 0;
326

    
327
     if(front)
328
       {
329
       mNormalZ[0] = 1.0f;
330
       mNormalZ[1] = 1.0f;
331
       mNormalZ[2] = 1.0f;
332
       mNormalZ[3] = 1.0f;
333
       }
334
     else
335
       {
336
       mNormalZ[0] =-1.0f;
337
       mNormalZ[1] =-1.0f;
338
       mNormalZ[2] =-1.0f;
339
       mNormalZ[3] =-1.0f;
340
       }
341

    
342
     td = nw+n-w-c;
343
     lr = c+n-w-nw;
344
     if( td<0 ) td=-1;
345
     if( td>0 ) td= 1;
346
     if( lr<0 ) lr=-1;
347
     if( lr>0 ) lr= 1;
348
     mNormalX[0] = lr*R;
349
     mNormalY[0] = td*R;
350
     
351
     td = w+c-sw-s;
352
     lr = c+s-w-sw;
353
     if( td<0 ) td=-1;
354
     if( td>0 ) td= 1;
355
     if( lr<0 ) lr=-1;
356
     if( lr>0 ) lr= 1;
357
     mNormalX[1] = lr*R;
358
     mNormalY[1] = td*R;
359
     
360
     td = n+ne-c-e;
361
     lr = e+ne-c-n;
362
     if( td<0 ) td=-1;
363
     if( td>0 ) td= 1;
364
     if( lr<0 ) lr=-1;
365
     if( lr>0 ) lr= 1;
366
     mNormalX[2] = lr*R;
367
     mNormalY[2] = td*R;
368
     
369
     td = c+e-s-se;
370
     lr = e+se-c-s;
371
     if( td<0 ) td=-1;
372
     if( td>0 ) td= 1;
373
     if( lr<0 ) lr=-1;
374
     if( lr>0 ) lr= 1;
375
     mNormalX[3] = lr*R;
376
     mNormalY[3] = td*R;
377
     /*
378
     android.util.Log.d("CUBES", "row="+row+" col="+col);
379
     android.util.Log.d("CUBES", mNormalX[0]+" "+mNormalY[0]);
380
     android.util.Log.d("CUBES", mNormalX[1]+" "+mNormalY[1]);
381
     android.util.Log.d("CUBES", mNormalX[2]+" "+mNormalY[2]);
382
     android.util.Log.d("CUBES", mNormalX[3]+" "+mNormalY[3]);
383
     */
384
     }
385

    
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387

    
388
  private int buildFrontBackGrid(boolean front, int vertex, float[] attribs)
389
     {
390
     int last, current;
391
     boolean seenLand=false;
392
     boolean lastBlockIsNE = false;
393
     boolean currentBlockIsNE;
394
     float vectZ = front?FRONTZ:BACKZ;
395

    
396
     //android.util.Log.d("CUBES", "buildFrontBack");
397

    
398
     for(int row=0; row<mRows; row++)
399
       {
400
       last =0;
401
         
402
       for(int col=0; col<mCols; col++)
403
         {
404
         current = mCubes[row][col];
405

    
406
         if( current%2 == 1 )
407
           {
408
           currentBlockIsNE = isNE(row,col);
409

    
410
           if( !seenLand && !front && ((vertex%2==1)^currentBlockIsNE) )
411
             {
412
             //android.util.Log.d("CUBES","repeating winding2 vertex");
413

    
414
             vertex = repeatLast(vertex,attribs);
415
             }
416

    
417
           createNormals(front,row,col);
418

    
419
           if( currentBlockIsNE )
420
             {
421
             if( (last!=current) || !lastBlockIsNE )
422
               {
423
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
424
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, attribs);
425
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
426
               if( !lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,attribs);
427
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, attribs);
428
               }
429
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row, attribs);
430
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, attribs);
431
             }
432
           else
433
             {
434
             if( (last!=current) || lastBlockIsNE )
435
               {
436
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
437
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, attribs);
438
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
439
               if( lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,attribs);
440
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, attribs);
441
               }
442
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, attribs);
443
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row  , attribs);
444
             }
445

    
446
           seenLand = true;
447
           lastBlockIsNE = currentBlockIsNE;
448
           }
449
            
450
         last = current;
451
         }
452
       }
453
     
454
     return vertex;
455
     }
456

    
457
///////////////////////////////////////////////////////////////////////////////////////////////////
458

    
459
  private int repeatLast(int vertex, float[] attribs)
460
     {
461
     //android.util.Log.e("CUBES", "repeating last vertex!");
462

    
463
     if( vertex>0 )
464
       {
465
       remainingVert--;
466

    
467
       attribs[8*vertex  ] = attribs[8*vertex-8];
468
       attribs[8*vertex+1] = attribs[8*vertex-7];
469
       attribs[8*vertex+2] = attribs[8*vertex-6];
470
       attribs[8*vertex+3] = attribs[8*vertex-5];
471
       attribs[8*vertex+4] = attribs[8*vertex-4];
472
       attribs[8*vertex+5] = attribs[8*vertex-3];
473
       attribs[8*vertex+6] = attribs[8*vertex-2];
474
       attribs[8*vertex+7] = attribs[8*vertex-1];
475
         
476
       vertex++;     
477
       }
478
     
479
     return vertex;
480
     }
481
   
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
  private int buildSideGrid(int vertex, float[] attribs)
485
     {
486
     //android.util.Log.d("CUBES", "buildSide");
487

    
488
     for(int i=0; i<mEdgeNum; i++)
489
       {
490
       vertex = buildIthSide(mEdges.get(i), vertex, attribs);
491
       } 
492
      
493
     return vertex;
494
     }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  private int buildIthSide(Edge curr, int vertex, float[] attribs)
499
     {
500
     Edge prev, next;
501
     int col, row, side;
502

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

    
513
     for(int i=0; i<mSlices; i++)
514
       {
515
       col = curr.col;
516
       row = curr.row;
517
       side= curr.side;
518
       next = getNextEdge(curr);
519
     
520
       addSideVertex(curr,BACK,i+1,prev.side,vertex++,attribs);
521

    
522
       do
523
         {
524
         if( prev.side!=curr.side )
525
           {
526
           addSideVertex(curr,BACK,i+1,prev.side,vertex++,attribs);
527
           addSideVertex(curr,BACK,i  ,prev.side,vertex++,attribs);
528
           }
529
       
530
         addSideVertex(curr,FRONT,i+1,next.side,vertex++,attribs);
531
         addSideVertex(curr,FRONT,i  ,next.side,vertex++,attribs);
532
       
533
         prev = curr;
534
         curr = next;
535
         next = getNextEdge(curr);
536
         }
537
       while( curr.col!=col || curr.row!=row || curr.side!=side );
538
     
539
       vertex = repeatLast(vertex,attribs);
540
       }
541

    
542
     return vertex;
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 int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] attribs)
597
     {
598
     remainingVert--;
599

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

    
603
     attribs[8*vertex  ] = x-0.5f;
604
     attribs[8*vertex+1] = 0.5f-y;
605
     attribs[8*vertex+2] = vectZ;
606
     attribs[8*vertex+3] = mNormalX[index];
607
     attribs[8*vertex+4] = mNormalY[index];
608
     attribs[8*vertex+5] = mNormalZ[index];
609
     attribs[8*vertex+6] = x;
610
     attribs[8*vertex+7] = 1.0f-y;
611

    
612
     return vertex+1;
613
     }
614

    
615
///////////////////////////////////////////////////////////////////////////////////////////////////
616
   
617
  private void addSideVertex(Edge curr, boolean back, int slice,int side, int vertex, float[] attribs)
618
     {
619
     //android.util.Log.e("CUBES", "adding Side vertex!");
620

    
621
     remainingVert--;
622

    
623
     float x, y;
624

    
625
     switch(curr.side)
626
       {
627
       case NORTH: x = (float)(back ? (curr.col  ):(curr.col+1))/mCols;
628

    
629
                   attribs[8*vertex  ] = x - 0.5f;
630
                   attribs[8*vertex+1] = 0.5f - (float)curr.row/mRows;
631
                   attribs[8*vertex+2] = FRONTZ + ((BACKZ-FRONTZ)*slice)/mSlices;
632
                   attribs[8*vertex+3] = side==NORTH ? 0.0f : (side==WEST?-R:R);
633
                   attribs[8*vertex+4] = 1.0f;
634
                   attribs[8*vertex+5] = (slice==0 ? R : (slice==mSlices ? -R:0) );
635
                   attribs[8*vertex+6] = x;
636
                   attribs[8*vertex+7] = 1.0f-(float)(curr.row-slice)/mRows;
637
                   break;
638
       case SOUTH: x = (float)(back ? (curr.col+1):(curr.col  ))/mCols;
639

    
640
                   attribs[8*vertex  ] = x - 0.5f;
641
                   attribs[8*vertex+1] = 0.5f - (float)(curr.row+1)/mRows;
642
                   attribs[8*vertex+2] = FRONTZ + ((BACKZ-FRONTZ)*slice)/mSlices;
643
                   attribs[8*vertex+3] = side==SOUTH ? 0.0f: (side==EAST?-R:R);
644
                   attribs[8*vertex+4] =-1.0f;
645
                   attribs[8*vertex+5] = (slice==0 ? R : (slice==mSlices ? -R:0) );
646
                   attribs[8*vertex+6] = x;
647
                   attribs[8*vertex+7] = 1.0f - (float)(curr.row+1+slice)/mRows;
648
                   break;
649
       case WEST : y = (float)(back  ? (curr.row+1):(curr.row))/mRows;
650

    
651
                   attribs[8*vertex  ] = (float)curr.col/mCols -0.5f;
652
                   attribs[8*vertex+1] = 0.5f - y;
653
                   attribs[8*vertex+2] = FRONTZ + ((BACKZ-FRONTZ)*slice)/mSlices;
654
                   attribs[8*vertex+3] =-1.0f;
655
                   attribs[8*vertex+4] = side==WEST ? 0.0f : (side==NORTH?-R:R);
656
                   attribs[8*vertex+5] = (slice==0 ? R : (slice==mSlices ? -R:0) );
657
                   attribs[8*vertex+6] = (float)(curr.col-slice)/mCols;
658
                   attribs[8*vertex+7] = 1.0f - y;
659
                   break;
660
       case EAST : y = (float)(back  ? (curr.row):(curr.row+1))/mRows;
661

    
662
                   attribs[8*vertex  ] = (float)(curr.col+1)/mCols -0.5f;
663
                   attribs[8*vertex+1] = 0.5f - y;
664
                   attribs[8*vertex+2] = FRONTZ + ((BACKZ-FRONTZ)*slice)/mSlices;
665
                   attribs[8*vertex+3] = 1.0f;
666
                   attribs[8*vertex+4] = side==EAST ? 0.0f : (side==SOUTH?-R:R);
667
                   attribs[8*vertex+5] = (slice==0 ? R : (slice==mSlices ? -R:0) );
668
                   attribs[8*vertex+6] = (float)(curr.col+1+slice)/mCols;
669
                   attribs[8*vertex+7] = 1.0f - y;
670
                   break;
671
       }
672
     
673
     if(attribs[8*vertex+6]>1.0f) attribs[8*vertex+6] = 2.0f-attribs[8*vertex+6];
674
     if(attribs[8*vertex+6]<0.0f) attribs[8*vertex+6] =     -attribs[8*vertex+6];
675
     if(attribs[8*vertex+7]>1.0f) attribs[8*vertex+7] = 2.0f-attribs[8*vertex+7];
676
     if(attribs[8*vertex+7]<0.0f) attribs[8*vertex+7] =     -attribs[8*vertex+7];
677
     }
678

    
679
///////////////////////////////////////////////////////////////////////////////////////////////////
680

    
681
  private void build()
682
     {
683
     int vertSoFar=0;
684
     float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
685

    
686
     //android.util.Log.d("MeshCubes","building front grid...");
687

    
688
     vertSoFar = buildFrontBackGrid(true, vertSoFar,attribs);
689

    
690
     if( mSlices>0 )
691
       {
692
       vertSoFar = repeatLast(vertSoFar,attribs);
693
       if( vertSoFar%2==1 )
694
         {
695
         //android.util.Log.d("MeshCubes","repeating winding1 vertex");
696

    
697
         vertSoFar = repeatLast(vertSoFar,attribs);
698
         }
699

    
700
       //android.util.Log.d("MeshCubes","building side grid...");
701

    
702
       vertSoFar = buildSideGrid (vertSoFar,attribs);
703

    
704
       //android.util.Log.d("MeshCubes","building back grid...");
705

    
706
       buildFrontBackGrid (false,vertSoFar,attribs);
707
       }
708

    
709
     //android.util.Log.e("MeshCubes", "dataLen="+numVertices);
710
     //android.util.Log.d("MeshCubes", "attribs: "+debug(attribs,8) );
711

    
712
     mEdges.clear();
713
     mEdges = null;
714
     mCubes = null;
715

    
716
     if( remainingVert!=0 )
717
       android.util.Log.e("MeshCubes", "remainingVert " +remainingVert );
718

    
719
     mVertAttribs = ByteBuffer.allocateDirect(numVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
720
     mVertAttribs.put(attribs).position(0);
721
     }
722

    
723
///////////////////////////////////////////////////////////////////////////////////////////////////
724
// PUBLIC API
725
///////////////////////////////////////////////////////////////////////////////////////////////////
726
/**
727
 * Creates the underlying mesh of vertices, normals, texture coords.
728
 *    
729
 * @param cols   Integer helping to parse the next parameter.
730
 * @param desc   String describing the subset of a MxNx1 cuboid that we want to create.
731
 *               Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
732
 *               <p></p>
733
 *               <p>
734
 *               <pre>
735
 *               For example, (cols=2, desc="111010") describes the following shape:
736
 *
737
 *               XX
738
 *               X
739
 *               X
740
 *
741
 *               whereas (cols=2,desc="110001") describes
742
 *
743
 *               XX
744
 *
745
 *                X
746
 *               </pre>
747
 *               </p>
748
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
749
 */
750
 public MeshCubes(int cols, String desc, int slices)
751
   {
752
   super( (float)slices/cols);
753
   prepareDataStructures(cols,desc,slices);
754
   build();
755
   }
756

    
757
///////////////////////////////////////////////////////////////////////////////////////////////////
758
/**
759
 * Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors.
760
 *
761
 * @param cols   Number of columns, i.e. 'width' of the Mesh.
762
 * @param rows   Number of rows, i.e. 'height' of the Mesh.
763
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
764
 */
765
 public MeshCubes(int cols, int rows, int slices)
766
   {
767
   super( (float)slices/cols);
768
   prepareDataStructures(cols,rows,slices);
769
   build();
770
   }
771
 }
(24-24/26)