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 int[][] 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
|
private float[] mBoundingVert;
|
76
|
|
77
|
private static float[] mBoundingVert1 = new float[] { -0.5f,+0.5f, FRONTZ,
|
78
|
-0.5f,-0.5f, FRONTZ,
|
79
|
+0.5f,+0.5f, FRONTZ,
|
80
|
+0.5f,-0.5f, FRONTZ };
|
81
|
|
82
|
private static float[] mBoundingVert2 = new float[] { -0.5f,+0.5f, FRONTZ,
|
83
|
-0.5f,-0.5f, FRONTZ,
|
84
|
+0.5f,+0.5f, FRONTZ,
|
85
|
+0.5f,-0.5f, FRONTZ,
|
86
|
-0.5f,+0.5f, BACKZ ,
|
87
|
-0.5f,-0.5f, BACKZ ,
|
88
|
+0.5f,+0.5f, BACKZ ,
|
89
|
+0.5f,-0.5f, BACKZ };
|
90
|
|
91
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
// a Block is split into two triangles along the NE-SW line iff it is in the top-right
|
93
|
// or bottom-left quadrant of the grid.
|
94
|
|
95
|
private boolean isNE(int row,int col)
|
96
|
{
|
97
|
return ( (2*row<mRows)^(2*col<mCols) );
|
98
|
}
|
99
|
|
100
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101
|
// return the number of vertices our grid will contain
|
102
|
|
103
|
private int computeDataLength(boolean frontOnly)
|
104
|
{
|
105
|
int frontWalls=0, frontSegments=0, triangleShifts=0, windingShifts=0;
|
106
|
int shiftCol = (mCols-1)/2;
|
107
|
|
108
|
boolean lastBlockIsNE=false;
|
109
|
boolean thisBlockIsNE; // the block we are currently looking at is split into
|
110
|
// two triangles along the NE-SW line (rather than NW-SE)
|
111
|
for(int row=0; row<mRows; row++)
|
112
|
{
|
113
|
if( mCols>=2 && (mCubes[row][shiftCol]%2 == 1) && (mCubes[row][shiftCol+1]%2 == 1) ) triangleShifts++;
|
114
|
|
115
|
for(int col=0; col<mCols; col++)
|
116
|
{
|
117
|
if( mCubes[row][col]%2 == 1 ) // land
|
118
|
{
|
119
|
thisBlockIsNE = isNE(row,col);
|
120
|
if( thisBlockIsNE^lastBlockIsNE ) windingShifts++;
|
121
|
lastBlockIsNE = thisBlockIsNE;
|
122
|
frontWalls++;
|
123
|
if( col==mCols-1 || mCubes[row][col+1]%2 == 0 ) frontSegments++;
|
124
|
}
|
125
|
}
|
126
|
}
|
127
|
|
128
|
int frontVert = 2*( frontWalls + 2*frontSegments - 1) +2*triangleShifts + windingShifts;
|
129
|
int sideVert = 2*( mSideWalls + mSideBends + mEdgeNum -1);
|
130
|
int firstWinding= (!frontOnly && (frontVert+1)%2==1 ) ? 1:0;
|
131
|
int dataL = frontOnly ? frontVert : (frontVert+1) +firstWinding+ (1+sideVert+1) + (1+frontVert);
|
132
|
/*
|
133
|
android.util.Log.e("CUBES","triangleShifts="+triangleShifts+" windingShifts="+windingShifts+" winding1="+firstWinding+" frontVert="+frontVert+" sideVert="+sideVert);
|
134
|
android.util.Log.e("CUBES", "frontW="+frontWalls+" fSegments="+frontSegments+" sWalls="+mSideWalls+" sSegments="+mEdgeNum+" sideBends="+mSideBends+" dataLen="+dataL );
|
135
|
*/
|
136
|
return dataL<0 ? 0:dataL;
|
137
|
}
|
138
|
|
139
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
140
|
/*
|
141
|
private static String debug(short[] val)
|
142
|
{
|
143
|
String ret="";j
|
144
|
|
145
|
for(int i=0; i<val.length; i++) ret+=(" "+val[i]);
|
146
|
|
147
|
return ret;
|
148
|
}
|
149
|
*/
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
/*
|
152
|
private static String debug(float[] val, int stop)
|
153
|
{
|
154
|
String ret="";
|
155
|
|
156
|
for(int i=0; i<val.length; i++)
|
157
|
{
|
158
|
if( i%stop==0 ) ret+="\n";
|
159
|
ret+=(" "+val[i]);
|
160
|
}
|
161
|
|
162
|
return ret;
|
163
|
}
|
164
|
*/
|
165
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166
|
/*
|
167
|
private static String debug(Edge e)
|
168
|
{
|
169
|
String d = "";
|
170
|
|
171
|
switch(e.side)
|
172
|
{
|
173
|
case NORTH: d+="NORTH "; break;
|
174
|
case SOUTH: d+="SOUTH "; break;
|
175
|
case WEST : d+="WEST "; break;
|
176
|
case EAST : d+="EAST "; break;
|
177
|
}
|
178
|
|
179
|
d+=("("+e.row+","+e.col+")");
|
180
|
|
181
|
return d;
|
182
|
}
|
183
|
*/
|
184
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
185
|
|
186
|
private void prepareDataStructures(int cols, String desc, boolean frontOnly)
|
187
|
{
|
188
|
mRows =0;
|
189
|
mCols =0;
|
190
|
dataLength=0;
|
191
|
|
192
|
if( cols>0 && desc.contains("1") )
|
193
|
{
|
194
|
mCols = cols;
|
195
|
mRows = desc.length()/cols;
|
196
|
|
197
|
mCubes = new int[mRows][mCols];
|
198
|
|
199
|
for(int j=0; j<mCols; j++)
|
200
|
for(int i=0; i<mRows; i++)
|
201
|
mCubes[i][j] = (desc.charAt(i*mCols+j) == '1' ? 1:0);
|
202
|
|
203
|
buildBoundingVert(frontOnly);
|
204
|
|
205
|
//android.util.Log.d("cubes", "VERT STRING:"+debug(mBoundingVert,3));
|
206
|
|
207
|
markRegions();
|
208
|
dataLength = computeDataLength(frontOnly);
|
209
|
|
210
|
remainingVert = dataLength;
|
211
|
}
|
212
|
}
|
213
|
|
214
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
215
|
// full grid
|
216
|
|
217
|
private void prepareDataStructures(int cols, int rows, boolean frontOnly)
|
218
|
{
|
219
|
mRows =rows;
|
220
|
mCols =cols;
|
221
|
dataLength= 0;
|
222
|
|
223
|
if( cols>0 && rows>0 )
|
224
|
{
|
225
|
mCubes = new int[mRows][mCols];
|
226
|
|
227
|
for(int j=0; j<mCols; j++)
|
228
|
for(int i=0; i<mRows; i++)
|
229
|
mCubes[i][j] = 1;
|
230
|
|
231
|
mBoundingVert = frontOnly ? mBoundingVert1 : mBoundingVert2;
|
232
|
|
233
|
//android.util.Log.d("cubes", "VERT FULL:"+debug(mBoundingVert,3));
|
234
|
|
235
|
markRegions();
|
236
|
dataLength = computeDataLength(frontOnly);
|
237
|
|
238
|
remainingVert = dataLength;
|
239
|
}
|
240
|
}
|
241
|
|
242
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
243
|
|
244
|
private float retRightmost(int row)
|
245
|
{
|
246
|
if( row==0 )
|
247
|
{
|
248
|
for(int i=mCols-1; i>=0; i--)
|
249
|
if( mCubes[0][i]==1 )
|
250
|
return (float)(i+1);
|
251
|
}
|
252
|
else if(row==mRows)
|
253
|
{
|
254
|
for(int i=mCols-1; i>=0; i--)
|
255
|
if( mCubes[mRows-1][i]==1 )
|
256
|
return (float)(i+1);
|
257
|
}
|
258
|
else
|
259
|
{
|
260
|
for(int i=mCols-1; i>=0; i--)
|
261
|
if( mCubes[row][i]==1 || mCubes[row-1][i]==1 )
|
262
|
return (float)(i+1);
|
263
|
}
|
264
|
|
265
|
return 0.0f;
|
266
|
}
|
267
|
|
268
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
269
|
|
270
|
private float retLeftmost(int row)
|
271
|
{
|
272
|
if( row==0 )
|
273
|
{
|
274
|
for(int i=0; i<mCols; i++)
|
275
|
if( mCubes[0][i]==1 )
|
276
|
return (float)i;
|
277
|
}
|
278
|
else if(row==mRows)
|
279
|
{
|
280
|
for(int i=0; i<mCols; i++)
|
281
|
if( mCubes[mRows-1][i]==1 )
|
282
|
return (float)i;
|
283
|
}
|
284
|
else
|
285
|
{
|
286
|
for(int i=0; i<mCols; i++)
|
287
|
if( mCubes[row][i]==1 || mCubes[row-1][i]==1 )
|
288
|
return (float)i;
|
289
|
}
|
290
|
|
291
|
return (float)mCols;
|
292
|
}
|
293
|
|
294
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
295
|
|
296
|
private int addLeftmost(float[] temp, int row, int index)
|
297
|
{
|
298
|
float x2 = retLeftmost(row)/mCols - 0.5f;
|
299
|
float y2 = 0.5f - (float)row/mRows;
|
300
|
|
301
|
if( index>1 )
|
302
|
{
|
303
|
float x0 = temp[2*index-4];
|
304
|
float y0 = temp[2*index-3];
|
305
|
float x1 = temp[2*index-2];
|
306
|
float y1 = temp[2*index-1];
|
307
|
|
308
|
while( (x0-x2)*(y0-y1) >= (x0-x1)*(y0-y2) )
|
309
|
{
|
310
|
if( --index>1 )
|
311
|
{
|
312
|
x1 = x0;
|
313
|
y1 = y0;
|
314
|
x0 = temp[2*index-4];
|
315
|
y0 = temp[2*index-3];
|
316
|
}
|
317
|
else break;
|
318
|
}
|
319
|
}
|
320
|
|
321
|
temp[2*index ] = x2;
|
322
|
temp[2*index+1] = y2;
|
323
|
return index+1;
|
324
|
}
|
325
|
|
326
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
327
|
|
328
|
private int addRightmost(float[] temp, int row, int index)
|
329
|
{
|
330
|
float x2 = retRightmost(row)/mCols - 0.5f;
|
331
|
float y2 = 0.5f - (float)row/mRows;
|
332
|
|
333
|
if( index>1 )
|
334
|
{
|
335
|
float x0 = temp[2*index-4];
|
336
|
float y0 = temp[2*index-3];
|
337
|
float x1 = temp[2*index-2];
|
338
|
float y1 = temp[2*index-1];
|
339
|
|
340
|
while( (x0-x2)*(y0-y1) <= (x0-x1)*(y0-y2) )
|
341
|
{
|
342
|
if( --index>1 )
|
343
|
{
|
344
|
x1 = x0;
|
345
|
y1 = y0;
|
346
|
x0 = temp[2*index-4];
|
347
|
y0 = temp[2*index-3];
|
348
|
}
|
349
|
else break;
|
350
|
}
|
351
|
}
|
352
|
|
353
|
temp[2*index ] = x2;
|
354
|
temp[2*index+1] = y2;
|
355
|
return index+1;
|
356
|
}
|
357
|
|
358
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
359
|
// fill up the mBoundingVert array with the smallest set of vertices which form the same convex hull
|
360
|
// like the whole thing.
|
361
|
|
362
|
private void buildBoundingVert(boolean frontOnly)
|
363
|
{
|
364
|
int lVert=0, rVert=0;
|
365
|
int firstRow=0, lastRow=mRows-1;
|
366
|
|
367
|
for(int i=0; i<mRows; i++)
|
368
|
for(int j=0; j<mCols; j++)
|
369
|
if(mCubes[i][j] !=0 )
|
370
|
{
|
371
|
firstRow = i;
|
372
|
i=mRows;
|
373
|
j=mCols;
|
374
|
}
|
375
|
|
376
|
for(int i=mRows-1; i>=0; i--)
|
377
|
for(int j=0; j<mCols; j++)
|
378
|
if(mCubes[i][j] !=0 )
|
379
|
{
|
380
|
lastRow = i;
|
381
|
i=-1;
|
382
|
j=mCols;
|
383
|
}
|
384
|
|
385
|
//android.util.Log.d("cubes", "first :"+firstRow+" last: "+lastRow);
|
386
|
|
387
|
int numLines = lastRow-firstRow+2;
|
388
|
|
389
|
float[] tempL = new float[2*numLines];
|
390
|
float[] tempR = new float[2*numLines];
|
391
|
|
392
|
for(int i=firstRow; i<=lastRow+1; i++)
|
393
|
{
|
394
|
lVert = addLeftmost (tempL,i,lVert);
|
395
|
rVert = addRightmost(tempR,i,rVert);
|
396
|
}
|
397
|
|
398
|
//android.util.Log.d("cubes", "LEFT :"+debug(tempL,2));
|
399
|
//android.util.Log.d("cubes", "RIGHT:"+debug(tempR,2));
|
400
|
|
401
|
int numVert = lVert+rVert;
|
402
|
|
403
|
mBoundingVert = new float[ numVert*(frontOnly ? 3:6) ];
|
404
|
|
405
|
for(int i=0; i<lVert; i++)
|
406
|
{
|
407
|
mBoundingVert[3*i ] = tempL[2*i ];
|
408
|
mBoundingVert[3*i+1] = tempL[2*i+1];
|
409
|
mBoundingVert[3*i+2] = FRONTZ;
|
410
|
}
|
411
|
|
412
|
for(int i=0; i<rVert; i++)
|
413
|
{
|
414
|
mBoundingVert[3*(i+lVert) ] = tempR[2*i ];
|
415
|
mBoundingVert[3*(i+lVert)+1] = tempR[2*i+1];
|
416
|
mBoundingVert[3*(i+lVert)+2] = FRONTZ;
|
417
|
}
|
418
|
|
419
|
if( !frontOnly )
|
420
|
{
|
421
|
for(int i=0; i<numVert; i++)
|
422
|
{
|
423
|
mBoundingVert[3*(i+numVert) ] = mBoundingVert[3*i ];
|
424
|
mBoundingVert[3*(i+numVert)+1] = mBoundingVert[3*i+1];
|
425
|
mBoundingVert[3*(i+numVert)+2] = BACKZ;
|
426
|
}
|
427
|
}
|
428
|
}
|
429
|
|
430
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
431
|
// Mark all the 'regions' of our grid - i.e. separate pieces of 'land' (connected blocks that will
|
432
|
// be rendered) and 'water' (connected holes in between) with integers. Each connected block of land
|
433
|
// gets a unique odd integer, each connected block of water a unique even integer.
|
434
|
//
|
435
|
// Water on the edges of the grid is also considered connected to itself!
|
436
|
//
|
437
|
// This function also creates a list of 'Edges'. Each Edge is a data structure from which later on we
|
438
|
// will start building the side walls of each connected block of land (and sides of holes of water
|
439
|
// inside). Each Edge needs to point from Land to Water (thus the '(SOUTH,i-1,j)' below) - otherwise
|
440
|
// later on setting up normal vectors wouldn't work.
|
441
|
|
442
|
private void markRegions()
|
443
|
{
|
444
|
int i, j, numWater=1, numLand=0;
|
445
|
|
446
|
for(i=0; i<mRows;i++) if( mCubes[ i][ 0]==0 ) markRegion((short)2, i, 0);
|
447
|
for(i=0; i<mRows;i++) if( mCubes[ i][mCols-1]==0 ) markRegion((short)2, i, mCols-1);
|
448
|
for(i=0; i<mCols;i++) if( mCubes[0 ][ i]==0 ) markRegion((short)2, 0, i);
|
449
|
for(i=0; i<mCols;i++) if( mCubes[mRows-1][ i]==0 ) markRegion((short)2,mRows-1, i);
|
450
|
|
451
|
for(i=0; i<mRows; i++)
|
452
|
for(j=0; j<mCols; j++)
|
453
|
{
|
454
|
if( mCubes[i][j] == 0 ) { numWater++; markRegion( (short)(2*numWater ),i,j); mEdges.add(new Edge(SOUTH,i-1,j)); }
|
455
|
if( mCubes[i][j] == 1 ) { numLand ++; markRegion( (short)(2*numLand+1),i,j); mEdges.add(new Edge(NORTH,i ,j)); }
|
456
|
}
|
457
|
|
458
|
// now we potentially need to kick out some Edges . Otherwise the following does not work:
|
459
|
//
|
460
|
// 0 1 0
|
461
|
// 1 0 1
|
462
|
// 0 1 0
|
463
|
|
464
|
mEdgeNum= mEdges.size();
|
465
|
int initCol, initRow, initSide, lastSide;
|
466
|
Edge e1,e2;
|
467
|
|
468
|
for(i=0; i<mEdgeNum; i++)
|
469
|
{
|
470
|
e1 = mEdges.get(i);
|
471
|
initRow= e1.row;
|
472
|
initCol= e1.col;
|
473
|
initSide=e1.side;
|
474
|
|
475
|
do
|
476
|
{
|
477
|
//android.util.Log.d("CUBES", "checking edge "+debug(e1));
|
478
|
|
479
|
mSideWalls++;
|
480
|
|
481
|
if( e1.side==NORTH || e1.side==SOUTH )
|
482
|
{
|
483
|
for(j=i+1;j<mEdgeNum;j++)
|
484
|
{
|
485
|
e2 = mEdges.get(j);
|
486
|
|
487
|
if( e2.side==e1.side && e2.row==e1.row && e2.col==e1.col )
|
488
|
{
|
489
|
mEdges.remove(j);
|
490
|
mEdgeNum--;
|
491
|
j--;
|
492
|
|
493
|
//android.util.Log.e("CUBES", "removing edge "+debug(e2));
|
494
|
}
|
495
|
}
|
496
|
}
|
497
|
|
498
|
lastSide = e1.side;
|
499
|
e1 = getNextEdge(e1);
|
500
|
if( e1.side!=lastSide ) mSideBends++;
|
501
|
}
|
502
|
while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide );
|
503
|
}
|
504
|
}
|
505
|
|
506
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
507
|
// when calling, make sure that newVal != val
|
508
|
|
509
|
private void markRegion(short newVal, int row, int col)
|
510
|
{
|
511
|
int val = mCubes[row][col];
|
512
|
mCubes[row][col] = newVal;
|
513
|
|
514
|
if( row>0 && mCubes[row-1][col ]==val ) markRegion(newVal, row-1, col );
|
515
|
if( row<mRows-1 && mCubes[row+1][col ]==val ) markRegion(newVal, row+1, col );
|
516
|
if( col>0 && mCubes[row ][col-1]==val ) markRegion(newVal, row , col-1);
|
517
|
if( col<mCols-1 && mCubes[row ][col+1]==val ) markRegion(newVal, row , col+1);
|
518
|
}
|
519
|
|
520
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
521
|
|
522
|
private void createNormals(boolean front, int row, int col)
|
523
|
{
|
524
|
int td,lr;
|
525
|
|
526
|
int nw = (col>0 && row>0 ) ? (mCubes[row-1][col-1]%2) : 0;
|
527
|
int w = (col>0 ) ? (mCubes[row ][col-1]%2) : 0;
|
528
|
int n = ( row>0 ) ? (mCubes[row-1][col ]%2) : 0;
|
529
|
int c = (mCubes[row ][col ]%2);
|
530
|
int sw = (col>0 && row<mRows-1) ? (mCubes[row+1][col-1]%2) : 0;
|
531
|
int s = ( row<mRows-1) ? (mCubes[row+1][col ]%2) : 0;
|
532
|
int ne = (col<mCols-1 && row>0 ) ? (mCubes[row-1][col+1]%2) : 0;
|
533
|
int e = (col<mCols-1 ) ? (mCubes[row ][col+1]%2) : 0;
|
534
|
int se = (col<mCols-1 && row<mRows-1) ? (mCubes[row+1][col+1]%2) : 0;
|
535
|
|
536
|
if(front)
|
537
|
{
|
538
|
mNormalZ[0] = 1.0f;
|
539
|
mNormalZ[1] = 1.0f;
|
540
|
mNormalZ[2] = 1.0f;
|
541
|
mNormalZ[3] = 1.0f;
|
542
|
}
|
543
|
else
|
544
|
{
|
545
|
mNormalZ[0] =-1.0f;
|
546
|
mNormalZ[1] =-1.0f;
|
547
|
mNormalZ[2] =-1.0f;
|
548
|
mNormalZ[3] =-1.0f;
|
549
|
}
|
550
|
|
551
|
td = nw+n-w-c;
|
552
|
lr = c+n-w-nw;
|
553
|
if( td<0 ) td=-1;
|
554
|
if( td>0 ) td= 1;
|
555
|
if( lr<0 ) lr=-1;
|
556
|
if( lr>0 ) lr= 1;
|
557
|
mNormalX[0] = lr*R;
|
558
|
mNormalY[0] = td*R;
|
559
|
|
560
|
td = w+c-sw-s;
|
561
|
lr = c+s-w-sw;
|
562
|
if( td<0 ) td=-1;
|
563
|
if( td>0 ) td= 1;
|
564
|
if( lr<0 ) lr=-1;
|
565
|
if( lr>0 ) lr= 1;
|
566
|
mNormalX[1] = lr*R;
|
567
|
mNormalY[1] = td*R;
|
568
|
|
569
|
td = n+ne-c-e;
|
570
|
lr = e+ne-c-n;
|
571
|
if( td<0 ) td=-1;
|
572
|
if( td>0 ) td= 1;
|
573
|
if( lr<0 ) lr=-1;
|
574
|
if( lr>0 ) lr= 1;
|
575
|
mNormalX[2] = lr*R;
|
576
|
mNormalY[2] = td*R;
|
577
|
|
578
|
td = c+e-s-se;
|
579
|
lr = e+se-c-s;
|
580
|
if( td<0 ) td=-1;
|
581
|
if( td>0 ) td= 1;
|
582
|
if( lr<0 ) lr=-1;
|
583
|
if( lr>0 ) lr= 1;
|
584
|
mNormalX[3] = lr*R;
|
585
|
mNormalY[3] = td*R;
|
586
|
/*
|
587
|
android.util.Log.d("CUBES", "row="+row+" col="+col);
|
588
|
android.util.Log.d("CUBES", mNormalX[0]+" "+mNormalY[0]);
|
589
|
android.util.Log.d("CUBES", mNormalX[1]+" "+mNormalY[1]);
|
590
|
android.util.Log.d("CUBES", mNormalX[2]+" "+mNormalY[2]);
|
591
|
android.util.Log.d("CUBES", mNormalX[3]+" "+mNormalY[3]);
|
592
|
*/
|
593
|
}
|
594
|
|
595
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
596
|
|
597
|
private int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] position, float[] normal, float[] texture)
|
598
|
{
|
599
|
remainingVert--;
|
600
|
|
601
|
float x = (float)col/mCols;
|
602
|
float y = (float)row/mRows;
|
603
|
|
604
|
position[3*vertex ] = x-0.5f;
|
605
|
position[3*vertex+1] = 0.5f-y;
|
606
|
position[3*vertex+2] = vectZ;
|
607
|
normal[3*vertex ] = mNormalX[index];
|
608
|
normal[3*vertex+1] = mNormalY[index];
|
609
|
normal[3*vertex+2] = mNormalZ[index];
|
610
|
texture[2*vertex ] = x;
|
611
|
texture[2*vertex+1] = 1.0f-y;
|
612
|
|
613
|
return vertex+1;
|
614
|
}
|
615
|
|
616
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
617
|
|
618
|
private int buildFrontBackGrid(boolean front, int vertex, float[] position, float[] normal, float[] texture)
|
619
|
{
|
620
|
int last, current;
|
621
|
boolean seenLand=false;
|
622
|
boolean lastBlockIsNE = false;
|
623
|
boolean currentBlockIsNE;
|
624
|
float vectZ = front?FRONTZ:BACKZ;
|
625
|
|
626
|
//android.util.Log.d("CUBES", "buildFrontBack");
|
627
|
|
628
|
for(int row=0; row<mRows; row++)
|
629
|
{
|
630
|
last =0;
|
631
|
|
632
|
for(int col=0; col<mCols; col++)
|
633
|
{
|
634
|
current = mCubes[row][col];
|
635
|
|
636
|
if( current%2 == 1 )
|
637
|
{
|
638
|
currentBlockIsNE = isNE(row,col);
|
639
|
|
640
|
if( !seenLand && !front && ((vertex%2==1)^currentBlockIsNE) )
|
641
|
{
|
642
|
//android.util.Log.d("CUBES","repeating winding2 vertex");
|
643
|
|
644
|
vertex = repeatLast(vertex,position,normal,texture);
|
645
|
}
|
646
|
|
647
|
createNormals(front,row,col);
|
648
|
|
649
|
if( currentBlockIsNE )
|
650
|
{
|
651
|
if( (last!=current) || !lastBlockIsNE )
|
652
|
{
|
653
|
if( seenLand && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
|
654
|
vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
|
655
|
if( seenLand && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
|
656
|
if( !lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
|
657
|
vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
|
658
|
}
|
659
|
vertex= addFrontVertex( vertex, 2, vectZ, col+1, row, position, normal, texture);
|
660
|
vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
|
661
|
}
|
662
|
else
|
663
|
{
|
664
|
if( (last!=current) || lastBlockIsNE )
|
665
|
{
|
666
|
if( seenLand && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
|
667
|
vertex= addFrontVertex( vertex, 1, vectZ, col, row+1, position, normal, texture);
|
668
|
if( seenLand && (last != current) ) vertex = repeatLast(vertex,position,normal,texture);
|
669
|
if( lastBlockIsNE || (!front && !seenLand) ) vertex = repeatLast(vertex,position,normal,texture);
|
670
|
vertex= addFrontVertex( vertex, 0, vectZ, col, row, position, normal, texture);
|
671
|
}
|
672
|
vertex= addFrontVertex( vertex, 3, vectZ, col+1, row+1, position, normal, texture);
|
673
|
vertex= addFrontVertex( vertex, 2, vectZ, col+1, row , position, normal, texture);
|
674
|
}
|
675
|
|
676
|
seenLand = true;
|
677
|
lastBlockIsNE = currentBlockIsNE;
|
678
|
}
|
679
|
|
680
|
last = current;
|
681
|
}
|
682
|
}
|
683
|
|
684
|
return vertex;
|
685
|
}
|
686
|
|
687
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
688
|
|
689
|
private int repeatLast(int vertex, float[] position, float[] normal, float[] texture)
|
690
|
{
|
691
|
//android.util.Log.e("CUBES", "repeating last vertex!");
|
692
|
|
693
|
if( vertex>0 )
|
694
|
{
|
695
|
remainingVert--;
|
696
|
|
697
|
position[3*vertex ] = position[3*vertex-3];
|
698
|
position[3*vertex+1] = position[3*vertex-2];
|
699
|
position[3*vertex+2] = position[3*vertex-1];
|
700
|
|
701
|
normal[3*vertex ] = normal[3*vertex-3];
|
702
|
normal[3*vertex+1] = normal[3*vertex-2];
|
703
|
normal[3*vertex+2] = normal[3*vertex-1];
|
704
|
|
705
|
texture[2*vertex ] = texture[2*vertex-2];
|
706
|
texture[2*vertex+1] = texture[2*vertex-1];
|
707
|
|
708
|
vertex++;
|
709
|
}
|
710
|
|
711
|
return vertex;
|
712
|
}
|
713
|
|
714
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
715
|
|
716
|
private int buildSideGrid(int vertex, float[] position, float[] normal, float[] texture)
|
717
|
{
|
718
|
//android.util.Log.d("CUBES", "buildSide");
|
719
|
|
720
|
for(int i=0; i<mEdgeNum; i++)
|
721
|
{
|
722
|
vertex = buildIthSide(mEdges.get(i), vertex, position, normal, texture);
|
723
|
}
|
724
|
|
725
|
return vertex;
|
726
|
}
|
727
|
|
728
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
729
|
|
730
|
private int buildIthSide(Edge curr, int vertex, float[] position, float[] normal, float[] texture)
|
731
|
{
|
732
|
Edge prev;
|
733
|
|
734
|
if( curr.side==NORTH ) // water outside
|
735
|
{
|
736
|
prev = new Edge(WEST,curr.row,curr.col);
|
737
|
}
|
738
|
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.
|
739
|
{
|
740
|
prev = curr;
|
741
|
curr = new Edge(EAST,curr.row+1,curr.col-1);
|
742
|
}
|
743
|
|
744
|
int col = curr.col;
|
745
|
int row = curr.row;
|
746
|
int side= curr.side;
|
747
|
Edge next = getNextEdge(curr);
|
748
|
|
749
|
addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
|
750
|
vertex++;
|
751
|
|
752
|
do
|
753
|
{
|
754
|
if( prev.side!=curr.side )
|
755
|
{
|
756
|
addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
|
757
|
vertex++;
|
758
|
addSideVertex(curr,BACK,UPPER,prev.side,vertex,position,normal,texture);
|
759
|
vertex++;
|
760
|
}
|
761
|
|
762
|
addSideVertex(curr,FRONT,LOWER,next.side,vertex,position,normal,texture);
|
763
|
vertex++;
|
764
|
addSideVertex(curr,FRONT,UPPER,next.side,vertex,position,normal,texture);
|
765
|
vertex++;
|
766
|
|
767
|
prev = curr;
|
768
|
curr = next;
|
769
|
next = getNextEdge(curr);
|
770
|
}
|
771
|
while( curr.col!=col || curr.row!=row || curr.side!=side );
|
772
|
|
773
|
vertex = repeatLast(vertex,position,normal,texture);
|
774
|
|
775
|
return vertex;
|
776
|
}
|
777
|
|
778
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
779
|
|
780
|
private Edge getNextEdge(Edge curr)
|
781
|
{
|
782
|
int col = curr.col;
|
783
|
int row = curr.row;
|
784
|
|
785
|
//android.util.Log.e("CUBES", "row="+row+" col="+col+" mRows="+mRows+" mCols="+mCols);
|
786
|
|
787
|
switch(curr.side)
|
788
|
{
|
789
|
case NORTH: if( col==mCols-1 )
|
790
|
return new Edge(EAST,row,col);
|
791
|
if( row>0 && mCubes[row-1][col+1]==mCubes[row][col] )
|
792
|
return new Edge(WEST,row-1,col+1);
|
793
|
if( mCubes[row][col+1]==mCubes[row][col] )
|
794
|
return new Edge(NORTH,row,col+1);
|
795
|
else
|
796
|
return new Edge(EAST,row,col);
|
797
|
|
798
|
case SOUTH: if( col==0 )
|
799
|
return new Edge(WEST,row,col);
|
800
|
if( (row<mRows-1) && mCubes[row+1][col-1]==mCubes[row][col] )
|
801
|
return new Edge(EAST,row+1,col-1);
|
802
|
if( mCubes[row][col-1]==mCubes[row][col] )
|
803
|
return new Edge(SOUTH,row,col-1);
|
804
|
else
|
805
|
return new Edge(WEST,row,col);
|
806
|
|
807
|
case EAST : if( row==mRows-1 )
|
808
|
return new Edge(SOUTH,row,col);
|
809
|
if( (col<mCols-1) && mCubes[row+1][col+1]==mCubes[row][col] )
|
810
|
return new Edge(NORTH,row+1,col+1);
|
811
|
if( mCubes[row+1][col]==mCubes[row][col] )
|
812
|
return new Edge(EAST,row+1,col);
|
813
|
else
|
814
|
return new Edge(SOUTH,row,col);
|
815
|
|
816
|
default : if( row==0 )
|
817
|
return new Edge(NORTH,row,col);
|
818
|
if( col>0 && mCubes[row-1][col-1]==mCubes[row][col] )
|
819
|
return new Edge(SOUTH,row-1,col-1);
|
820
|
if( mCubes[row-1][col]==mCubes[row][col] )
|
821
|
return new Edge(WEST,row-1,col);
|
822
|
else
|
823
|
return new Edge(NORTH,row,col);
|
824
|
}
|
825
|
}
|
826
|
|
827
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
828
|
|
829
|
private void addSideVertex(Edge curr, boolean back, boolean lower,int side, int vertex, float[] position, float[] normal, float[] texture)
|
830
|
{
|
831
|
//android.util.Log.e("CUBES", "adding Side vertex!");
|
832
|
|
833
|
remainingVert--;
|
834
|
|
835
|
float x, y;
|
836
|
|
837
|
switch(curr.side)
|
838
|
{
|
839
|
case NORTH: x = (float)(back ? (curr.col ):(curr.col+1))/mCols;
|
840
|
|
841
|
position[3*vertex ] = x - 0.5f;
|
842
|
position[3*vertex+1] = 0.5f - (float)curr.row/mRows;
|
843
|
position[3*vertex+2] = lower ? BACKZ : FRONTZ;
|
844
|
|
845
|
normal[3*vertex ] = side==NORTH ? 0.0f : (side==WEST?-R:R);
|
846
|
normal[3*vertex+1] = 1.0f;
|
847
|
normal[3*vertex+2] = lower ? -R:R;
|
848
|
|
849
|
texture[2*vertex ] = x;
|
850
|
texture[2*vertex+1] = 1.0f-(float)(lower? (curr.row-1):(curr.row ))/mRows;
|
851
|
break;
|
852
|
case SOUTH: x = (float)(back ? (curr.col+1):(curr.col ))/mCols;
|
853
|
|
854
|
position[3*vertex ] = x - 0.5f;
|
855
|
position[3*vertex+1] = 0.5f - (float)(curr.row+1)/mRows;
|
856
|
position[3*vertex+2] = lower ? BACKZ : FRONTZ;
|
857
|
|
858
|
normal[3*vertex ] = side==SOUTH ? 0.0f: (side==EAST?-R:R);
|
859
|
normal[3*vertex+1] =-1.0f;
|
860
|
normal[3*vertex+2] = lower ? -R:R;
|
861
|
|
862
|
texture[2*vertex ] = x;
|
863
|
texture[2*vertex+1] = 1.0f-(float)(lower? (curr.row+2):(curr.row+1))/mRows;
|
864
|
break;
|
865
|
case WEST : y = (float)(back ? (curr.row+1):(curr.row))/mRows;
|
866
|
|
867
|
position[3*vertex ] = (float)curr.col/mCols -0.5f;
|
868
|
position[3*vertex+1] = 0.5f - y;
|
869
|
position[3*vertex+2] = lower ? BACKZ : FRONTZ;
|
870
|
|
871
|
normal[3*vertex ] =-1.0f;
|
872
|
normal[3*vertex+1] = side==WEST ? 0.0f : (side==NORTH?-R:R);
|
873
|
normal[3*vertex+2] = lower ? -R:R;
|
874
|
|
875
|
texture[2*vertex ] = (float)(lower ? (curr.col-1):(curr.col ))/mCols;
|
876
|
texture[2*vertex+1] = 1.0f - y;
|
877
|
break;
|
878
|
case EAST : y = (float)(back ? (curr.row):(curr.row+1))/mRows;
|
879
|
|
880
|
position[3*vertex ] = (float)(curr.col+1)/mCols -0.5f;
|
881
|
position[3*vertex+1] = 0.5f - y;
|
882
|
position[3*vertex+2] = lower ? BACKZ : FRONTZ;
|
883
|
|
884
|
normal[3*vertex ] = 1.0f;
|
885
|
normal[3*vertex+1] = side==EAST ? 0.0f : (side==SOUTH?-R:R);
|
886
|
normal[3*vertex+2] = lower ? -R:R;
|
887
|
|
888
|
texture[2*vertex ] = (float)(lower ? (curr.col+2):(curr.col+1))/mCols;
|
889
|
texture[2*vertex+1] = 1.0f - y;
|
890
|
break;
|
891
|
}
|
892
|
|
893
|
if(texture[2*vertex ]>1.0f) texture[2*vertex ] =2.0f-texture[2*vertex ];
|
894
|
if(texture[2*vertex ]<0.0f) texture[2*vertex ] = -texture[2*vertex ];
|
895
|
if(texture[2*vertex+1]>1.0f) texture[2*vertex+1] =2.0f-texture[2*vertex+1];
|
896
|
if(texture[2*vertex+1]<0.0f) texture[2*vertex+1] = -texture[2*vertex+1];
|
897
|
}
|
898
|
|
899
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
900
|
|
901
|
private void build(boolean frontOnly)
|
902
|
{
|
903
|
int numVertices=0;
|
904
|
float[] positionData= new float[POSITION_DATA_SIZE*dataLength];
|
905
|
float[] normalData = new float[NORMAL_DATA_SIZE *dataLength];
|
906
|
float[] textureData = new float[TEX_DATA_SIZE *dataLength];
|
907
|
|
908
|
//android.util.Log.d("CUBES","building front grid...");
|
909
|
|
910
|
numVertices = buildFrontBackGrid(true, numVertices,positionData,normalData,textureData);
|
911
|
|
912
|
if( !frontOnly )
|
913
|
{
|
914
|
numVertices = repeatLast(numVertices,positionData,normalData,textureData);
|
915
|
if( numVertices%2==1 )
|
916
|
{
|
917
|
//android.util.Log.d("CUBES","repeating winding1 vertex");
|
918
|
|
919
|
numVertices = repeatLast(numVertices,positionData,normalData,textureData);
|
920
|
}
|
921
|
|
922
|
//android.util.Log.d("CUBES","building side grid...");
|
923
|
|
924
|
numVertices = buildSideGrid (numVertices,positionData,normalData,textureData);
|
925
|
|
926
|
//android.util.Log.d("CUBES","building back grid...");
|
927
|
|
928
|
numVertices = buildFrontBackGrid (false,numVertices,positionData,normalData,textureData);
|
929
|
}
|
930
|
|
931
|
/*
|
932
|
android.util.Log.e("CUBES","dataLen="+dataLength+" vertex="+numVertices);
|
933
|
android.util.Log.d("CUBES", "position: "+debug(positionData,3) );
|
934
|
android.util.Log.d("CUBES", "normal: " +debug( normalData,3) );
|
935
|
android.util.Log.d("CUBES", "texture: " +debug( textureData,2) );
|
936
|
*/
|
937
|
|
938
|
mEdges.clear();
|
939
|
mEdges = null;
|
940
|
mCubes = null;
|
941
|
|
942
|
if( remainingVert!=0 )
|
943
|
android.util.Log.d("CUBES", "remainingVert " +remainingVert );
|
944
|
|
945
|
mMeshPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
946
|
mMeshPositions.put(positionData).position(0);
|
947
|
|
948
|
mMeshNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
949
|
mMeshNormals.put(normalData).position(0);
|
950
|
|
951
|
mMeshTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
952
|
mMeshTexture.put(textureData).position(0);
|
953
|
}
|
954
|
|
955
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
956
|
|
957
|
float[] getBoundingVertices()
|
958
|
{
|
959
|
return mBoundingVert;
|
960
|
}
|
961
|
|
962
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
963
|
// PUBLIC API
|
964
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
965
|
/**
|
966
|
* Creates the underlying mesh of vertices, normals, texture coords.
|
967
|
*
|
968
|
* @param cols Integer helping to parse the next parameter.
|
969
|
* @param desc String describing the subset of a MxNx1 cuboid that we want to create.
|
970
|
* Its MxN characters - all 0 or 1 - decide of appropriate field is taken or not.
|
971
|
* <p></p>
|
972
|
* <p>
|
973
|
* <pre>
|
974
|
* For example, (cols=2, desc="111010") describes the following shape:
|
975
|
*
|
976
|
* XX
|
977
|
* X
|
978
|
* X
|
979
|
*
|
980
|
* whereas (cols=2,desc="110001") describes
|
981
|
*
|
982
|
* XX
|
983
|
*
|
984
|
* X
|
985
|
* </pre>
|
986
|
* </p>
|
987
|
* @param frontOnly Only create the front wall or side and back as well?
|
988
|
*/
|
989
|
public MeshCubes(int cols, String desc, boolean frontOnly)
|
990
|
{
|
991
|
super(frontOnly ? 0.0f:1.0f/cols);
|
992
|
prepareDataStructures(cols,desc,frontOnly);
|
993
|
build(frontOnly);
|
994
|
}
|
995
|
|
996
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
997
|
/**
|
998
|
* Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors.
|
999
|
*
|
1000
|
* @param cols Number of columns.
|
1001
|
* @param rows Number of rows.
|
1002
|
* @param frontOnly Only create the front wall or side and back as well?
|
1003
|
*/
|
1004
|
public MeshCubes(int cols, int rows, boolean frontOnly)
|
1005
|
{
|
1006
|
super(frontOnly ? 0.0f:1.0f/cols);
|
1007
|
prepareDataStructures(cols,rows,frontOnly);
|
1008
|
build(frontOnly);
|
1009
|
}
|
1010
|
}
|
1011
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1012
|
|