Project

General

Profile

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

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

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