Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / MeshCubes.java @ d23592bb

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 c4d06f90 Leszek Koltunski
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3 d333eb6b Leszek Koltunski
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 d333eb6b Leszek Koltunski
//                                                                                               //
6 c4d06f90 Leszek Koltunski
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10 d333eb6b Leszek Koltunski
//                                                                                               //
11 c4d06f90 Leszek Koltunski
// This library is distributed in the hope that it will be useful,                               //
12 d333eb6b Leszek Koltunski
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13 c4d06f90 Leszek Koltunski
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15 d333eb6b Leszek Koltunski
//                                                                                               //
16 c4d06f90 Leszek Koltunski
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
20
21 6c00149d Leszek Koltunski
package org.distorted.library.mesh;
22 6a06a912 Leszek Koltunski
23 91cfe462 Leszek Koltunski
import org.distorted.library.type.Static4D;
24
25 6a06a912 Leszek Koltunski
import java.util.ArrayList;
26
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28 e0b6c593 Leszek Koltunski
/**
29
 * Create a 3D grid composed of Cubes.
30
 * <p>
31 91cfe462 Leszek Koltunski
 * Any subset of a MxNx1 cuboid is possible. (repeated arbitrary number of times into Z-dir)
32 e0b6c593 Leszek Koltunski
 */
