Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshCubes.java @ 9d0df4c6

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

    
20
package org.distorted.library.mesh;
21

    
22
import java.util.ArrayList;
23

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

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

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

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

    
62
   private int currVert;
63
   private int numVertices;
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
       mInflateX = new byte[mRows+1][mCols+1];
195
       mInflateY = new byte[mRows+1][mCols+1];
196

    
197
       for(int j=0; j<mCols; j++)
198
         for(int i=0; i<mRows; i++)
199
           mCubes[i][j] = (desc.charAt(i * mCols + j) == '1' ? 1 : 0);
200

    
201
       for(int j=0; j<mCols+1; j++)
202
         for(int i=0; i<mRows+1; i++)
203
           {
204
           fillInflate(j,i);
205
           }
206

    
207
       markRegions();
208
       numVertices = computeDataLength();
209
       currVert = 0;
210
       }
211
     }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
// full grid
215

    
216
   private void prepareDataStructures(int cols, int rows, int slices)
217
     {
218
     mRows        =rows;
219
     mCols        =cols;
220
     mSlices      =slices;
221
     numVertices  =0;
222

    
223
     if( cols>0 && rows>0 )
224
       {
225
       mCubes    = new int[mRows][mCols];
226
       mInflateX = new byte[mRows+1][mCols+1];
227
       mInflateY = new byte[mRows+1][mCols+1];
228

    
229
       for(int j=0; j<mCols; j++)
230
         for(int i=0; i<mRows; i++)
231
           mCubes[i][j] = 1;
232

    
233
       for(int j=0; j<mCols+1; j++)
234
         for(int i=0; i<mRows+1; i++)
235
           {
236
           fillInflate(j,i);
237
           }
238

    
239
       markRegions();
240
       numVertices = computeDataLength();
241
       currVert = 0;
242
       }
243
     }
244

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

    
290
       do
291
         {
292
         //android.util.Log.d("CUBES", "checking edge "+debug(e1));
293

    
294
         mSideWalls++;
295

    
296
         if( e1.side==NORTH || e1.side==SOUTH )
297
           {
298
           for(j=i+1;j<mEdgeNum;j++)
299
             {
300
             e2 = mEdges.get(j);
301

    
302
             if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col )
303
               {
304
               mEdges.remove(j);
305
               mEdgeNum--;
306
               j--;
307

    
308
               //android.util.Log.e("CUBES", "removing edge "+debug(e2));
309
               }
310
             }
311
           }
312

    
313
         lastSide = e1.side;
314
         e1 = getNextEdge(e1);
315
         if( e1.side!=lastSide ) mSideBends++;
316
         }
317
       while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide );
318
       }
319
     }
320

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

    
351
     if(front)
352
       {
353
       mNormalZ[0] = 1.0f;
354
       mNormalZ[1] = 1.0f;
355
       mNormalZ[2] = 1.0f;
356
       mNormalZ[3] = 1.0f;
357
       }
358
     else
359
       {
360
       mNormalZ[0] =-1.0f;
361
       mNormalZ[1] =-1.0f;
362
       mNormalZ[2] =-1.0f;
363
       mNormalZ[3] =-1.0f;
364
       }
365

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

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

    
412
  private void buildFrontBackGrid(boolean front, float[] attribs)
