Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshCubes.java @ ece89b28

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 org.distorted.library.type.Static4D;
23

    
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. (repeated arbitrary number of times into Z-dir)
31
 */
32
public class MeshCubes extends MeshBase
33
   {
34
   private static final float R = 0.0f;//0.2f;
35

    
36
   private static final int FRONT = 0;
37
   private static final int BACK  = 1;
38
   private static final int LEFT  = 2;
39
   private static final int RIGHT = 3;
40
   private static final int TOP   = 4;
41
   private static final int BOTTOM= 5;
42

    
43
   private static final int NORTH = 0;
44
   private static final int WEST  = 1;
45
   private static final int EAST  = 2;
46
   private static final int SOUTH = 3;
47

    
48
   private static final float[] mNormalX = new float[4];
49
   private static final float[] mNormalY = new float[4];
50
   private static final float[] mNormalZ = new float[4];
51

    
52
   private class Edge
53
     {
54
     final int side; 
55
     final int row;
56
     final int col;
57
     
58
     Edge(int s, int r, int c)
59
       {
60
       side= s; 
61
       row = r;
62
       col = c;
63
       }
64
     }
65

    
66
   private float[] mTexMappingX,mTexMappingY, mTexMappingW, mTexMappingH;
67

    
68
   private int mCols, mRows, mSlices;
69
   private int[][] mCubes;
70
   private byte[][] mInflateX, mInflateY;
71
   private ArrayList<Edge> mEdges = new ArrayList<>();
72

    
73
   private int currVert;
74
   private int numVertices;
75
   private int mSideBends;
76
   private int mEdgeNum;
77
   private int mSideWalls;
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
// a Block is split into two triangles along the NE-SW line iff it is in the top-right
81
// or bottom-left quadrant of the grid.
82

    
83
   private boolean isNE(int row,int col)
84
     {
85
     return ( (2*row<mRows)^(2*col<mCols) );
86
     }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
// fill per-side texture mappings. Default: all 6 sides map to the whole texture.
90
// Each Static4D describes the way its side will map. Format:
91
//
92
// 1st float: X-coord of the texture point that our top-left corner of the side maps to
93
// 2nd float: Y-coord of the texture point that our top-left corner of the side maps to
94
// 3rd float: X-coord of the texture point that our bot-right corner of the side maps to
95
// 4th float: Y-coord of the texture point that our bot-right corner of the side maps to
96

    
97
   private void fillTexMappings(Static4D front, Static4D back, Static4D left, Static4D right, Static4D top, Static4D bottom)
98
     {
99
     if( mTexMappingX==null ) mTexMappingX = new float[6];
100
     if( mTexMappingY==null ) mTexMappingY = new float[6];
101
     if( mTexMappingW==null ) mTexMappingW = new float[6];
102
     if( mTexMappingH==null ) mTexMappingH = new float[6];
103

    
104
     mTexMappingX[FRONT]  = front.get0();
105
     mTexMappingY[FRONT]  = front.get1();
106
     mTexMappingW[FRONT]  = front.get2() - front.get0();
107
     mTexMappingH[FRONT]  = front.get3() - front.get1();
108

    
109
     mTexMappingX[BACK]   = back.get0();
110
     mTexMappingY[BACK]   = back.get1();
111
     mTexMappingW[BACK]   = back.get2() - back.get0();
112
     mTexMappingH[BACK]   = back.get3() - back.get1();
113

    
114
     mTexMappingX[LEFT]   = left.get0();
115
     mTexMappingY[LEFT]   = left.get1();
116
     mTexMappingW[LEFT]   = left.get2() - left.get0();
117
     mTexMappingH[LEFT]   = left.get3() - left.get1();
118

    
119
     mTexMappingX[RIGHT]  = right.get0();
120
     mTexMappingY[RIGHT]  = right.get1();
121
     mTexMappingW[RIGHT]  = right.get2() - right.get0();
122
     mTexMappingH[RIGHT]  = right.get3() - right.get1();
123

    
124
     mTexMappingX[TOP]    = top.get0();
125
     mTexMappingY[TOP]    = top.get1();
126
     mTexMappingW[TOP]    = top.get2() - top.get0();
127
     mTexMappingH[TOP]    = top.get3() - top.get1();
128

    
129
     mTexMappingX[BOTTOM] = bottom.get0();
130
     mTexMappingY[BOTTOM] = bottom.get1();
131
     mTexMappingW[BOTTOM] = bottom.get2() - bottom.get0();
132
     mTexMappingH[BOTTOM] = bottom.get3() - bottom.get1();
133
     }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
// return the number of vertices our grid will contain
137

    
138
   private int computeDataLength()
139
      {
140
      int frontWalls=0, frontSegments=0, triangleShifts=0, windingShifts=0;
141
      int shiftCol = (mCols-1)/2;
142

    
143
      boolean lastBlockIsNE=false;
144
      boolean thisBlockIsNE;        // the block we are currently looking at is split into
145
                                    // two triangles along the NE-SW line (rather than NW-SE)
146
      for(int row=0; row<mRows; row++)
147
        {
148
        if( mCols>=2 && (mCubes[row][shiftCol]%2 == 1) && (mCubes[row][shiftCol+1]%2 == 1) ) triangleShifts++;
149

    
150
        for(int col=0; col<mCols; col++)
151
          {
152
          if( mCubes[row][col]%2 == 1 )  // land
153
            {
154
            thisBlockIsNE = isNE(row,col);
155
            if( thisBlockIsNE^lastBlockIsNE ) windingShifts++;
156
            lastBlockIsNE = thisBlockIsNE;
157
            frontWalls++;
158
            if( col==mCols-1 || mCubes[row][col+1]%2 == 0 ) frontSegments++;
159
            }
160
          }
161
        }
162

    
163
      int frontVert       = 2*( frontWalls + 2*frontSegments - 1) +2*triangleShifts + windingShifts;
164
      int sideVertOneSlice= 2*( mSideWalls + mSideBends + mEdgeNum -1);
165
      int sideVert        = 2*(mSlices-1) + mSlices*sideVertOneSlice;
166
      int firstWinding    = (mSlices>0 && (frontVert+1)%2==1 ) ? 1:0;
167
      int dataL           = mSlices==0 ? frontVert : (frontVert+1) +firstWinding+ (1+sideVert+1) + (1+frontVert);
168
/*
169
      android.util.Log.e("CUBES","triangleShifts="+triangleShifts+" windingShifts="+windingShifts+" winding1="+firstWinding+" frontVert="+frontVert+" sideVert="+sideVert);
170
      android.util.Log.e("CUBES", "frontW="+frontWalls+" fSegments="+frontSegments+" sWalls="+mSideWalls+" sSegments="+mEdgeNum+" sideBends="+mSideBends+" dataLen="+dataL );
171
*/
172
      return dataL<0 ? 0:dataL;
173
      }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
/*
177
   private static String debug(short[] val)
178
     {
179
     String ret="";j
180
     
181
     for(int i=0; i<val.length; i++) ret+=(" "+val[i]); 
182
     
183
     return ret;
184
     }
185
*/
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
/*
188
   private static String debug(float[] val, int stop)
189
     {
190
     String ret="";
191
     float v;
192
     boolean neg;
193
     int mod;
194

    
195
     for(int i=0; i<val.length; i++) 
196
        {
197
        if( i%stop==0 ) ret+="\n";
198

    
199
        mod = i%stop;
200

    
201
        if( mod==0 || mod==3 || mod==6 ) ret+=" (";
202

    
203
        v = val[i];
204
        if( v==-0.0f ) v=0.0f;
205

    
206

    
207
        neg = v<0;
208
        v = (v<0 ? -v:v);
209

    
210
        ret+=((neg? " -":" +")+v);
211

    
212
        if( mod==2 || mod==5 || mod==7 ) ret+=")";
213
        }
214

    
215
     return ret;
216
     }
217
*/
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
/*
220
   private static String debug(Edge e)
221
     {
222
     String d = "";
223
     
224
     switch(e.side)
225
       {
226
       case NORTH: d+="NORTH "; break;
227
       case SOUTH: d+="SOUTH "; break;
228
       case WEST : d+="WEST  "; break;
229
       case EAST : d+="EAST  "; break;
230
       }
231
     
232
     d+=("("+e.row+","+e.col+")");
233
     
234
     return d;
235
     }   
236
*/
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
   private void prepareDataStructures(int cols, String desc, int slices)
240
     {
241
     mRows       =0;
242
     mCols       =0;
243
     mSlices     =slices;
244
     numVertices =0;
245

    
246
     if( cols>0 && desc.contains("1") )
247
       {
248
       mCols = cols;
249
       mRows = desc.length()/cols;
250

    
251
       mCubes    = new int[mRows][mCols];
252
       mInflateX = new byte[mRows+1][mCols+1];
253
       mInflateY = new byte[mRows+1][mCols+1];
254

    
255
       for(int col=0; col<mCols; col++)
256
         for(int row=0; row<mRows; row++)
257
           mCubes[row][col] = (desc.charAt(row * mCols + col) == '1' ? 1 : 0);
258

    
259
       for(int col=0; col<mCols+1; col++)
260
         for(int row=0; row<mRows+1; row++)
261
           {
262
           fillInflate(row,col);
263
           }
264

    
265
       markRegions();
266
       numVertices = computeDataLength();
267
       currVert = 0;
268
       }
269
     }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272
// full grid
273

    
274
   private void prepareDataStructures(int cols, int rows, int slices)
275
     {
276
     mRows        =rows;
277
     mCols        =cols;
278
     mSlices      =slices;
279
     numVertices  =0;
280

    
281
     if( cols>0 && rows>0 )
282
       {
283
       mCubes    = new int[mRows][mCols];
284
       mInflateX = new byte[mRows+1][mCols+1];
285
       mInflateY = new byte[mRows+1][mCols+1];
286

    
287
       for(int col=0; col<mCols; col++)
288
         for(int row=0; row<mRows; row++)
289
           mCubes[row][col] = 1;
290

    
291
       for(int col=0; col<mCols+1; col++)
292
         for(int row=0; row<mRows+1; row++)
293
           {
294
           fillInflate(row,col);
295
           }
296

    
297
       markRegions();
298
       numVertices = computeDataLength();
299
       currVert = 0;
300
       }
301
     }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304
// Mark all the 'regions' of our grid  - i.e. separate pieces of 'land' (connected blocks that will 
305
// be rendered) and 'water' (connected holes in between) with integers. Each connected block of land
306
// gets a unique odd integer, each connected block of water a unique even integer.
307
//
308
// Water on the edges of the grid is also considered connected to itself!   
309
//   
310
// This function also creates a list of 'Edges'. Each Edge is a data structure from which later on we
311
// will start building the side walls of each connected block of land (and sides of holes of water
312
// inside). Each Edge needs to point from Land to Water (thus the '(SOUTH,row-1,col)' below) - otherwise
313
// later on setting up normal vectors wouldn't work.
314
   
315
  private void markRegions()
316
     {
317
     int row, col, numWater=1, numLand=0;
318
     
319
     for(row=0; row<mRows; row++) if( mCubes[    row][      0]==0 ) markRegion((short)2,    row,       0);
320
     for(row=0; row<mRows; row++) if( mCubes[    row][mCols-1]==0 ) markRegion((short)2,    row, mCols-1);
321
     for(col=0; col<mCols; col++) if( mCubes[0      ][    col]==0 ) markRegion((short)2,      0,     col);
322
     for(col=0; col<mCols; col++) if( mCubes[mRows-1][    col]==0 ) markRegion((short)2,mRows-1,     col);
323
           
324
     for(row=0; row<mRows; row++)
325
        for(col=0; col<mCols; col++)
326
           {
327
           if( mCubes[row][col] == 0 ) { numWater++; markRegion( (short)(2*numWater ),row,col); mEdges.add(new Edge(SOUTH,row-1,col)); }
328
           if( mCubes[row][col] == 1 ) { numLand ++; markRegion( (short)(2*numLand+1),row,col); mEdges.add(new Edge(NORTH,row  ,col)); }
329
           }
330
     
331
     // now we potentially need to kick out some Edges . Otherwise the following does not work:
332
     //
333
     // 0 1 0
334
     // 1 0 1
335
     // 0 1 0
336
     
337
     mEdgeNum= mEdges.size();
338
     int initCol, initRow, initSide, lastSide;
339
     Edge e1,e2;
340
     
341
     for(int edge=0; edge<mEdgeNum; edge++)
342
       {
343
       e1 = mEdges.get(edge);
344
       initRow= e1.row;
345
       initCol= e1.col;
346
       initSide=e1.side;
347

    
348
       do
349
         {
350
         //android.util.Log.d("CUBES", "checking edge "+debug(e1));
351

    
352
         mSideWalls++;
353

    
354
         if( e1.side==NORTH || e1.side==SOUTH )
355
           {
356
           for(int j=edge+1;j<mEdgeNum;j++)
357
             {
358
             e2 = mEdges.get(j);
359

    
360
             if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col )
361
               {
362
               mEdges.remove(j);
363
               mEdgeNum--;
364
               j--;
365

    
366
               //android.util.Log.e("CUBES", "removing edge "+debug(e2));
367
               }
368
             }
369
           }
370

    
371
         lastSide = e1.side;
372
         e1 = getNextEdge(e1);
373
         if( e1.side!=lastSide ) mSideBends++;
374
         }
375
       while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide );
376
       }
