Project

General

Profile

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

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

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 6a06a912 Leszek Koltunski
package org.distorted.library;
21
22
import java.nio.ByteBuffer;
23
import java.nio.ByteOrder;
24
import java.util.ArrayList;
25
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27 e0b6c593 Leszek Koltunski
/**
28
 * Create a 3D grid composed of Cubes.
29
 * <p>
30
 * Any subset of a MxNx1 cuboid is possible.
31
 */
32 b455eb48 Leszek Koltunski
public class GridCubes extends GridObject
33 6a06a912 Leszek Koltunski
   {
34 06d71892 Leszek Koltunski
   private static final float R = 0.0f;//0.2f;
35 6a06a912 Leszek Koltunski
   private static final float FRONTZ = 0.5f;
36
   private static final float BACKZ  =-0.5f;
37
   
38
   private static final int NORTH = 0;
39
   private static final int WEST  = 1;
40
   private static final int EAST  = 2;
41
   private static final int SOUTH = 3;
42 ce7f3833 Leszek Koltunski
43 6a06a912 Leszek Koltunski
   private static final boolean BACK  = true;
44
   private static final boolean FRONT = false;
45
   private static final boolean UPPER = false;
46
   private static final boolean LOWER = true;
47
   
48
   private static final float[] mNormalX = new float[4];
49
   private static final float[] mNormalY = new float[4];
50 ce7f3833 Leszek Koltunski
   private static final float[] mNormalZ = new float[4];
51
52 6a06a912 Leszek Koltunski
   private class Edge
53
     {
54
     final int side; 
55
     final int row;
56
     final int col;
57
     
58 06d71892 Leszek Koltunski
     Edge(int s, int r, int c)
59 6a06a912 Leszek Koltunski
       {
60
       side= s; 
61
       row = r;
62
       col = c;
63
       }
64 39cbf9dc Leszek Koltunski
     }
65 6a06a912 Leszek Koltunski
   
66
   private int mCols, mRows;
67
   private short[][] mCubes;
68 39cbf9dc Leszek Koltunski
   private ArrayList<Edge> mEdges = new ArrayList<>();
69 ce7f3833 Leszek Koltunski
70 84ee2a6a Leszek Koltunski
   private int remainingVert;
71 8d9da98a Leszek Koltunski
   private int mSideBends;
72
   private int mEdgeNum;
73 b62632fc Leszek Koltunski
   private int mSideWalls;
74 ce7f3833 Leszek Koltunski
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
// a Block is split into two triangles along the NE-SW line iff it is in the top-right
77
// or bottom-left quadrant of the grid.
78
79
   private boolean isNE(int row,int col)
80
     {
81 29dd01c6 Leszek Koltunski
     return ( (2*row<mRows)^(2*col<mCols) );
82 ce7f3833 Leszek Koltunski
     }
83
84 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
85 84ee2a6a Leszek Koltunski
// return the number of vertices our grid will contain
86 6a06a912 Leszek Koltunski
87
   private int computeDataLength(boolean frontOnly)
88
      {
89 b62632fc Leszek Koltunski
      int frontWalls=0, frontSegments=0, triangleShifts=0, windingShifts=0;
90 ce7f3833 Leszek Koltunski
      int shiftCol = (mCols-1)/2;
91
92
      boolean lastBlockIsNE=false;
93
      boolean thisBlockIsNE;        // the block we are currently looking at is split into
94
                                    // two triangles along the NE-SW line (rather than NW-SE)
95 29dd01c6 Leszek Koltunski
      for(int row=0; row<mRows; row++)
96 b62632fc Leszek Koltunski
        {
97
        if( mCols>=2 && (mCubes[row][shiftCol]%2 == 1) && (mCubes[row][shiftCol+1]%2 == 1) ) triangleShifts++;
98 ce7f3833 Leszek Koltunski
99 b62632fc Leszek Koltunski
        for(int col=0; col<mCols; col++)
100
          {
101
          if( mCubes[row][col]%2 == 1 )  // land
102 6a06a912 Leszek Koltunski
            {
103 b62632fc Leszek Koltunski
            thisBlockIsNE = isNE(row,col);
104
            if( thisBlockIsNE^lastBlockIsNE ) windingShifts++;
105
            lastBlockIsNE = thisBlockIsNE;
106
            frontWalls++;
107
            if( col==mCols-1 || mCubes[row][col+1]%2 == 0 ) frontSegments++;
108 6a06a912 Leszek Koltunski
            }
109 b62632fc Leszek Koltunski
          }
110
        }
111 ce7f3833 Leszek Koltunski
112 84ee2a6a Leszek Koltunski
      int frontVert = 2*( frontWalls + 2*frontSegments - 1) +2*triangleShifts + windingShifts;
113 b62632fc Leszek Koltunski
      int sideVert  = 2*( mSideWalls + mSideBends + mEdgeNum -1);
114 8d9da98a Leszek Koltunski
      int firstWinding= (!frontOnly && (frontVert+1)%2==1 ) ? 1:0;
115 84ee2a6a Leszek Koltunski
      int dataL = frontOnly ? frontVert : (frontVert+1) +firstWinding+ (1+sideVert+1) + (1+frontVert);
116 8d9da98a Leszek Koltunski
/*
117 b62632fc Leszek Koltunski
      android.util.Log.e("CUBES","triangleShifts="+triangleShifts+" windingShifts="+windingShifts+" winding1="+firstWinding+" frontVert="+frontVert+" sideVert="+sideVert);
118
      android.util.Log.e("CUBES", "frontW="+frontWalls+" fSegments="+frontSegments+" sWalls="+mSideWalls+" sSegments="+mEdgeNum+" sideBends="+mSideBends+" dataLen="+dataL );
119 8d9da98a Leszek Koltunski
*/
120 6a06a912 Leszek Koltunski
      return dataL<0 ? 0:dataL;
121
      }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
/*
125
   private static String debug(short[] val)
126
     {
127 ce7f3833 Leszek Koltunski
     String ret="";j
128 6a06a912 Leszek Koltunski
     
129
     for(int i=0; i<val.length; i++) ret+=(" "+val[i]); 
130
     
131
     return ret;
132
     }
133
*/
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
/*
136
   private static String debug(float[] val, int stop)
137
     {
138
     String ret="";
139
140
     for(int i=0; i<val.length; i++) 
141
        {
142
        if( i%stop==0 ) ret+="\n";
143
        ret+=(" "+val[i]);
144
        }
145
146
     return ret;
147
     }
148
*/  
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
/*
151
   private static String debug(Edge e)
152
     {
153
     String d = "";
154
     
155
     switch(e.side)
156
       {
157
       case NORTH: d+="NORTH "; break;
158
       case SOUTH: d+="SOUTH "; break;
159
       case WEST : d+="WEST  "; break;
160
       case EAST : d+="EAST  "; break;
161
       }
162
     
163
     d+=("("+e.row+","+e.col+")");
164
     
165
     return d;
166
     }   
167 8d9da98a Leszek Koltunski
*/
168 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
169
170 2e96ee72 Leszek Koltunski
   private void prepareDataStructures(int cols, String desc, boolean frontOnly)
171 6a06a912 Leszek Koltunski
     {
172
     mRows     =0;
173
     mCols     =0;
174
     dataLength=0;
175
     
176 0729bc41 Leszek Koltunski
     if( cols>0 && desc.contains("1") )
177 6a06a912 Leszek Koltunski
       {
178 0729bc41 Leszek Koltunski
       mCols = cols;
179
       mRows = desc.length()/cols;
180 6a06a912 Leszek Koltunski
181 0729bc41 Leszek Koltunski
       mCubes = new short[mRows][mCols];
182 6a06a912 Leszek Koltunski
       
183 0729bc41 Leszek Koltunski
       for(int j=0; j<mCols; j++)
184
         for(int i=0; i<mRows; i++)
185
           mCubes[i][j] = (short)(desc.charAt(i*mCols+j) == '1' ? 1:0);
186 6a06a912 Leszek Koltunski
       
187 0729bc41 Leszek Koltunski
       markRegions();
188
       dataLength = computeDataLength(frontOnly);
189 84ee2a6a Leszek Koltunski
190
       remainingVert = dataLength;
191 6a06a912 Leszek Koltunski
       }
192
     }
193 665e2c45 Leszek Koltunski
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
// full grid
196
197
   private void prepareDataStructures(int cols, int rows, boolean frontOnly)
198
     {
199
     mRows     =rows;
200
     mCols     =cols;
201
     dataLength=   0;
202
203
     if( cols>0 && rows>0 )
204
       {
205
       mCubes = new short[mRows][mCols];
206
207
       for(int j=0; j<mCols; j++)
208
         for(int i=0; i<mRows; i++)
209
           mCubes[i][j] = (short)1;
210
211
       markRegions();
212
       dataLength = computeDataLength(frontOnly);
213
214
       remainingVert = dataLength;
215
       }
216
     }
217
218 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
219
// Mark all the 'regions' of our grid  - i.e. separate pieces of 'land' (connected blocks that will 
220
// be rendered) and 'water' (connected holes in between) with integers. Each connected block of land
221
// gets a unique odd integer, each connected block of water a unique even integer.
222
//
223
// Water on the edges of the grid is also considered connected to itself!   
224
//   
225
// This function also creates a list of 'Edges'. Each Edge is a data structure from which later on we
226 2e96ee72 Leszek Koltunski
// will start building the side walls of each connected block of land (and sides of holes of water
227 8d9da98a Leszek Koltunski
// inside). Each Edge needs to point from Land to Water (thus the '(SOUTH,i-1,j)' below) - otherwise
228
// later on setting up normal vectors wouldn't work.
229 6a06a912 Leszek Koltunski
   
230
   private void markRegions()
231
     {
232
     int i, j, numWater=1, numLand=0;
233
     
234
     for(i=0; i<mRows;i++) if( mCubes[      i][      0]==0 ) markRegion((short)2,      i,       0);
235
     for(i=0; i<mRows;i++) if( mCubes[      i][mCols-1]==0 ) markRegion((short)2,      i, mCols-1);
236
     for(i=0; i<mCols;i++) if( mCubes[0      ][      i]==0 ) markRegion((short)2,      0,       i);
237
     for(i=0; i<mCols;i++) if( mCubes[mRows-1][      i]==0 ) markRegion((short)2,mRows-1,       i);
238
           
239
     for(i=0; i<mRows; i++)
240
        for(j=0; j<mCols; j++)
241
           {
242 8d9da98a Leszek Koltunski
           if( mCubes[i][j] == 0 ) { numWater++; markRegion( (short)(2*numWater ),i,j); mEdges.add(new Edge(SOUTH,i-1,j)); }
243
           if( mCubes[i][j] == 1 ) { numLand ++; markRegion( (short)(2*numLand+1),i,j); mEdges.add(new Edge(NORTH,i  ,j)); }
244 6a06a912 Leszek Koltunski
           }
245
     
246 8d9da98a Leszek Koltunski
     // now we potentially need to kick out some Edges . Otherwise the following does not work:
247 6a06a912 Leszek Koltunski
     //
248
     // 0 1 0
249
     // 1 0 1
250
     // 0 1 0
251
     
252 8d9da98a Leszek Koltunski
     mEdgeNum= mEdges.size();
253
     int initCol, initRow, initSide, lastSide;
254
     Edge e1,e2;
255 6a06a912 Leszek Koltunski
     
256 8d9da98a Leszek Koltunski
     for(i=0; i<mEdgeNum; i++)
257 6a06a912 Leszek Koltunski
       {
258 8d9da98a Leszek Koltunski
       e1 = mEdges.get(i);
259
       initRow= e1.row;
260
       initCol= e1.col;
261
       initSide=e1.side;
262
263
       do
264 6a06a912 Leszek Koltunski
         {
265 8d9da98a Leszek Koltunski
         //android.util.Log.d("CUBES", "checking edge "+debug(e1));
266
267 b62632fc Leszek Koltunski
         mSideWalls++;
268
269 8d9da98a Leszek Koltunski
         if( e1.side==NORTH || e1.side==SOUTH )
270 6a06a912 Leszek Koltunski
           {
271 8d9da98a Leszek Koltunski
           for(j=i+1;j<mEdgeNum;j++)
272 6a06a912 Leszek Koltunski
             {
273 8d9da98a Leszek Koltunski
             e2 = mEdges.get(j);
274
275
             if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col )
276
               {
277
               mEdges.remove(j);
278
               mEdgeNum--;
279
               j--;
280
281
               //android.util.Log.e("CUBES", "removing edge "+debug(e2));
282
               }
283 6a06a912 Leszek Koltunski
             }
284
           }
285 8d9da98a Leszek Koltunski
286
         lastSide = e1.side;
287
         e1 = getNextEdge(e1);
288
         if( e1.side!=lastSide ) mSideBends++;
289 6a06a912 Leszek Koltunski
         }
290 8d9da98a Leszek Koltunski
       while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide );
291 6a06a912 Leszek Koltunski
       }
