Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedCubesGrid.java @ e458a4ba

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 frontVert, sideVert;
62
   private int mCols, mRows;
63
   private short[][] mCubes;
64
   private ArrayList<Edge> mEdges = new ArrayList<Edge>();
65
   
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

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

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

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

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

    
140
     return ret;
141
     }
142
*/  
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
/*
145
   private static String debug(Edge e)
146
     {
147
     String d = "";
148
     
149
     switch(e.side)
150
       {
151
       case NORTH: d+="NORTH "; break;
152
       case SOUTH: d+="SOUTH "; break;
153
       case WEST : d+="WEST  "; break;
154
       case EAST : d+="EAST  "; break;
155
       }
156
     
157
     d+=("("+e.row+","+e.col+")");
158
     
159
     return d;
160
     }   
161
*/ 
162
///////////////////////////////////////////////////////////////////////////////////////////////////
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 )
171
       {
172
       int reallen = desc.length();
173
       int len = reallen;
174

    
175
       if( (reallen/cols)*cols != reallen )
176
         {
177
         len = ((reallen/cols)+1)*cols; 
178
         for(int i=reallen; i<len; i++) desc += "0";
179
         }
180
    
181
       if( desc.indexOf("1")>=0 )
182
         {
183
         mCols = cols;
184
         mRows = len/cols;
185

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

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

    
387
             if( front ) // NW corner
388
               {
389
               position[3*vertex+0] = (centerX-0.5f)/mCols;
390
               position[3*vertex+1] = (centerY+0.5f)/mRows;
391
               position[3*vertex+2] = FRONTZ;
392
               normal[3*vertex+0]   = mNormalX[0];
393
               normal[3*vertex+1]   = mNormalY[0];
394
               normal[3*vertex+2]   = 1.0f;
395
               texture[2*vertex+0]  = (float)j/mCols;
396
               texture[2*vertex+1]  = (float)i/mRows;     
397
               vertex++;
398
               }
399
             else  // SW corner
400
               { 
401
               position[3*vertex+0] = (centerX-0.5f)/mCols;
402
               position[3*vertex+1] = (centerY-0.5f)/mRows; 
403
               position[3*vertex+2] = BACKZ; 
404
               normal[3*vertex+0]   = mNormalX[1];
405
               normal[3*vertex+1]   = mNormalY[1];
406
               normal[3*vertex+2]   =-1.0f;
407
               texture[2*vertex+0]  = (float)j/mCols;
408
               texture[2*vertex+1]  = (float)(i+1)/mRows;
409
               vertex++;
410
               
411
               if( !seenland ) vertex = repeatLast(vertex,position,normal,texture);   //  if drawing the back, repeat the very first vertex
412
               }
413
             
414
             if( seenland ) vertex = repeatLast(vertex,position,normal,texture);    
415

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

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

    
504
   private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
505
     {
506
     if( vertex>0 )
507
       {
508
       position[3*vertex+0] = position[3*vertex-3]; 
509
       position[3*vertex+1] = position[3*vertex-2];
510
       position[3*vertex+2] = position[3*vertex-1];
511

    
512
       normal[3*vertex+0]   = normal[3*vertex-3]; 
513
       normal[3*vertex+1]   = normal[3*vertex-2];
514
       normal[3*vertex+2]   = normal[3*vertex-1];
515

    
516
       texture[2*vertex+0]  = texture[2*vertex-2];
517
       texture[2*vertex+1]  = texture[2*vertex-1];
518
         
519
       vertex++;     
520
       }
521
     
522
     return vertex;
523
     }
524
   
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526

    
527
   private int buildSideGrid(int vertex, float[] position, float[] normal, float[] texture)
528
     {
529
     int edges= mEdges.size();
530
     
531
     for(int i=0; i<edges; i++) 
532
       {
533
       vertex = buildIthSide(mEdges.get(i), vertex, position, normal, texture);  
534
       } 
535
      
536
     return vertex;
537
     }
538

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540

    
541
   private int buildIthSide(Edge curr, int vertex, float[] position, float[] normal, float[] texture)
542
     {
543
     Edge prev; 
544
     
545
     if( curr.side==NORTH ) // water outside
546
       {
547
       prev = new Edge(WEST,curr.row,curr.col);
548
       }
549
     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.
550
       {
551
       prev = curr;
552
       curr = new Edge(EAST,curr.row+1,curr.col-1);
553
       }
554
     
555
     int col = curr.col;
556
     int row = curr.row;
557
     int side= curr.side;  
558
     Edge next = getNextEdge(curr);
559
     
560
     addVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
561
     vertex++;
562
     
563
     do
564
       {
565
       if( prev.side!=curr.side )
566
         {
567
         addVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
568
         vertex++;
569
         addVertex(curr,BACK,UPPER,prev.side,vertex,position,normal,texture);
570
         vertex++;
571
         }
572
       
573
       addVertex(curr,FRONT,LOWER,next.side,vertex,position,normal,texture);
574
       vertex++;
575
       addVertex(curr,FRONT,UPPER,next.side,vertex,position,normal,texture);
576
       vertex++;
577
       
578
       prev = curr;
579
       curr = next; 
580
       next = getNextEdge(curr);
581
       }
582
     while( curr.col!=col || curr.row!=row || curr.side!=side );
583
     
584
     vertex = repeatLast(vertex,position,normal,texture);
585
     
586
     return vertex;
587
     }
588

    
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590

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

    
640
///////////////////////////////////////////////////////////////////////////////////////////////////
641
   
642
   private void addVertex(Edge curr, boolean back, boolean lower,int side, int vertex, float[] position, float[] normal, float[] texture)
643
     {
644
     float centerX = curr.col-(mCols-1.0f)/2.0f;
645
     float centerY = (mRows-1.0f)/2.0f-curr.row;
646
  
647
     switch(curr.side)
648
       {
649
       case NORTH: position[3*vertex+0] = (back ? (centerX-0.5f) : (centerX+0.5f))/mCols; 
650
                   position[3*vertex+1] = (centerY+0.5f)/mRows;
651
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
652

    
653
                   normal[3*vertex+0]   = side==NORTH ? 0.0f : (side==WEST?-R:R);
654
                   normal[3*vertex+1]   = 1.0f;
655
                   normal[3*vertex+2]   = lower ? -R:R;
656

    
657
                   texture[2*vertex+0]  = (float)(back ? (curr.col  ):(curr.col+1))/mCols;
658
                   texture[2*vertex+1]  = (float)(lower? (curr.row-1):(curr.row  ))/mRows;  
659
                   break;
660
       case SOUTH: position[3*vertex+0] = (back ? (centerX+0.5f) : (centerX-0.5f))/mCols;
661
                   position[3*vertex+1] = (centerY-0.5f)/mRows;
662
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;  
663
            
664
                   normal[3*vertex+0]   = side==SOUTH ? 0.0f: (side==EAST?-R:R);
665
                   normal[3*vertex+1]   =-1.0f;
666
                   normal[3*vertex+2]   = lower ? -R:R;
667

    
668
                   texture[2*vertex+0]  = (float)(back ? (curr.col+1):(curr.col  ))/mCols;
669
                   texture[2*vertex+1]  = (float)(lower? (curr.row+2):(curr.row+1))/mRows;
670
                   break;
671
       case WEST : position[3*vertex+0] = (centerX-0.5f)/mCols;
672
                   position[3*vertex+1] = (back ? (centerY-0.5f):(centerY+0.5f))/mRows;
673
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
674

    
675
                   normal[3*vertex+0]   =-1.0f;
676
                   normal[3*vertex+1]   = side==WEST ? 0.0f : (side==NORTH?-R:R);
677
                   normal[3*vertex+2]   = lower ? -R:R;
678
 
679
                   texture[2*vertex+0]  = (float)(lower ? (curr.col-1):(curr.col  ))/mCols;
680
                   texture[2*vertex+1]  = (float)(back  ? (curr.row+1):(curr.row  ))/mRows;
681
                   break;
682
       case EAST : position[3*vertex+0] = (centerX+0.5f)/mCols;
683
                   position[3*vertex+1] = (back ? (centerY+0.5f):(centerY-0.5f))/mRows;
684
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
685

    
686
                   normal[3*vertex+0]   = 1.0f;
687
                   normal[3*vertex+1]   = side==EAST ? 0.0f : (side==SOUTH?-R:R);
688
                   normal[3*vertex+2]   = lower ? -R:R; 
689

    
690
                   texture[2*vertex+0]  = (float)(lower ? (curr.col+2):(curr.col+1))/mCols;
691
                   texture[2*vertex+1]  = (float)(back  ? (curr.row  ):(curr.row+1))/mRows;
692
                   break;
693
       }
694
     
695
     if(texture[2*vertex+0]>1.0f) texture[2*vertex+0] =2.0f-texture[2*vertex+0];
696
     if(texture[2*vertex+0]<0.0f) texture[2*vertex+0] =    -texture[2*vertex+0];
697
     if(texture[2*vertex+1]>1.0f) texture[2*vertex+1] =2.0f-texture[2*vertex+1];
698
     if(texture[2*vertex+1]<0.0f) texture[2*vertex+1] =    -texture[2*vertex+1];
699
     }
700

    
701
///////////////////////////////////////////////////////////////////////////////////////////////////
702
// PUBLIC API
703
///////////////////////////////////////////////////////////////////////////////////////////////////
704
   
705
/**
706
 * Creates the underlying grid of vertices, normals, texture coords and colors.
707
 *    
708
 * @param rows See {@link DistortedCubes#DistortedCubes(String)} 
709
 * @param desc See {@link DistortedCubes#DistortedCubes(String)}
710
 */
711
   public DistortedCubesGrid(int cols, String desc, boolean frontOnly)
712
      {
713
      buildGrid(cols,desc,frontOnly);
714
       
715
      int numVertices=0;
716
      float[] colorData   = new float[COLOR_DATA_SIZE   *dataLength];
717
      float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
718
      float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
719
      float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
720
      
721
      for(int i=0; i<dataLength; i++)
722
        {
723
        colorData[COLOR_DATA_SIZE*i+0] = 1.0f; // r
724
        colorData[COLOR_DATA_SIZE*i+1] = 1.0f; // g
725
        colorData[COLOR_DATA_SIZE*i+2] = 1.0f; // b
726
        colorData[COLOR_DATA_SIZE*i+3] = 1.0f; // a
727
        }
728

    
729
      numVertices = buildFrontBackGrid(true, numVertices,positionData,normalData,textureData);
730
      
731
      if( !frontOnly )
732
        {
733
        numVertices = repeatLast(numVertices,positionData,normalData,textureData);
734
        numVertices = buildSideGrid (numVertices,positionData,normalData,textureData);
735
        numVertices = buildFrontBackGrid (false,numVertices,positionData,normalData,textureData);
736
        }
737
      
738
      /*
739
      android.util.Log.e("CUBES","dataLen="+dataLength+" vertex="+numVertices);
740
      android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
741
      android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
742
      android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
743
      */
744
      mGridColors = ByteBuffer.allocateDirect(COLOR_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
745
      mGridColors.put(colorData).position(0); 
746

    
747
      mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
748
      mGridPositions.put(positionData).position(0); 
749
      
750
      mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
751
      mGridNormals.put(normalData).position(0); 
752

    
753
      mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();                                                        
754
      mGridTexture.put(textureData).position(0); 
755
      }
756
   }
757
///////////////////////////////////////////////////////////////////////////////////////////////////
758

    
(5-5/18)