377
     }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380
// when calling, make sure that newVal != val
381
   
382
  private void markRegion(short newVal, int row, int col)
383
     {
384
     int val = mCubes[row][col];
385
     mCubes[row][col] = newVal;
386
     
387
     if( row>0       && mCubes[row-1][col  ]==val ) markRegion(newVal, row-1, col  );
388
     if( row<mRows-1 && mCubes[row+1][col  ]==val ) markRegion(newVal, row+1, col  );
389
     if( col>0       && mCubes[row  ][col-1]==val ) markRegion(newVal, row  , col-1);
390
     if( col<mCols-1 && mCubes[row  ][col+1]==val ) markRegion(newVal, row  , col+1);
391
     }
392
   
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394
   
395
  private void createNormals(boolean front, int row, int col)
396
     {
397
     int td,lr; 
398
      
399
     int nw = (col>0       && row>0      ) ? (mCubes[row-1][col-1]%2) : 0;
400
     int w  = (col>0                     ) ? (mCubes[row  ][col-1]%2) : 0;
401
     int n  = (               row>0      ) ? (mCubes[row-1][col  ]%2) : 0;
402
     int c  =                                (mCubes[row  ][col  ]%2);
403
     int sw = (col>0       && row<mRows-1) ? (mCubes[row+1][col-1]%2) : 0;
404
     int s  = (               row<mRows-1) ? (mCubes[row+1][col  ]%2) : 0;
405
     int ne = (col<mCols-1 && row>0      ) ? (mCubes[row-1][col+1]%2) : 0;
406
     int e  = (col<mCols-1               ) ? (mCubes[row  ][col+1]%2) : 0;
407
     int se = (col<mCols-1 && row<mRows-1) ? (mCubes[row+1][col+1]%2) : 0;
408

    
409
     if(front)
410
       {
411
       mNormalZ[0] = 1.0f;
412
       mNormalZ[1] = 1.0f;
413
       mNormalZ[2] = 1.0f;
414
       mNormalZ[3] = 1.0f;
415
       }
416
     else
417
       {
418
       mNormalZ[0] =-1.0f;
419
       mNormalZ[1] =-1.0f;
420
       mNormalZ[2] =-1.0f;
421
       mNormalZ[3] =-1.0f;
422
       }
423

    
424
     td = nw+n-w-c;
425
     lr = c+n-w-nw;
426
     if( td<0 ) td=-1;
427
     if( td>0 ) td= 1;
428
     if( lr<0 ) lr=-1;
429
     if( lr>0 ) lr= 1;
430
     mNormalX[0] = lr*R;
431
     mNormalY[0] = td*R;
432
     
433
     td = w+c-sw-s;
434
     lr = c+s-w-sw;
435
     if( td<0 ) td=-1;
436
     if( td>0 ) td= 1;
437
     if( lr<0 ) lr=-1;
438
     if( lr>0 ) lr= 1;
439
     mNormalX[1] = lr*R;
440
     mNormalY[1] = td*R;
441
     
442
     td = n+ne-c-e;
443
     lr = e+ne-c-n;
444
     if( td<0 ) td=-1;
445
     if( td>0 ) td= 1;
446
     if( lr<0 ) lr=-1;
447
     if( lr>0 ) lr= 1;
448
     mNormalX[2] = lr*R;
449
     mNormalY[2] = td*R;
450
     
451
     td = c+e-s-se;
452
     lr = e+se-c-s;
453
     if( td<0 ) td=-1;
454
     if( td>0 ) td= 1;
455
     if( lr<0 ) lr=-1;
456
     if( lr>0 ) lr= 1;
457
     mNormalX[3] = lr*R;
458
     mNormalY[3] = td*R;
459
     /*
460
     android.util.Log.d("CUBES", "row="+row+" col="+col);
461
     android.util.Log.d("CUBES", mNormalX[0]+" "+mNormalY[0]);
462
     android.util.Log.d("CUBES", mNormalX[1]+" "+mNormalY[1]);
463
     android.util.Log.d("CUBES", mNormalX[2]+" "+mNormalY[2]);
464
     android.util.Log.d("CUBES", mNormalX[3]+" "+mNormalY[3]);
465
     */
466
     }
