Project

General

Profile

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

library / src / main / java / org / distorted / library / GridCubes.java @ cacc63de

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;
21

    
22
import java.nio.ByteBuffer;
23
import java.nio.ByteOrder;
24
import java.util.ArrayList;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

    
28
public class GridCubes extends GridObject
29
   {
30
   private static final float R = 0.0f;//0.2f;
31
   private static final float FRONTZ = 0.5f;
32
   private static final float BACKZ  =-0.5f;
33
   
34
   private static final int NORTH = 0;
35
   private static final int WEST  = 1;
36
   private static final int EAST  = 2;
37
   private static final int SOUTH = 3;
38

    
39
   private static final boolean BACK  = true;
40
   private static final boolean FRONT = false;
41
   private static final boolean UPPER = false;
42
   private static final boolean LOWER = true;
43
   
44
   private static final float[] mNormalX = new float[4];
45
   private static final float[] mNormalY = new float[4];
46
   private static final float[] mNormalZ = new float[4];
47

    
48
   private class Edge
49
     {
50
     final int side; 
51
     final int row;
52
     final int col;
53
     
54
     Edge(int s, int r, int c)
55
       {
56
       side= s; 
57
       row = r;
58
       col = c;
59
       }
60
     }
61
   
62
   private int mCols, mRows;
63
   private short[][] mCubes;
64
   private ArrayList<Edge> mEdges = new ArrayList<>();
65

    
66
   private int remainingVert;
67
   private int mSideBends;
68
   private int mEdgeNum;
69
   private int mSideWalls;
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
// a Block is split into two triangles along the NE-SW line iff it is in the top-right
73
// or bottom-left quadrant of the grid.
74

    
75
   private boolean isNE(int row,int col)
76
     {
77
     return ( (2*row<mRows)^(2*col<mCols) );
78
     }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
// return the number of vertices our grid will contain
82

    
83
   private int computeDataLength(boolean frontOnly)
84
      {
85
      int frontWalls=0, frontSegments=0, triangleShifts=0, windingShifts=0;
86
      int shiftCol = (mCols-1)/2;
87

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

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

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

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
/*
121
   private static String debug(short[] val)
122
     {
123
     String ret="";j
124
     
125
     for(int i=0; i<val.length; i++) ret+=(" "+val[i]); 
126
     
127
     return ret;
128
     }
129
*/
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
/*
132
   private static String debug(float[] val, int stop)
133
     {
134
     String ret="";
135

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

    
142
     return ret;
143
     }
144
*/  
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
/*
147
   private static String debug(Edge e)
148
     {
149
     String d = "";
150
     
151
     switch(e.side)
152
       {
153
       case NORTH: d+="NORTH "; break;
154
       case SOUTH: d+="SOUTH "; break;
155
       case WEST : d+="WEST  "; break;
156
       case EAST : d+="EAST  "; break;
157
       }
158
     
159
     d+=("("+e.row+","+e.col+")");
160
     
161
     return d;
162
     }   
163
*/
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
   private void prepareDataStructures(int cols, String desc, boolean frontOnly)
167
     {
168
     mRows     =0;
169
     mCols     =0;
170
     dataLength=0;
171
     
172
     if( cols>0 && desc.contains("1") )
173
       {
174
       mCols = cols;
175
       mRows = desc.length()/cols;
176

    
177
       mCubes = new short[mRows][mCols];
178
       
179
       for(int j=0; j<mCols; j++)
180
         for(int i=0; i<mRows; i++)
181
           mCubes[i][j] = (short)(desc.charAt(i*mCols+j) == '1' ? 1:0);
182
       
183
       markRegions();
184
       dataLength = computeDataLength(frontOnly);
185

    
186
       remainingVert = dataLength;
187
       }
188
     }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191
// full grid
192

    
193
   private void prepareDataStructures(int cols, int rows, boolean frontOnly)
194
     {
195
     mRows     =rows;
196
     mCols     =cols;
197
     dataLength=   0;
198

    
199
     if( cols>0 && rows>0 )
200
       {
201
       mCubes = new short[mRows][mCols];
202

    
203
       for(int j=0; j<mCols; j++)
204
         for(int i=0; i<mRows; i++)
205
           mCubes[i][j] = (short)1;
206

    
207
       markRegions();
208
       dataLength = computeDataLength(frontOnly);
209

    
210
       remainingVert = dataLength;
211
       }
212
     }
213

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

    
259
       do
260
         {
261
         //android.util.Log.d("CUBES", "checking edge "+debug(e1));
262

    
263
         mSideWalls++;
264

    
265
         if( e1.side==NORTH || e1.side==SOUTH )
266
           {
267
           for(j=i+1;j<mEdgeNum;j++)
268
             {
269
             e2 = mEdges.get(j);
270

    
271
             if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col )
272
               {
273
               mEdges.remove(j);
274
               mEdgeNum--;
275
               j--;
276

    
277
               //android.util.Log.e("CUBES", "removing edge "+debug(e2));
278
               }
279
             }
280
           }
281

    
282
         lastSide = e1.side;
283
         e1 = getNextEdge(e1);
284
         if( e1.side!=lastSide ) mSideBends++;
285
         }
286
       while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide );
