Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedCubesGrid.java @ 0729bc41

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
class DistortedCubesGrid extends DistortedObjectGrid
29
   {
30
   private static final float R = 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
   
47
   private class Edge
48
     {
49
     final int side; 
50
     final int row;
51
     final int col;
52
     
53
     public Edge(int s, int r, int c)
54
       {
55
       side= s; 
56
       row = r;
57
       col = c;
58
       }
59
     }
60
   
61
   private int mCols, mRows;
62
   private short[][] mCubes;
63
   private ArrayList<Edge> mEdges = new ArrayList<>();
64
   
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
   private int computeDataLength(boolean frontOnly)
68
      {
69
      int frontWalls=0, frontSegments=0, sideWalls=0, sideBends=0;
70
      
71
      for(int i=0; i<mRows; i++)
72
         for(int j=0; j<mCols; j++)
73
            {
74
            if( mCubes[i][j]%2 == 1 )  // land
75
              {
76
              frontWalls++;
77
              if( j==mCols-1 || mCubes[i][j+1]%2 == 0 ) frontSegments++;
78
              }
79
              
80
            if( (i==0 && mCubes[i][j]!=2) || (i!=0 && mCubes[i][j] != mCubes[i-1][j  ]) ) sideWalls++; // up
81
            if( (j==0 && mCubes[i][j]!=2) || (j!=0 && mCubes[i][j] != mCubes[i  ][j-1]) ) sideWalls++; // left
82
            if( i==mRows-1 && mCubes[i][j]!=2                                           ) sideWalls++; // bottom
83
            if( j==mCols-1 && mCubes[i][j]!=2                                           ) sideWalls++; // right
84
            }
85

    
86
      int edges= mEdges.size();
87
      
88
      for(int i=0; i<edges; i++) 
89
        {
90
        Edge curr = mEdges.get(i);
91
        Edge next = getNextEdge(curr);
92
        int startX = curr.col;
93
        int startY = curr.row;
94
        int startS = curr.side;
95
        
96
        do
97
          {
98
          if( next.side != curr.side ) sideBends++; 
99
          curr  = next; 
100
          next = getNextEdge(curr);
101
          }
102
        while( curr.col!=startX || curr.row!=startY || curr.side!=startS );
103
        }
104
      
105
      int frontVert = 2*( frontWalls + 2*frontSegments - 1);
106
      int sideVert  = 2*( sideWalls + sideBends + edges -1);
107
      
108
      int dataL = frontOnly ? frontVert : (frontVert+1) + (1+sideVert+1) + (1+frontVert);
109
      
110
      //android.util.Log.e("CUBES","frontVert="+frontVert+" sideVert="+sideVert);
111
      //android.util.Log.e("CUBES", "frontW="+frontWalls+" fSegments="+frontSegments+" sWalls="+sideWalls+" sSegments="+edges+" sideBends="+sideBends+" dataLen="+dataL );
112
      
113
      return dataL<0 ? 0:dataL;
114
      }
115

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

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

    
139
     return ret;
140
     }
141
*/  
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
/*
144
   private static String debug(Edge e)
145
     {
146
     String d = "";
147
     
148
     switch(e.side)
149
       {
150
       case NORTH: d+="NORTH "; break;
151
       case SOUTH: d+="SOUTH "; break;
152
       case WEST : d+="WEST  "; break;
153
       case EAST : d+="EAST  "; break;
154
       }
155
     
156
     d+=("("+e.row+","+e.col+")");
157
     
158
     return d;
159
     }   
160
*/ 
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
// desc is guaranteed to be padded with 0s in the end (DistortedCubes constructor does it)
163

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

    
175
       mCubes = new short[mRows][mCols];
176
       
177
       for(int j=0; j<mCols; j++)
178
         for(int i=0; i<mRows; i++)
179
           mCubes[i][j] = (short)(desc.charAt(i*mCols+j) == '1' ? 1:0);
180
       
181
       markRegions();
182
       dataLength = computeDataLength(frontOnly);
183
       }
184
     }