292
     }
293
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
// when calling, make sure that newVal != val
296
   
297
   private void markRegion(short newVal, int row, int col)
298
     {
299
     short val = mCubes[row][col];
300
     mCubes[row][col] = newVal;
301
     
302
     if( row>0       && mCubes[row-1][col  ]==val ) markRegion(newVal, row-1, col  );
303
     if( row<mRows-1 && mCubes[row+1][col  ]==val ) markRegion(newVal, row+1, col  );
304
     if( col>0       && mCubes[row  ][col-1]==val ) markRegion(newVal, row  , col-1);
305
     if( col<mCols-1 && mCubes[row  ][col+1]==val ) markRegion(newVal, row  , col+1);
306
     }
307
   
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309
   
310 ce7f3833 Leszek Koltunski
   private void createNormals(boolean front, int row, int col)
311 6a06a912 Leszek Koltunski
     {
312
     int td,lr; 
313
      
314 2e96ee72 Leszek Koltunski
     int nw = (col>0       && row>0      ) ? (mCubes[row-1][col-1]%2) : 0;
315
     int w  = (col>0                     ) ? (mCubes[row  ][col-1]%2) : 0;
316
     int n  = (               row>0      ) ? (mCubes[row-1][col  ]%2) : 0;
317
     int c  =                                (mCubes[row  ][col  ]%2);
318
     int sw = (col>0       && row<mRows-1) ? (mCubes[row+1][col-1]%2) : 0;
319 6a06a912 Leszek Koltunski
     int s  = (               row<mRows-1) ? (mCubes[row+1][col  ]%2) : 0;
320
     int ne = (col<mCols-1 && row>0      ) ? (mCubes[row-1][col+1]%2) : 0;
321
     int e  = (col<mCols-1               ) ? (mCubes[row  ][col+1]%2) : 0;
322
     int se = (col<mCols-1 && row<mRows-1) ? (mCubes[row+1][col+1]%2) : 0;
323 ce7f3833 Leszek Koltunski
324
     if(front)
325
       {
326
       mNormalZ[0] = 1.0f;
327
       mNormalZ[1] = 1.0f;
328
       mNormalZ[2] = 1.0f;
329
       mNormalZ[3] = 1.0f;
330
       }
331
     else
332
       {
333
       mNormalZ[0] =-1.0f;
334
       mNormalZ[1] =-1.0f;
335
       mNormalZ[2] =-1.0f;
336
       mNormalZ[3] =-1.0f;
337
       }
338
339 6a06a912 Leszek Koltunski
     td = nw+n-w-c;
340
     lr = c+n-w-nw;
341
     if( td<0 ) td=-1;
342
     if( td>0 ) td= 1;
343
     if( lr<0 ) lr=-1;
344
     if( lr>0 ) lr= 1;
345
     mNormalX[0] = lr*R;
346
     mNormalY[0] = td*R;
347
     
348
     td = w+c-sw-s;
349
     lr = c+s-w-sw;
350
     if( td<0 ) td=-1;
351
     if( td>0 ) td= 1;
352
     if( lr<0 ) lr=-1;
353
     if( lr>0 ) lr= 1;
354
     mNormalX[1] = lr*R;
355
     mNormalY[1] = td*R;
356
     
357
     td = n+ne-c-e;
358
     lr = e+ne-c-n;
359
     if( td<0 ) td=-1;
360
     if( td>0 ) td= 1;
361
     if( lr<0 ) lr=-1;
362
     if( lr>0 ) lr= 1;
363
     mNormalX[2] = lr*R;
364
     mNormalY[2] = td*R;
365
     
366
     td = c+e-s-se;
367
     lr = e+se-c-s;
368
     if( td<0 ) td=-1;
369
     if( td>0 ) td= 1;
370
     if( lr<0 ) lr=-1;
371
     if( lr>0 ) lr= 1;
372
     mNormalX[3] = lr*R;
373
     mNormalY[3] = td*R;
374
     /*
375 2e96ee72 Leszek Koltunski
     android.util.Log.d("CUBES", "row="+row+" col="+col);
376
     android.util.Log.d("CUBES", mNormalX[0]+" "+mNormalY[0]);
377
     android.util.Log.d("CUBES", mNormalX[1]+" "+mNormalY[1]);
378
     android.util.Log.d("CUBES", mNormalX[2]+" "+mNormalY[2]);
379
     android.util.Log.d("CUBES", mNormalX[3]+" "+mNormalY[3]);
380 6a06a912 Leszek Koltunski
     */
381
     }