33 715e7726 Leszek Koltunski
public class MeshCubes extends MeshBase
34 6a06a912 Leszek Koltunski
   {
35 c90aca24 Leszek Koltunski
   private static final float R = 0.0f;
36 f08b268d Leszek Koltunski
37 91cfe462 Leszek Koltunski
   private static final int FRONT = 0;
38
   private static final int BACK  = 1;
39
   private static final int LEFT  = 2;
40
   private static final int RIGHT = 3;
41
   private static final int TOP   = 4;
42
   private static final int BOTTOM= 5;
43
44 6a06a912 Leszek Koltunski
   private static final int NORTH = 0;
45
   private static final int WEST  = 1;
46
   private static final int EAST  = 2;
47
   private static final int SOUTH = 3;
48 ce7f3833 Leszek Koltunski
49 6a06a912 Leszek Koltunski
   private static final float[] mNormalX = new float[4];
50
   private static final float[] mNormalY = new float[4];
51 ce7f3833 Leszek Koltunski
   private static final float[] mNormalZ = new float[4];
52
53 cbd502ec Leszek Koltunski
   private static class Edge
54 6a06a912 Leszek Koltunski
     {
55
     final int side; 
56
     final int row;
57
     final int col;
58
     
59 06d71892 Leszek Koltunski
     Edge(int s, int r, int c)
60 6a06a912 Leszek Koltunski
       {
61
       side= s; 
62
       row = r;
63
       col = c;
64
       }
65 39cbf9dc Leszek Koltunski
     }
66 91cfe462 Leszek Koltunski
67
   private float[] mTexMappingX,mTexMappingY, mTexMappingW, mTexMappingH;
68
69 80cb15ab leszek
   private int mCols, mRows, mSlices;
70 d6994cc6 Leszek Koltunski
   private int[][] mCubes;
71 9d0df4c6 Leszek Koltunski
   private byte[][] mInflateX, mInflateY;
72 39cbf9dc Leszek Koltunski
   private ArrayList<Edge> mEdges = new ArrayList<>();
73 ce7f3833 Leszek Koltunski
74 15290f35 Leszek Koltunski
   private int currVert;
75 da681e7e Leszek Koltunski
   private int numVertices;
76 8d9da98a Leszek Koltunski
   private int mSideBends;
77
   private int mEdgeNum;
78 b62632fc Leszek Koltunski
   private int mSideWalls;
79 ce7f3833 Leszek Koltunski
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
// a Block is split into two triangles along the NE-SW line iff it is in the top-right
82
// or bottom-left quadrant of the grid.
83
84
   private boolean isNE(int row,int col)
85
     {
86 29dd01c6 Leszek Koltunski
     return ( (2*row<mRows)^(2*col<mCols) );
87 ce7f3833 Leszek Koltunski
     }
88
89 91cfe462 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
90
// fill per-side texture mappings. Default: all 6 sides map to the whole texture.
91
// Each Static4D describes the way its side will map. Format:
92
//
93
// 1st float: X-coord of the texture point that our top-left corner of the side maps to
94
// 2nd float: Y-coord of the texture point that our top-left corner of the side maps to
95
// 3rd float: X-coord of the texture point that our bot-right corner of the side maps to
96
// 4th float: Y-coord of the texture point that our bot-right corner of the side maps to
97
98
   private void fillTexMappings(Static4D front, Static4D back, Static4D left, Static4D right, Static4D top, Static4D bottom)
99
     {
100
     if( mTexMappingX==null ) mTexMappingX = new float[6];
101
     if( mTexMappingY==null ) mTexMappingY = new float[6];
102
     if( mTexMappingW==null ) mTexMappingW = new float[6];
103
     if( mTexMappingH==null ) mTexMappingH = new float[6];
104
105 ece89b28 Leszek Koltunski
     mTexMappingX[FRONT]  = front.get0();
106
     mTexMappingY[FRONT]  = front.get1();
107
     mTexMappingW[FRONT]  = front.get2() - front.get0();
108
     mTexMappingH[FRONT]  = front.get3() - front.get1();
109
110
     mTexMappingX[BACK]   = back.get0();
111
     mTexMappingY[BACK]   = back.get1();
112
     mTexMappingW[BACK]   = back.get2() - back.get0();
113
     mTexMappingH[BACK]   = back.get3() - back.get1();
114
115
     mTexMappingX[LEFT]   = left.get0();
116
     mTexMappingY[LEFT]   = left.get1();
117
     mTexMappingW[LEFT]   = left.get2() - left.get0();
118
     mTexMappingH[LEFT]   = left.get3() - left.get1();
119
120
     mTexMappingX[RIGHT]  = right.get0();
121
     mTexMappingY[RIGHT]  = right.get1();
122
     mTexMappingW[RIGHT]  = right.get2() - right.get0();
123
     mTexMappingH[RIGHT]  = right.get3() - right.get1();
124
125
     mTexMappingX[TOP]    = top.get0();
126
     mTexMappingY[TOP]    = top.get1();
127
     mTexMappingW[TOP]    = top.get2() - top.get0();
128
     mTexMappingH[TOP]    = top.get3() - top.get1();
129
130
     mTexMappingX[BOTTOM] = bottom.get0();
131
     mTexMappingY[BOTTOM] = bottom.get1();
132
     mTexMappingW[BOTTOM] = bottom.get2() - bottom.get0();
133
     mTexMappingH[BOTTOM] = bottom.get3() - bottom.get1();
134 91cfe462 Leszek Koltunski
     }
135
136 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
137 84ee2a6a Leszek Koltunski
// return the number of vertices our grid will contain
138 6a06a912 Leszek Koltunski
139 d1a396b2 leszek
   private int computeDataLength()
140 6a06a912 Leszek Koltunski
      {
141 b62632fc Leszek Koltunski
      int frontWalls=0, frontSegments=0, triangleShifts=0, windingShifts=0;
142 ce7f3833 Leszek Koltunski
      int shiftCol = (mCols-1)/2;
143
144
      boolean lastBlockIsNE=false;
145
      boolean thisBlockIsNE;        // the block we are currently looking at is split into
146
                                    // two triangles along the NE-SW line (rather than NW-SE)
147 29dd01c6 Leszek Koltunski
      for(int row=0; row<mRows; row++)
148 b62632fc Leszek Koltunski
        {
149
        if( mCols>=2 && (mCubes[row][shiftCol]%2 == 1) && (mCubes[row][shiftCol+1]%2 == 1) ) triangleShifts++;
150 ce7f3833 Leszek Koltunski
151 b62632fc Leszek Koltunski
        for(int col=0; col<mCols; col++)
152
          {
153
          if( mCubes[row][col]%2 == 1 )  // land
154 6a06a912 Leszek Koltunski
            {
155 b62632fc Leszek Koltunski
            thisBlockIsNE = isNE(row,col);
156
            if( thisBlockIsNE^lastBlockIsNE ) windingShifts++;
157
            lastBlockIsNE = thisBlockIsNE;
158
            frontWalls++;
159
            if( col==mCols-1 || mCubes[row][col+1]%2 == 0 ) frontSegments++;
160 6a06a912 Leszek Koltunski
            }
161 b62632fc Leszek Koltunski
          }
162
        }
163 ce7f3833 Leszek Koltunski
164 5f2853be Leszek Koltunski
      int frontVert       = 2*( frontWalls + 2*frontSegments - 1) +2*triangleShifts + windingShifts;
165
      int sideVertOneSlice= 2*( mSideWalls + mSideBends + mEdgeNum -1);
166
      int sideVert        = 2*(mSlices-1) + mSlices*sideVertOneSlice;
167
      int firstWinding    = (mSlices>0 && (frontVert+1)%2==1 ) ? 1:0;
168
      int dataL           = mSlices==0 ? frontVert : (frontVert+1) +firstWinding+ (1+sideVert+1) + (1+frontVert);
169 c90aca24 Leszek Koltunski
170 6a06a912 Leszek Koltunski
      return dataL<0 ? 0:dataL;
171
      }
172
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
175 80cb15ab leszek
   private void prepareDataStructures(int cols, String desc, int slices)
176 6a06a912 Leszek Koltunski
     {
177 e92785ba Leszek Koltunski
     mRows       =0;
178
     mCols       =0;
179
     mSlices     =slices;
180 da681e7e Leszek Koltunski
     numVertices =0;
181 554ce72b Leszek Koltunski
182 0729bc41 Leszek Koltunski
     if( cols>0 && desc.contains("1") )
183 6a06a912 Leszek Koltunski
       {
184 0729bc41 Leszek Koltunski
       mCols = cols;
185
       mRows = desc.length()/cols;
186 6a06a912 Leszek Koltunski
187 9d0df4c6 Leszek Koltunski
       mCubes    = new int[mRows][mCols];
188
       mInflateX = new byte[mRows+1][mCols+1];
189
       mInflateY = new byte[mRows+1][mCols+1];
190
191 20156af7 Leszek Koltunski
       for(int col=0; col<mCols; col++)
192
         for(int row=0; row<mRows; row++)
193
           mCubes[row][col] = (desc.charAt(row * mCols + col) == '1' ? 1 : 0);
194 9d0df4c6 Leszek Koltunski
195 20156af7 Leszek Koltunski
       for(int col=0; col<mCols+1; col++)
196
         for(int row=0; row<mRows+1; row++)
197 9d0df4c6 Leszek Koltunski
           {
198 20156af7 Leszek Koltunski
           fillInflate(row,col);
199 9d0df4c6 Leszek Koltunski
           }
200 d6994cc6 Leszek Koltunski
201 0729bc41 Leszek Koltunski
       markRegions();
202 da681e7e Leszek Koltunski
       numVertices = computeDataLength();
203 15290f35 Leszek Koltunski
       currVert = 0;
204 6a06a912 Leszek Koltunski
       }
205
     }
206 665e2c45 Leszek Koltunski
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
// full grid
209
210 80cb15ab leszek
   private void prepareDataStructures(int cols, int rows, int slices)
211 665e2c45 Leszek Koltunski
     {
212 e92785ba Leszek Koltunski
     mRows        =rows;
213
     mCols        =cols;
214
     mSlices      =slices;
215 da681e7e Leszek Koltunski
     numVertices  =0;
216 665e2c45 Leszek Koltunski
217
     if( cols>0 && rows>0 )
218
       {
219 9d0df4c6 Leszek Koltunski
       mCubes    = new int[mRows][mCols];
220
       mInflateX = new byte[mRows+1][mCols+1];
221
       mInflateY = new byte[mRows+1][mCols+1];
222 665e2c45 Leszek Koltunski
223 20156af7 Leszek Koltunski
       for(int col=0; col<mCols; col++)
224
         for(int row=0; row<mRows; row++)
225
           mCubes[row][col] = 1;
226 665e2c45 Leszek Koltunski
227 20156af7 Leszek Koltunski
       for(int col=0; col<mCols+1; col++)
228
         for(int row=0; row<mRows+1; row++)
229 9d0df4c6 Leszek Koltunski
           {
230 20156af7 Leszek Koltunski
           fillInflate(row,col);
231 9d0df4c6 Leszek Koltunski
           }
232
233 665e2c45 Leszek Koltunski
       markRegions();
234 da681e7e Leszek Koltunski
       numVertices = computeDataLength();
235 15290f35 Leszek Koltunski
       currVert = 0;
236 665e2c45 Leszek Koltunski
       }
237
     }
238
239 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
240
// Mark all the 'regions' of our grid  - i.e. separate pieces of 'land' (connected blocks that will 
241
// be rendered) and 'water' (connected holes in between) with integers. Each connected block of land
242
// gets a unique odd integer, each connected block of water a unique even integer.
243
//
244
// Water on the edges of the grid is also considered connected to itself!   
245
//   
246
// This function also creates a list of 'Edges'. Each Edge is a data structure from which later on we
247 2e96ee72 Leszek Koltunski
// will start building the side walls of each connected block of land (and sides of holes of water
248 91cfe462 Leszek Koltunski
// inside). Each Edge needs to point from Land to Water (thus the '(SOUTH,row-1,col)' below) - otherwise
249 8d9da98a Leszek Koltunski
// later on setting up normal vectors wouldn't work.
250 6a06a912 Leszek Koltunski
   
251 a51fe521 Leszek Koltunski
  private void markRegions()
252 6a06a912 Leszek Koltunski
     {
253 91cfe462 Leszek Koltunski
     int row, col, numWater=1, numLand=0;
254 6a06a912 Leszek Koltunski
     
255 91cfe462 Leszek Koltunski
     for(row=0; row<mRows; row++) if( mCubes[    row][      0]==0 ) markRegion((short)2,    row,       0);
256
     for(row=0; row<mRows; row++) if( mCubes[    row][mCols-1]==0 ) markRegion((short)2,    row, mCols-1);
257
     for(col=0; col<mCols; col++) if( mCubes[0      ][    col]==0 ) markRegion((short)2,      0,     col);
258
     for(col=0; col<mCols; col++) if( mCubes[mRows-1][    col]==0 ) markRegion((short)2,mRows-1,     col);
259 6a06a912 Leszek Koltunski
           
260 91cfe462 Leszek Koltunski
     for(row=0; row<mRows; row++)
261
        for(col=0; col<mCols; col++)
262 6a06a912 Leszek Koltunski
           {
263 91cfe462 Leszek Koltunski
           if( mCubes[row][col] == 0 ) { numWater++; markRegion( (short)(2*numWater ),row,col); mEdges.add(new Edge(SOUTH,row-1,col)); }
264
           if( mCubes[row][col] == 1 ) { numLand ++; markRegion( (short)(2*numLand+1),row,col); mEdges.add(new Edge(NORTH,row  ,col)); }
265 6a06a912 Leszek Koltunski
           }
266
     
267 8d9da98a Leszek Koltunski
     // now we potentially need to kick out some Edges . Otherwise the following does not work:
268 6a06a912 Leszek Koltunski
     //
269
     // 0 1 0
270
     // 1 0 1
271
     // 0 1 0
272
     
273 8d9da98a Leszek Koltunski
     mEdgeNum= mEdges.size();
274
     int initCol, initRow, initSide, lastSide;
275
     Edge e1,e2;
276 6a06a912 Leszek Koltunski
     
277 91cfe462 Leszek Koltunski
     for(int edge=0; edge<mEdgeNum; edge++)
278 6a06a912 Leszek Koltunski
       {
279 91cfe462 Leszek Koltunski
       e1 = mEdges.get(edge);
280 8d9da98a Leszek Koltunski
       initRow= e1.row;
281
       initCol= e1.col;
282
       initSide=e1.side;
283
284
       do
285 6a06a912 Leszek Koltunski
         {
286 b62632fc Leszek Koltunski
         mSideWalls++;
287
288 8d9da98a Leszek Koltunski
         if( e1.side==NORTH || e1.side==SOUTH )
289 6a06a912 Leszek Koltunski
           {
290 91cfe462 Leszek Koltunski
           for(int j=edge+1;j<mEdgeNum;j++)
291 6a06a912 Leszek Koltunski
             {
292 8d9da98a Leszek Koltunski
             e2 = mEdges.get(j);
293
294
             if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col )
295
               {
296
               mEdges.remove(j);
297
               mEdgeNum--;
298
               j--;
299
               }
300 6a06a912 Leszek Koltunski
             }
301
           }
302 8d9da98a Leszek Koltunski
303
         lastSide = e1.side;
304
         e1 = getNextEdge(e1);
305
         if( e1.side!=lastSide ) mSideBends++;
306 6a06a912 Leszek Koltunski
         }
307 8d9da98a Leszek Koltunski
       while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide );
308 6a06a912 Leszek Koltunski
       }
