Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.mesh;
22

    
23
import org.distorted.library.type.Static4D;
24

    
25
import java.util.ArrayList;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
/**
29
 * Create a 3D grid composed of Cubes.
30
 * <p>
31
 * Any subset of a MxNx1 cuboid is possible. (repeated arbitrary number of times into Z-dir)
32
 */
33
public class MeshCubes extends MeshBase
34
   {
35
   private static final float R = 0.0f;
36

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
164
      int frontVert       = 2*( frontWalls + 2*frontSegments - 1) +2*triangleShifts + windingShifts;
165
      int sideVertOneSlice= 2*( mSideWalls + mSideBends + mEdgeNum -1);
166
      int sideVert        = 2*(mSlices-1) + mSlices*sideVertOneSlice;
167
      int firstWinding    = (mSlices>0 && (frontVert+1)%2==1 ) ? 1:0;
168
      int dataL           = mSlices==0 ? frontVert : (frontVert+1) +firstWinding+ (1+sideVert+1) + (1+frontVert);
169

    
170
      return dataL<0 ? 0:dataL;
171
      }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
   private void prepareDataStructures(int cols, String desc, int slices)
176
     {
177
     mRows       =0;
178
     mCols       =0;
179
     mSlices     =slices;
180
     numVertices =0;
181

    
182
     if( cols>0 && desc.contains("1") )
183
       {
184
       mCols = cols;
185
       mRows = desc.length()/cols;
186

    
187
       mCubes    = new int[mRows][mCols];
188
       mInflateX = new byte[mRows+1][mCols+1];
189
       mInflateY = new byte[mRows+1][mCols+1];
190

    
191
       for(int col=0; col<mCols; col++)
192
         for(int row=0; row<mRows; row++)
193
           mCubes[row][col] = (desc.charAt(row * mCols + col) == '1' ? 1 : 0);
194

    
195
       for(int col=0; col<mCols+1; col++)
196
         for(int row=0; row<mRows+1; row++)
197
           {
198
           fillInflate(row,col);
199
           }
200

    
201
       markRegions();
202
       numVertices = computeDataLength();
203
       currVert = 0;
204
       }
205
     }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
// full grid
209

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

    
217
     if( cols>0 && rows>0 )
218
       {
219
       mCubes    = new int[mRows][mCols];
220
       mInflateX = new byte[mRows+1][mCols+1];
221
       mInflateY = new byte[mRows+1][mCols+1];
222

    
223
       for(int col=0; col<mCols; col++)
224
         for(int row=0; row<mRows; row++)
225
           mCubes[row][col] = 1;
226

    
227
       for(int col=0; col<mCols+1; col++)
228
         for(int row=0; row<mRows+1; row++)
229
           {
230
           fillInflate(row,col);
231
           }
232

    
233
       markRegions();
234
       numVertices = computeDataLength();
235
       currVert = 0;
236
       }
237
     }
238

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

    
284
       do
285
         {
286
         mSideWalls++;
287

    
288
         if( e1.side==NORTH || e1.side==SOUTH )
289
           {
290
           for(int j=edge+1;j<mEdgeNum;j++)
291
             {
292
             e2 = mEdges.get(j);
293

    
294
             if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col )
295
               {
296
               mEdges.remove(j);
297
               mEdgeNum--;
298
               j--;
299
               }
300
             }
301
           }
302

    
303
         lastSide = e1.side;
304
         e1 = getNextEdge(e1);
305
         if( e1.side!=lastSide ) mSideBends++;
306
         }
307
       while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide );
308
       }
309
     }
310

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

    
341
     if(front)
342
       {
343
       mNormalZ[0] = 1.0f;
344
       mNormalZ[1] = 1.0f;
345
       mNormalZ[2] = 1.0f;
346
       mNormalZ[3] = 1.0f;
347
       }
348
     else
349
       {
350
       mNormalZ[0] =-1.0f;
351
       mNormalZ[1] =-1.0f;
352
       mNormalZ[2] =-1.0f;
353
       mNormalZ[3] =-1.0f;
354
       }
355

    
356
     td = nw+n-w-c;
357
     lr = c+n-w-nw;
358
     if( td<0 ) td=-1;
359
     if( td>0 ) td= 1;
360
     if( lr<0 ) lr=-1;
361
     if( lr>0 ) lr= 1;
362
     mNormalX[0] = lr*R;
363
     mNormalY[0] = td*R;
