Project

General

Profile

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

library / src / main / java / org / distorted / library / main / MeshCubes.java @ 466450b5

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.main;
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

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

    
41
   private static final float[] mNormalX = new float[4];
42
   private static final float[] mNormalY = new float[4];
43
   private static final float[] mNormalZ = new float[4];
44

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

    
63
   private int remainingVert;
64
   private int mSideBends;
65
   private int mEdgeNum;
66
   private int mSideWalls;
67

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

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

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

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

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

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

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

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

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

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

    
141
        mod = i%stop;
142

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

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

    
148

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

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

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

    
157
     return ret;
158
     }
159

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

    
181
   private void prepareDataStructures(int cols, String desc, int slices)
182
     {
183
     mRows      =0;
184
     mCols      =0;
185
     mSlices    =slices;
186
     numVertices=0;
187
     
188
     if( cols>0 && desc.contains("1") )
189
       {
190
       mCols = cols;
191
       mRows = desc.length()/cols;
192

    
193
       mCubes = new int[mRows][mCols];
194
       
195
       for(int j=0; j<mCols; j++)
196
         for(int i=0; i<mRows; i++)
197
           mCubes[i][j] = (desc.charAt(i*mCols+j) == '1' ? 1:0);
198

    
199
       markRegions();
200
       numVertices = computeDataLength();
201

    
202
       remainingVert = numVertices;
203
       }
204
     }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
// full grid
208

    
209
   private void prepareDataStructures(int cols, int rows, int slices)
210
     {
211
     mRows       =rows;
212
     mCols       =cols;
213
     mSlices     =slices;
214
     numVertices =   0;
215

    
216
     if( cols>0 && rows>0 )
217
       {
218
       mCubes = new int[mRows][mCols];
219

    
220
       for(int j=0; j<mCols; j++)
221
         for(int i=0; i<mRows; i++)
222
           mCubes[i][j] = 1;
223

    
224
       markRegions();
225
       numVertices = computeDataLength();
226

    
227
       remainingVert = numVertices;
228
       }
229
     }
230

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

    
276
       do
277
         {
278
         //android.util.Log.d("CUBES", "checking edge "+debug(e1));
279

    
280
         mSideWalls++;
281

    
282
         if( e1.side==NORTH || e1.side==SOUTH )
283
           {
284
           for(j=i+1;j<mEdgeNum;j++)
285
             {
286
             e2 = mEdges.get(j);
287

    
288
             if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col )
289
               {
290
               mEdges.remove(j);
291
               mEdgeNum--;
292
               j--;
293

    
294
               //android.util.Log.e("CUBES", "removing edge "+debug(e2));
295
               }
296
             }
297
           }
298

    
299
         lastSide = e1.side;
300
         e1 = getNextEdge(e1);
301
         if( e1.side!=lastSide ) mSideBends++;
302
         }
303
       while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide );
304
       }
305
     }
306

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

    
337
     if(front)
338
       {
339
       mNormalZ[0] = 1.0f;
340
       mNormalZ[1] = 1.0f;
341
       mNormalZ[2] = 1.0f;
342
       mNormalZ[3] = 1.0f;
343
       }
344
     else
345
       {
346
       mNormalZ[0] =-1.0f;
347
       mNormalZ[1] =-1.0f;
348
       mNormalZ[2] =-1.0f;
349
       mNormalZ[3] =-1.0f;
350
       }
351

    
352
     td = nw+n-w-c;
353
     lr = c+n-w-nw;
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[0] = lr*R;
359
     mNormalY[0] = td*R;
360
     
361
     td = w+c-sw-s;
362
     lr = c+s-w-sw;
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[1] = lr*R;
368
     mNormalY[1] = td*R;
369
     
370
     td = n+ne-c-e;
371
     lr = e+ne-c-n;
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[2] = lr*R;
377
     mNormalY[2] = td*R;
378
     
379
     td = c+e-s-se;
380
     lr = e+se-c-s;
381
     if( td<0 ) td=-1;
382
     if( td>0 ) td= 1;
383
     if( lr<0 ) lr=-1;
384
     if( lr>0 ) lr= 1;
385
     mNormalX[3] = lr*R;
386
     mNormalY[3] = td*R;
387
     /*
388
     android.util.Log.d("CUBES", "row="+row+" col="+col);
389
     android.util.Log.d("CUBES", mNormalX[0]+" "+mNormalY[0]);
390
     android.util.Log.d("CUBES", mNormalX[1]+" "+mNormalY[1]);
391
     android.util.Log.d("CUBES", mNormalX[2]+" "+mNormalY[2]);
392
     android.util.Log.d("CUBES", mNormalX[3]+" "+mNormalY[3]);
393
     */
