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