309
     }
310
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312
// when calling, make sure that newVal != val
313
   
314 a51fe521 Leszek Koltunski
  private void markRegion(short newVal, int row, int col)
315 6a06a912 Leszek Koltunski
     {
316 d6994cc6 Leszek Koltunski
     int val = mCubes[row][col];
317 6a06a912 Leszek Koltunski
     mCubes[row][col] = newVal;
318
     
319
     if( row>0       && mCubes[row-1][col  ]==val ) markRegion(newVal, row-1, col  );
320
     if( row<mRows-1 && mCubes[row+1][col  ]==val ) markRegion(newVal, row+1, col  );
321
     if( col>0       && mCubes[row  ][col-1]==val ) markRegion(newVal, row  , col-1);
322
     if( col<mCols-1 && mCubes[row  ][col+1]==val ) markRegion(newVal, row  , col+1);
323
     }
324
   
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326
   
327 a51fe521 Leszek Koltunski
  private void createNormals(boolean front, int row, int col)
328 6a06a912 Leszek Koltunski
     {
329
     int td,lr; 
330
      
331 2e96ee72 Leszek Koltunski
     int nw = (col>0       && row>0      ) ? (mCubes[row-1][col-1]%2) : 0;
332
     int w  = (col>0                     ) ? (mCubes[row  ][col-1]%2) : 0;
333
     int n  = (               row>0      ) ? (mCubes[row-1][col  ]%2) : 0;
334
     int c  =                                (mCubes[row  ][col  ]%2);
335
     int sw = (col>0       && row<mRows-1) ? (mCubes[row+1][col-1]%2) : 0;
336 6a06a912 Leszek Koltunski
     int s  = (               row<mRows-1) ? (mCubes[row+1][col  ]%2) : 0;
337
     int ne = (col<mCols-1 && row>0      ) ? (mCubes[row-1][col+1]%2) : 0;
338
     int e  = (col<mCols-1               ) ? (mCubes[row  ][col+1]%2) : 0;
339
     int se = (col<mCols-1 && row<mRows-1) ? (mCubes[row+1][col+1]%2) : 0;
340 ce7f3833 Leszek Koltunski
341
     if(front)
342
       {
343
       mNormalZ[0] = 1.0f;
344
       mNormalZ[1] = 1.0f;
345
       mNormalZ[2] = 1.0f;
346
       mNormalZ[3] = 1.0f;
347
       }
348
     else
349
       {
350
       mNormalZ[0] =-1.0f;
351
       mNormalZ[1] =-1.0f;
352
       mNormalZ[2] =-1.0f;
353
       mNormalZ[3] =-1.0f;
354
       }
355
356 6a06a912 Leszek Koltunski
     td = nw+n-w-c;
357
     lr = c+n-w-nw;
358
     if( td<0 ) td=-1;
359
     if( td>0 ) td= 1;
360
     if( lr<0 ) lr=-1;
361
     if( lr>0 ) lr= 1;
362
     mNormalX[0] = lr*R;
363
     mNormalY[0] = td*R;
364
     
365
     td = w+c-sw-s;
366
     lr = c+s-w-sw;
367
     if( td<0 ) td=-1;
368
     if( td>0 ) td= 1;
369
     if( lr<0 ) lr=-1;
370
     if( lr>0 ) lr= 1;
371
     mNormalX[1] = lr*R;
372
     mNormalY[1] = td*R;
373
     
374
     td = n+ne-c-e;
375
     lr = e+ne-c-n;
376
     if( td<0 ) td=-1;
377
     if( td>0 ) td= 1;
378
     if( lr<0 ) lr=-1;
379
     if( lr>0 ) lr= 1;
380
     mNormalX[2] = lr*R;
381
     mNormalY[2] = td*R;
382
     
383
     td = c+e-s-se;
384
     lr = e+se-c-s;
385
     if( td<0 ) td=-1;
386
     if( td>0 ) td= 1;
387
     if( lr<0 ) lr=-1;
388
     if( lr>0 ) lr= 1;
389
     mNormalX[3] = lr*R;
390
     mNormalY[3] = td*R;
391
     }