467

    
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469

    
470
  private void buildFrontBackGrid(boolean front, float[] attribs)
471
     {
472
     int last, current;
473
     boolean seenLand=false;
474
     boolean lastBlockIsNE = false;
475
     boolean currentBlockIsNE;
476
     float vectZ = (front ? 0.5f : -0.5f);
477

    
478
     //android.util.Log.d("CUBES", "buildFrontBack");
479

    
480
     for(int row=0; row<mRows; row++)
481
       {
482
       last =0;
483
         
484
       for(int col=0; col<mCols; col++)
485
         {
486
         current = mCubes[row][col];
487

    
488
         if( current%2 == 1 )
489
           {
490
           currentBlockIsNE = isNE(row,col);
491

    
492
           if( !seenLand && !front && ((currVert%2==1)^currentBlockIsNE) )
493
             {
494
             //android.util.Log.d("CUBES","repeating winding2 vertex");
495

    
496
             repeatLast(attribs);
497
             }
498

    
499
           createNormals(front,row,col);
500

    
501
           if( currentBlockIsNE )
502
             {
503
             if( (last!=current) || !lastBlockIsNE )
504
               {
505
               if( seenLand  && (last != current) ) repeatLast(attribs);
506
               addFrontVertex( 0, vectZ, col, row, attribs);
507
               if( seenLand  && (last != current) ) repeatLast(attribs);
508
               if( !lastBlockIsNE || (!front && !seenLand) ) repeatLast(attribs);
509
               addFrontVertex( 1, vectZ, col, row+1, attribs);
510
               }
511
             addFrontVertex( 2, vectZ, col+1, row  , attribs);
512
             addFrontVertex( 3, vectZ, col+1, row+1, attribs);
513
             }
514
           else
515
             {
516
             if( (last!=current) || lastBlockIsNE )
517
               {
518
               if( seenLand  && (last != current) ) repeatLast(attribs);
519
               addFrontVertex( 1, vectZ, col, row+1, attribs);
520
               if( seenLand  && (last != current) ) repeatLast(attribs);
521
               if( lastBlockIsNE || (!front && !seenLand) ) repeatLast(attribs);
522
               addFrontVertex( 0, vectZ, col, row, attribs);
523
               }
524
             addFrontVertex( 3, vectZ, col+1, row+1, attribs);
525
             addFrontVertex( 2, vectZ, col+1, row  , attribs);
526
             }
527

    
528
           seenLand = true;
529
           lastBlockIsNE = currentBlockIsNE;
530
           }
531
            
532
         last = current;
533
         }
534
       }
535
     }