185
 
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
// Mark all the 'regions' of our grid  - i.e. separate pieces of 'land' (connected blocks that will 
188
// be rendered) and 'water' (connected holes in between) with integers. Each connected block of land
189
// gets a unique odd integer, each connected block of water a unique even integer.
190
//
191
// Water on the edges of the grid is also considered connected to itself!   
192
//   
193
// This function also creates a list of 'Edges'. Each Edge is a data structure from which later on we
194
// will start building the side walls of each connected black of land (and sides of holes of water 
195
// inside)   
196
   
197
   private void markRegions()
198
     {
199
     int i, j, numWater=1, numLand=0;
200
     
201
     for(i=0; i<mRows;i++) if( mCubes[      i][      0]==0 ) markRegion((short)2,      i,       0);
202
     for(i=0; i<mRows;i++) if( mCubes[      i][mCols-1]==0 ) markRegion((short)2,      i, mCols-1);
203
     for(i=0; i<mCols;i++) if( mCubes[0      ][      i]==0 ) markRegion((short)2,      0,       i);
204
     for(i=0; i<mCols;i++) if( mCubes[mRows-1][      i]==0 ) markRegion((short)2,mRows-1,       i);
205
           
206
     for(i=0; i<mRows; i++)
207
        for(j=0; j<mCols; j++)
208
           {
209
           if( mCubes[i][j] == 0 ) { numWater++; markRegion( (short)(2*numWater ),i,j); mEdges.add(new Edge(NORTH,i,j)); }
210
           if( mCubes[i][j] == 1 ) { numLand ++; markRegion( (short)(2*numLand+1),i,j); mEdges.add(new Edge(NORTH,i,j)); }
211
           }
212
     
213
     // now we potentially need to kick out some Edges - precisely the edges with water inside -
214
     // which are surrounded by more than one type of land. Otherwise the following does not work:
215
     //
216
     // 0 1 0
217
     // 1 0 1
218
     // 0 1 0
219
     //
220
     // The 'water inside' edges that did not get kicked out by this procedure need to be transformed
221
     // with Edge(NORTH,row,col) -> Edge(SOUTH,row-1,col) so that later on normals work correctly
222
     // (Edge always needs to point out from land to water for that)
223
     
224
     int numEdges= mEdges.size();
225
     short initLand;
226
     int initCol, initRow;
227
     boolean kicked;
228
     Edge e;
229
     
230
     for(i=0; i<numEdges; i++) 
231
       {
232
       e = mEdges.get(i);
233
       initRow= e.row;
234
       initCol= e.col;
235
         
236
       //android.util.Log.e("CUBES", "checking edge "+debug(e));
237
             
238
       if( mCubes[initRow][initCol]%2==0 )
239
         {
240
         kicked = false; 
241
         initLand = mCubes[initRow-1][initCol];
242
         
243
         do
244
           {
245
           e = getNextEdge(e); 
246
           //android.util.Log.e("CUBES", " next edge "+debug(e));   
247
       
248
           switch(e.side)
249
             {
250
             case NORTH: if( initLand!=mCubes[e.row-1][e.col  ] ) kicked=true; break;
251
             case SOUTH: if( initLand!=mCubes[e.row+1][e.col  ] ) kicked=true; break;
252
             case WEST:  if( initLand!=mCubes[e.row  ][e.col-1] ) kicked=true; break;
253
             case EAST:  if( initLand!=mCubes[e.row  ][e.col+1] ) kicked=true; break;
254
             }
255
           
256
           if( kicked )
257
             {
258
             //android.util.Log.e("CUBES", "kicking out edge!");
259
             mEdges.remove(i);
260
             i--;
261
             numEdges--; 
262
             }
263
           }
264
         while( kicked==false && (e.col!=initCol || e.row!=initRow || e.side!=NORTH) );
265
         
266
         if( kicked==false )
267
           {
268
           mEdges.set(i, new Edge(SOUTH,e.row-1,e.col)); 
269
           }
270
         }
271
       }
272
     }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275
// when calling, make sure that newVal != val
276
   
277
   private void markRegion(short newVal, int row, int col)