392 ce7f3833 Leszek Koltunski
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394
395 e54bfada Leszek Koltunski
  private void buildFrontBackGrid(boolean front, float[] attribs1, float[] attribs2)
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 e5d9b235 Leszek Koltunski
     for(int row=0; row<mRows; row++)
404 6a06a912 Leszek Koltunski
       {
405
       last =0;
406
         
407 e5d9b235 Leszek Koltunski
       for(int col=0; col<mCols; col++)
408 6a06a912 Leszek Koltunski
         {
409 e5d9b235 Leszek Koltunski
         current = mCubes[row][col];
410 ce7f3833 Leszek Koltunski
411 6a06a912 Leszek Koltunski
         if( current%2 == 1 )
412
           {
413 e5d9b235 Leszek Koltunski
           currentBlockIsNE = isNE(row,col);
414 84ee2a6a Leszek Koltunski
415 15290f35 Leszek Koltunski
           if( !seenLand && !front && ((currVert%2==1)^currentBlockIsNE) )
416 84ee2a6a Leszek Koltunski
             {
417 e54bfada Leszek Koltunski
             repeatLast(attribs1,attribs2);
418 84ee2a6a Leszek Koltunski
             }
419
420 e5d9b235 Leszek Koltunski
           createNormals(front,row,col);
421 ce7f3833 Leszek Koltunski
422 e5d9b235 Leszek Koltunski
           if( currentBlockIsNE )
423 6a06a912 Leszek Koltunski
             {
424 e5d9b235 Leszek Koltunski
             if( (last!=current) || !lastBlockIsNE )
425
               {
426 e54bfada Leszek Koltunski
               if( seenLand  && (last != current) ) repeatLast(attribs1,attribs2);
427
               addFrontVertex( 0, vectZ, col, row, attribs1,attribs2);
428
               if( seenLand  && (last != current) ) repeatLast(attribs1,attribs2);
429
               if( !lastBlockIsNE || (!front && !seenLand) ) repeatLast(attribs1,attribs2);
430
               addFrontVertex( 1, vectZ, col, row+1, attribs1,attribs2);
431 e5d9b235 Leszek Koltunski
               }
432 e54bfada Leszek Koltunski
             addFrontVertex( 2, vectZ, col+1, row  , attribs1,attribs2);
433
             addFrontVertex( 3, vectZ, col+1, row+1, attribs1,attribs2);
434 e5d9b235 Leszek Koltunski
             }
435
           else
436
             {
437
             if( (last!=current) || lastBlockIsNE )
438
               {
439 e54bfada Leszek Koltunski
               if( seenLand  && (last != current) ) repeatLast(attribs1,attribs2);
440
               addFrontVertex( 1, vectZ, col, row+1, attribs1,attribs2);
441
               if( seenLand  && (last != current) ) repeatLast(attribs1,attribs2);
442
               if( lastBlockIsNE || (!front && !seenLand) ) repeatLast(attribs1,attribs2);
443
               addFrontVertex( 0, vectZ, col, row, attribs1,attribs2);
444 e5d9b235 Leszek Koltunski
               }
445 e54bfada Leszek Koltunski
             addFrontVertex( 3, vectZ, col+1, row+1, attribs1,attribs2);
446
             addFrontVertex( 2, vectZ, col+1, row  , attribs1,attribs2);
447 6a06a912 Leszek Koltunski
             }
448 ce7f3833 Leszek Koltunski
449
           seenLand = true;
450
           lastBlockIsNE = currentBlockIsNE;
451 6a06a912 Leszek Koltunski
           }
452
            
453
         last = current;
454
         }
455
       }