536

    
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538

    
539
  private void buildSideGrid(float[] attribs)
540
     {
541
     //android.util.Log.d("CUBES", "buildSide");
542

    
543
     for(int i=0; i<mEdgeNum; i++)
544
       {
545
       buildIthSide(mEdges.get(i), attribs);
546
       }
547
     }
548

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550

    
551
  private void buildIthSide(Edge curr, float[] attribs)
552
     {
553
     Edge prev, next;
554
     int col, row, side;
555

    
556
     if( curr.side==NORTH ) // water outside
557
       {
558
       prev = new Edge(WEST,curr.row,curr.col);
559
       }
560
     else                   // land outside; we need to move forward one link because we are
561
       {                    // going in opposite direction and we need to start from a bend.
562
       prev = curr;
563
       curr = new Edge(EAST,curr.row+1,curr.col-1);
564
       }
565

    
566
     for(int slice=0; slice<mSlices; slice++)
567
       {
568
       col = curr.col;
569
       row = curr.row;
570
       side= curr.side;
571
       next = getNextEdge(curr);
572
     
573
       addSideVertex(curr,true,slice+1,prev.side,attribs);
574

    
575
       do
576
         {
577
         if( prev.side!=curr.side )
578
           {
579
           addSideVertex(curr,true,slice+1,prev.side,attribs);
580
           addSideVertex(curr,true,slice  ,prev.side,attribs);
581
           }
582
       
583
         addSideVertex(curr,false,slice+1,next.side,attribs);
584
         addSideVertex(curr,false,slice  ,next.side,attribs);
585
       
586
         prev = curr;
587
         curr = next;
588
         next = getNextEdge(curr);
589
         }
590
       while( curr.col!=col || curr.row!=row || curr.side!=side );
591
     
592
       repeatLast(attribs);
593
       }
594
     }
