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 |
d6994cc6
|
Leszek Koltunski
|
private int[][] 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 |
7d755851
|
Leszek Koltunski
|
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 |
69ed1eb4
|
Leszek Koltunski
|
|
82 |
7d755851
|
Leszek Koltunski
|
private static float[] mBoundingVert2 = new float[] { -0.5f,+0.5f, FRONTZ,
|
83 |
|
|
-0.5f,-0.5f, FRONTZ,
|
84 |
69ed1eb4
|
Leszek Koltunski
|
+0.5f,+0.5f, FRONTZ,
|
85 |
7d755851
|
Leszek Koltunski
|
+0.5f,-0.5f, FRONTZ,
|
86 |
69ed1eb4
|
Leszek Koltunski
|
-0.5f,+0.5f, BACKZ ,
|
87 |
7d755851
|
Leszek Koltunski
|
-0.5f,-0.5f, BACKZ ,
|
88 |
|
|
+0.5f,+0.5f, BACKZ ,
|
89 |
|
|
+0.5f,-0.5f, BACKZ };
|
90 |
69ed1eb4
|
Leszek Koltunski
|
|
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 |
d6994cc6
|
Leszek Koltunski
|
*/
|
165 |
6a06a912
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
d6994cc6
|
Leszek Koltunski
|
mCubes = new int[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 |
d6994cc6
|
Leszek Koltunski
|
mCubes[i][j] = (desc.charAt(i*mCols+j) == '1' ? 1:0);
|
202 |
|
|
|
203 |
|
|
buildBoundingVert(frontOnly);
|
204 |
|
|
|
205 |
7d755851
|
Leszek Koltunski
|
//android.util.Log.d("cubes", "VERT STRING:"+debug(mBoundingVert,3));
|
206 |
|
|
|
207 |
0729bc41
|
Leszek Koltunski
|
markRegions();
|
208 |
|
|
dataLength = computeDataLength(frontOnly);
|
209 |
84ee2a6a
|
Leszek Koltunski
|
|
210 |
|
|
remainingVert = dataLength;
|
211 |
6a06a912
|
Leszek Koltunski
|
}
|
212 |
|
|
}
|
213 |
665e2c45
|
Leszek Koltunski
|
|
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 |
d6994cc6
|
Leszek Koltunski
|
mCubes = new int[mRows][mCols];
|
226 |
665e2c45
|
Leszek Koltunski
|
|
227 |
|
|
for(int j=0; j<mCols; j++)
|
228 |
|
|
for(int i=0; i<mRows; i++)
|
229 |
d6994cc6
|
Leszek Koltunski
|
mCubes[i][j] = 1;
|
230 |
665e2c45
|
Leszek Koltunski
|
|
231 |
7d755851
|
Leszek Koltunski
|
mBoundingVert = frontOnly ? mBoundingVert1 : mBoundingVert2;
|
232 |
|
|
|
233 |
|
|
//android.util.Log.d("cubes", "VERT FULL:"+debug(mBoundingVert,3));
|
234 |
|
|
|
235 |
665e2c45
|
Leszek Koltunski
|
markRegions();
|
236 |
|
|
dataLength = computeDataLength(frontOnly);
|
237 |
|
|
|
238 |
|
|
remainingVert = dataLength;
|
239 |
|
|
}
|
240 |
|
|
}
|
241 |
|
|
|
242 |
69ed1eb4
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
243 |
|
|
|
244 |
d6994cc6
|
Leszek Koltunski
|
private float retRightmost(int row)
|
245 |
69ed1eb4
|
Leszek Koltunski
|
{
|
246 |
d6994cc6
|
Leszek Koltunski
|
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 |
69ed1eb4
|
Leszek Koltunski
|
|
265 |
|
|
return 0.0f;
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
269 |
|
|
|
270 |
d6994cc6
|
Leszek Koltunski
|
private float retLeftmost(int row)
|
271 |
69ed1eb4
|
Leszek Koltunski
|
{
|
272 |
|
|
if( row==0 )
|
273 |
|
|
{
|
274 |
|
|
for(int i=0; i<mCols; i++)
|
275 |
d6994cc6
|
Leszek Koltunski
|
if( mCubes[0][i]==1 )
|
276 |
|
|
return (float)i;
|
277 |
69ed1eb4
|
Leszek Koltunski
|
}
|
278 |
|
|
else if(row==mRows)
|
279 |
|
|
{
|
280 |
|
|
for(int i=0; i<mCols; i++)
|
281 |
d6994cc6
|
Leszek Koltunski
|
if( mCubes[mRows-1][i]==1 )
|
282 |
|
|
return (float)i;
|
283 |
69ed1eb4
|
Leszek Koltunski
|
}
|
284 |
|
|
else
|
285 |
|
|
{
|
286 |
|
|
for(int i=0; i<mCols; i++)
|
287 |
d6994cc6
|
Leszek Koltunski
|
if( mCubes[row][i]==1 || mCubes[row-1][i]==1 )
|
288 |
|
|
return (float)i;
|
289 |
69ed1eb4
|
Leszek Koltunski
|
}
|
290 |
|
|
|
291 |
d6994cc6
|
Leszek Koltunski
|
return (float)mCols;
|
292 |
69ed1eb4
|
Leszek Koltunski
|
}
|
293 |
|
|
|
294 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
295 |
|
|
|
296 |
d6994cc6
|
Leszek Koltunski
|
private int addLeftmost(float[] temp, int row, int index)
|
297 |
69ed1eb4
|
Leszek Koltunski
|
{
|
298 |
d6994cc6
|
Leszek Koltunski
|
float x2 = retLeftmost(row)/mCols - 0.5f;
|
299 |
e8b2f311
|
Leszek Koltunski
|
float y2 = 0.5f - (float)row/mRows;
|
300 |
69ed1eb4
|
Leszek Koltunski
|
|
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 |
e8b2f311
|
Leszek Koltunski
|
while( (x0-x2)*(y0-y1) >= (x0-x1)*(y0-y2) )
|
309 |
69ed1eb4
|
Leszek Koltunski
|
{
|
310 |
d6994cc6
|
Leszek Koltunski
|
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 |
69ed1eb4
|
Leszek Koltunski
|
}
|
319 |
|
|
}
|
320 |
|
|
|
321 |
d6994cc6
|
Leszek Koltunski
|
temp[2*index ] = x2;
|
322 |
69ed1eb4
|
Leszek Koltunski
|
temp[2*index+1] = y2;
|
323 |
|
|
return index+1;
|
324 |
|
|
}
|
325 |
|
|
|
326 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
327 |
|
|
|
328 |
d6994cc6
|
Leszek Koltunski
|
private int addRightmost(float[] temp, int row, int index)
|
329 |
69ed1eb4
|
Leszek Koltunski
|
{
|
330 |
d6994cc6
|
Leszek Koltunski
|
float x2 = retRightmost(row)/mCols - 0.5f;
|
331 |
e8b2f311
|
Leszek Koltunski
|
float y2 = 0.5f - (float)row/mRows;
|
332 |
69ed1eb4
|
Leszek Koltunski
|
|
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 |
e8b2f311
|
Leszek Koltunski
|
while( (x0-x2)*(y0-y1) <= (x0-x1)*(y0-y2) )
|
341 |
69ed1eb4
|
Leszek Koltunski
|
{
|
342 |
d6994cc6
|
Leszek Koltunski
|
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 |
69ed1eb4
|
Leszek Koltunski
|
}
|
351 |
|
|
}
|
352 |
|
|
|
353 |
d6994cc6
|
Leszek Koltunski
|
temp[2*index ] = x2;
|
354 |
69ed1eb4
|
Leszek Koltunski
|
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 |
d6994cc6
|
Leszek Koltunski
|
private void buildBoundingVert(boolean frontOnly)
|
363 |
69ed1eb4
|
Leszek Koltunski
|
{
|
364 |
|
|
int lVert=0, rVert=0;
|
365 |
d6994cc6
|
Leszek Koltunski
|
int firstRow=0, lastRow=mRows-1;
|
366 |
69ed1eb4
|
Leszek Koltunski
|
|
367 |
d6994cc6
|
Leszek Koltunski
|
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 |
69ed1eb4
|
Leszek Koltunski
|
{
|
394 |
d6994cc6
|
Leszek Koltunski
|
lVert = addLeftmost (tempL,i,lVert);
|
395 |
|
|
rVert = addRightmost(tempR,i,rVert);
|
396 |
69ed1eb4
|
Leszek Koltunski
|
}
|
397 |
|
|
|
398 |
d6994cc6
|
Leszek Koltunski
|
//android.util.Log.d("cubes", "LEFT :"+debug(tempL,2));
|
399 |
|
|
//android.util.Log.d("cubes", "RIGHT:"+debug(tempR,2));
|
400 |
|
|
|
401 |
69ed1eb4
|
Leszek Koltunski
|
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 |
6a06a912
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
2e96ee72
|
Leszek Koltunski
|
// will start building the side walls of each connected block of land (and sides of holes of water
|
439 |
8d9da98a
|
Leszek Koltunski
|
// 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 |
6a06a912
|
Leszek Koltunski
|
|
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 |
8d9da98a
|
Leszek Koltunski
|
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 |
6a06a912
|
Leszek Koltunski
|
}
|
457 |
|
|
|
458 |
8d9da98a
|
Leszek Koltunski
|
// now we potentially need to kick out some Edges . Otherwise the following does not work:
|
459 |
6a06a912
|
Leszek Koltunski
|
//
|
460 |
|
|
// 0 1 0
|
461 |
|
|
// 1 0 1
|
462 |
|
|
// 0 1 0
|
463 |
|
|
|
464 |
8d9da98a
|
Leszek Koltunski
|
mEdgeNum= mEdges.size();
|
465 |
|
|
int initCol, initRow, initSide, lastSide;
|
466 |
|
|
Edge e1,e2;
|
467 |
6a06a912
|
Leszek Koltunski
|
|
468 |
8d9da98a
|
Leszek Koltunski
|
for(i=0; i<mEdgeNum; i++)
|
469 |
6a06a912
|
Leszek Koltunski
|
{
|
470 |
8d9da98a
|
Leszek Koltunski
|
e1 = mEdges.get(i);
|
471 |
|
|
initRow= e1.row;
|
472 |
|
|
initCol= e1.col;
|
473 |
|
|
initSide=e1.side;
|
474 |
|
|
|
475 |
|
|
do
|
476 |
6a06a912
|
Leszek Koltunski
|
{
|
477 |
8d9da98a
|
Leszek Koltunski
|
//android.util.Log.d("CUBES", "checking edge "+debug(e1));
|
478 |
|
|
|
479 |
b62632fc
|
Leszek Koltunski
|
mSideWalls++;
|
480 |
|
|
|
481 |
8d9da98a
|
Leszek Koltunski
|
if( e1.side==NORTH || e1.side==SOUTH )
|
482 |
6a06a912
|
Leszek Koltunski
|
{
|
483 |
8d9da98a
|
Leszek Koltunski
|
for(j=i+1;j<mEdgeNum;j++)
|
484 |
6a06a912
|
Leszek Koltunski
|
{
|
485 |
8d9da98a
|
Leszek Koltunski
|
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 |
6a06a912
|
Leszek Koltunski
|
}
|
496 |
|
|
}
|
497 |
8d9da98a
|
Leszek Koltunski
|
|
498 |
|
|
lastSide = e1.side;
|
499 |
|
|
e1 = getNextEdge(e1);
|
500 |
|
|
if( e1.side!=lastSide ) mSideBends++;
|
501 |
6a06a912
|
Leszek Koltunski
|
}
|
502 |
8d9da98a
|
Leszek Koltunski
|
while( e1.col!=initCol || e1.row!=initRow || e1.side!=initSide );
|
503 |
6a06a912
|
Leszek Koltunski
|
}
|
504 |
|
|
}
|
505 |
|
|
|
506 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
507 |
|
|
// when calling, make sure that newVal != val
|
508 |
|
|
|
509 |
|
|
private void markRegion(short newVal, int row, int col)
|
510 |
|
|
{
|
511 |
d6994cc6
|
Leszek Koltunski
|
int val = mCubes[row][col];
|
512 |
6a06a912
|
Leszek Koltunski
|
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 |
ce7f3833
|
Leszek Koltunski
|
private void createNormals(boolean front, int row, int col)
|
523 |
6a06a912
|
Leszek Koltunski
|
{
|
524 |
|
|
int td,lr;
|
525 |
|
|
|
526 |
2e96ee72
|
Leszek Koltunski
|
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 |
6a06a912
|
Leszek Koltunski
|
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 |
ce7f3833
|
Leszek Koltunski
|
|
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 |
6a06a912
|
Leszek Koltunski
|
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 |
2e96ee72
|
Leszek Koltunski
|
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 |
6a06a912
|
Leszek Koltunski
|
*/
|
593 |
|
|
}
|
594 |
|
|
|
595 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
596 |
ce7f3833
|
Leszek Koltunski
|
|
597 |
e5d9b235
|
Leszek Koltunski
|
private int addFrontVertex(int vertex, int index, float vectZ, int col, int row, float[] position, float[] normal, float[] texture)
|
598 |
ce7f3833
|
Leszek Koltunski
|
{
|
599 |
84ee2a6a
|
Leszek Koltunski
|
remainingVert--;
|
600 |
15873844
|
Leszek Koltunski
|
|
601 |
e5d9b235
|
Leszek Koltunski
|
float x = (float)col/mCols;
|
602 |
|
|
float y = (float)row/mRows;
|
603 |
ce7f3833
|
Leszek Koltunski
|
|
604 |
15873844
|
Leszek Koltunski
|
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 |
985ea9c5
|
Leszek Koltunski
|
texture[2*vertex+1] = 1.0f-y;
|
612 |
15873844
|
Leszek Koltunski
|
|
613 |
|
|
return vertex+1;
|
614 |
ce7f3833
|
Leszek Koltunski
|
}
|
615 |
|
|
|
616 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
617 |
|
|
|
618 |
6a06a912
|
Leszek Koltunski
|
private int buildFrontBackGrid(boolean front, int vertex, float[] position, float[] normal, float[] texture)
|
619 |
|
|
{
|
620 |
d6994cc6
|
Leszek Koltunski
|
int last, current;
|
621 |
ce7f3833
|
Leszek Koltunski
|
boolean seenLand=false;
|
622 |
|
|
boolean lastBlockIsNE = false;
|
623 |
|
|
boolean currentBlockIsNE;
|
624 |
|
|
float vectZ = front?FRONTZ:BACKZ;
|
625 |
|
|
|
626 |
84ee2a6a
|
Leszek Koltunski
|
//android.util.Log.d("CUBES", "buildFrontBack");
|
627 |
|
|
|
628 |
e5d9b235
|
Leszek Koltunski
|
for(int row=0; row<mRows; row++)
|
629 |
6a06a912
|
Leszek Koltunski
|
{
|
630 |
|
|
last =0;
|
631 |
|
|
|
632 |
e5d9b235
|
Leszek Koltunski
|
for(int col=0; col<mCols; col++)
|
633 |
6a06a912
|
Leszek Koltunski
|
{
|
634 |
e5d9b235
|
Leszek Koltunski
|
current = mCubes[row][col];
|
635 |
ce7f3833
|
Leszek Koltunski
|
|
636 |
6a06a912
|
Leszek Koltunski
|
if( current%2 == 1 )
|
637 |
|
|
{
|
638 |
e5d9b235
|
Leszek Koltunski
|
currentBlockIsNE = isNE(row,col);
|
639 |
84ee2a6a
|
Leszek Koltunski
|
|
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 |
e5d9b235
|
Leszek Koltunski
|
createNormals(front,row,col);
|
648 |
ce7f3833
|
Leszek Koltunski
|
|
649 |
e5d9b235
|
Leszek Koltunski
|
if( currentBlockIsNE )
|
650 |
6a06a912
|
Leszek Koltunski
|
{
|
651 |
e5d9b235
|
Leszek Koltunski
|
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 |
6a06a912
|
Leszek Koltunski
|
}
|
675 |
ce7f3833
|
Leszek Koltunski
|
|
676 |
|
|
seenLand = true;
|
677 |
|
|
lastBlockIsNE = currentBlockIsNE;
|
678 |
6a06a912
|
Leszek Koltunski
|
}
|
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 |
84ee2a6a
|
Leszek Koltunski
|
//android.util.Log.e("CUBES", "repeating last vertex!");
|
692 |
|
|
|
693 |
6a06a912
|
Leszek Koltunski
|
if( vertex>0 )
|
694 |
|
|
{
|
695 |
e5d9b235
|
Leszek Koltunski
|
remainingVert--;
|
696 |
|
|
|
697 |
39cbf9dc
|
Leszek Koltunski
|
position[3*vertex ] = position[3*vertex-3];
|
698 |
6a06a912
|
Leszek Koltunski
|
position[3*vertex+1] = position[3*vertex-2];
|
699 |
|
|
position[3*vertex+2] = position[3*vertex-1];
|
700 |
|
|
|
701 |
39cbf9dc
|
Leszek Koltunski
|
normal[3*vertex ] = normal[3*vertex-3];
|
702 |
6a06a912
|
Leszek Koltunski
|
normal[3*vertex+1] = normal[3*vertex-2];
|
703 |
|
|
normal[3*vertex+2] = normal[3*vertex-1];
|
704 |
|
|
|
705 |
39cbf9dc
|
Leszek Koltunski
|
texture[2*vertex ] = texture[2*vertex-2];
|
706 |
6a06a912
|
Leszek Koltunski
|
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 |
84ee2a6a
|
Leszek Koltunski
|
//android.util.Log.d("CUBES", "buildSide");
|
719 |
ce7f3833
|
Leszek Koltunski
|
|
720 |
8d9da98a
|
Leszek Koltunski
|
for(int i=0; i<mEdgeNum; i++)
|
721 |
6a06a912
|
Leszek Koltunski
|
{
|
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 |
84ee2a6a
|
Leszek Koltunski
|
addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
|
750 |
6a06a912
|
Leszek Koltunski
|
vertex++;
|
751 |
|
|
|
752 |
|
|
do
|
753 |
|
|
{
|
754 |
|
|
if( prev.side!=curr.side )
|
755 |
|
|
{
|
756 |
84ee2a6a
|
Leszek Koltunski
|
addSideVertex(curr,BACK,LOWER,prev.side,vertex,position,normal,texture);
|
757 |
6a06a912
|
Leszek Koltunski
|
vertex++;
|
758 |
84ee2a6a
|
Leszek Koltunski
|
addSideVertex(curr,BACK,UPPER,prev.side,vertex,position,normal,texture);
|
759 |
6a06a912
|
Leszek Koltunski
|
vertex++;
|
760 |
|
|
}
|
761 |
|
|
|
762 |
84ee2a6a
|
Leszek Koltunski
|
addSideVertex(curr,FRONT,LOWER,next.side,vertex,position,normal,texture);
|
763 |
6a06a912
|
Leszek Koltunski
|
vertex++;
|
764 |
84ee2a6a
|
Leszek Koltunski
|
addSideVertex(curr,FRONT,UPPER,next.side,vertex,position,normal,texture);
|
765 |
6a06a912
|
Leszek Koltunski
|
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 |
8d9da98a
|
Leszek Koltunski
|
default : if( row==0 )
|
817 |
6a06a912
|
Leszek Koltunski
|
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 |
84ee2a6a
|
Leszek Koltunski
|
private void addSideVertex(Edge curr, boolean back, boolean lower,int side, int vertex, float[] position, float[] normal, float[] texture)
|
830 |
6a06a912
|
Leszek Koltunski
|
{
|
831 |
84ee2a6a
|
Leszek Koltunski
|
//android.util.Log.e("CUBES", "adding Side vertex!");
|
832 |
|
|
|
833 |
|
|
remainingVert--;
|
834 |
|
|
|
835 |
985ea9c5
|
Leszek Koltunski
|
float x, y;
|
836 |
|
|
|
837 |
6a06a912
|
Leszek Koltunski
|
switch(curr.side)
|
838 |
|
|
{
|
839 |
985ea9c5
|
Leszek Koltunski
|
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 |
6a06a912
|
Leszek Koltunski
|
position[3*vertex+2] = lower ? BACKZ : FRONTZ;
|
844 |
|
|
|
845 |
39cbf9dc
|
Leszek Koltunski
|
normal[3*vertex ] = side==NORTH ? 0.0f : (side==WEST?-R:R);
|
846 |
6a06a912
|
Leszek Koltunski
|
normal[3*vertex+1] = 1.0f;
|
847 |
|
|
normal[3*vertex+2] = lower ? -R:R;
|
848 |
|
|
|
849 |
985ea9c5
|
Leszek Koltunski
|
texture[2*vertex ] = x;
|
850 |
|
|
texture[2*vertex+1] = 1.0f-(float)(lower? (curr.row-1):(curr.row ))/mRows;
|
851 |
6a06a912
|
Leszek Koltunski
|
break;
|
852 |
985ea9c5
|
Leszek Koltunski
|
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 |
6a06a912
|
Leszek Koltunski
|
position[3*vertex+2] = lower ? BACKZ : FRONTZ;
|
857 |
|
|
|
858 |
39cbf9dc
|
Leszek Koltunski
|
normal[3*vertex ] = side==SOUTH ? 0.0f: (side==EAST?-R:R);
|
859 |
6a06a912
|
Leszek Koltunski
|
normal[3*vertex+1] =-1.0f;
|
860 |
|
|
normal[3*vertex+2] = lower ? -R:R;
|
861 |
|
|
|
862 |
985ea9c5
|
Leszek Koltunski
|
texture[2*vertex ] = x;
|
863 |
|
|
texture[2*vertex+1] = 1.0f-(float)(lower? (curr.row+2):(curr.row+1))/mRows;
|
864 |
6a06a912
|
Leszek Koltunski
|
break;
|
865 |
985ea9c5
|
Leszek Koltunski
|
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 |
6a06a912
|
Leszek Koltunski
|
position[3*vertex+2] = lower ? BACKZ : FRONTZ;
|
870 |
|
|
|
871 |
39cbf9dc
|
Leszek Koltunski
|
normal[3*vertex ] =-1.0f;
|
872 |
6a06a912
|
Leszek Koltunski
|
normal[3*vertex+1] = side==WEST ? 0.0f : (side==NORTH?-R:R);
|
873 |
|
|
normal[3*vertex+2] = lower ? -R:R;
|
874 |
|
|
|
875 |
39cbf9dc
|
Leszek Koltunski
|
texture[2*vertex ] = (float)(lower ? (curr.col-1):(curr.col ))/mCols;
|
876 |
985ea9c5
|
Leszek Koltunski
|
texture[2*vertex+1] = 1.0f - y;
|
877 |
6a06a912
|
Leszek Koltunski
|
break;
|
878 |
985ea9c5
|
Leszek Koltunski
|
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 |
6a06a912
|
Leszek Koltunski
|
position[3*vertex+2] = lower ? BACKZ : FRONTZ;
|
883 |
|
|
|
884 |
39cbf9dc
|
Leszek Koltunski
|
normal[3*vertex ] = 1.0f;
|
885 |
6a06a912
|
Leszek Koltunski
|
normal[3*vertex+1] = side==EAST ? 0.0f : (side==SOUTH?-R:R);
|
886 |
|
|
normal[3*vertex+2] = lower ? -R:R;
|
887 |
|
|
|
888 |
39cbf9dc
|
Leszek Koltunski
|
texture[2*vertex ] = (float)(lower ? (curr.col+2):(curr.col+1))/mCols;
|
889 |
985ea9c5
|
Leszek Koltunski
|
texture[2*vertex+1] = 1.0f - y;
|
890 |
6a06a912
|
Leszek Koltunski
|
break;
|
891 |
|
|
}
|
892 |
|
|
|
893 |
39cbf9dc
|
Leszek Koltunski
|
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 |
6a06a912
|
Leszek Koltunski
|
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 |
665e2c45
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
16d8b8f3
|
Leszek Koltunski
|
|
938 |
8d9da98a
|
Leszek Koltunski
|
mEdges.clear();
|
939 |
|
|
mEdges = null;
|
940 |
|
|
mCubes = null;
|
941 |
|
|
|
942 |
16d8b8f3
|
Leszek Koltunski
|
if( remainingVert!=0 )
|
943 |
|
|
android.util.Log.d("CUBES", "remainingVert " +remainingVert );
|
944 |
665e2c45
|
Leszek Koltunski
|
|
945 |
05403bba
|
Leszek Koltunski
|
mMeshPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
946 |
|
|
mMeshPositions.put(positionData).position(0);
|
947 |
665e2c45
|
Leszek Koltunski
|
|
948 |
05403bba
|
Leszek Koltunski
|
mMeshNormals = ByteBuffer.allocateDirect(NORMAL_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
949 |
|
|
mMeshNormals.put(normalData).position(0);
|
950 |
665e2c45
|
Leszek Koltunski
|
|
951 |
05403bba
|
Leszek Koltunski
|
mMeshTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
952 |
|
|
mMeshTexture.put(textureData).position(0);
|
953 |
665e2c45
|
Leszek Koltunski
|
}
|
954 |
|
|
|
955 |
11fb6ce0
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
956 |
|
|
|
957 |
69ed1eb4
|
Leszek Koltunski
|
float[] getBoundingVertices()
|
958 |
|
|
{
|
959 |
|
|
return mBoundingVert;
|
960 |
|
|
}
|
961 |
11fb6ce0
|
Leszek Koltunski
|
|
962 |
6a06a912
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
963 |
|
|
// PUBLIC API
|
964 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
965 |
|
|
/**
|
966 |
05403bba
|
Leszek Koltunski
|
* Creates the underlying mesh of vertices, normals, texture coords.
|
967 |
6a06a912
|
Leszek Koltunski
|
*
|
968 |
a56bc359
|
Leszek Koltunski
|
* @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 |
6a06a912
|
Leszek Koltunski
|
*/
|
989 |
26df012c
|
Leszek Koltunski
|
public MeshCubes(int cols, String desc, boolean frontOnly)
|
990 |
6a06a912
|
Leszek Koltunski
|
{
|
991 |
3ef3364d
|
Leszek Koltunski
|
super(frontOnly ? 0.0f:1.0f/cols);
|
992 |
2e96ee72
|
Leszek Koltunski
|
prepareDataStructures(cols,desc,frontOnly);
|
993 |
665e2c45
|
Leszek Koltunski
|
build(frontOnly);
|
994 |
|
|
}
|
995 |
6a06a912
|
Leszek Koltunski
|
|
996 |
665e2c45
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
997 |
|
|
/**
|
998 |
05403bba
|
Leszek Koltunski
|
* Creates a full, hole-less underlying mesh of vertices, normals, texture coords and colors.
|
999 |
665e2c45
|
Leszek Koltunski
|
*
|
1000 |
a56bc359
|
Leszek Koltunski
|
* @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 |
665e2c45
|
Leszek Koltunski
|
*/
|
1004 |
26df012c
|
Leszek Koltunski
|
public MeshCubes(int cols, int rows, boolean frontOnly)
|
1005 |
665e2c45
|
Leszek Koltunski
|
{
|
1006 |
3ef3364d
|
Leszek Koltunski
|
super(frontOnly ? 0.0f:1.0f/cols);
|
1007 |
665e2c45
|
Leszek Koltunski
|
prepareDataStructures(cols,rows,frontOnly);
|
1008 |
|
|
build(frontOnly);
|
1009 |
6a06a912
|
Leszek Koltunski
|
}
|
1010 |
|
|
}
|
1011 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|