382
   
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384 ce7f3833 Leszek Koltunski
385 e5d9b235 Leszek Koltunski
   private int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] position, float[] normal, float[] texture)
386 ce7f3833 Leszek Koltunski
     {
387 84ee2a6a Leszek Koltunski
     remainingVert--;
388 15873844 Leszek Koltunski
389 e5d9b235 Leszek Koltunski
     float x = (float)col/mCols;
390
     float y = (float)row/mRows;
391 ce7f3833 Leszek Koltunski
392 15873844 Leszek Koltunski
     position[3*vertex  ] = x-0.5f;
393
     position[3*vertex+1] = 0.5f-y;
394
     position[3*vertex+2] = vectZ;
395
     normal[3*vertex  ]   = mNormalX[index];
396
     normal[3*vertex+1]   = mNormalY[index];
397
     normal[3*vertex+2]   = mNormalZ[index];
398
     texture[2*vertex  ]  = x;
399 985ea9c5 Leszek Koltunski
     texture[2*vertex+1]  = 1.0f-y;
400 15873844 Leszek Koltunski
401
     return vertex+1;
402 ce7f3833 Leszek Koltunski
     }
403
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405
406 6a06a912 Leszek Koltunski
   private int buildFrontBackGrid(boolean front, int vertex, float[] position, float[] normal, float[] texture)