595

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

    
598
  private Edge getNextEdge(Edge curr)
599
     {
600
     int col = curr.col;
601
     int row = curr.row;
602
      
603
     //android.util.Log.e("CUBES", "row="+row+" col="+col+" mRows="+mRows+" mCols="+mCols);
604
                       
605
     switch(curr.side) 
606
       {
607
       case NORTH: if( col==mCols-1 ) 
608
                     return new Edge(EAST,row,col);
609
                   if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] )
610
                     return new Edge(WEST,row-1,col+1);
611
                   if( mCubes[row][col+1]==mCubes[row][col] )
612
                     return new Edge(NORTH,row,col+1);
613
                   else  
614
                     return new Edge(EAST,row,col);
615
                   
616
       case SOUTH: if( col==0 ) 
617
                     return new Edge(WEST,row,col);
618
                   if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] )
619
                     return new Edge(EAST,row+1,col-1); 
620
                   if( mCubes[row][col-1]==mCubes[row][col] )
621
                     return new Edge(SOUTH,row,col-1);
622
                   else
623
                     return new Edge(WEST,row,col); 
624
                     
625
       case EAST : if( row==mRows-1 ) 
626
                     return new Edge(SOUTH,row,col);
627
                   if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] )
628
                     return new Edge(NORTH,row+1,col+1);
629
                   if( mCubes[row+1][col]==mCubes[row][col] )
630
                     return new Edge(EAST,row+1,col);
631
                   else 
632
                     return new Edge(SOUTH,row,col);
633
                   
634
       default   : if( row==0 )
635
                     return new Edge(NORTH,row,col);
636
                   if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] )
637
                     return new Edge(SOUTH,row-1,col-1);