287
       }
288
     }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291
// when calling, make sure that newVal != val
292
   
293
   private void markRegion(short newVal, int row, int col)
294
     {
295
     short val = mCubes[row][col];
296
     mCubes[row][col] = newVal;
297
     
298
     if( row>0       && mCubes[row-1][col  ]==val ) markRegion(newVal, row-1, col  );
299
     if( row<mRows-1 && mCubes[row+1][col  ]==val ) markRegion(newVal, row+1, col  );
300
     if( col>0       && mCubes[row  ][col-1]==val ) markRegion(newVal, row  , col-1);
301
     if( col<mCols-1 && mCubes[row  ][col+1]==val ) markRegion(newVal, row  , col+1);
302
     }
303
   
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305
   
306
   private void createNormals(boolean front, int row, int col)
307
     {
308
     int td,lr; 
309
      
310
     int nw = (col>0       && row>0      ) ? (mCubes[row-1][col-1]%2) : 0;
311
     int w  = (col>0                     ) ? (mCubes[row  ][col-1]%2) : 0;
312
     int n  = (               row>0      ) ? (mCubes[row-1][col  ]%2) : 0;
313
     int c  =                                (mCubes[row  ][col  ]%2);
314
     int sw = (col>0       && row<mRows-1) ? (mCubes[row+1][col-1]%2) : 0;
315
     int s  = (               row<mRows-1) ? (mCubes[row+1][col  ]%2) : 0;
316
     int ne = (col<mCols-1 && row>0      ) ? (mCubes[row-1][col+1]%2) : 0;
317
     int e  = (col<mCols-1               ) ? (mCubes[row  ][col+1]%2) : 0;
318
     int se = (col<mCols-1 && row<mRows-1) ? (mCubes[row+1][col+1]%2) : 0;
319

    
320
     if(front)
321
       {
322
       mNormalZ[0] = 1.0f;
323
       mNormalZ[1] = 1.0f;
324
       mNormalZ[2] = 1.0f;
325
       mNormalZ[3] = 1.0f;
326
       }
327
     else
328
       {
329
       mNormalZ[0] =-1.0f;
330
       mNormalZ[1] =-1.0f;
331
       mNormalZ[2] =-1.0f;
332
       mNormalZ[3] =-1.0f;
333
       }
334

    
335
     td = nw+n-w-c;
336
     lr = c+n-w-nw;
337
     if( td<0 ) td=-1;
338
     if( td>0 ) td= 1;
339
     if( lr<0 ) lr=-1;
340
     if( lr>0 ) lr= 1;
341
     mNormalX[0] = lr*R;
342
     mNormalY[0] = td*R;
343
     
344
     td = w+c-sw-s;
345
     lr = c+s-w-sw;
346
     if( td<0 ) td=-1;
347
     if( td>0 ) td= 1;
348
     if( lr<0 ) lr=-1;
349
     if( lr>0 ) lr= 1;
350
     mNormalX[1] = lr*R;
351
     mNormalY[1] = td*R;
352
     
353
     td = n+ne-c-e;
354
     lr = e+ne-c-n;
355
     if( td<0 ) td=-1;
356
     if( td>0 ) td= 1;
357
     if( lr<0 ) lr=-1;
358
     if( lr>0 ) lr= 1;
359
     mNormalX[2] = lr*R;
360
     mNormalY[2] = td*R;
361
     
362
     td = c+e-s-se;
363
     lr = e+se-c-s;
364
     if( td<0 ) td=-1;
365
     if( td>0 ) td= 1;
366
     if( lr<0 ) lr=-1;
367
     if( lr>0 ) lr= 1;
368
     mNormalX[3] = lr*R;
369
     mNormalY[3] = td*R;
370
     /*
371
     android.util.Log.d("CUBES", "row="+row+" col="+col);
372
     android.util.Log.d("CUBES", mNormalX[0]+" "+mNormalY[0]);
373
     android.util.Log.d("CUBES", mNormalX[1]+" "+mNormalY[1]);
374
     android.util.Log.d("CUBES", mNormalX[2]+" "+mNormalY[2]);
375
     android.util.Log.d("CUBES", mNormalX[3]+" "+mNormalY[3]);
376
     */
377
     }
