Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshCubes.java @ 7a5e538a

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library.mesh;
21

    
22
import java.util.ArrayList;
23

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

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

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

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

    
61
   private int remainingVert;
62
   private int numVertices;
63
   private int mSideBends;
64
   private int mEdgeNum;
65
   private int mSideWalls;
66

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

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

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

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

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

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

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

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

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

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

    
140
        mod = i%stop;
141

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

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

    
147

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

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

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

    
156
     return ret;
157
     }
158

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

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

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

    
198
       markRegions();
199
       numVertices = computeDataLength();
200
       remainingVert = numVertices;
201
       }
202
     }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
// full grid
206

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

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

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

    
222
       markRegions();
223
       numVertices = computeDataLength();
224
       remainingVert = numVertices;
225
       }
226
     }
227

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

    
273
       do
274
         {
275
         //android.util.Log.d("CUBES", "checking edge "+debug(e1));
276

    
277
         mSideWalls++;
278

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

    
285
             if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col )
286
               {
287
               mEdges.remove(j);
288
               mEdgeNum--;
289
               j--;
290

    
291
               //android.util.Log.e("CUBES", "removing edge "+debug(e2));
292
               }
293
             }
294
           }
295

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

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

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

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

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

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

    
403
     //android.util.Log.d("CUBES", "buildFrontBack");
404

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

    
413
         if( current%2 == 1 )
414
           {
415
           currentBlockIsNE = isNE(row,col);
416

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

    
421
             vertex = repeatLast(vertex,attribs);
422
             }
423

    
424
           createNormals(front,row,col);
425

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

    
453
           seenLand = true;
454
           lastBlockIsNE = currentBlockIsNE;
455
           }
456
            
457
         last = current;
458
         }
459
       }
460
     
461
     return vertex;
462
     }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465

    
466
  private int buildSideGrid(int vertex, float[] attribs)
467
     {
468
     //android.util.Log.d("CUBES", "buildSide");
469

    
470
     for(int i=0; i<mEdgeNum; i++)
471
       {
472
       vertex = buildIthSide(mEdges.get(i), vertex, attribs);
473
       } 
474
      
475
     return vertex;
476
     }
477

    
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479

    
480
  private int buildIthSide(Edge curr, int vertex, float[] attribs)
481
     {
482
     Edge prev, next;
483
     int col, row, side;
484

    
485
     if( curr.side==NORTH ) // water outside
486
       {
487
       prev = new Edge(WEST,curr.row,curr.col);
488
       }
489
     else                   // land outside; we need to move forward one link because we are
490
       {                    // going in opposite direction and we need to start from a bend.
491
       prev = curr;
492
       curr = new Edge(EAST,curr.row+1,curr.col-1);
493
       }
494

    
495
     for(int i=0; i<mSlices; i++)
496
       {
497
       col = curr.col;
498
       row = curr.row;
499
       side= curr.side;
500
       next = getNextEdge(curr);
501
     
502
       addSideVertex(curr,true,i+1,prev.side,vertex++,attribs);
503

    
504
       do
505
         {
506
         if( prev.side!=curr.side )
507
           {
508
           addSideVertex(curr,true,i+1,prev.side,vertex++,attribs);
509
           addSideVertex(curr,true,i  ,prev.side,vertex++,attribs);
510
           }
511
       
512
         addSideVertex(curr,false,i+1,next.side,vertex++,attribs);
513
         addSideVertex(curr,false,i  ,next.side,vertex++,attribs);
514
       
515
         prev = curr;
516
         curr = next;
517
         next = getNextEdge(curr);
518
         }
519
       while( curr.col!=col || curr.row!=row || curr.side!=side );
520
     
521
       vertex = repeatLast(vertex,attribs);
522
       }
523

    
524
     return vertex;
525
     }
526

    
527
///////////////////////////////////////////////////////////////////////////////////////////////////
528

    
529
  private Edge getNextEdge(Edge curr)
530
     {
531
     int col = curr.col;
532
     int row = curr.row;
533
      
534
     //android.util.Log.e("CUBES", "row="+row+" col="+col+" mRows="+mRows+" mCols="+mCols);
535
                       
536
     switch(curr.side) 
537
       {
538
       case NORTH: if( col==mCols-1 ) 
539
                     return new Edge(EAST,row,col);
540
                   if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] )
541
                     return new Edge(WEST,row-1,col+1);
542
                   if( mCubes[row][col+1]==mCubes[row][col] )
543
                     return new Edge(NORTH,row,col+1);
544
                   else  
545
                     return new Edge(EAST,row,col);
546
                   
547
       case SOUTH: if( col==0 ) 
548
                     return new Edge(WEST,row,col);
549
                   if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] )