278
     {
279
     short val = mCubes[row][col];
280
     mCubes[row][col] = newVal;
281
     
282
     if( row>0       && mCubes[row-1][col  ]==val ) markRegion(newVal, row-1, col  );
283
     if( row<mRows-1 && mCubes[row+1][col  ]==val ) markRegion(newVal, row+1, col  );
284
     if( col>0       && mCubes[row  ][col-1]==val ) markRegion(newVal, row  , col-1);
285
     if( col<mCols-1 && mCubes[row  ][col+1]==val ) markRegion(newVal, row  , col+1);
286
     }
287
   
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
   
290
   private void createNormals(int row, int col)
291
     {
292
     int td,lr; 
293
      
294
	  int nw = (col>0       && row>0      ) ? (mCubes[row-1][col-1]%2) : 0;
295
	  int w  = (col>0                     ) ? (mCubes[row  ][col-1]%2) : 0;
296
	  int n  = (               row>0      ) ? (mCubes[row-1][col  ]%2) : 0;
297
	  int c  =                                (mCubes[row  ][col  ]%2);
298
	  int sw = (col>0       && row<mRows-1) ? (mCubes[row+1][col-1]%2) : 0;
299
     int s  = (               row<mRows-1) ? (mCubes[row+1][col  ]%2) : 0;
300
     int ne = (col<mCols-1 && row>0      ) ? (mCubes[row-1][col+1]%2) : 0;
301
     int e  = (col<mCols-1               ) ? (mCubes[row  ][col+1]%2) : 0;
302
     int se = (col<mCols-1 && row<mRows-1) ? (mCubes[row+1][col+1]%2) : 0;
303
     
304
     td = nw+n-w-c;
305
     lr = c+n-w-nw;
306
     if( td<0 ) td=-1;
307
     if( td>0 ) td= 1;
308
     if( lr<0 ) lr=-1;
309
     if( lr>0 ) lr= 1;
310
     mNormalX[0] = lr*R;
311
     mNormalY[0] = td*R;
312
     
313
     td = w+c-sw-s;
314
     lr = c+s-w-sw;
315
     if( td<0 ) td=-1;
316
     if( td>0 ) td= 1;
317
     if( lr<0 ) lr=-1;
318
     if( lr>0 ) lr= 1;
319
     mNormalX[1] = lr*R;
320
     mNormalY[1] = td*R;
321
     
322
     td = n+ne-c-e;
323
     lr = e+ne-c-n;
324
     if( td<0 ) td=-1;
325
     if( td>0 ) td= 1;
326
     if( lr<0 ) lr=-1;
327
     if( lr>0 ) lr= 1;
328
     mNormalX[2] = lr*R;
329
     mNormalY[2] = td*R;
330
     
331
     td = c+e-s-se;
332
     lr = e+se-c-s;
333
     if( td<0 ) td=-1;
334
     if( td>0 ) td= 1;
335
     if( lr<0 ) lr=-1;
336
     if( lr>0 ) lr= 1;
337
     mNormalX[3] = lr*R;
338
     mNormalY[3] = td*R;
339
     /*
340
	  android.util.Log.d("CUBES", "row="+row+" col="+col);
341
	  android.util.Log.d("CUBES", mNormalX[0]+" "+mNormalY[0]);
342
	  android.util.Log.d("CUBES", mNormalX[1]+" "+mNormalY[1]);
343
	  android.util.Log.d("CUBES", mNormalX[2]+" "+mNormalY[2]);
344
	  android.util.Log.d("CUBES", mNormalX[3]+" "+mNormalY[3]);
345
     */
346
     }
347
   
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349
   
350
   private int buildFrontBackGrid(boolean front, int vertex, float[] position, float[] normal, float[] texture)
