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