638
                   if( mCubes[row-1][col]==mCubes[row][col] )
639
                     return new Edge(WEST,row-1,col);
640
                   else
641
                     return new Edge(NORTH,row,col);     
642
       }
643
     }
644

    
645
///////////////////////////////////////////////////////////////////////////////////////////////////
646

    
647
  private void fillInflate(int row, int col)
648
    {
649
    int diff;
650

    
651
         if( col==0     ) mInflateX[row][col] = -1;
652
    else if( col==mCols ) mInflateX[row][col] = +1;
653
    else
654
      {
655
      if( row==0 )
656
        {
657
        diff = mCubes[0][col-1]-mCubes[0][col];
658
        }
659
      else if( row==mRows )
660
        {
661
        diff = mCubes[mRows-1][col-1]-mCubes[mRows-1][col];
662
        }
663
      else
664
        {
665
        diff = (mCubes[row  ][col-1]-mCubes[row  ][col]) +
666
               (mCubes[row-1][col-1]-mCubes[row-1][col]) ;
667

    
668
        if( diff==-2 ) diff=-1;
669
        if( diff== 2 ) diff= 1;
670
        }
671

    
672
      mInflateX[row][col] = (byte)diff;
673
      }
674

    
675
         if( row==0     ) mInflateY[row][col] = +1;
676
    else if( row==mRows ) mInflateY[row][col] = -1;
677
    else
678
      {
679
      if( col==0 )
680
        {
681
        diff = mCubes[row][0]-mCubes[row-1][0];
682
        }
683
      else if( col==mCols )
684
        {
685
        diff = mCubes[row][mCols-1]-mCubes[row-1][mCols-1];
686
        }
687
      else
688
        {
689
        diff = (mCubes[row  ][col-1]+mCubes[row  ][col]) -
690
               (mCubes[row-1][col-1]+mCubes[row-1][col]) ;
691

    
692
        if( diff==-2 ) diff=-1;
693
        if( diff== 2 ) diff= 1;
694
        }
695

    
696
      mInflateY[row][col] = (byte)diff;
697
      }
698

    
699
    //android.util.Log.e("mesh","col="+col+" row="+row+" inflateX="+mInflateX[col][row]+" InflateY="+mInflateY[col][row]);
700
    }
701

    
702
///////////////////////////////////////////////////////////////////////////////////////////////////
703

    
704
  private void addFrontVertex(int index, float vectZ, int col, int row, float[] attribs)
705
     {
706
     float x = (float)col/mCols;
707
     float y = (float)row/mRows;
708

    
709
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x-0.5f;
710
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f-y;
711
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = vectZ;
712

    
713
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = mNormalX[index];
714
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = mNormalY[index];
715
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = mNormalZ[index];
716

    
717
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInflateX[row][col]/2.0f;
718
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInflateY[row][col]/2.0f;
719
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = vectZ;
720

    
721
     if( vectZ>0 )
722
       {
723
       attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[FRONT] +       x  * mTexMappingW[FRONT];
724
       attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[FRONT] + (1.0f-y) * mTexMappingH[FRONT];
725
       }
726
     else
727
       {
728
       attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[BACK]  +       x  * mTexMappingW[BACK];
729
       attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[BACK]  + (1.0f-y) * mTexMappingH[BACK];
730
       }
731

    
732
     currVert++;
733
     }
734

    
735
///////////////////////////////////////////////////////////////////////////////////////////////////
736

    
737
  private void addSideVertex(Edge curr, boolean back, int slice, int side, float[] attribs)
738
     {
739
     //android.util.Log.e("CUBES", "adding Side vertex!");
740
     float x, y, z;
741
     int row, col;
742

    
743
     switch(curr.side)
744
       {
745
       case NORTH: row = curr.row;
746
                   col = (back ? (curr.col  ):(curr.col+1));
747
                   x = (float)col/mCols;
748
                   y = 0.5f - (float)row/mRows;
749
                   z = 0.5f - (float)slice/mSlices;
750

    
751
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x - 0.5f;
752
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = y;
753
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
754

    
755
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = side==NORTH ? 0.0f : (side==WEST?-R:R);
756
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = 1.0f;
757
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
758

    
759
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInflateX[row][col]/2.0f;
760
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInflateY[row][col]/2.0f;
761
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = z;
762

    
763
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[TOP] +       x  * mTexMappingW[TOP];
764
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[TOP] + (0.5f-z) * mTexMappingH[TOP];
765

    
766
                   break;
767
       case SOUTH: row = curr.row+1;
768
                   col = (back ? (curr.col+1):(curr.col));
769
                   x = (float)col/mCols;
770
                   y = 0.5f - (float)row/mRows;
771
                   z = 0.5f - (float)slice/mSlices;
772

    
773
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x - 0.5f;
774
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = y;
775
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
776

    
777
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = side==SOUTH ? 0.0f: (side==EAST?-R:R);
778
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] =-1.0f;
779
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
780

    
781
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInflateX[row][col]/2.0f;
782
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInflateY[row][col]/2.0f;
783
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = z;
784

    
785
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[BOTTOM] +       x  * mTexMappingW[BOTTOM];
786
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[BOTTOM] + (0.5f-z) * mTexMappingH[BOTTOM];
787

    
788
                   break;
789
       case WEST : row = (back  ? (curr.row+1):(curr.row));
790
                   col = curr.col;
791
                   x = (float)col/mCols -0.5f;
792
                   y = (float)row/mRows;
793
                   z = 0.5f - (float)slice/mSlices;
794

    
795
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x;
796
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f - y;
797
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
798

    
799
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] =-1.0f;
800
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = side==WEST ? 0.0f : (side==NORTH?-R:R);
801
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
802

    
803
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInflateX[row][col]/2.0f;
804
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInflateY[row][col]/2.0f;
805
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = z;
806

    
807
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[LEFT] + (0.5f-z) * mTexMappingW[LEFT];
808
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[LEFT] + (1.0f-y) * mTexMappingH[LEFT];
809

    
810
                   break;
