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