394
     }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
  private int buildFrontBackGrid(boolean front, int vertex, float[] attribs)
399
     {
400
     int last, current;
401
     boolean seenLand=false;
402
     boolean lastBlockIsNE = false;
403
     boolean currentBlockIsNE;
404
     float vectZ = (front ? 0.5f : -0.5f);
405

    
406
     //android.util.Log.d("CUBES", "buildFrontBack");
407

    
408
     for(int row=0; row<mRows; row++)
409
       {
410
       last =0;
411
         
412
       for(int col=0; col<mCols; col++)
413
         {
414
         current = mCubes[row][col];
415

    
416
         if( current%2 == 1 )
417
           {
418
           currentBlockIsNE = isNE(row,col);
419

    
420
           if( !seenLand && !front && ((vertex%2==1)^currentBlockIsNE) )
421
             {
422
             //android.util.Log.d("CUBES","repeating winding2 vertex");
423

    
424
             vertex = repeatLast(vertex,attribs);
425
             }
426

    
427
           createNormals(front,row,col);
428

    
429
           if( currentBlockIsNE )
430
             {
431
             if( (last!=current) || !lastBlockIsNE )
432
               {
433
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
434
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, attribs);
435
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
436
               if( !lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,attribs);
437
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, attribs);
438
               }
439
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row, attribs);
440
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, attribs);
441
             }
442
           else
443
             {
444
             if( (last!=current) || lastBlockIsNE )
445
               {
446
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
447
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, attribs);
448
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,attribs);
449
               if( lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,attribs);
450
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, attribs);
451
               }
452
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, attribs);
453
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row  , attribs);
454
             }
455

    
456
           seenLand = true;
457
           lastBlockIsNE = currentBlockIsNE;
458
           }
459
            
460
         last = current;
461
         }
462
       }
463
     
464
     return vertex;
465
     }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

    
469
  private int repeatLast(int vertex, float[] attribs)
470
     {
471
     //android.util.Log.e("CUBES", "repeating last vertex!");
472

    
473
     if( vertex>0 )
474
       {
475
       remainingVert--;
476

    
477
       attribs[8*vertex  ] = attribs[8*vertex-8];
478
       attribs[8*vertex+1] = attribs[8*vertex-7];
479
       attribs[8*vertex+2] = attribs[8*vertex-6];
480
       attribs[8*vertex+3] = attribs[8*vertex-5];
481
       attribs[8*vertex+4] = attribs[8*vertex-4];
482
       attribs[8*vertex+5] = attribs[8*vertex-3];
483
       attribs[8*vertex+6] = attribs[8*vertex-2];
484
       attribs[8*vertex+7] = attribs[8*vertex-1];
485
         
486
       vertex++;     
487
       }
488
     
489
     return vertex;
490
     }
491
   
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493

    
494
  private int buildSideGrid(int vertex, float[] attribs)
495
     {
496
     //android.util.Log.d("CUBES", "buildSide");
497

    
498
     for(int i=0; i<mEdgeNum; i++)
499
       {
500
       vertex = buildIthSide(mEdges.get(i), vertex, attribs);
501
       } 
502
      
503
     return vertex;
504
     }
505

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507

    
508
  private int buildIthSide(Edge curr, int vertex, float[] attribs)
509
     {
510
     Edge prev, next;
511
     int col, row, side;
512

    
513
     if( curr.side==NORTH ) // water outside
514
       {
515
       prev = new Edge(WEST,curr.row,curr.col);
516
       }
517
     else                   // land outside; we need to move forward one link because we are
518
       {                    // going in opposite direction and we need to start from a bend.
519
       prev = curr;
520
       curr = new Edge(EAST,curr.row+1,curr.col-1);
521
       }
522

    
523
     for(int i=0; i<mSlices; i++)
524
       {
525
       col = curr.col;
526
       row = curr.row;
527
       side= curr.side;
528
       next = getNextEdge(curr);
529
     
530
       addSideVertex(curr,true,i+1,prev.side,vertex++,attribs);
531

    
532
       do
533
         {
534
         if( prev.side!=curr.side )
535
           {
536
           addSideVertex(curr,true,i+1,prev.side,vertex++,attribs);
537
           addSideVertex(curr,true,i  ,prev.side,vertex++,attribs);
538
           }
539
       
540
         addSideVertex(curr,false,i+1,next.side,vertex++,attribs);
541
         addSideVertex(curr,false,i  ,next.side,vertex++,attribs);
542
       
543
         prev = curr;
544
         curr = next;
545
         next = getNextEdge(curr);
546
         }
547
       while( curr.col!=col || curr.row!=row || curr.side!=side );
548
     
549
       vertex = repeatLast(vertex,attribs);
550
       }
551

    
552
     return vertex;
553
     }