550
                     return new Edge(EAST,row+1,col-1); 
551
                   if( mCubes[row][col-1]==mCubes[row][col] )
552
                     return new Edge(SOUTH,row,col-1);
553
                   else
554
                     return new Edge(WEST,row,col); 
555
                     
556
       case EAST : if( row==mRows-1 ) 
557
                     return new Edge(SOUTH,row,col);
558
                   if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] )
559
                     return new Edge(NORTH,row+1,col+1);
560
                   if( mCubes[row+1][col]==mCubes[row][col] )
561
                     return new Edge(EAST,row+1,col);
562
                   else 
563
                     return new Edge(SOUTH,row,col);
564
                   
565
       default   : if( row==0 )
566
                     return new Edge(NORTH,row,col);
567
                   if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] )
568
                     return new Edge(SOUTH,row-1,col-1);
569
                   if( mCubes[row-1][col]==mCubes[row][col] )
570
                     return new Edge(WEST,row-1,col);
571
                   else
572
                     return new Edge(NORTH,row,col);     
573
       }
574
     }
575

    
576
///////////////////////////////////////////////////////////////////////////////////////////////////
577

    
578
  private int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] attribs)
579
     {
580
     remainingVert--;
581

    
582
     float x = (float)col/mCols;
583
     float y = (float)row/mRows;
584

    
585
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB  ] = x-0.5f;
586
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = 0.5f-y;
587
     attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = vectZ;
588

    
589
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB  ] = mNormalX[index];
590
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = mNormalY[index];
591
     attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = mNormalZ[index];
592

    
593
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = 1.0f;  //
594
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = 0.0f;  // TODO
595
     attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = 0.0f;  //
596

    
597
     attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = x;
598
     attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = 1.0f-y;
599

    
600
     return vertex+1;
601
     }
602

    
603
///////////////////////////////////////////////////////////////////////////////////////////////////
604
   
605
  private void addSideVertex(Edge curr, boolean back, int slice,int side, int vertex, float[] attribs)
606
     {
607
     //android.util.Log.e("CUBES", "adding Side vertex!");
608

    
609
     remainingVert--;
610

    
611
     float x, y;
612

    
613
     switch(curr.side)
614
       {
615
       case NORTH: x = (float)(back ? (curr.col  ):(curr.col+1))/mCols;
616

    
617
                   attribs[VERT_ATTRIBS*vertex + POS_ATTRIB  ] = x - 0.5f;
618
                   attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = 0.5f - (float)curr.row/mRows;
619
                   attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = 0.5f - (float)slice/mSlices;
620

    
621
                   attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB  ] = side==NORTH ? 0.0f : (side==WEST?-R:R);
622
                   attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = 1.0f;
623
                   attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
624

    
625
                   attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = 1.0f;  //
626
                   attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = 0.0f;  // TODO
627
                   attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = 0.0f;  //
628

    
629
                   attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = x;
630
                   attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = 1.0f-(float)(curr.row-slice)/mRows;
631
                   break;
632
       case SOUTH: x = (float)(back ? (curr.col+1):(curr.col  ))/mCols;
633

    
634
                   attribs[VERT_ATTRIBS*vertex + POS_ATTRIB  ] = x - 0.5f;
635
                   attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = 0.5f - (float)(curr.row+1)/mRows;
636
                   attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = 0.5f - (float)slice/mSlices;
637

    
638
                   attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB  ] = side==SOUTH ? 0.0f: (side==EAST?-R:R);
639
                   attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] =-1.0f;
640
                   attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
641

    
642
                   attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = 1.0f;  //
643
                   attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = 0.0f;  // TODO
644
                   attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = 0.0f;  //
645

    
646
                   attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = x;
647
                   attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = 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[VERT_ATTRIBS*vertex + POS_ATTRIB  ] = (float)curr.col/mCols -0.5f;
652
                   attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = 0.5f - y;
653
                   attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = 0.5f - (float)slice/mSlices;
654

    
655
                   attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB  ] =-1.0f;
656
                   attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = side==WEST ? 0.0f : (side==NORTH?-R:R);
657
                   attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
658

    
659
                   attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = 1.0f;  //
660
                   attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = 0.0f;  // TODO
661
                   attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = 0.0f;  //
662

    
663
                   attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = (float)(curr.col-slice)/mCols;
664
                   attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = 1.0f - y;
665
                   break;
666
       case EAST : y = (float)(back  ? (curr.row):(curr.row+1))/mRows;
667

    
668
                   attribs[VERT_ATTRIBS*vertex + POS_ATTRIB  ] = (float)(curr.col+1)/mCols -0.5f;
669
                   attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = 0.5f - y;
