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