378
   
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
   private int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] position, float[] normal, float[] texture)
382
     {
383
     remainingVert--;
384

    
385
     float x = (float)col/mCols;
386
     float y = (float)row/mRows;
387

    
388
     position[3*vertex  ] = x-0.5f;
389
     position[3*vertex+1] = 0.5f-y;
390
     position[3*vertex+2] = vectZ;
391
     normal[3*vertex  ]   = mNormalX[index];
392
     normal[3*vertex+1]   = mNormalY[index];
393
     normal[3*vertex+2]   = mNormalZ[index];
394
     texture[2*vertex  ]  = x;
395
     texture[2*vertex+1]  = 1.0f-y;
396

    
397
     return vertex+1;
398
     }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
   private int buildFrontBackGrid(boolean front, int vertex, float[] position, float[] normal, float[] texture)
403
     {
404
     short last, current;
405
     boolean seenLand=false;
406
     boolean lastBlockIsNE = false;
407
     boolean currentBlockIsNE;
408
     float vectZ = front?FRONTZ:BACKZ;
409

    
410
     //android.util.Log.d("CUBES", "buildFrontBack");
411

    
412
     for(int row=0; row<mRows; row++)
413
       {
414
       last =0;
415
         
416
       for(int col=0; col<mCols; col++)
417
         {
418
         current = mCubes[row][col];
419

    
420
         if( current%2 == 1 )
421
           {
422
           currentBlockIsNE = isNE(row,col);
423

    
424
           if( !seenLand && !front && ((vertex%2==1)^currentBlockIsNE) )
425
             {
426
             //android.util.Log.d("CUBES","repeating winding2 vertex");
427

    
428
             vertex = repeatLast(vertex,position,normal,texture);
429
             }
430

    
431
           createNormals(front,row,col);
432

    
433
           if( currentBlockIsNE )
434
             {
435
             if( (last!=current) || !lastBlockIsNE )
436
               {
437
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
438
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
439
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
440
               if( !lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
441
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
442
               }
443
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row, position, normal, texture);
444
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
445
             }
446
           else
447
             {
448
             if( (last!=current) || lastBlockIsNE )
449
               {
450
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
451
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
452
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
453
               if( lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
454
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
455
               }
456
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
457
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row  , position, normal, texture);
458
             }
459

    
460
           seenLand = true;
461
           lastBlockIsNE = currentBlockIsNE;
462
           }
463
            
464
         last = current;
465
         }
466
       }
467
     
468
     return vertex;
469
     }
470

    
471
///////////////////////////////////////////////////////////////////////////////////////////////////
472

    
473
   private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