413
     {
414
     int last, current;
415
     boolean seenLand=false;
416
     boolean lastBlockIsNE = false;
417
     boolean currentBlockIsNE;
418
     float vectZ = (front ? 0.5f : -0.5f);
419

    
420
     //android.util.Log.d("CUBES", "buildFrontBack");
421

    
422
     for(int row=0; row<mRows; row++)
423
       {
424
       last =0;
425
         
426
       for(int col=0; col<mCols; col++)
427
         {
428
         current = mCubes[row][col];
429

    
430
         if( current%2 == 1 )
431
           {
432
           currentBlockIsNE = isNE(row,col);
433

    
434
           if( !seenLand && !front && ((currVert%2==1)^currentBlockIsNE) )
435
             {
436
             //android.util.Log.d("CUBES","repeating winding2 vertex");
437

    
438
             repeatLast(attribs);
439
             }
440

    
441
           createNormals(front,row,col);
442

    
443
           if( currentBlockIsNE )
444
             {
445
             if( (last!=current) || !lastBlockIsNE )
446
               {
447
               if( seenLand  && (last != current) ) repeatLast(attribs);
448
               addFrontVertex( 0, vectZ, col, row, attribs);
449
               if( seenLand  && (last != current) ) repeatLast(attribs);
450
               if( !lastBlockIsNE || (!front && !seenLand) ) repeatLast(attribs);
451
               addFrontVertex( 1, vectZ, col, row+1, attribs);
452
               }
453
             addFrontVertex( 2, vectZ, col+1, row, attribs);
454
             addFrontVertex( 3, vectZ, col+1, row+1, attribs);
455
             }
456
           else
457
             {
458
             if( (last!=current) || lastBlockIsNE )
459
               {
460
               if( seenLand  && (last != current) ) repeatLast(attribs);
461
               addFrontVertex( 1, vectZ, col, row+1, attribs);
462
               if( seenLand  && (last != current) ) repeatLast(attribs);
463
               if( lastBlockIsNE || (!front && !seenLand) ) repeatLast(attribs);
464
               addFrontVertex( 0, vectZ, col, row, attribs);
465
               }
466
             addFrontVertex( 3, vectZ, col+1, row+1, attribs);
467
             addFrontVertex( 2, vectZ, col+1, row  , attribs);
468
             }
469

    
470
           seenLand = true;
471
           lastBlockIsNE = currentBlockIsNE;
472
           }
473
            
474
         last = current;
475
         }
476
       }
477
     }
478

    
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480

    
481
  private void buildSideGrid(float[] attribs)
482
     {
483
     //android.util.Log.d("CUBES", "buildSide");
484

    
485
     for(int i=0; i<mEdgeNum; i++)
486
       {
487
       buildIthSide(mEdges.get(i), attribs);
488
       }
489
     }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492

    
493
  private void buildIthSide(Edge curr, float[] attribs)
494
     {
495
     Edge prev, next;
496
     int col, row, side;
497

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

    
508
     for(int slice=0; slice<mSlices; slice++)
509
       {
510
       col = curr.col;
511
       row = curr.row;
512
       side= curr.side;
513
       next = getNextEdge(curr);
514
     
515
       addSideVertex(curr,true,slice+1,prev.side,attribs);
516

    
517
       do
518
         {
519
         if( prev.side!=curr.side )
520
           {
521
           addSideVertex(curr,true,slice+1,prev.side,attribs);
522
           addSideVertex(curr,true,slice  ,prev.side,attribs);
523
           }
524
       
525
         addSideVertex(curr,false,slice+1,next.side,attribs);
526
         addSideVertex(curr,false,slice  ,next.side,attribs);
527
       
528
         prev = curr;
529
         curr = next;
530
         next = getNextEdge(curr);
531
         }
532
       while( curr.col!=col || curr.row!=row || curr.side!=side );
533
     
534
       repeatLast(attribs);
535
       }
536
     }
537

    
538
///////////////////////////////////////////////////////////////////////////////////////////////////
539

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

    
587
///////////////////////////////////////////////////////////////////////////////////////////////////
588

    
589
  private void fillInflate(int col, int row)
590
    {
591
    int diff;
592

    
593
         if( col==0     ) mInflateX[col][row] = -1;
594
    else if( col==mCols ) mInflateX[col][row] = +1;
595
    else
596
      {
597
      if( row==0 )
598
        {
599
        diff = mCubes[0][col-1]-mCubes[0][col];
600
        }
601
      else if( row==mRows )
602
        {
603
        diff = mCubes[mRows-1][col-1]-mCubes[mRows-1][col];
604
        }
605
      else
606
        {
607
        diff = (mCubes[row  ][col-1]-mCubes[row  ][col]) +
608
               (mCubes[row-1][col-1]-mCubes[row-1][col]) ;
609

    
610
        if( diff==-2 ) diff=-1;
611
        if( diff== 2 ) diff= 1;
612
        }
613

    
614
      mInflateX[col][row] = (byte)diff;
615
      }
616

    
617
         if( row==0    ) mInflateY[col][row] = +1;
618
    else if(row==mRows ) mInflateY[col][row] = -1;
619
    else
620
      {
621
      if( col==0 )
622
        {
623
        diff = mCubes[row][0]-mCubes[row-1][0];
624
        }
625
      else if( col==mCols )
626
        {
627
        diff = mCubes[row][mCols-1]-mCubes[row-1][mCols-1];
628
        }
629
      else
630
        {
631
        diff = (mCubes[row  ][col-1]+mCubes[row  ][col]) -
632
               (mCubes[row-1][col-1]+mCubes[row-1][col]) ;
633

    
634
        if( diff==-2 ) diff=-1;
635
        if( diff== 2 ) diff= 1;
636
        }
637

    
638
      mInflateY[col][row] = (byte)diff;
639
      }
640

    
641
    //android.util.Log.e("mesh","col="+col+" row="+row+" inflateX="+mInflateX[col][row]+" InflateY="+mInflateY[col][row]);
642
    }