456
     }
457
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459
460 e54bfada Leszek Koltunski
  private void buildSideGrid(float[] attribs1,float[] attribs2)
461 6a06a912 Leszek Koltunski
     {
462 8d9da98a Leszek Koltunski
     for(int i=0; i<mEdgeNum; i++)
463 6a06a912 Leszek Koltunski
       {
464 e54bfada Leszek Koltunski
       buildIthSide(mEdges.get(i), attribs1, attribs2);
465 15290f35 Leszek Koltunski
       }
466 6a06a912 Leszek Koltunski
     }
467
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469
470 e54bfada Leszek Koltunski
  private void buildIthSide(Edge curr, float[] attribs1, float[] attribs2)
471 6a06a912 Leszek Koltunski
     {
472 5f2853be Leszek Koltunski
     Edge prev, next;
473
     int col, row, side;
474
475 6a06a912 Leszek Koltunski
     if( curr.side==NORTH ) // water outside
476
       {
477
       prev = new Edge(WEST,curr.row,curr.col);
478
       }
479 d1a396b2 leszek
     else                   // land outside; we need to move forward one link because we are
480
       {                    // going in opposite direction and we need to start from a bend.
481 6a06a912 Leszek Koltunski
       prev = curr;
482
       curr = new Edge(EAST,curr.row+1,curr.col-1);
483
       }
484 5f2853be Leszek Koltunski
485 9d0df4c6 Leszek Koltunski
     for(int slice=0; slice<mSlices; slice++)
486 6a06a912 Leszek Koltunski
       {
487 5f2853be Leszek Koltunski
       col = curr.col;
488
       row = curr.row;
489
       side= curr.side;
490
       next = getNextEdge(curr);
491
     
492 e54bfada Leszek Koltunski
       addSideVertex(curr,true,slice+1,prev.side,attribs1,attribs2);
493 5f2853be Leszek Koltunski
494
       do
495 6a06a912 Leszek Koltunski
         {
496 5f2853be Leszek Koltunski
         if( prev.side!=curr.side )
497
           {
498 e54bfada Leszek Koltunski
           addSideVertex(curr,true,slice+1,prev.side,attribs1,attribs2);
499
           addSideVertex(curr,true,slice  ,prev.side,attribs1,attribs2);
500 5f2853be Leszek Koltunski
           }
501 6a06a912 Leszek Koltunski
       
502 e54bfada Leszek Koltunski
         addSideVertex(curr,false,slice+1,next.side,attribs1,attribs2);
503
         addSideVertex(curr,false,slice  ,next.side,attribs1,attribs2);
504 6a06a912 Leszek Koltunski
       
505 5f2853be Leszek Koltunski
         prev = curr;
506
         curr = next;
507
         next = getNextEdge(curr);
508
         }
509
       while( curr.col!=col || curr.row!=row || curr.side!=side );
510 6a06a912 Leszek Koltunski
     
511 e54bfada Leszek Koltunski
       repeatLast(attribs1,attribs2);
512 5f2853be Leszek Koltunski
       }
513 6a06a912 Leszek Koltunski
     }
514
515
///////////////////////////////////////////////////////////////////////////////////////////////////
516
517 a51fe521 Leszek Koltunski
  private Edge getNextEdge(Edge curr)
518 6a06a912 Leszek Koltunski
     {
519
     int col = curr.col;
520
     int row = curr.row;
521 c90aca24 Leszek Koltunski
522 6a06a912 Leszek Koltunski
     switch(curr.side) 
523
       {
524
       case NORTH: if( col==mCols-1 ) 
525
                     return new Edge(EAST,row,col);
526
                   if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] )
527
                     return new Edge(WEST,row-1,col+1);
528
                   if( mCubes[row][col+1]==mCubes[row][col] )
529
                     return new Edge(NORTH,row,col+1);
530
                   else  
531
                     return new Edge(EAST,row,col);
532
                   
533
       case SOUTH: if( col==0 ) 
534
                     return new Edge(WEST,row,col);
535
                   if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] )
536
                     return new Edge(EAST,row+1,col-1); 
537
                   if( mCubes[row][col-1]==mCubes[row][col] )
538
                     return new Edge(SOUTH,row,col-1);
539
                   else
540
                     return new Edge(WEST,row,col); 
541
                     
542
       case EAST : if( row==mRows-1 ) 
543
                     return new Edge(SOUTH,row,col);
544
                   if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] )
545
                     return new Edge(NORTH,row+1,col+1);
546
                   if( mCubes[row+1][col]==mCubes[row][col] )
547
                     return new Edge(EAST,row+1,col);
548
                   else 
549
                     return new Edge(SOUTH,row,col);
550
                   
551 8d9da98a Leszek Koltunski
       default   : if( row==0 )
552 6a06a912 Leszek Koltunski
                     return new Edge(NORTH,row,col);
553
                   if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] )
554
                     return new Edge(SOUTH,row-1,col-1);
555
                   if( mCubes[row-1][col]==mCubes[row][col] )
556
                     return new Edge(WEST,row-1,col);
557
                   else
558
                     return new Edge(NORTH,row,col);     
559
       }
560
     }
561
562 9d0df4c6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
563
564 20156af7 Leszek Koltunski
  private void fillInflate(int row, int col)
