Project

General

Profile

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

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

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.main.DistortedLibrary;
24
import org.distorted.library.type.Static4D;
25

    
26
import java.util.ArrayList;
27

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

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

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

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

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

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

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

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
// full grid
210

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

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

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

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

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

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

    
285
       do
286
         {
287
         mSideWalls++;
288

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

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

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

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

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

    
357
     td = nw+n-w-c;
358
     lr = c+n-w-nw;
359
     if( td<0 ) td=-1;
360
     if( td>0 ) td= 1;
361
     if( lr<0 ) lr=-1;
362
     if( lr>0 ) lr= 1;
363
     mNormalX[0] = lr*R;
364
     mNormalY[0] = td*R;
365
     
366
     td = w+c-sw-s;
367
     lr = c+s-w-sw;
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[1] = lr*R;
373
     mNormalY[1] = td*R;
374
     
375
     td = n+ne-c-e;
376
     lr = e+ne-c-n;
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[2] = lr*R;
382
     mNormalY[2] = td*R;
383
     
384
     td = c+e-s-se;
385
     lr = e+se-c-s;
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[3] = lr*R;
391
     mNormalY[3] = td*R;
392
     }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395

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

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

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

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

    
421
           createNormals(front,row,col);
422

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

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

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460

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

    
469
///////////////////////////////////////////////////////////////////////////////////////////////////
470

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

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

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

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

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517

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

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

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564

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

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

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

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

    
593
         if( row==0     ) mInflateY[row][col] = +1;
594
    else if( row==mRows ) mInflateY[row][col] = -1;
595
    else
596
      {
597
      if( col==0 )
598
        {
599
        diff = mCubes[row][0]-mCubes[row-1][0];
600
        }
601
      else if( col==mCols )
602
        {
603
        diff = mCubes[row][mCols-1]-mCubes[row-1][mCols-1];
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
      mInflateY[row][col] = (byte)diff;
615
      }
616
    }
617

    
618
///////////////////////////////////////////////////////////////////////////////////////////////////
619

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

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

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

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

    
644
     currVert++;
645
     }
646

    
647
///////////////////////////////////////////////////////////////////////////////////////////////////
648

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
727
                   break;
728
       }
729

    
730
     currVert++;
731
     }
732

    
733
///////////////////////////////////////////////////////////////////////////////////////////////////
734

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

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

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

    
748
     currVert++;
749
     }
750

    
751
///////////////////////////////////////////////////////////////////////////////////////////////////
752

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

    
758
     buildFrontBackGrid(true,attribs1,attribs2);
759

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

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

    
774
     if( currVert!=numVertices )
775
       DistortedLibrary.logMessage("MeshCubes: currVert " +currVert+" numVertices="+numVertices );
776

    
777
     setAttribs(attribs1,attribs2);
778
     }
779

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

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

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

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

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

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