474
     {
475
     //android.util.Log.e("CUBES", "repeating last vertex!");
476

    
477
     if( vertex>0 )
478
       {
479
       remainingVert--;
480

    
481
       position[3*vertex  ] = position[3*vertex-3];
482
       position[3*vertex+1] = position[3*vertex-2];
483
       position[3*vertex+2] = position[3*vertex-1];
484

    
485
       normal[3*vertex  ]   = normal[3*vertex-3];
486
       normal[3*vertex+1]   = normal[3*vertex-2];
487
       normal[3*vertex+2]   = normal[3*vertex-1];
488

    
489
       texture[2*vertex  ]  = texture[2*vertex-2];
490
       texture[2*vertex+1]  = texture[2*vertex-1];
491
         
492
       vertex++;     
493
       }
494
     
495
     return vertex;
496
     }
497
   
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499

    
500
   private int buildSideGrid(int vertex, float[] position, float[] normal, float[] texture)
501
     {
502
     //android.util.Log.d("CUBES", "buildSide");
503

    
504
     for(int i=0; i<mEdgeNum; i++)
505
       {
506
       vertex = buildIthSide(mEdges.get(i), vertex, position, normal, texture);  
507
       } 
508
      
509
     return vertex;
510
     }
511

    
512
///////////////////////////////////////////////////////////////////////////////////////////////////
513

    
514
   private int buildIthSide(Edge curr, int vertex, float[] position, float[] normal, float[] texture)
515
     {
516
     Edge prev; 
517
     
518
     if( curr.side==NORTH ) // water outside
519
       {
520
       prev = new Edge(WEST,curr.row,curr.col);
521
       }
522
     else                   // land outside; we need to move forward one link because we are going in opposite direction and we need to start from a bend.
523
       {
524
       prev = curr;
525
       curr = new Edge(EAST,curr.row+1,curr.col-1);
526
       }
527
     
528
     int col = curr.col;
529
     int row = curr.row;
530
     int side= curr.side;  
531
     Edge next = getNextEdge(curr);
532
     
533
     addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
534
     vertex++;
535
     
536
     do
537
       {
538
       if( prev.side!=curr.side )
539
         {
540
         addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
541
         vertex++;
542
         addSideVertex(curr,BACK,UPPER,prev.side,vertex,position,normal,texture);
543
         vertex++;
544
         }
545
       
546
       addSideVertex(curr,FRONT,LOWER,next.side,vertex,position,normal,texture);
547
       vertex++;
548
       addSideVertex(curr,FRONT,UPPER,next.side,vertex,position,normal,texture);
549
       vertex++;
550
       
551
       prev = curr;
552
       curr = next; 
553
       next = getNextEdge(curr);
554
       }
555
     while( curr.col!=col || curr.row!=row || curr.side!=side );
556
     
557
     vertex = repeatLast(vertex,position,normal,texture);
558
     
559
     return vertex;
560
     }
561

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

    
564
   private Edge getNextEdge(Edge curr)
565
     {
566
     int col = curr.col;
567
     int row = curr.row;
568
      
569
     //android.util.Log.e("CUBES", "row="+row+" col="+col+" mRows="+mRows+" mCols="+mCols);
570
                       
571
     switch(curr.side) 
572
       {
573
       case NORTH: if( col==mCols-1 ) 
574
                     return new Edge(EAST,row,col);
575
                   if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] )
576
                     return new Edge(WEST,row-1,col+1);
577
                   if( mCubes[row][col+1]==mCubes[row][col] )
578
                     return new Edge(NORTH,row,col+1);
579
                   else  
580
                     return new Edge(EAST,row,col);
581
                   
582
       case SOUTH: if( col==0 ) 
583
                     return new Edge(WEST,row,col);
584
                   if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] )
585
                     return new Edge(EAST,row+1,col-1); 
586
                   if( mCubes[row][col-1]==mCubes[row][col] )
587
                     return new Edge(SOUTH,row,col-1);
588
                   else
589
                     return new Edge(WEST,row,col); 
590
                     
591
       case EAST : if( row==mRows-1 ) 
592
                     return new Edge(SOUTH,row,col);
593
                   if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] )
594
                     return new Edge(NORTH,row+1,col+1);
595
                   if( mCubes[row+1][col]==mCubes[row][col] )
