Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshCubes.java @ 15290f35

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