565 9d0df4c6 Leszek Koltunski
    {
566
    int diff;
567
568 20156af7 Leszek Koltunski
         if( col==0     ) mInflateX[row][col] = -1;
569
    else if( col==mCols ) mInflateX[row][col] = +1;
570 9d0df4c6 Leszek Koltunski
    else
571
      {
572
      if( row==0 )
573
        {
574
        diff = mCubes[0][col-1]-mCubes[0][col];
575
        }
576
      else if( row==mRows )
577
        {
578
        diff = mCubes[mRows-1][col-1]-mCubes[mRows-1][col];
579
        }
580
      else
581
        {
582
        diff = (mCubes[row  ][col-1]-mCubes[row  ][col]) +
583
               (mCubes[row-1][col-1]-mCubes[row-1][col]) ;
584
585
        if( diff==-2 ) diff=-1;
586
        if( diff== 2 ) diff= 1;
587
        }
588
589 20156af7 Leszek Koltunski
      mInflateX[row][col] = (byte)diff;
590 9d0df4c6 Leszek Koltunski
      }
591
592 20156af7 Leszek Koltunski
         if( row==0     ) mInflateY[row][col] = +1;
593
    else if( row==mRows ) mInflateY[row][col] = -1;
594 9d0df4c6 Leszek Koltunski
    else
595
      {
596
      if( col==0 )
597
        {
598
        diff = mCubes[row][0]-mCubes[row-1][0];
599
        }
600
      else if( col==mCols )
601
        {
602
        diff = mCubes[row][mCols-1]-mCubes[row-1][mCols-1];
603
        }
604
      else
605
        {
606
        diff = (mCubes[row  ][col-1]+mCubes[row  ][col]) -
607
               (mCubes[row-1][col-1]+mCubes[row-1][col]) ;
608
609
        if( diff==-2 ) diff=-1;
610
        if( diff== 2 ) diff= 1;
611
        }
612
613 20156af7 Leszek Koltunski
      mInflateY[row][col] = (byte)diff;
614 9d0df4c6 Leszek Koltunski
      }
615
    }
616
617 a51fe521 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
618
619 e54bfada Leszek Koltunski
  private void addFrontVertex(int index, float vectZ, int col, int row, float[] attribs1, float[] attribs2)
620 a51fe521 Leszek Koltunski
     {
621
     float x = (float)col/mCols;
622
     float y = (float)row/mRows;
623
624 e54bfada Leszek Koltunski
     attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB  ] = x-0.5f;
625
     attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f-y;
626
     attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+2] = vectZ;
627 6f2d931d Leszek Koltunski
628 e54bfada Leszek Koltunski
     attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB  ] = mNormalX[index];
629
     attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+1] = mNormalY[index];
630
     attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+2] = mNormalZ[index];
631 6f2d931d Leszek Koltunski
632 91cfe462 Leszek Koltunski
     if( vectZ>0 )
633
       {
634 e54bfada Leszek Koltunski
       attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[FRONT] +       x  * mTexMappingW[FRONT];
635
       attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[FRONT] + (1.0f-y) * mTexMappingH[FRONT];
636 91cfe462 Leszek Koltunski
       }
637
     else
638
       {
639 e54bfada Leszek Koltunski
       attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[BACK]  +       x  * mTexMappingW[BACK];
640
       attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[BACK]  + (1.0f-y) * mTexMappingH[BACK];
641 91cfe462 Leszek Koltunski
       }
642 a51fe521 Leszek Koltunski
643 15290f35 Leszek Koltunski
     currVert++;
644 a51fe521 Leszek Koltunski
     }
645
646 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
647 2d203318 Leszek Koltunski
648 e54bfada Leszek Koltunski
  private void addSideVertex(Edge curr, boolean back, int slice, int side, float[] attribs1, float[] attribs2)
649 6a06a912 Leszek Koltunski
     {
650 9d0df4c6 Leszek Koltunski
     float x, y, z;
651 91cfe462 Leszek Koltunski
     int row, col;
652 985ea9c5 Leszek Koltunski
653 6a06a912 Leszek Koltunski
     switch(curr.side)
654
       {
655 9d0df4c6 Leszek Koltunski
       case NORTH: row = curr.row;
656
                   col = (back ? (curr.col  ):(curr.col+1));
657
                   x = (float)col/mCols;
658
                   y = 0.5f - (float)row/mRows;
659
                   z = 0.5f - (float)slice/mSlices;
660 985ea9c5 Leszek Koltunski
661 e54bfada Leszek Koltunski
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB  ] = x - 0.5f;
662
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+1] = y;
663
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+2] = z;
664 6f2d931d Leszek Koltunski
665 e54bfada Leszek Koltunski
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB  ] = side==NORTH ? 0.0f : (side==WEST?-R:R);
666
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+1] = 1.0f;
667
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
668 6f2d931d Leszek Koltunski
669 e54bfada Leszek Koltunski
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[TOP] +       x  * mTexMappingW[TOP];
670
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[TOP] + (0.5f-z) * mTexMappingH[TOP];
671 15ed35e6 Leszek Koltunski
672 6a06a912 Leszek Koltunski
                   break;
673 9d0df4c6 Leszek Koltunski
       case SOUTH: row = curr.row+1;
674
                   col = (back ? (curr.col+1):(curr.col));
675
                   x = (float)col/mCols;
676
                   y = 0.5f - (float)row/mRows;
677
                   z = 0.5f - (float)slice/mSlices;
678 985ea9c5 Leszek Koltunski
679 e54bfada Leszek Koltunski
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB  ] = x - 0.5f;
680
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+1] = y;
681
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+2] = z;
682 6f2d931d Leszek Koltunski
683 e54bfada Leszek Koltunski
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB  ] = side==SOUTH ? 0.0f: (side==EAST?-R:R);
684
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+1] =-1.0f;
685
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
686 6f2d931d Leszek Koltunski
687 e54bfada Leszek Koltunski
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[BOTTOM] +       x  * mTexMappingW[BOTTOM];
688
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[BOTTOM] + (0.5f-z) * mTexMappingH[BOTTOM];
689 15ed35e6 Leszek Koltunski
690 6a06a912 Leszek Koltunski
                   break;
691 9d0df4c6 Leszek Koltunski
       case WEST : row = (back  ? (curr.row+1):(curr.row));
692
                   col = curr.col;
693
                   x = (float)col/mCols -0.5f;
694
                   y = (float)row/mRows;
695
                   z = 0.5f - (float)slice/mSlices;
696 985ea9c5 Leszek Koltunski
697 e54bfada Leszek Koltunski
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB  ] = x;
698
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f - y;
699
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+2] = z;
700 6f2d931d Leszek Koltunski
701 e54bfada Leszek Koltunski
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB  ] =-1.0f;
702
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+1] = side==WEST ? 0.0f : (side==NORTH?-R:R);
703
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
704 6f2d931d Leszek Koltunski
705 e54bfada Leszek Koltunski
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[LEFT] + (0.5f-z) * mTexMappingW[LEFT];
706
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[LEFT] + (1.0f-y) * mTexMappingH[LEFT];
707 2d203318 Leszek Koltunski
708 6a06a912 Leszek Koltunski
                   break;