596
                     return new Edge(EAST,row+1,col);
597
                   else 
598
                     return new Edge(SOUTH,row,col);
599
                   
600
       default   : if( row==0 )
601
                     return new Edge(NORTH,row,col);
602
                   if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] )
603
                     return new Edge(SOUTH,row-1,col-1);
604
                   if( mCubes[row-1][col]==mCubes[row][col] )
605
                     return new Edge(WEST,row-1,col);
606
                   else
607
                     return new Edge(NORTH,row,col);     
608
       }
609
     }
610

    
611
///////////////////////////////////////////////////////////////////////////////////////////////////
612
   
613
   private void addSideVertex(Edge curr, boolean back, boolean lower,int side, int vertex, float[] position, float[] normal, float[] texture)
614
     {
615
     //android.util.Log.e("CUBES", "adding Side vertex!");
616

    
617
     remainingVert--;
618

    
619
     float x, y;
620

    
621
     switch(curr.side)
622
       {
623
       case NORTH: x = (float)(back ? (curr.col  ):(curr.col+1))/mCols;
624

    
625
                   position[3*vertex  ] = x - 0.5f;
626
                   position[3*vertex+1] = 0.5f - (float)curr.row/mRows;
627
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
628

    
629
                   normal[3*vertex  ]   = side==NORTH ? 0.0f : (side==WEST?-R:R);
630
                   normal[3*vertex+1]   = 1.0f;
631
                   normal[3*vertex+2]   = lower ? -R:R;
632

    
633
                   texture[2*vertex  ]  = x;
634
                   texture[2*vertex+1]  = 1.0f-(float)(lower? (curr.row-1):(curr.row  ))/mRows;
635
                   break;
636
       case SOUTH: x = (float)(back ? (curr.col+1):(curr.col  ))/mCols;
637

    
638
                   position[3*vertex  ] = x - 0.5f;
639
                   position[3*vertex+1] = 0.5f - (float)(curr.row+1)/mRows;
640
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;  
641
            
642
                   normal[3*vertex  ]   = side==SOUTH ? 0.0f: (side==EAST?-R:R);
643
                   normal[3*vertex+1]   =-1.0f;
644
                   normal[3*vertex+2]   = lower ? -R:R;
645

    
646
                   texture[2*vertex  ]  = x;
647
                   texture[2*vertex+1]  = 1.0f-(float)(lower? (curr.row+2):(curr.row+1))/mRows;
648
                   break;
649
       case WEST : y = (float)(back  ? (curr.row+1):(curr.row))/mRows;
650

    
651
                   position[3*vertex  ] = (float)curr.col/mCols -0.5f;
652
                   position[3*vertex+1] = 0.5f - y;
653
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
654

    
655
                   normal[3*vertex  ]   =-1.0f;
656
                   normal[3*vertex+1]   = side==WEST ? 0.0f : (side==NORTH?-R:R);
657
                   normal[3*vertex+2]   = lower ? -R:R;
658
 
659
                   texture[2*vertex  ]  = (float)(lower ? (curr.col-1):(curr.col  ))/mCols;
660
                   texture[2*vertex+1]  = 1.0f - y;
661
                   break;
662
       case EAST : y = (float)(back  ? (curr.row):(curr.row+1))/mRows;
663

    
664
                   position[3*vertex  ] = (float)(curr.col+1)/mCols -0.5f;
665
                   position[3*vertex+1] = 0.5f - y;
666
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
667

    
668
                   normal[3*vertex  ]   = 1.0f;
669
                   normal[3*vertex+1]   = side==EAST ? 0.0f : (side==SOUTH?-R:R);
670
                   normal[3*vertex+2]   = lower ? -R:R; 
671

    
672
                   texture[2*vertex  ]  = (float)(lower ? (curr.col+2):(curr.col+1))/mCols;
673
                   texture[2*vertex+1]  = 1.0f - y;
674
                   break;
675
       }
676
     
677
     if(texture[2*vertex  ]>1.0f) texture[2*vertex  ] =2.0f-texture[2*vertex  ];
678
     if(texture[2*vertex  ]<0.0f) texture[2*vertex  ] =    -texture[2*vertex  ];
679
     if(texture[2*vertex+1]>1.0f) texture[2*vertex+1] =2.0f-texture[2*vertex+1];
680
     if(texture[2*vertex+1]<0.0f) texture[2*vertex+1] =    -texture[2*vertex+1];
681
     }