407
     {
408
     short last, current;
409 ce7f3833 Leszek Koltunski
     boolean seenLand=false;
410
     boolean lastBlockIsNE = false;
411
     boolean currentBlockIsNE;
412
     float vectZ = front?FRONTZ:BACKZ;
413
414 84ee2a6a Leszek Koltunski
     //android.util.Log.d("CUBES", "buildFrontBack");
415
416 e5d9b235 Leszek Koltunski
     for(int row=0; row<mRows; row++)
417 6a06a912 Leszek Koltunski
       {
418
       last =0;
419
         
420 e5d9b235 Leszek Koltunski
       for(int col=0; col<mCols; col++)
421 6a06a912 Leszek Koltunski
         {
422 e5d9b235 Leszek Koltunski
         current = mCubes[row][col];
423 ce7f3833 Leszek Koltunski
424 6a06a912 Leszek Koltunski
         if( current%2 == 1 )
425
           {
426 e5d9b235 Leszek Koltunski
           currentBlockIsNE = isNE(row,col);
427 84ee2a6a Leszek Koltunski
428
           if( !seenLand && !front && ((vertex%2==1)^currentBlockIsNE) )
429
             {
430
             //android.util.Log.d("CUBES","repeating winding2 vertex");
431
432
             vertex = repeatLast(vertex,position,normal,texture);
433
             }
434
435 e5d9b235 Leszek Koltunski
           createNormals(front,row,col);
436 ce7f3833 Leszek Koltunski
437 e5d9b235 Leszek Koltunski
           if( currentBlockIsNE )
438 6a06a912 Leszek Koltunski
             {
439 e5d9b235 Leszek Koltunski
             if( (last!=current) || !lastBlockIsNE )
440
               {
441
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
442
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
443
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
444
               if( !lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
445
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
446
               }
447
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row, position, normal, texture);
448
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
449
             }
450
           else
451
             {
452
             if( (last!=current) || lastBlockIsNE )
453
               {
454
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
455
               vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
456
               if( seenLand  && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
457
               if( lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
458
               vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
459
               }
460
             vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
461
             vertex= addFrontVertex( vertex, 2, vectZ, col+1, row  , position, normal, texture);
462 6a06a912 Leszek Koltunski
             }
463 ce7f3833 Leszek Koltunski
464
           seenLand = true;
465
           lastBlockIsNE = currentBlockIsNE;
466 6a06a912 Leszek Koltunski
           }
467
            
468
         last = current;
469
         }
470
       }
471
     
472
     return vertex;
473
     }
474
475
///////////////////////////////////////////////////////////////////////////////////////////////////
476
477
   private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
478
     {
479 84ee2a6a Leszek Koltunski
     //android.util.Log.e("CUBES", "repeating last vertex!");
480
481 6a06a912 Leszek Koltunski
     if( vertex>0 )
482
       {
483 e5d9b235 Leszek Koltunski
       remainingVert--;
484
485 39cbf9dc Leszek Koltunski
       position[3*vertex  ] = position[3*vertex-3];
486 6a06a912 Leszek Koltunski
       position[3*vertex+1] = position[3*vertex-2];
487
       position[3*vertex+2] = position[3*vertex-1];
488
489 39cbf9dc Leszek Koltunski
       normal[3*vertex  ]   = normal[3*vertex-3];
490 6a06a912 Leszek Koltunski
       normal[3*vertex+1]   = normal[3*vertex-2];
491
       normal[3*vertex+2]   = normal[3*vertex-1];
492
493 39cbf9dc Leszek Koltunski
       texture[2*vertex  ]  = texture[2*vertex-2];
494 6a06a912 Leszek Koltunski
       texture[2*vertex+1]  = texture[2*vertex-1];
495
         
496
       vertex++;     
497
       }
498
     
499
     return vertex;
500
     }
501
   
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503
504
   private int buildSideGrid(int vertex, float[] position, float[] normal, float[] texture)
505
     {
506 84ee2a6a Leszek Koltunski
     //android.util.Log.d("CUBES", "buildSide");
507 ce7f3833 Leszek Koltunski
508 8d9da98a Leszek Koltunski
     for(int i=0; i<mEdgeNum; i++)
509 6a06a912 Leszek Koltunski
       {
510
       vertex = buildIthSide(mEdges.get(i), vertex, position, normal, texture);  
511
       } 
512
      
513
     return vertex;
514
     }
515
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517
518
   private int buildIthSide(Edge curr, int vertex, float[] position, float[] normal, float[] texture)
519
     {
520
     Edge prev; 
521
     
522
     if( curr.side==NORTH ) // water outside
523
       {
524
       prev = new Edge(WEST,curr.row,curr.col);
525
       }
526
     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.
527
       {
528
       prev = curr;
529
       curr = new Edge(EAST,curr.row+1,curr.col-1);
530
       }
531
     
532
     int col = curr.col;
533
     int row = curr.row;
534
     int side= curr.side;  
535
     Edge next = getNextEdge(curr);
536
     
537 84ee2a6a Leszek Koltunski
     addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
538 6a06a912 Leszek Koltunski
     vertex++;
539
     
540
     do
541
       {
542
       if( prev.side!=curr.side )
543
         {
544 84ee2a6a Leszek Koltunski
         addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
545 6a06a912 Leszek Koltunski
         vertex++;
546 84ee2a6a Leszek Koltunski
         addSideVertex(curr,BACK,UPPER,prev.side,vertex,position,normal,texture);
547 6a06a912 Leszek Koltunski
         vertex++;
548
         }
549
       
550 84ee2a6a Leszek Koltunski
       addSideVertex(curr,FRONT,LOWER,next.side,vertex,position,normal,texture);
551 6a06a912 Leszek Koltunski
       vertex++;
552 84ee2a6a Leszek Koltunski
       addSideVertex(curr,FRONT,UPPER,next.side,vertex,position,normal,texture);
553 6a06a912 Leszek Koltunski
       vertex++;
554
       
555
       prev = curr;
556
       curr = next; 
557
       next = getNextEdge(curr);
558
       }
559
     while( curr.col!=col || curr.row!=row || curr.side!=side );
560
     
561
     vertex = repeatLast(vertex,position,normal,texture);
562
     
563
     return vertex;
564
     }
565
566
///////////////////////////////////////////////////////////////////////////////////////////////////
567
568
   private Edge getNextEdge(Edge curr)
569
     {
570
     int col = curr.col;
571
     int row = curr.row;
572
      
573
     //android.util.Log.e("CUBES", "row="+row+" col="+col+" mRows="+mRows+" mCols="+mCols);
574
                       
575
     switch(curr.side) 
576
       {
577
       case NORTH: if( col==mCols-1 ) 
578
                     return new Edge(EAST,row,col);
579
                   if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] )
580
                     return new Edge(WEST,row-1,col+1);
581
                   if( mCubes[row][col+1]==mCubes[row][col] )
582
                     return new Edge(NORTH,row,col+1);
583
                   else  
584
                     return new Edge(EAST,row,col);
585
                   
586
       case SOUTH: if( col==0 ) 
587
                     return new Edge(WEST,row,col);
588
                   if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] )