351
     {
352
     short last, current;
353
     boolean seenland=false;
354
     float centerX, centerY;
355
    
356
     for(int i=0; i<mRows; i++)
357
       {
358
       last =0;
359
         
360
       for(int j=0; j<mCols; j++)
361
         {
362
         current = mCubes[i][j];
363
            
364
         if( current%2 == 1 )
365
           {
366
           centerX = j-(mCols-1.0f)/2.0f;
367
           centerY = (mRows-1.0f)/2.0f-i;
368
      
369
           createNormals(i,j);
370
          
371
           if( last != current )
372
             {
373
             if( seenland ) vertex = repeatLast(vertex,position,normal,texture);    
374

    
375
             if( front ) // NW corner
376
               {
377
               position[3*vertex  ] = (centerX-0.5f)/mCols;
378
               position[3*vertex+1] = (centerY+0.5f)/mRows;
379
               position[3*vertex+2] = FRONTZ;
380
               normal[3*vertex  ]   = mNormalX[0];
381
               normal[3*vertex+1]   = mNormalY[0];
382
               normal[3*vertex+2]   = 1.0f;
383
               texture[2*vertex  ]  = (float)j/mCols;
384
               texture[2*vertex+1]  = (float)i/mRows;     
385
               vertex++;
386
               }
387
             else  // SW corner
388
               { 
389
               position[3*vertex  ] = (centerX-0.5f)/mCols;
390
               position[3*vertex+1] = (centerY-0.5f)/mRows; 
391
               position[3*vertex+2] = BACKZ; 
392
               normal[3*vertex  ]   = mNormalX[1];
393
               normal[3*vertex+1]   = mNormalY[1];
394
               normal[3*vertex+2]   =-1.0f;
395
               texture[2*vertex  ]  = (float)j/mCols;
396
               texture[2*vertex+1]  = (float)(i+1)/mRows;
397
               vertex++;
398
               
399
               if( !seenland ) vertex = repeatLast(vertex,position,normal,texture);   //  if drawing the back, repeat the very first vertex
400
               }
401
             
402
             if( seenland ) vertex = repeatLast(vertex,position,normal,texture);    
403

    
404
             if( front ) // SW corner
405
               {
406
               position[3*vertex  ] = (centerX-0.5f)/mCols;
407
               position[3*vertex+1] = (centerY-0.5f)/mRows; 
408
               position[3*vertex+2] = FRONTZ; 
409
               normal[3*vertex  ]   = mNormalX[1];
410
               normal[3*vertex+1]   = mNormalY[1];
411
               normal[3*vertex+2]   = 1.0f;
412
               texture[2*vertex  ]  = (float)j/mCols;
413
               texture[2*vertex+1]  = (float)(i+1)/mRows;
414
               vertex++; 
415
               }
416
             else  // NW corner
417
               {
418
               position[3*vertex  ] = (centerX-0.5f)/mCols;
419
               position[3*vertex+1] = (centerY+0.5f)/mRows;
420
               position[3*vertex+2] = BACKZ;
421
               normal[3*vertex  ]   = mNormalX[0];
422
               normal[3*vertex+1]   = mNormalY[0];
423
               normal[3*vertex+2]   =-1.0f;
424
               texture[2*vertex  ]  = (float)j/mCols;
425
               texture[2*vertex+1]  = (float)i/mRows;     
426
               vertex++; 
427
               }
428
             }
429
              
430
           if( front )  // NE corner
431
             {
432
             position[3*vertex  ] = (centerX+0.5f)/mCols;
433
             position[3*vertex+1] = (centerY+0.5f)/mRows;
434
             position[3*vertex+2] = FRONTZ; 
435
             normal[3*vertex  ]   = mNormalX[2];
436
             normal[3*vertex+1]   = mNormalY[2];
437
             normal[3*vertex+2]   = 1.0f;
438
             texture[2*vertex  ]  = (float)(j+1)/mCols;
439
             texture[2*vertex+1]  = (float)i/mRows;
440
             vertex++;
441
             }
442
           else // SE corner
443
             {
444
             position[3*vertex  ] = (centerX+0.5f)/mCols;
445
             position[3*vertex+1] = (centerY-0.5f)/mRows;
446
             position[3*vertex+2] = BACKZ; 
447
             normal[3*vertex  ]   = mNormalX[3];
448
             normal[3*vertex+1]   = mNormalY[3];
449
             normal[3*vertex+2]   =-1.0f;
450
             texture[2*vertex  ]  = (float)(j+1)/mCols;
451
             texture[2*vertex+1]  = (float)(i+1)/mRows;
452
             vertex++; 
453
             }
454
           
455
           if( front )  // SE corner
456
             {
457
             position[3*vertex  ] = (centerX+0.5f)/mCols;
458
             position[3*vertex+1] = (centerY-0.5f)/mRows;
459
             position[3*vertex+2] = FRONTZ; 
460
             normal[3*vertex  ]   = mNormalX[3];
461
             normal[3*vertex+1]   = mNormalY[3];
462
             normal[3*vertex+2]   = 1.0f;
463
             texture[2*vertex  ]  = (float)(j+1)/mCols;
464
             texture[2*vertex+1]  = (float)(i+1)/mRows;
465
             vertex++;
466
             }
467
           else // NE corner
468
             {
469
             position[3*vertex  ] = (centerX+0.5f)/mCols;
470
             position[3*vertex+1] = (centerY+0.5f)/mRows;
471
             position[3*vertex+2] = BACKZ; 
472
             normal[3*vertex  ]   = mNormalX[2];
473
             normal[3*vertex+1]   = mNormalY[2];
474
             normal[3*vertex+2]   =-1.0f;
475
             texture[2*vertex  ]  = (float)(j+1)/mCols;
476
             texture[2*vertex+1]  = (float)i/mRows;
477
             vertex++; 
478
             }
479
           
480
           seenland = true;
481
           }
482
            
483
         last = current;
484
         }
485
       }
486
     
487
     return vertex;
488
     }