364
     
365
     td = w+c-sw-s;
366
     lr = c+s-w-sw;
367
     if( td<0 ) td=-1;
368
     if( td>0 ) td= 1;
369
     if( lr<0 ) lr=-1;
370
     if( lr>0 ) lr= 1;
371
     mNormalX[1] = lr*R;
372
     mNormalY[1] = td*R;
373
     
374
     td = n+ne-c-e;
375
     lr = e+ne-c-n;
376
     if( td<0 ) td=-1;
377
     if( td>0 ) td= 1;
378
     if( lr<0 ) lr=-1;
379
     if( lr>0 ) lr= 1;
380
     mNormalX[2] = lr*R;
381
     mNormalY[2] = td*R;
382
     
383
     td = c+e-s-se;
384
     lr = e+se-c-s;
385
     if( td<0 ) td=-1;
386
     if( td>0 ) td= 1;
387
     if( lr<0 ) lr=-1;
388
     if( lr>0 ) lr= 1;
389
     mNormalX[3] = lr*R;
390
     mNormalY[3] = td*R;
391
     }
392

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

    
395
  private void buildFrontBackGrid(boolean front, float[] attribs1, float[] attribs2)
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
     for(int row=0; row<mRows; row++)
404
       {
405
       last =0;
406
         
407
       for(int col=0; col<mCols; col++)
408
         {
409
         current = mCubes[row][col];
410

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

    
415
           if( !seenLand && !front && ((currVert%2==1)^currentBlockIsNE) )
416
             {
417
             repeatLast(attribs1,attribs2);
418
             }
419

    
420
           createNormals(front,row,col);
421

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

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

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

    
460
  private void buildSideGrid(float[] attribs1,float[] attribs2)
461
     {
462
     for(int i=0; i<mEdgeNum; i++)
463
       {
464
       buildIthSide(mEdges.get(i), attribs1, attribs2);
465
       }
466
     }
467

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

    
470
  private void buildIthSide(Edge curr, float[] attribs1, float[] attribs2)
471
     {
472
     Edge prev, next;
473
     int col, row, side;
474

    
475
     if( curr.side==NORTH ) // water outside
476
       {
477
       prev = new Edge(WEST,curr.row,curr.col);
478
       }
479
     else                   // land outside; we need to move forward one link because we are
480
       {                    // going in opposite direction and we need to start from a bend.
481
       prev = curr;
482
       curr = new Edge(EAST,curr.row+1,curr.col-1);
483
       }
484

    
485
     for(int slice=0; slice<mSlices; slice++)
486
       {
487
       col = curr.col;
488
       row = curr.row;
489
       side= curr.side;
490
       next = getNextEdge(curr);
491
     
492
       addSideVertex(curr,true,slice+1,prev.side,attribs1,attribs2);
493

    
494
       do
495
         {
496
         if( prev.side!=curr.side )
497
           {
498
           addSideVertex(curr,true,slice+1,prev.side,attribs1,attribs2);
499
           addSideVertex(curr,true,slice  ,prev.side,attribs1,attribs2);
500
           }
501
       
502
         addSideVertex(curr,false,slice+1,next.side,attribs1,attribs2);
503
         addSideVertex(curr,false,slice  ,next.side,attribs1,attribs2);
504
       
505
         prev = curr;
506
         curr = next;
507
         next = getNextEdge(curr);
508
         }
509
       while( curr.col!=col || curr.row!=row || curr.side!=side );
510
     
511
       repeatLast(attribs1,attribs2);
512
       }
513
     }
514

    
515
///////////////////////////////////////////////////////////////////////////////////////////////////
516

    
517
  private Edge getNextEdge(Edge curr)
518
     {
519
     int col = curr.col;
520
     int row = curr.row;
521

    
522
     switch(curr.side) 
523
       {
524
       case NORTH: if( col==mCols-1 ) 
525
                     return new Edge(EAST,row,col);
526
                   if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] )
527
                     return new Edge(WEST,row-1,col+1);
528
                   if( mCubes[row][col+1]==mCubes[row][col] )
529
                     return new Edge(NORTH,row,col+1);
530
                   else  
531
                     return new Edge(EAST,row,col);
532
                   
533
       case SOUTH: if( col==0 ) 
534
                     return new Edge(WEST,row,col);
535
                   if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] )
536
                     return new Edge(EAST,row+1,col-1); 