589
                     return new Edge(EAST,row+1,col-1); 
590
                   if( mCubes[row][col-1]==mCubes[row][col] )
591
                     return new Edge(SOUTH,row,col-1);
592
                   else
593
                     return new Edge(WEST,row,col); 
594
                     
595
       case EAST : if( row==mRows-1 ) 
596
                     return new Edge(SOUTH,row,col);
597
                   if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] )
598
                     return new Edge(NORTH,row+1,col+1);
599
                   if( mCubes[row+1][col]==mCubes[row][col] )
600
                     return new Edge(EAST,row+1,col);
601
                   else 
602
                     return new Edge(SOUTH,row,col);
603
                   
604 8d9da98a Leszek Koltunski
       default   : if( row==0 )
605 6a06a912 Leszek Koltunski
                     return new Edge(NORTH,row,col);
606
                   if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] )
607
                     return new Edge(SOUTH,row-1,col-1);
608
                   if( mCubes[row-1][col]==mCubes[row][col] )
609
                     return new Edge(WEST,row-1,col);
610
                   else
611
                     return new Edge(NORTH,row,col);     
612
       }
613
     }
614
615
///////////////////////////////////////////////////////////////////////////////////////////////////
616
   
617 84ee2a6a Leszek Koltunski
   private void addSideVertex(Edge curr, boolean back, boolean lower,int side, int vertex, float[] position, float[] normal, float[] texture)