643

    
644
///////////////////////////////////////////////////////////////////////////////////////////////////
645

    
646
  private void addFrontVertex(int index, float vectZ, int col, int row, float[] attribs)
647
     {
648
     float x = (float)col/mCols;
649
     float y = (float)row/mRows;
650

    
651
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x-0.5f;
652
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f-y;
653
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = vectZ;
654

    
655
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = mNormalX[index];
656
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = mNormalY[index];
657
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = mNormalZ[index];
658

    
659
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInflateX[col][row]/2.0f;
660
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInflateY[col][row]/2.0f;
661
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = vectZ;
662

    
663
     attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = x;
664
     attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f-y;
665

    
666
     currVert++;
667
     }
668

    
669
///////////////////////////////////////////////////////////////////////////////////////////////////
670
   
671
  private void addSideVertex(Edge curr, boolean back, int slice, int side, float[] attribs)
672
     {
673
     //android.util.Log.e("CUBES", "adding Side vertex!");
674
     float x, y, z;
675
     int row, col;
676

    
677
     switch(curr.side)
678
       {
679
       case NORTH: row = curr.row;
680
                   col = (back ? (curr.col  ):(curr.col+1));
681
                   x = (float)col/mCols;
682
                   y = 0.5f - (float)row/mRows;
683
                   z = 0.5f - (float)slice/mSlices;
684

    
685
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x - 0.5f;
686
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = y;
687
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
688

    
689
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = side==NORTH ? 0.0f : (side==WEST?-R:R);
690
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = 1.0f;
691
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
692

    
693
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInflateX[col][row]/2.0f;
694
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInflateY[col][row]/2.0f;
695
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = z;
696

    
697
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = x;
698
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f-(float)(row-slice)/mRows;
699
                   break;
700
       case SOUTH: row = curr.row+1;
701
                   col = (back ? (curr.col+1):(curr.col));
702
                   x = (float)col/mCols;
703
                   y = 0.5f - (float)row/mRows;
704
                   z = 0.5f - (float)slice/mSlices;
705

    
706
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x - 0.5f;
707
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = y;
708
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
709

    
710
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = side==SOUTH ? 0.0f: (side==EAST?-R:R);
711
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] =-1.0f;
712
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
713

    
714
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInflateX[col][row]/2.0f;
715
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInflateY[col][row]/2.0f;
716
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = z;
717

    
718
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = x;
719
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f - (float)(row+slice)/mRows;
720
                   break;
721
       case WEST : row = (back  ? (curr.row+1):(curr.row));
722
                   col = curr.col;
723
                   x = (float)col/mCols -0.5f;
724
                   y = (float)row/mRows;
725
                   z = 0.5f - (float)slice/mSlices;
726

    
727
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x;
728
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f - y;
729
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
730

    
731
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] =-1.0f;
732
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = side==WEST ? 0.0f : (side==NORTH?-R:R);
733
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
734

    
735
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInflateX[col][row]/2.0f;
736
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInflateY[col][row]/2.0f;
737
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = z;
738

    
739
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = (float)(col-slice)/mCols;
740
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f - y;
741
                   break;
742
       case EAST : row = (back  ? (curr.row):(curr.row+1));
743
                   col = (curr.col+1);
744
                   x = (float)col/mCols -0.5f;
745
                   y = (float)row/mRows;
746
                   z = 0.5f - (float)slice/mSlices;