537
                   if( mCubes[row][col-1]==mCubes[row][col] )
538
                     return new Edge(SOUTH,row,col-1);
539
                   else
540
                     return new Edge(WEST,row,col); 
541
                     
542
       case EAST : if( row==mRows-1 ) 
543
                     return new Edge(SOUTH,row,col);
544
                   if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] )
545
                     return new Edge(NORTH,row+1,col+1);
546
                   if( mCubes[row+1][col]==mCubes[row][col] )
547
                     return new Edge(EAST,row+1,col);
548
                   else 
549
                     return new Edge(SOUTH,row,col);
550
                   
551
       default   : if( row==0 )
552
                     return new Edge(NORTH,row,col);
553
                   if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] )
554
                     return new Edge(SOUTH,row-1,col-1);
555
                   if( mCubes[row-1][col]==mCubes[row][col] )
556
                     return new Edge(WEST,row-1,col);
557
                   else
558
                     return new Edge(NORTH,row,col);     
559
       }
560
     }
561

    
562
///////////////////////////////////////////////////////////////////////////////////////////////////
563

    
564
  private void fillInflate(int row, int col)
565
    {
566
    int diff;
567

    
568
         if( col==0     ) mInflateX[row][col] = -1;
569
    else if( col==mCols ) mInflateX[row][col] = +1;
570
    else
571
      {
572
      if( row==0 )
573
        {
574
        diff = mCubes[0][col-1]-mCubes[0][col];
575
        }
576
      else if( row==mRows )
577
        {
578
        diff = mCubes[mRows-1][col-1]-mCubes[mRows-1][col];
579
        }
580
      else
581
        {
582
        diff = (mCubes[row  ][col-1]-mCubes[row  ][col]) +
583
               (mCubes[row-1][col-1]-mCubes[row-1][col]) ;
584

    
585
        if( diff==-2 ) diff=-1;
586
        if( diff== 2 ) diff= 1;
587
        }
588

    
589
      mInflateX[row][col] = (byte)diff;
590
      }
591

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

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

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

    
617
///////////////////////////////////////////////////////////////////////////////////////////////////
618

    
619
  private void addFrontVertex(int index, float vectZ, int col, int row, float[] attribs1, float[] attribs2)
620
     {
621
     float x = (float)col/mCols;
622
     float y = (float)row/mRows;
623

    
624
     attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB  ] = x-0.5f;
625
     attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f-y;
626
     attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+2] = vectZ;
627

    
628
     attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB  ] = mNormalX[index];
629
     attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+1] = mNormalY[index];
630
     attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+2] = mNormalZ[index];
631

    
632
     if( vectZ>0 )
633
       {
634
       attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[FRONT] +       x  * mTexMappingW[FRONT];
635
       attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[FRONT] + (1.0f-y) * mTexMappingH[FRONT];
636
       }
637
     else
638
       {
639
       attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[BACK]  +       x  * mTexMappingW[BACK];
640
       attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[BACK]  + (1.0f-y) * mTexMappingH[BACK];
641
       }
642

    
643
     currVert++;
644
     }
645

    
646
///////////////////////////////////////////////////////////////////////////////////////////////////
647

    
648
  private void addSideVertex(Edge curr, boolean back, int slice, int side, float[] attribs1, float[] attribs2)
649
     {
650
     float x, y, z;
651
     int row, col;
652

    
653
     switch(curr.side)
654
       {
655
       case NORTH: row = curr.row;
656
                   col = (back ? (curr.col  ):(curr.col+1));
657
                   x = (float)col/mCols;
658
                   y = 0.5f - (float)row/mRows;
659
                   z = 0.5f - (float)slice/mSlices;
660

    
661
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB  ] = x - 0.5f;
662
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+1] = y;
663
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+2] = z;
664

    
665
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB  ] = side==NORTH ? 0.0f : (side==WEST?-R:R);
666
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+1] = 1.0f;
667
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
668

    
669
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[TOP] +       x  * mTexMappingW[TOP];
670
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[TOP] + (0.5f-z) * mTexMappingH[TOP];
671

    
672
                   break;
673
       case SOUTH: row = curr.row+1;
674
                   col = (back ? (curr.col+1):(curr.col));
675
                   x = (float)col/mCols;
676
                   y = 0.5f - (float)row/mRows;
677
                   z = 0.5f - (float)slice/mSlices;