618 6a06a912 Leszek Koltunski
     {
619 84ee2a6a Leszek Koltunski
     //android.util.Log.e("CUBES", "adding Side vertex!");
620
621
     remainingVert--;
622
623 985ea9c5 Leszek Koltunski
     float x, y;
624
625 6a06a912 Leszek Koltunski
     switch(curr.side)
626
       {
627 985ea9c5 Leszek Koltunski
       case NORTH: x = (float)(back ? (curr.col  ):(curr.col+1))/mCols;
628
629
                   position[3*vertex  ] = x - 0.5f;
630
                   position[3*vertex+1] = 0.5f - (float)curr.row/mRows;
631 6a06a912 Leszek Koltunski
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
632
633 39cbf9dc Leszek Koltunski
                   normal[3*vertex  ]   = side==NORTH ? 0.0f : (side==WEST?-R:R);
634 6a06a912 Leszek Koltunski
                   normal[3*vertex+1]   = 1.0f;
635
                   normal[3*vertex+2]   = lower ? -R:R;
636
637 985ea9c5 Leszek Koltunski
                   texture[2*vertex  ]  = x;
638
                   texture[2*vertex+1]  = 1.0f-(float)(lower? (curr.row-1):(curr.row  ))/mRows;
639 6a06a912 Leszek Koltunski
                   break;
640 985ea9c5 Leszek Koltunski
       case SOUTH: x = (float)(back ? (curr.col+1):(curr.col  ))/mCols;
641
642
                   position[3*vertex  ] = x - 0.5f;
643
                   position[3*vertex+1] = 0.5f - (float)(curr.row+1)/mRows;
644 6a06a912 Leszek Koltunski
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;  
645
            
646 39cbf9dc Leszek Koltunski
                   normal[3*vertex  ]   = side==SOUTH ? 0.0f: (side==EAST?-R:R);
647 6a06a912 Leszek Koltunski
                   normal[3*vertex+1]   =-1.0f;
648
                   normal[3*vertex+2]   = lower ? -R:R;
649
650 985ea9c5 Leszek Koltunski
                   texture[2*vertex  ]  = x;
651
                   texture[2*vertex+1]  = 1.0f-(float)(lower? (curr.row+2):(curr.row+1))/mRows;
652 6a06a912 Leszek Koltunski
                   break;
653 985ea9c5 Leszek Koltunski
       case WEST : y = (float)(back  ? (curr.row+1):(curr.row))/mRows;
654
655
                   position[3*vertex  ] = (float)curr.col/mCols -0.5f;
656
                   position[3*vertex+1] = 0.5f - y;
657 6a06a912 Leszek Koltunski
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
658
659 39cbf9dc Leszek Koltunski
                   normal[3*vertex  ]   =-1.0f;
660 6a06a912 Leszek Koltunski
                   normal[3*vertex+1]   = side==WEST ? 0.0f : (side==NORTH?-R:R);
661
                   normal[3*vertex+2]   = lower ? -R:R;
662
 
663 39cbf9dc Leszek Koltunski
                   texture[2*vertex  ]  = (float)(lower ? (curr.col-1):(curr.col  ))/mCols;
664 985ea9c5 Leszek Koltunski
                   texture[2*vertex+1]  = 1.0f - y;
665 6a06a912 Leszek Koltunski
                   break;
666 985ea9c5 Leszek Koltunski
       case EAST : y = (float)(back  ? (curr.row):(curr.row+1))/mRows;
667
668
                   position[3*vertex  ] = (float)(curr.col+1)/mCols -0.5f;
669
                   position[3*vertex+1] = 0.5f - y;
670 6a06a912 Leszek Koltunski
                   position[3*vertex+2] = lower ? BACKZ : FRONTZ;
671
672 39cbf9dc Leszek Koltunski
                   normal[3*vertex  ]   = 1.0f;
673 6a06a912 Leszek Koltunski
                   normal[3*vertex+1]   = side==EAST ? 0.0f : (side==SOUTH?-R:R);
674
                   normal[3*vertex+2]   = lower ? -R:R; 
675
676 39cbf9dc Leszek Koltunski
                   texture[2*vertex  ]  = (float)(lower ? (curr.col+2):(curr.col+1))/mCols;
677 985ea9c5 Leszek Koltunski
                   texture[2*vertex+1]  = 1.0f - y;
678 6a06a912 Leszek Koltunski
                   break;
679
       }
680
     
681 39cbf9dc Leszek Koltunski
     if(texture[2*vertex  ]>1.0f) texture[2*vertex  ] =2.0f-texture[2*vertex  ];
682
     if(texture[2*vertex  ]<0.0f) texture[2*vertex  ] =    -texture[2*vertex  ];
683 6a06a912 Leszek Koltunski
     if(texture[2*vertex+1]>1.0f) texture[2*vertex+1] =2.0f-texture[2*vertex+1];
684
     if(texture[2*vertex+1]<0.0f) texture[2*vertex+1] =    -texture[2*vertex+1];
685
     }