747

    
748
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x;
749
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f - y;
750
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
751

    
752
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = 1.0f;
753
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = side==EAST ? 0.0f : (side==SOUTH?-R:R);
754
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
755

    
756
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInflateX[col][row]/2.0f;
757
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInflateY[col][row]/2.0f;
758
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = z;
759

    
760
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = (float)(col+slice)/mCols;
761
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 1.0f - y;
762
                   break;
763
       }
764

    
765
     float tex0 =  attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ];
766
     float tex1 =  attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1];
767

    
768
     if(tex0>1.0f) attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = 2.0f-tex0;
769
     if(tex0<0.0f) attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] =     -tex0;
770
     if(tex1>1.0f) attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = 2.0f-tex1;
771
     if(tex1<0.0f) attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] =     -tex1;
772

    
773
     currVert++;
774
     }
775

    
776
///////////////////////////////////////////////////////////////////////////////////////////////////
777

    
778
   private void repeatLast(float[] attribs)
779
     {
780
     //android.util.Log.e("CUBES", "repeating last vertex!");
781

    
782
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + POS_ATTRIB  ];
783
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + POS_ATTRIB+1];
784
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = attribs[VERT_ATTRIBS*(currVert-1) + POS_ATTRIB+2];
785

    
786
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + NOR_ATTRIB  ];
787
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + NOR_ATTRIB+1];
788
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = attribs[VERT_ATTRIBS*(currVert-1) + NOR_ATTRIB+2];
789

    
790
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + INF_ATTRIB  ];
791
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + INF_ATTRIB+1];
792
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = attribs[VERT_ATTRIBS*(currVert-1) + INF_ATTRIB+2];
793

    
794
     attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + TEX_ATTRIB  ];
795
     attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + TEX_ATTRIB+1];
796

    
797
     currVert++;
798
     }
799

    
800
///////////////////////////////////////////////////////////////////////////////////////////////////
801

    
802
  private void build()
803
     {
804
     float[] attribs= new float[VERT_ATTRIBS*numVertices];
805

    
806
     buildFrontBackGrid(true,attribs);
807

    
808
     if( mSlices>0 )
809
       {
810
       repeatLast(attribs);
811
       if( currVert%2==1 ) repeatLast(attribs);
812
       buildSideGrid(attribs);
813
       buildFrontBackGrid(false,attribs);
814
       }
815

    
816
     mEdges.clear();
817
     mEdges = null;
818
     mCubes = null;
819
     mInflateX = null;
820
     mInflateY = null;
821

    
822
     if( currVert!=numVertices )
823
       android.util.Log.e("MeshCubes", "currVert " +currVert+" numVertices="+numVertices );
824

    
825
     setAttribs(attribs);
826
     }
827

    
828
///////////////////////////////////////////////////////////////////////////////////////////////////
829
// PUBLIC API
830
///////////////////////////////////////////////////////////////////////////////////////////////////
831
/**
832
 * Creates the underlying mesh of vertices, normals, texture coords.
833
 *    
834
 * @param cols   Integer helping to parse the next parameter.
835
 * @param desc   String describing the subset of a MxNx1 cuboid that we want to create.
836
 *               Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
837
 *               <p></p>
838
 *               <p>
839
 *               <pre>
840
 *               For example, (cols=2, desc="111010") describes the following shape:
841
 *
842
 *               XX
843
 *               X
844
 *               X
845
 *
846
 *               whereas (cols=2,desc="110001") describes
847
 *
848
 *               XX
849
 *
850
 *                X
851
 *               </pre>
852
 *               </p>
853
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
854
 */
855
 public MeshCubes(int cols, String desc, int slices)
856
   {
857
   super( (float)slices/cols);
858
   prepareDataStructures(cols,desc,slices);
859
   build();
860
   }
861

    
862
///////////////////////////////////////////////////////////////////////////////////////////////////
863
/**
864
 * Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors.
865
 *
866
 * @param cols   Number of columns, i.e. 'width' of the Mesh.
867
 * @param rows   Number of rows, i.e. 'height' of the Mesh.
868
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
869
 */
870
 public MeshCubes(int cols, int rows, int slices)
871
   {
872
   super( (float)slices/cols);
873
   prepareDataStructures(cols,rows,slices);
874
   build();
875
   }
876
 }
(2-2/3)