678

    
679
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB  ] = x - 0.5f;
680
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+1] = y;
681
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+2] = z;
682

    
683
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB  ] = side==SOUTH ? 0.0f: (side==EAST?-R:R);
684
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+1] =-1.0f;
685
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
686

    
687
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[BOTTOM] +       x  * mTexMappingW[BOTTOM];
688
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[BOTTOM] + (0.5f-z) * mTexMappingH[BOTTOM];
689

    
690
                   break;
691
       case WEST : row = (back  ? (curr.row+1):(curr.row));
692
                   col = curr.col;
693
                   x = (float)col/mCols -0.5f;
694
                   y = (float)row/mRows;
695
                   z = 0.5f - (float)slice/mSlices;
696

    
697
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB  ] = x;
698
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f - y;
699
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+2] = z;
700

    
701
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB  ] =-1.0f;
702
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+1] = side==WEST ? 0.0f : (side==NORTH?-R:R);
703
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
704

    
705
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[LEFT] + (0.5f-z) * mTexMappingW[LEFT];
706
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[LEFT] + (1.0f-y) * mTexMappingH[LEFT];
707

    
708
                   break;
709
       case EAST : row = (back  ? (curr.row):(curr.row+1));
710
                   col = (curr.col+1);
711
                   x = (float)col/mCols -0.5f;
712
                   y = (float)row/mRows;
713
                   z = 0.5f - (float)slice/mSlices;
714

    
715
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB  ] = x;
716
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f - y;
717
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+2] = z;
718

    
719
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB  ] = 1.0f;
720
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+1] = side==EAST ? 0.0f : (side==SOUTH?-R:R);
721
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
722

    
723
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[RIGHT] + (0.5f-z) * mTexMappingW[RIGHT];
724
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[RIGHT] + (1.0f-y) * mTexMappingH[RIGHT];
725

    
726
                   break;
727
       }
728

    
729
     currVert++;
730
     }
731

    
732
///////////////////////////////////////////////////////////////////////////////////////////////////
733

    
734
   private void repeatLast(float[] attribs1, float[] attribs2)
735
     {
736
     attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(currVert-1) + POS_ATTRIB  ];
737
     attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(currVert-1) + POS_ATTRIB+1];
738
     attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(currVert-1) + POS_ATTRIB+2];
739

    
740
     attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(currVert-1) + NOR_ATTRIB  ];
741
     attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(currVert-1) + NOR_ATTRIB+1];
742
     attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(currVert-1) + NOR_ATTRIB+2];
743

    
744
     attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB  ] = attribs2[VERT2_ATTRIBS*(currVert-1) + TEX_ATTRIB  ];
745
     attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB+1] = attribs2[VERT2_ATTRIBS*(currVert-1) + TEX_ATTRIB+1];
746

    
747
     currVert++;
748
     }
749

    
750
///////////////////////////////////////////////////////////////////////////////////////////////////
751

    
752
  private void build()
753
     {
754
     float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
755
     float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
756

    
757
     buildFrontBackGrid(true,attribs1,attribs2);
758

    
759
     if( mSlices>0 )
760
       {
761
       repeatLast(attribs1,attribs2);
762
       if( currVert%2==1 ) repeatLast(attribs1,attribs2);
763
       buildSideGrid(attribs1,attribs2);
764
       buildFrontBackGrid(false,attribs1,attribs2);
765
       }
766

    
767
     mEdges.clear();
768
     mEdges = null;
769
     mCubes = null;
770
     mInflateX = null;
771
     mInflateY = null;
772

    
773
     if( currVert!=numVertices )
774
       android.util.Log.e("MeshCubes", "currVert " +currVert+" numVertices="+numVertices );
775

    
776
     setAttribs(attribs1,attribs2);
777
     }
778

    
779
///////////////////////////////////////////////////////////////////////////////////////////////////
780
// PUBLIC API
781
///////////////////////////////////////////////////////////////////////////////////////////////////
782
/**
783
 * Creates the underlying mesh of vertices, normals, texture coords.
784
 *    
785
 * @param cols   Integer helping to parse the next parameter.
786
 * @param desc   String describing the subset of a MxNx1 cuboid that we want to create.
787
 *               Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
788
 *               <p></p>
789
 *               <p>
790
 *               <pre>
791
 *               For example, (cols=2, desc="111010", slices=1) describes the following shape:
792
 *
793
 *               XX
794
 *               X
795
 *               X
796
 *
797
 *               whereas (cols=2,desc="110001", slices=1) describes
798
 *
799
 *               XX
800
 *
801
 *                X
802
 *               </pre>
803
 *               </p>
804
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
805
 */