686
687 665e2c45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
688
689
   private void build(boolean frontOnly)
690
     {
691
     int numVertices=0;
692
     float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
693
     float[] normalData  = new float[NORMAL_DATA_SIZE  *dataLength];
694
     float[] textureData = new float[TEX_DATA_SIZE     *dataLength];
695
696
     //android.util.Log.d("CUBES","building front grid...");
697
698
     numVertices = buildFrontBackGrid(true, numVertices,positionData,normalData,textureData);
699
700
     if( !frontOnly )
701
       {
702
       numVertices = repeatLast(numVertices,positionData,normalData,textureData);
703
       if( numVertices%2==1 )
704
         {
705
         //android.util.Log.d("CUBES","repeating winding1 vertex");
706
707
         numVertices = repeatLast(numVertices,positionData,normalData,textureData);
708
         }
709
710
       //android.util.Log.d("CUBES","building side grid...");
711
712
       numVertices = buildSideGrid (numVertices,positionData,normalData,textureData);
713
714
       //android.util.Log.d("CUBES","building back grid...");
715
716
       numVertices = buildFrontBackGrid (false,numVertices,positionData,normalData,textureData);
717
       }
718
719
     /*
720
     android.util.Log.e("CUBES","dataLen="+dataLength+" vertex="+numVertices);
721
     android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
722
     android.util.Log.d("CUBES", "normal: "  +debug(  normalData,3) );
723
     android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
724
     */
725 16d8b8f3 Leszek Koltunski
726 8d9da98a Leszek Koltunski
     mEdges.clear();
727
     mEdges = null;
728
     mCubes = null;
729
730 16d8b8f3 Leszek Koltunski
     if( remainingVert!=0 )
731
       android.util.Log.d("CUBES", "remainingVert " +remainingVert );
732 665e2c45 Leszek Koltunski
733
     mGridPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
734
     mGridPositions.put(positionData).position(0);
735
736
     mGridNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
737
     mGridNormals.put(normalData).position(0);
738
739
     mGridTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
740
     mGridTexture.put(textureData).position(0);
741
     }