811
       case EAST : row = (back  ? (curr.row):(curr.row+1));
812
                   col = (curr.col+1);
813
                   x = (float)col/mCols -0.5f;
814
                   y = (float)row/mRows;
815
                   z = 0.5f - (float)slice/mSlices;
816

    
817
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = x;
818
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f - y;
819
                   attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = z;
820

    
821
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = 1.0f;
822
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = side==EAST ? 0.0f : (side==SOUTH?-R:R);
823
                   attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
824

    
825
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = mInflateX[row][col]/2.0f;
826
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = mInflateY[row][col]/2.0f;
827
                   attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = z;
828

    
829
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[RIGHT] + (0.5f-z) * mTexMappingW[RIGHT];
830
                   attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[RIGHT] + (1.0f-y) * mTexMappingH[RIGHT];
831

    
832
                   break;
833
       }
834

    
835
     currVert++;
836
     }
837

    
838
///////////////////////////////////////////////////////////////////////////////////////////////////
839

    
840
   private void repeatLast(float[] attribs)
841
     {
842
     //android.util.Log.e("CUBES", "repeating last vertex!");
843

    
844
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + POS_ATTRIB  ];
845
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + POS_ATTRIB+1];
846
     attribs[VERT_ATTRIBS*currVert + POS_ATTRIB+2] = attribs[VERT_ATTRIBS*(currVert-1) + POS_ATTRIB+2];
847

    
848
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + NOR_ATTRIB  ];
849
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + NOR_ATTRIB+1];
850
     attribs[VERT_ATTRIBS*currVert + NOR_ATTRIB+2] = attribs[VERT_ATTRIBS*(currVert-1) + NOR_ATTRIB+2];
851

    
852
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + INF_ATTRIB  ];
853
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + INF_ATTRIB+1];
854
     attribs[VERT_ATTRIBS*currVert + INF_ATTRIB+2] = attribs[VERT_ATTRIBS*(currVert-1) + INF_ATTRIB+2];
855

    
856
     attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB  ] = attribs[VERT_ATTRIBS*(currVert-1) + TEX_ATTRIB  ];
857
     attribs[VERT_ATTRIBS*currVert + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(currVert-1) + TEX_ATTRIB+1];
858

    
859
     currVert++;
860
     }
861

    
862
///////////////////////////////////////////////////////////////////////////////////////////////////
863

    
864
  private void build()
865
     {
866
     float[] attribs= new float[VERT_ATTRIBS*numVertices];
867

    
868
     buildFrontBackGrid(true,attribs);
869

    
870
     if( mSlices>0 )
871
       {
872
       repeatLast(attribs);
873
       if( currVert%2==1 ) repeatLast(attribs);
874
       buildSideGrid(attribs);
875
       buildFrontBackGrid(false,attribs);
876
       }
877

    
878
     mEdges.clear();
879
     mEdges = null;
880
     mCubes = null;
881
     mInflateX = null;
882
     mInflateY = null;
883

    
884
     if( currVert!=numVertices )
885
       android.util.Log.e("MeshCubes", "currVert " +currVert+" numVertices="+numVertices );
886

    
887
     setAttribs(attribs);
888
     }