489

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491

    
492
   private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
493
     {
494
     if( vertex>0 )
495
       {
496
       position[3*vertex  ] = position[3*vertex-3];
497
       position[3*vertex+1] = position[3*vertex-2];
498
       position[3*vertex+2] = position[3*vertex-1];
499

    
500
       normal[3*vertex  ]   = normal[3*vertex-3];
501
       normal[3*vertex+1]   = normal[3*vertex-2];
502
       normal[3*vertex+2]   = normal[3*vertex-1];
503

    
504
       texture[2*vertex  ]  = texture[2*vertex-2];
505
       texture[2*vertex+1]  = texture[2*vertex-1];
506
         
507
       vertex++;     
508
       }
509
     
510
     return vertex;
511
     }
512
   
513
///////////////////////////////////////////////////////////////////////////////////////////////////
514

    
515
   private int buildSideGrid(int vertex, float[] position, float[] normal, float[] texture)
516
     {
517
     int edges= mEdges.size();
518
     
519
     for(int i=0; i<edges; i++) 
520
       {
521
       vertex = buildIthSide(mEdges.get(i), vertex, position, normal, texture);  
522
       } 
523
      
524
     return vertex;
525
     }
526

    
527
///////////////////////////////////////////////////////////////////////////////////////////////////
528

    
529
   private int buildIthSide(Edge curr, int vertex, float[] position, float[] normal, float[] texture)
530
     {
531
     Edge prev; 
532
     
533
     if( curr.side==NORTH ) // water outside
534
       {
535
       prev = new Edge(WEST,curr.row,curr.col);
536
       }
537
     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.
538
       {
539
       prev = curr;
540
       curr = new Edge(EAST,curr.row+1,curr.col-1);
541
       }
542
     
543
     int col = curr.col;
544
     int row = curr.row;
545
     int side= curr.side;  
546
     Edge next = getNextEdge(curr);
547
     
548
     addVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
549
     vertex++;
550
     
551
     do
552
       {
553
       if( prev.side!=curr.side )
554
         {
555
         addVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
556
         vertex++;
557
         addVertex(curr,BACK,UPPER,prev.side,vertex,position,normal,texture);
558
         vertex++;
559
         }
560
       
561
       addVertex(curr,FRONT,LOWER,next.side,vertex,position,normal,texture);
562
       vertex++;
563
       addVertex(curr,FRONT,UPPER,next.side,vertex,position,normal,texture);
564
       vertex++;
565
       
566
       prev = curr;
567
       curr = next; 
568
       next = getNextEdge(curr);
569
       }
570
     while( curr.col!=col || curr.row!=row || curr.side!=side );
571
     
572
     vertex = repeatLast(vertex,position,normal,texture);
573
     
574
     return vertex;
575
     }