682

    
683
///////////////////////////////////////////////////////////////////////////////////////////////////
684

    
685
   private void build(boolean frontOnly)
686
     {
687
     int numVertices=0;
688
     float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
689
     float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
690
     float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
691

    
692
     //android.util.Log.d("CUBES","building front grid...");
693

    
694
     numVertices = buildFrontBackGrid(true, numVertices,positionData,normalData,textureData);
695

    
696
     if( !frontOnly )
697
       {
698
       numVertices = repeatLast(numVertices,positionData,normalData,textureData);
699
       if( numVertices%2==1 )
700
         {
701
         //android.util.Log.d("CUBES","repeating winding1 vertex");
702

    
703
         numVertices = repeatLast(numVertices,positionData,normalData,textureData);
704
         }
705

    
706
       //android.util.Log.d("CUBES","building side grid...");
707

    
708
       numVertices = buildSideGrid (numVertices,positionData,normalData,textureData);
709

    
710
       //android.util.Log.d("CUBES","building back grid...");
711

    
712
       numVertices = buildFrontBackGrid (false,numVertices,positionData,normalData,textureData);
713
       }
714

    
715
     /*
716
     android.util.Log.e("CUBES","dataLen="+dataLength+" vertex="+numVertices);
717
     android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
718
     android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
719
     android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
720
     */
721

    
722
     mEdges.clear();
723
     mEdges = null;
724
     mCubes = null;
725

    
726
     if( remainingVert!=0 )
727
       android.util.Log.d("CUBES", "remainingVert " +remainingVert );
728

    
729
     mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
730
     mGridPositions.put(positionData).position(0);
731

    
732
     mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
733
     mGridNormals.put(normalData).position(0);
734

    
735
     mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
736
     mGridTexture.put(textureData).position(0);
737
     }
738

    
739
///////////////////////////////////////////////////////////////////////////////////////////////////
740
// PUBLIC API
741
///////////////////////////////////////////////////////////////////////////////////////////////////
742
/**
743
 * Creates the underlying grid of vertices, normals, texture coords and colors.
744
 *    
745
 * @param cols      Integer helping to parse the next parameter.
746
 * @param desc      String describing the subset of a MxNx1 cuboid that we want to create.
747
 *                  Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
748
 *                  <p></p>
749
 *                  <p>
750
 *                  <pre>
751
 *                  For example, (cols=2, desc="111010") describes the following shape:
752
 *
753
 *                  XX
754
 *                  X
755
 *                  X
756
 *
757
 *                  whereas (cols=2,desc="110001") describes
758
 *
759
 *                  XX
760
 *
761
 *                   X
762
 *                  </pre>
763
 *                  </p>
764
 * @param frontOnly Only create the front wall or side and back as well?
765
 */
766
   public GridCubes(int cols, String desc, boolean frontOnly)
767
      {
768
      super(frontOnly ? 0.0f:1.0f/cols);
769
      prepareDataStructures(cols,desc,frontOnly);
770
      build(frontOnly);
771
      }
772

    
773
///////////////////////////////////////////////////////////////////////////////////////////////////
774
/**
775
 * Creates a full, hole-less underlying grid of vertices, normals, texture coords and colors.
776
 *
777
 * @param cols      Number of columns.
778
 * @param rows      Number of rows.
779
 * @param frontOnly Only create the front wall or side and back as well?
780
 */
781
   public GridCubes(int cols, int rows, boolean frontOnly)
782
      {
783
      super(frontOnly ? 0.0f:1.0f/cols);
784
      prepareDataStructures(cols,rows,frontOnly);
785
      build(frontOnly);
786
      }
787
   }
788
///////////////////////////////////////////////////////////////////////////////////////////////////
789

    
(13-13/15)