806
 public MeshCubes(int cols, String desc, int slices)
807
   {
808
   super();
809
   Static4D map = new Static4D(0.0f,0.0f,1.0f,1.0f);
810
   fillTexMappings(map,map,map,map,map,map);
811
   prepareDataStructures(cols,desc,slices);
812
   build();
813
   }
814

    
815
///////////////////////////////////////////////////////////////////////////////////////////////////
816
/**
817
 * Creates the underlying mesh of vertices, normals, texture coords with custom texture mappings.
818
 *
819
 * The mappings decide what part of the texture appears on a given side. Example:
820
 * front = (0.0,0.5,0.5,1.0) means that the front side of the grid will be textured with the
821
 * bottom-left quadrant of the texture.
822
 *
823
 * Default is: each of the 6 sides maps to the whole texture, i.e. (0.0,0.0,1.0,1.0)
824
 *
825
 * @param cols   Integer helping to parse the next parameter.
826
 * @param desc   String describing the subset of a MxNx1 cuboid that we want to create.
827
 *               Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
828
 *               <p></p>
829
 *               <p>
830
 *               <pre>
831
 *               For example, (cols=2, desc="111010", slices=1) describes the following shape:
832
 *
833
 *               XX
834
 *               X
835
 *               X
836
 *
837
 *               whereas (cols=2,desc="110001", slices=1) describes
838
 *
839
 *               XX
840
 *
841
 *                X
842
 *               </pre>
843
 *               </p>
844
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
845
 * @param front  Texture mapping the front side maps to.
846
 * @param back   Texture mapping the back side maps to.
847
 * @param left   Texture mapping the left side maps to.
848
 * @param right  Texture mapping the right side maps to.
849
 * @param top    Texture mapping the top side maps to.
850
 * @param bottom Texture mapping the bottom side maps to.
851
 */
852
 public MeshCubes(int cols, String desc, int slices, Static4D front, Static4D back, Static4D left, Static4D right, Static4D top, Static4D bottom)
853
   {
854
   super();
855
   fillTexMappings(front,back,left,right,top,bottom);
856
   prepareDataStructures(cols,desc,slices);
857
   build();
858
   }
859

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

    
877
///////////////////////////////////////////////////////////////////////////////////////////////////
878
/**
879
 * Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors with
880
 * custom texture mappings.
881
 *
882
 * The mappings decide what part of the texture appears on a given side. Example:
883
 * front = (0.0,0.5,0.5,1.0) means that the front side of the grid will be textured with the
884
 * bottom-left quadrant of the texture.
885
 *
886
 * Default is: each of the 6 sides maps to the whole texture, i.e. (0.0,0.0,1.0,1.0)
887
 *
888
 * @param cols   Number of columns, i.e. 'width' of the Mesh.
889
 * @param rows   Number of rows, i.e. 'height' of the Mesh.
890
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
891
 * @param front  Texture mapping the front side maps to.
892
 * @param back   Texture mapping the back side maps to.
893
 * @param left   Texture mapping the left side maps to.
894
 * @param right  Texture mapping the right side maps to.
895
 * @param top    Texture mapping the top side maps to.
896
 * @param bottom Texture mapping the bottom side maps to.
897
 */
898
 public MeshCubes(int cols, int rows, int slices, Static4D front, Static4D back, Static4D left, Static4D right, Static4D top, Static4D bottom)
899
   {
900
   super();
901
   fillTexMappings(front,back,left,right,top,bottom);
902
   prepareDataStructures(cols,rows,slices);
903
   build();
904
   }
905

    
906
///////////////////////////////////////////////////////////////////////////////////////////////////
907
/**
908
 * Copy constructor.
909
 */
910
 public MeshCubes(MeshCubes mesh, boolean deep)
911
   {
912
   super(mesh,deep);
913
   }
914

    
915
///////////////////////////////////////////////////////////////////////////////////////////////////
916
/**
917
 * Copy the Mesh.
918
 *
919
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
920
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
921
 *             coordinates and effect associations, is always deep copied).
922
 */
923
 public MeshCubes copy(boolean deep)
924
   {
925
   return new MeshCubes(this,deep);
926
   }
927
 }
(3-3/10)