709 9d0df4c6 Leszek Koltunski
       case EAST : row = (back  ? (curr.row):(curr.row+1));
710
                   col = (curr.col+1);
711
                   x = (float)col/mCols -0.5f;
712
                   y = (float)row/mRows;
713
                   z = 0.5f - (float)slice/mSlices;
714 985ea9c5 Leszek Koltunski
715 e54bfada Leszek Koltunski
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB  ] = x;
716
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+1] = 0.5f - y;
717
                   attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+2] = z;
718 6f2d931d Leszek Koltunski
719 e54bfada Leszek Koltunski
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB  ] = 1.0f;
720
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+1] = side==EAST ? 0.0f : (side==SOUTH?-R:R);
721
                   attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+2] = (slice==0 ? R : (slice==mSlices ? -R:0) );
722 6f2d931d Leszek Koltunski
723 e54bfada Leszek Koltunski
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB  ] = mTexMappingX[RIGHT] + (0.5f-z) * mTexMappingW[RIGHT];
724
                   attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB+1] = mTexMappingY[RIGHT] + (1.0f-y) * mTexMappingH[RIGHT];
725 a16bf106 Leszek Koltunski
726 2d203318 Leszek Koltunski
                   break;
727 a16bf106 Leszek Koltunski
       }
728 2d203318 Leszek Koltunski
729 15290f35 Leszek Koltunski
     currVert++;
730 6f2d931d Leszek Koltunski
     }
731
732
///////////////////////////////////////////////////////////////////////////////////////////////////
733
734 e54bfada Leszek Koltunski
   private void repeatLast(float[] attribs1, float[] attribs2)
735 6f2d931d Leszek Koltunski
     {
736 e54bfada Leszek Koltunski
     attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(currVert-1) + POS_ATTRIB  ];
737
     attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(currVert-1) + POS_ATTRIB+1];
738
     attribs1[VERT1_ATTRIBS*currVert + POS_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(currVert-1) + POS_ATTRIB+2];
739 6f2d931d Leszek Koltunski
740 e54bfada Leszek Koltunski
     attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB  ] = attribs1[VERT1_ATTRIBS*(currVert-1) + NOR_ATTRIB  ];
741
     attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+1] = attribs1[VERT1_ATTRIBS*(currVert-1) + NOR_ATTRIB+1];
742
     attribs1[VERT1_ATTRIBS*currVert + NOR_ATTRIB+2] = attribs1[VERT1_ATTRIBS*(currVert-1) + NOR_ATTRIB+2];
743 6f2d931d Leszek Koltunski
744 e54bfada Leszek Koltunski
     attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB  ] = attribs2[VERT2_ATTRIBS*(currVert-1) + TEX_ATTRIB  ];
745
     attribs2[VERT2_ATTRIBS*currVert + TEX_ATTRIB+1] = attribs2[VERT2_ATTRIBS*(currVert-1) + TEX_ATTRIB+1];
746 6f2d931d Leszek Koltunski
747 15290f35 Leszek Koltunski
     currVert++;
748 6a06a912 Leszek Koltunski
     }
749
750 665e2c45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
751
752 d1a396b2 leszek
  private void build()
753 665e2c45 Leszek Koltunski
     {
754 e54bfada Leszek Koltunski
     float[] attribs1= new float[VERT1_ATTRIBS*numVertices];
755
     float[] attribs2= new float[VERT2_ATTRIBS*numVertices];
756 665e2c45 Leszek Koltunski
757 e54bfada Leszek Koltunski
     buildFrontBackGrid(true,attribs1,attribs2);
758 665e2c45 Leszek Koltunski
759 d1a396b2 leszek
     if( mSlices>0 )
760 665e2c45 Leszek Koltunski
       {
761 e54bfada Leszek Koltunski
       repeatLast(attribs1,attribs2);
762
       if( currVert%2==1 ) repeatLast(attribs1,attribs2);
763
       buildSideGrid(attribs1,attribs2);
764
       buildFrontBackGrid(false,attribs1,attribs2);
765 665e2c45 Leszek Koltunski
       }
766
767 8d9da98a Leszek Koltunski
     mEdges.clear();
768
     mEdges = null;
769
     mCubes = null;
770 9d0df4c6 Leszek Koltunski
     mInflateX = null;
771
     mInflateY = null;
772 8d9da98a Leszek Koltunski
773 15290f35 Leszek Koltunski
     if( currVert!=numVertices )
774
       android.util.Log.e("MeshCubes", "currVert " +currVert+" numVertices="+numVertices );
775 665e2c45 Leszek Koltunski
776 e54bfada Leszek Koltunski
     setAttribs(attribs1,attribs2);
777 665e2c45 Leszek Koltunski
     }
778
779 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
780
// PUBLIC API
781
///////////////////////////////////////////////////////////////////////////////////////////////////
782
/**
783 05403bba Leszek Koltunski
 * Creates the underlying mesh of vertices, normals, texture coords.
784 6a06a912 Leszek Koltunski
 *    
785 80cb15ab leszek
 * @param cols   Integer helping to parse the next parameter.
786
 * @param desc   String describing the subset of a MxNx1 cuboid that we want to create.
787
 *               Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
788
 *               <p></p>
789
 *               <p>
790
 *               <pre>
791 91cfe462 Leszek Koltunski
 *               For example, (cols=2, desc="111010", slices=1) describes the following shape:
792 a56bc359 Leszek Koltunski
 *
793 80cb15ab leszek
 *               XX
794
 *               X
795
 *               X
796 a56bc359 Leszek Koltunski
 *
797 91cfe462 Leszek Koltunski
 *               whereas (cols=2,desc="110001", slices=1) describes
798 a56bc359 Leszek Koltunski
 *
799 80cb15ab leszek
 *               XX
800 a56bc359 Leszek Koltunski
 *
801 80cb15ab leszek
 *                X
802
 *               </pre>
803
 *               </p>
804
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
805 6a06a912 Leszek Koltunski
 */