576

    
577
///////////////////////////////////////////////////////////////////////////////////////////////////
578

    
579
   private Edge getNextEdge(Edge curr)
580
     {
581
     int col = curr.col;
582
     int row = curr.row;
583
      
584
     //android.util.Log.e("CUBES", "row="+row+" col="+col+" mRows="+mRows+" mCols="+mCols);
585
                       
586
     switch(curr.side) 
587
       {
588
       case NORTH: if( col==mCols-1 ) 
589
                     return new Edge(EAST,row,col);
590
                   if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] )
591
                     return new Edge(WEST,row-1,col+1);
592
                   if( mCubes[row][col+1]==mCubes[row][col] )
593
                     return new Edge(NORTH,row,col+1);
594
                   else  
595
                     return new Edge(EAST,row,col);
596
                   
597
       case SOUTH: if( col==0 ) 
598
                     return new Edge(WEST,row,col);
599
                   if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] )
600
                     return new Edge(EAST,row+1,col-1); 
601
                   if( mCubes[row][col-1]==mCubes[row][col] )
602
                     return new Edge(SOUTH,row,col-1);
603
                   else
604
                     return new Edge(WEST,row,col); 
605
                     
606
       case EAST : if( row==mRows-1 ) 
607
                     return new Edge(SOUTH,row,col);
608
                   if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] )
609
                     return new Edge(NORTH,row+1,col+1);
610
                   if( mCubes[row+1][col]==mCubes[row][col] )
611
                     return new Edge(EAST,row+1,col);
612
                   else 
613
                     return new Edge(SOUTH,row,col);
614
                   
615
       case WEST : if( row==0 )
616
                     return new Edge(NORTH,row,col);
617
                   if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] )
618
                     return new Edge(SOUTH,row-1,col-1);
619
                   if( mCubes[row-1][col]==mCubes[row][col] )
620
                     return new Edge(WEST,row-1,col);
621
                   else
622
                     return new Edge(NORTH,row,col);     
623
       }
624
     
625
     return null;
626
     }
627

    
628
///////////////////////////////////////////////////////////////////////////////////////////////////
629
   
630
   private void addVertex(Edge curr, boolean back, boolean lower,int side, int vertex, float[] position, float[] normal, float[] texture)