670
                   attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = 0.5f - (float)slice/mSlices;
671

    
672
                   attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB  ] = 1.0f;
673
                   attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = side==EAST ? 0.0f : (side==SOUTH?-R:R);
674
                   attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
675

    
676
                   attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = 1.0f;  //
677
                   attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = 0.0f;  // TODO
678
                   attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = 0.0f;  //
679

    
680
                   attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = (float)(curr.col+1+slice)/mCols;
681
                   attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = 1.0f - y;
682
                   break;
683
       }
684

    
685
     float tex0 =  attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ];
686
     float tex1 =  attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1];
687

    
688
     if(tex0>1.0f) attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = 2.0f-tex0;
689
     if(tex0<0.0f) attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] =     -tex0;
690
     if(tex1>1.0f) attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = 2.0f-tex1;
691
     if(tex1<0.0f) attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] =     -tex1;
692
     }
693

    
694
///////////////////////////////////////////////////////////////////////////////////////////////////
695

    
696
   private int repeatLast(int vertex, float[] attribs)
697
     {
698
     //android.util.Log.e("CUBES", "repeating last vertex!");
699

    
700
     if( vertex>0 )
701
       {
702
       remainingVert--;
703

    
704
       attribs[VERT_ATTRIBS*vertex + POS_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB  ];
705
       attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+1];
706
       attribs[VERT_ATTRIBS*vertex + POS_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + POS_ATTRIB+2];
707

    
708
       attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB  ];
709
       attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+1];
710
       attribs[VERT_ATTRIBS*vertex + NOR_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + NOR_ATTRIB+2];
711

    
712
       attribs[VERT_ATTRIBS*vertex + INF_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB  ];
713
       attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+1];
714
       attribs[VERT_ATTRIBS*vertex + INF_ATTRIB+2] = attribs[VERT_ATTRIBS*(vertex-1) + INF_ATTRIB+2];
715

    
716
       attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB  ] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB  ];
717
       attribs[VERT_ATTRIBS*vertex + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(vertex-1) + TEX_ATTRIB+1];
718

    
719

    
720
       vertex++;
721
       }
722

    
723
     return vertex;
724
     }
725

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

    
728
  private void build()
729
     {
730
     int vertSoFar=0;
731
     float[] attribs= new float[VERT_ATTRIBS*numVertices];
732

    
733
     vertSoFar = buildFrontBackGrid(true, vertSoFar,attribs);
734

    
735
     if( mSlices>0 )
736
       {
737
       vertSoFar = repeatLast(vertSoFar,attribs);
738
       if( vertSoFar%2==1 )
739
         {
740
         vertSoFar = repeatLast(vertSoFar,attribs);
741
         }
742
       vertSoFar = buildSideGrid (vertSoFar,attribs);
743
       buildFrontBackGrid (false,vertSoFar,attribs);
744
       }
745

    
746
     mEdges.clear();
747
     mEdges = null;
748
     mCubes = null;
749

    
750
     if( remainingVert!=0 )
751
       android.util.Log.e("MeshCubes", "remainingVert " +remainingVert );
752

    
753
     setAttribs(attribs);
754
     }
755

    
756
///////////////////////////////////////////////////////////////////////////////////////////////////
757
// PUBLIC API
758
///////////////////////////////////////////////////////////////////////////////////////////////////
759
/**
760
 * Creates the underlying mesh of vertices, normals, texture coords.
761
 *    
762
 * @param cols   Integer helping to parse the next parameter.
763
 * @param desc   String describing the subset of a MxNx1 cuboid that we want to create.
764
 *               Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
765
 *               <p></p>
766
 *               <p>
767
 *               <pre>
768
 *               For example, (cols=2, desc="111010") describes the following shape:
769
 *
770
 *               XX
771
 *               X
772
 *               X
773
 *
774
 *               whereas (cols=2,desc="110001") describes
775
 *
776
 *               XX
777
 *
778
 *                X
779
 *               </pre>
780
 *               </p>
781
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
782
 */
783
 public MeshCubes(int cols, String desc, int slices)
784
   {
785
   super( (float)slices/cols);
786
   prepareDataStructures(cols,desc,slices);
787
   build();
788
   }
789

    
790
///////////////////////////////////////////////////////////////////////////////////////////////////
791
/**
792
 * Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors.
793
 *
794
 * @param cols   Number of columns, i.e. 'width' of the Mesh.
795
 * @param rows   Number of rows, i.e. 'height' of the Mesh.
796
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
797
 */
798
 public MeshCubes(int cols, int rows, int slices)
799
   {
800
   super( (float)slices/cols);
801
   prepareDataStructures(cols,rows,slices);
802
   build();
803
   }
804
 }
(2-2/3)