889

    
890
///////////////////////////////////////////////////////////////////////////////////////////////////
891
// PUBLIC API
892
///////////////////////////////////////////////////////////////////////////////////////////////////
893
/**
894
 * Creates the underlying mesh of vertices, normals, texture coords.
895
 *    
896
 * @param cols   Integer helping to parse the next parameter.
897
 * @param desc   String describing the subset of a MxNx1 cuboid that we want to create.
898
 *               Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
899
 *               <p></p>
900
 *               <p>
901
 *               <pre>
902
 *               For example, (cols=2, desc="111010", slices=1) describes the following shape:
903
 *
904
 *               XX
905
 *               X
906
 *               X
907
 *
908
 *               whereas (cols=2,desc="110001", slices=1) describes
909
 *
910
 *               XX
911
 *
912
 *                X
913
 *               </pre>
914
 *               </p>
915
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
916
 */
917
 public MeshCubes(int cols, String desc, int slices)
918
   {
919
   super( (float)slices/cols);
920

    
921
   Static4D map = new Static4D(0.0f,0.0f,1.0f,1.0f);
922
   fillTexMappings(map,map,map,map,map,map);
923
   prepareDataStructures(cols,desc,slices);
924
   build();
925
   }
926

    
927
///////////////////////////////////////////////////////////////////////////////////////////////////
928
/**
929
 * Creates the underlying mesh of vertices, normals, texture coords with custom texture mappings.
930
 *
931
 * The mappings decide what part of the texture appears on a given side. Example:
932
 * front = (0.0,0.5,0.5,1.0) means that the front side of the grid will be textured with the
933
 * bottom-left quadrant of the texture.
934
 *
935
 * Default is: each of the 6 sides maps to the whole texture, i.e. (0.0,0.0,1.0,1.0)
936
 *
937
 * @param cols   Integer helping to parse the next parameter.
938
 * @param desc   String describing the subset of a MxNx1 cuboid that we want to create.
939
 *               Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
940
 *               <p></p>
941
 *               <p>
942
 *               <pre>
943
 *               For example, (cols=2, desc="111010", slices=1) describes the following shape:
944
 *
945
 *               XX
946
 *               X
947
 *               X
948
 *
949
 *               whereas (cols=2,desc="110001", slices=1) describes
950
 *
951
 *               XX
952
 *
953
 *                X
954
 *               </pre>
955
 *               </p>
956
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
957
 * @param front  Texture mapping the front side maps to.
958
 * @param back   Texture mapping the back side maps to.
959
 * @param left   Texture mapping the left side maps to.
960
 * @param right  Texture mapping the right side maps to.
961
 * @param top    Texture mapping the top side maps to.
962
 * @param bottom Texture mapping the bottom side maps to.
963
 */
964
 public MeshCubes(int cols, String desc, int slices, Static4D front, Static4D back, Static4D left, Static4D right, Static4D top, Static4D bottom)
965
   {
966
   super( (float)slices/cols);
967
   fillTexMappings(front,back,left,right,top,bottom);
968
   prepareDataStructures(cols,desc,slices);
969
   build();
970
   }
971

    
972
///////////////////////////////////////////////////////////////////////////////////////////////////
973
/**
974
 * Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors.
975
 *
976
 * @param cols   Number of columns, i.e. 'width' of the Mesh.
977
 * @param rows   Number of rows, i.e. 'height' of the Mesh.
978
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
979
 */
980
 public MeshCubes(int cols, int rows, int slices)
981
   {
982
   super( (float)slices/cols);
983

    
984
   Static4D map = new Static4D(0.0f,0.0f,1.0f,1.0f);
985
   fillTexMappings(map,map,map,map,map,map);
986
   prepareDataStructures(cols,rows,slices);
987
   build();
988
   }
989

    
990
///////////////////////////////////////////////////////////////////////////////////////////////////
991
/**
992
 * Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors with
993
 * custom texture mappings.
994
 *
995
 * The mappings decide what part of the texture appears on a given side. Example:
996
 * front = (0.0,0.5,0.5,1.0) means that the front side of the grid will be textured with the
997
 * bottom-left quadrant of the texture.
998
 *
999
 * Default is: each of the 6 sides maps to the whole texture, i.e. (0.0,0.0,1.0,1.0)
1000
 *
1001
 * @param cols   Number of columns, i.e. 'width' of the Mesh.
1002
 * @param rows   Number of rows, i.e. 'height' of the Mesh.
1003
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
1004
 * @param front  Texture mapping the front side maps to.
1005
 * @param back   Texture mapping the back side maps to.
1006
 * @param left   Texture mapping the left side maps to.
1007
 * @param right  Texture mapping the right side maps to.
1008
 * @param top    Texture mapping the top side maps to.
1009
 * @param bottom Texture mapping the bottom side maps to.
1010
 */
1011
 public MeshCubes(int cols, int rows, int slices, Static4D front, Static4D back, Static4D left, Static4D right, Static4D top, Static4D bottom)
1012
   {
1013
   super( (float)slices/cols);
1014
   fillTexMappings(front,back,left,right,top,bottom);
1015
   prepareDataStructures(cols,rows,slices);
1016
   build();
1017
   }
1018
 }
(2-2/5)