631
     {
632
     float centerX = curr.col-(mCols-1.0f)/2.0f;
633
     float centerY = (mRows-1.0f)/2.0f-curr.row;
634
  
635
     switch(curr.side)
636
       {
637
       case NORTH: position[3*vertex  ] = (back ? (centerX-0.5f) : (centerX+0.5f))/mCols;
638
                   position[3*vertex+1] = (centerY+0.5f)/mRows;
639
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
640

    
641
                   normal[3*vertex  ]   = side==NORTH ? 0.0f : (side==WEST?-R:R);
642
                   normal[3*vertex+1]   = 1.0f;
643
                   normal[3*vertex+2]   = lower ? -R:R;
644

    
645
                   texture[2*vertex  ]  = (float)(back ? (curr.col  ):(curr.col+1))/mCols;
646
                   texture[2*vertex+1]  = (float)(lower? (curr.row-1):(curr.row  ))/mRows;  
647
                   break;
648
       case SOUTH: position[3*vertex  ] = (back ? (centerX+0.5f) : (centerX-0.5f))/mCols;
649
                   position[3*vertex+1] = (centerY-0.5f)/mRows;
650
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;  
651
            
652
                   normal[3*vertex  ]   = side==SOUTH ? 0.0f: (side==EAST?-R:R);
653
                   normal[3*vertex+1]   =-1.0f;
654
                   normal[3*vertex+2]   = lower ? -R:R;
655

    
656
                   texture[2*vertex  ]  = (float)(back ? (curr.col+1):(curr.col  ))/mCols;
657
                   texture[2*vertex+1]  = (float)(lower? (curr.row+2):(curr.row+1))/mRows;
658
                   break;
659
       case WEST : position[3*vertex  ] = (centerX-0.5f)/mCols;
660
                   position[3*vertex+1] = (back ? (centerY-0.5f):(centerY+0.5f))/mRows;
661
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
662

    
663
                   normal[3*vertex  ]   =-1.0f;
664
                   normal[3*vertex+1]   = side==WEST ? 0.0f : (side==NORTH?-R:R);
665
                   normal[3*vertex+2]   = lower ? -R:R;
666
 
667
                   texture[2*vertex  ]  = (float)(lower ? (curr.col-1):(curr.col  ))/mCols;
668
                   texture[2*vertex+1]  = (float)(back  ? (curr.row+1):(curr.row  ))/mRows;
669
                   break;
670
       case EAST : position[3*vertex  ] = (centerX+0.5f)/mCols;
671
                   position[3*vertex+1] = (back ? (centerY+0.5f):(centerY-0.5f))/mRows;
672
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
673

    
674
                   normal[3*vertex  ]   = 1.0f;
675
                   normal[3*vertex+1]   = side==EAST ? 0.0f : (side==SOUTH?-R:R);
676
                   normal[3*vertex+2]   = lower ? -R:R; 
677

    
678
                   texture[2*vertex  ]  = (float)(lower ? (curr.col+2):(curr.col+1))/mCols;
679
                   texture[2*vertex+1]  = (float)(back  ? (curr.row  ):(curr.row+1))/mRows;
680
                   break;
681
       }
682
     
683
     if(texture[2*vertex  ]>1.0f) texture[2*vertex  ] =2.0f-texture[2*vertex  ];
684
     if(texture[2*vertex  ]<0.0f) texture[2*vertex  ] =    -texture[2*vertex  ];
685
     if(texture[2*vertex+1]>1.0f) texture[2*vertex+1] =2.0f-texture[2*vertex+1];
686
     if(texture[2*vertex+1]<0.0f) texture[2*vertex+1] =    -texture[2*vertex+1];
687
     }
688

    
689
///////////////////////////////////////////////////////////////////////////////////////////////////
690
// PUBLIC API
691
///////////////////////////////////////////////////////////////////////////////////////////////////
692
   
693
/**
694
 * Creates the underlying grid of vertices, normals, texture coords and colors.
695
 *    
696
 * @param cols      See {@link DistortedCubes#DistortedCubes(int,String,int,boolean)}
697
 * @param desc      See {@link DistortedCubes#DistortedCubes(int,String,int,boolean)}
698
 * @param frontOnly See {@link DistortedCubes#DistortedCubes(int,String,int,boolean)}
699
 */
700
   public DistortedCubesGrid(int cols, String desc, boolean frontOnly)
701
      {
702
      buildGrid(cols,desc,frontOnly);
703
       
704
      int numVertices=0;
705
      float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
706
      float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
707
      float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
708

    
709
      numVertices = buildFrontBackGrid(true, numVertices,positionData,normalData,textureData);
710
      
711
      if( !frontOnly )
712
        {
713
        numVertices = repeatLast(numVertices,positionData,normalData,textureData);
714
        numVertices = buildSideGrid (numVertices,positionData,normalData,textureData);
715
        numVertices = buildFrontBackGrid (false,numVertices,positionData,normalData,textureData);
716
        }
717
      
718
      /*
719
      android.util.Log.e("CUBES","dataLen="+dataLength+" vertex="+numVertices);
720
      android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
721
      android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
722
      android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
723
      */
724

    
725
      mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
726
      mGridPositions.put(positionData).position(0); 
727
      
728
      mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
729
      mGridNormals.put(normalData).position(0); 
730

    
731
      mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
732
      mGridTexture.put(textureData).position(0); 
733
      }
734
   }
735
///////////////////////////////////////////////////////////////////////////////////////////////////
736

    
(5-5/18)