554

    
555
///////////////////////////////////////////////////////////////////////////////////////////////////
556

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

    
604
///////////////////////////////////////////////////////////////////////////////////////////////////
605

    
606
  private int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] attribs)
607
     {
608
     remainingVert--;
609

    
610
     float x = (float)col/mCols;
611
     float y = (float)row/mRows;
612

    
613
     attribs[8*vertex  ] = x-0.5f;
614
     attribs[8*vertex+1] = 0.5f-y;
615
     attribs[8*vertex+2] = vectZ;
616
     attribs[8*vertex+3] = mNormalX[index];
617
     attribs[8*vertex+4] = mNormalY[index];
618
     attribs[8*vertex+5] = mNormalZ[index];
619
     attribs[8*vertex+6] = x;
620
     attribs[8*vertex+7] = 1.0f-y;
621

    
622
     return vertex+1;
623
     }
624

    
625
///////////////////////////////////////////////////////////////////////////////////////////////////
626
   
627
  private void addSideVertex(Edge curr, boolean back, int slice,int side, int vertex, float[] attribs)
628
     {
629
     //android.util.Log.e("CUBES", "adding Side vertex!");
630

    
631
     remainingVert--;
632

    
633
     float x, y;
634

    
635
     switch(curr.side)
636
       {
637
       case NORTH: x = (float)(back ? (curr.col  ):(curr.col+1))/mCols;
638

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

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

    
661
                   attribs[8*vertex  ] = (float)curr.col/mCols -0.5f;
662
                   attribs[8*vertex+1] = 0.5f - y;
663
                   attribs[8*vertex+2] = 0.5f - (float)slice/mSlices;
664
                   attribs[8*vertex+3] =-1.0f;
665
                   attribs[8*vertex+4] = side==WEST ? 0.0f : (side==NORTH?-R:R);
666
                   attribs[8*vertex+5] = (slice==0 ? R : (slice==mSlices ? -R:0) );
667
                   attribs[8*vertex+6] = (float)(curr.col-slice)/mCols;
668
                   attribs[8*vertex+7] = 1.0f - y;
669
                   break;
670
       case EAST : y = (float)(back  ? (curr.row):(curr.row+1))/mRows;
671

    
672
                   attribs[8*vertex  ] = (float)(curr.col+1)/mCols -0.5f;
673
                   attribs[8*vertex+1] = 0.5f - y;
674
                   attribs[8*vertex+2] = 0.5f - (float)slice/mSlices;
675
                   attribs[8*vertex+3] = 1.0f;
676
                   attribs[8*vertex+4] = side==EAST ? 0.0f : (side==SOUTH?-R:R);
677
                   attribs[8*vertex+5] = (slice==0 ? R : (slice==mSlices ? -R:0) );
678
                   attribs[8*vertex+6] = (float)(curr.col+1+slice)/mCols;
679
                   attribs[8*vertex+7] = 1.0f - y;
680
                   break;
681
       }
682
     
683
     if(attribs[8*vertex+6]>1.0f) attribs[8*vertex+6] = 2.0f-attribs[8*vertex+6];
684
     if(attribs[8*vertex+6]<0.0f) attribs[8*vertex+6] =     -attribs[8*vertex+6];
685
     if(attribs[8*vertex+7]>1.0f) attribs[8*vertex+7] = 2.0f-attribs[8*vertex+7];
686
     if(attribs[8*vertex+7]<0.0f) attribs[8*vertex+7] =     -attribs[8*vertex+7];
687
     }
688

    
689
///////////////////////////////////////////////////////////////////////////////////////////////////
690

    
691
  private void build()
692
     {
693
     int vertSoFar=0;
694
     float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
695

    
696
     vertSoFar = buildFrontBackGrid(true, vertSoFar,attribs);
697

    
698
     if( mSlices>0 )
699
       {
700
       vertSoFar = repeatLast(vertSoFar,attribs);
701
       if( vertSoFar%2==1 )
702
         {
703
         vertSoFar = repeatLast(vertSoFar,attribs);
704
         }
705
       vertSoFar = buildSideGrid (vertSoFar,attribs);
706
       buildFrontBackGrid (false,vertSoFar,attribs);
707
       }
708

    
709
     mEdges.clear();
710
     mEdges = null;
711
     mCubes = null;
712

    
713
     if( remainingVert!=0 )
714
       android.util.Log.e("MeshCubes", "remainingVert " +remainingVert );
715

    
716
     mVertAttribs = ByteBuffer.allocateDirect(numVertices*VERTSIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
717
     mVertAttribs.put(attribs).position(0);
718

    
719
     setData(numVertices, mVertAttribs);
720
     }
721

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

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