742
743 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
744
// PUBLIC API
745
///////////////////////////////////////////////////////////////////////////////////////////////////
746
/**
747 e0b6c593 Leszek Koltunski
 * Creates the underlying grid of vertices, normals, texture coords.
748 6a06a912 Leszek Koltunski
 *    
749 a56bc359 Leszek Koltunski
 * @param cols      Integer helping to parse the next parameter.
750
 * @param desc      String describing the subset of a MxNx1 cuboid that we want to create.
751
 *                  Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
752
 *                  <p></p>
753
 *                  <p>
754
 *                  <pre>
755
 *                  For example, (cols=2, desc="111010") describes the following shape:
756
 *
757
 *                  XX
758
 *                  X
759
 *                  X
760
 *
761
 *                  whereas (cols=2,desc="110001") describes
762
 *
763
 *                  XX
764
 *
765
 *                   X
766
 *                  </pre>
767
 *                  </p>
768
 * @param frontOnly Only create the front wall or side and back as well?
769 6a06a912 Leszek Koltunski
 */
770 b455eb48 Leszek Koltunski
   public GridCubes(int cols, String desc, boolean frontOnly)
771 6a06a912 Leszek Koltunski
      {
772 3ef3364d Leszek Koltunski
      super(frontOnly ? 0.0f:1.0f/cols);
773 2e96ee72 Leszek Koltunski
      prepareDataStructures(cols,desc,frontOnly);
774 665e2c45 Leszek Koltunski
      build(frontOnly);
775
      }
776 6a06a912 Leszek Koltunski
777 665e2c45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
778
/**
779
 * Creates a full, hole-less underlying grid of vertices, normals, texture coords and colors.
780
 *
781 a56bc359 Leszek Koltunski
 * @param cols      Number of columns.
782
 * @param rows      Number of rows.
783
 * @param frontOnly Only create the front wall or side and back as well?
784 665e2c45 Leszek Koltunski
 */
785 b455eb48 Leszek Koltunski
   public GridCubes(int cols, int rows, boolean frontOnly)
786 665e2c45 Leszek Koltunski
      {
787 3ef3364d Leszek Koltunski
      super(frontOnly ? 0.0f:1.0f/cols);
788 665e2c45 Leszek Koltunski
      prepareDataStructures(cols,rows,frontOnly);
789
      build(frontOnly);
790 6a06a912 Leszek Koltunski
      }
791
   }
792
///////////////////////////////////////////////////////////////////////////////////////////////////