Project

General

Profile

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

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

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

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

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

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

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

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

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

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

    
193
       remainingVert = numVertices;
194
       }
195
     }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
// full grid
199

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

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

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

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

    
218
       remainingVert = numVertices;
219
       }
220
     }
221

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

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

    
271
         mSideWalls++;
272

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

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

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

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

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

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

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

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

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

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

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

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

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

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

    
418
           createNormals(front,row,col);
419

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

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

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459

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

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

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

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

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

    
497
///////////////////////////////////////////////////////////////////////////////////////////////////
498

    
499
  private int buildIthSide(Edge curr, int vertex, float[] attribs)
500
     {
501
     Edge prev; 
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
     int col = curr.col;
514
     int row = curr.row;
515
     int side= curr.side;  
516
     Edge next = getNextEdge(curr);
517
     
518
     addSideVertex(curr,BACK,LOWER,prev.side,vertex,attribs);
519
     vertex++;
520
     
521
     do
522
       {
523
       if( prev.side!=curr.side )
524
         {
525
         addSideVertex(curr,BACK,LOWER,prev.side,vertex,attribs);
526
         vertex++;
527
         addSideVertex(curr,BACK,UPPER,prev.side,vertex,attribs);
528
         vertex++;
529
         }
530
       
531
       addSideVertex(curr,FRONT,LOWER,next.side,vertex,attribs);
532
       vertex++;
533
       addSideVertex(curr,FRONT,UPPER,next.side,vertex,attribs);
534
       vertex++;
535
       
536
       prev = curr;
537
       curr = next; 
538
       next = getNextEdge(curr);
539
       }
540
     while( curr.col!=col || curr.row!=row || curr.side!=side );
541
     
542
     vertex = repeatLast(vertex,attribs);
543
     
544
     return vertex;
545
     }
546

    
547
///////////////////////////////////////////////////////////////////////////////////////////////////
548

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

    
596
///////////////////////////////////////////////////////////////////////////////////////////////////
597

    
598
  private int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] attribs)
599
     {
600
     remainingVert--;
601

    
602
     float x = (float)col/mCols;
603
     float y = (float)row/mRows;
604

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

    
614
     return vertex+1;
615
     }
616

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

    
623
     remainingVert--;
624

    
625
     float x, y;
626

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

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

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

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

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

    
681
///////////////////////////////////////////////////////////////////////////////////////////////////
682

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

    
688
     //android.util.Log.d("MeshCubes","building front grid...");
689

    
690
     vertSoFar = buildFrontBackGrid(true, vertSoFar,attribs);
691

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

    
699
         vertSoFar = repeatLast(vertSoFar,attribs);
700
         }
701

    
702
       //android.util.Log.d("MeshCubes","building side grid...");
703

    
704
       vertSoFar = buildSideGrid (vertSoFar,attribs);
705

    
706
       //android.util.Log.d("MeshCubes","building back grid...");
707

    
708
       buildFrontBackGrid (false,vertSoFar,attribs);
709
       }
710

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

    
714
     mEdges.clear();
715
     mEdges = null;
716
     mCubes = null;
717

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

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

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

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