806 80cb15ab leszek
 public MeshCubes(int cols, String desc, int slices)
807 a51fe521 Leszek Koltunski
   {
808 0f10a0b6 Leszek Koltunski
   super();
809 91cfe462 Leszek Koltunski
   Static4D map = new Static4D(0.0f,0.0f,1.0f,1.0f);
810
   fillTexMappings(map,map,map,map,map,map);
811
   prepareDataStructures(cols,desc,slices);
812
   build();
813
   }
814
815
///////////////////////////////////////////////////////////////////////////////////////////////////
816
/**
817
 * Creates the underlying mesh of vertices, normals, texture coords with custom texture mappings.
818
 *
819
 * The mappings decide what part of the texture appears on a given side. Example:
820
 * front = (0.0,0.5,0.5,1.0) means that the front side of the grid will be textured with the
821
 * bottom-left quadrant of the texture.
822
 *
823
 * Default is: each of the 6 sides maps to the whole texture, i.e. (0.0,0.0,1.0,1.0)
824
 *
825
 * @param cols   Integer helping to parse the next parameter.
826
 * @param desc   String describing the subset of a MxNx1 cuboid that we want to create.
827
 *               Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
828
 *               <p></p>
829
 *               <p>
830
 *               <pre>
831
 *               For example, (cols=2, desc="111010", slices=1) describes the following shape:
832
 *
833
 *               XX
834
 *               X
835
 *               X
836
 *
837
 *               whereas (cols=2,desc="110001", slices=1) describes
838
 *
839
 *               XX
840
 *
841
 *                X
842
 *               </pre>
843
 *               </p>
844
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
845
 * @param front  Texture mapping the front side maps to.
846
 * @param back   Texture mapping the back side maps to.
847
 * @param left   Texture mapping the left side maps to.
848
 * @param right  Texture mapping the right side maps to.
849
 * @param top    Texture mapping the top side maps to.
850
 * @param bottom Texture mapping the bottom side maps to.
851
 */
852
 public MeshCubes(int cols, String desc, int slices, Static4D front, Static4D back, Static4D left, Static4D right, Static4D top, Static4D bottom)
853
   {
854 0f10a0b6 Leszek Koltunski
   super();
855 91cfe462 Leszek Koltunski
   fillTexMappings(front,back,left,right,top,bottom);
856 80cb15ab leszek
   prepareDataStructures(cols,desc,slices);
857 d1a396b2 leszek
   build();
858 a51fe521 Leszek Koltunski
   }
859 6a06a912 Leszek Koltunski
860 665e2c45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
861
/**
862 05403bba Leszek Koltunski
 * Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors.
863 665e2c45 Leszek Koltunski
 *
864 80cb15ab leszek
 * @param cols   Number of columns, i.e. 'width' of the Mesh.
865
 * @param rows   Number of rows, i.e. 'height' of the Mesh.
866
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
867 665e2c45 Leszek Koltunski
 */
868 80cb15ab leszek
 public MeshCubes(int cols, int rows, int slices)
869 a51fe521 Leszek Koltunski
   {
870 0f10a0b6 Leszek Koltunski
   super();
871 91cfe462 Leszek Koltunski
   Static4D map = new Static4D(0.0f,0.0f,1.0f,1.0f);
872
   fillTexMappings(map,map,map,map,map,map);
873
   prepareDataStructures(cols,rows,slices);
874
   build();
875
   }
876
877
///////////////////////////////////////////////////////////////////////////////////////////////////
878
/**
879
 * Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors with
880
 * custom texture mappings.
881
 *
882
 * The mappings decide what part of the texture appears on a given side. Example:
883
 * front = (0.0,0.5,0.5,1.0) means that the front side of the grid will be textured with the
884
 * bottom-left quadrant of the texture.
885
 *
886
 * Default is: each of the 6 sides maps to the whole texture, i.e. (0.0,0.0,1.0,1.0)
887
 *
888
 * @param cols   Number of columns, i.e. 'width' of the Mesh.
889
 * @param rows   Number of rows, i.e. 'height' of the Mesh.
890
 * @param slices Number of slices, i.e. 'depth' of the Mesh.
891
 * @param front  Texture mapping the front side maps to.
892
 * @param back   Texture mapping the back side maps to.
893
 * @param left   Texture mapping the left side maps to.
894
 * @param right  Texture mapping the right side maps to.
895
 * @param top    Texture mapping the top side maps to.
896
 * @param bottom Texture mapping the bottom side maps to.
897
 */
898
 public MeshCubes(int cols, int rows, int slices, Static4D front, Static4D back, Static4D left, Static4D right, Static4D top, Static4D bottom)
899
   {
900 0f10a0b6 Leszek Koltunski
   super();
901 91cfe462 Leszek Koltunski
   fillTexMappings(front,back,left,right,top,bottom);
902 80cb15ab leszek
   prepareDataStructures(cols,rows,slices);
903 d1a396b2 leszek
   build();
904 6a06a912 Leszek Koltunski
   }
905 cbd502ec Leszek Koltunski
906
///////////////////////////////////////////////////////////////////////////////////////////////////
907
/**
908 4f81e0c8 Leszek Koltunski
 * Copy constructor.
909 cbd502ec Leszek Koltunski
 */
910 4f81e0c8 Leszek Koltunski
 public MeshCubes(MeshCubes mesh, boolean deep)
911 cbd502ec Leszek Koltunski
   {
912 4f81e0c8 Leszek Koltunski
   super(mesh,deep);
913 cbd502ec Leszek Koltunski
   }
914
915
///////////////////////////////////////////////////////////////////////////////////////////////////
916
/**
917 4f81e0c8 Leszek Koltunski
 * Copy the Mesh.
918
 *
919
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
920
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
921 aee078f8 Leszek Koltunski
 *             coordinates and effect associations, is always deep copied).
922 cbd502ec Leszek Koltunski
 */
923 4f81e0c8 Leszek Koltunski
 public MeshCubes copy(boolean deep)
924 cbd502ec Leszek Koltunski
   {
925 4f81e0c8 Leszek Koltunski
   return new MeshCubes(this,deep);
926 cbd502ec Leszek Koltunski
   }
927 a51fe521 Leszek Koltunski
 }