Revision b2913a86
Added by Leszek Koltunski 11 months ago
src/main/java/org/distorted/library/mesh/MeshPolygon.java | ||
---|---|---|
33 | 33 |
*/ |
34 | 34 |
public class MeshPolygon extends MeshBase |
35 | 35 |
{ |
36 |
private static final int SHAPE_DD = 0; |
|
37 |
private static final int SHAPE_DU = 1; |
|
38 |
private static final int SHAPE_UD = 2; |
|
39 |
private static final int SHAPE_UU = 3; |
|
40 |
private static final int SHAPE_DUD= 4; |
|
41 |
|
|
36 | 42 |
private float[] mPolygonVertices; |
37 | 43 |
private int mNumPolygonVertices; |
38 | 44 |
private float[] mPolygonBands; |
39 | 45 |
private int mNumPolygonBands; |
46 |
private boolean[] mEdgeUp; |
|
40 | 47 |
|
41 | 48 |
private int remainingVert; |
42 | 49 |
private int numVertices; |
43 | 50 |
private int extraIndex, extraVertices; |
44 | 51 |
|
45 |
private float[] mBandQuot; |
|
52 |
//private float[] mBandQuot;
|
|
46 | 53 |
|
47 | 54 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
48 | 55 |
// polygonVertices>=3 , polygonBands>=2 |
... | ... | |
61 | 68 |
|
62 | 69 |
remainingVert = numVertices; |
63 | 70 |
} |
64 |
|
|
71 |
/* |
|
65 | 72 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
66 | 73 |
|
67 | 74 |
private void computeCache() |
... | ... | |
78 | 85 |
mBandQuot[band] = (mPolygonBands[2*prev+1]-mPolygonBands[2*next+1]) / (mPolygonBands[2*next]-mPolygonBands[2*prev]); |
79 | 86 |
} |
80 | 87 |
} |
81 |
|
|
88 |
*/ |
|
82 | 89 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
83 | 90 |
|
84 | 91 |
private float getSpecialQuot(int index) |
... | ... | |
129 | 136 |
|
130 | 137 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
131 | 138 |
|
132 |
private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot, float[] attribs1, float[] attribs2) |
|
139 |
private float computeZEdge(float quot) |
|
140 |
{ |
|
141 |
if( quot>=1.0f ) return 0.0f; |
|
142 |
|
|
143 |
for(int band=1; band<mNumPolygonBands; band++) |
|
144 |
{ |
|
145 |
float curr = mPolygonBands[2*band]; |
|
146 |
|
|
147 |
if( curr<=quot ) |
|
148 |
{ |
|
149 |
float prev = mPolygonBands[2*band-2]; |
|
150 |
float prevH= mPolygonBands[2*band-1]; |
|
151 |
float currH= mPolygonBands[2*band+1]; |
|
152 |
|
|
153 |
float A = (prev-quot)/(prev-curr); |
|
154 |
|
|
155 |
return A*currH + (1-A)*prevH; |
|
156 |
} |
|
157 |
} |
|
158 |
|
|
159 |
return 0.0f; |
|
160 |
} |
|
161 |
|
|
162 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
163 |
|
|
164 |
private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot, |
|
165 |
int edgeShape, float[] attribs1, float[] attribs2) |
|
133 | 166 |
{ |
134 | 167 |
remainingVert--; |
135 | 168 |
|
136 |
float vx,vy,vz; |
|
137 | 169 |
float Xfirst= mPolygonVertices[2*polyVertex ]; |
138 | 170 |
float Yfirst= mPolygonVertices[2*polyVertex+1]; |
139 | 171 |
float Xlast = mPolygonVertices[2*polyEndVer ]; |
... | ... | |
141 | 173 |
|
142 | 174 |
float xEdge = Xfirst + quot*(Xlast-Xfirst); |
143 | 175 |
float yEdge = Yfirst + quot*(Ylast-Yfirst); |
176 |
float zEdge; |
|
144 | 177 |
|
145 |
float x = mPolygonBands[2*polyBand]*xEdge; |
|
146 |
float y = mPolygonBands[2*polyBand]*yEdge; |
|
147 |
|
|
148 |
if( quot==0.0f || quot==1.0f ) |
|
149 |
{ |
|
150 |
vx = mBandQuot[polyBand]*xEdge; |
|
151 |
vy = mBandQuot[polyBand]*yEdge; |
|
152 |
vz = xEdge*xEdge + yEdge*yEdge; |
|
153 |
} |
|
154 |
else |
|
178 |
switch(edgeShape) |
|
155 | 179 |
{ |
156 |
vx = mBandQuot[polyBand]*(Ylast-Yfirst); |
|
157 |
vy = mBandQuot[polyBand]*(Xfirst-Xlast); |
|
158 |
float tmp = Xfirst*Ylast - Xlast*Yfirst; |
|
159 |
vz = (tmp<0 ? -tmp:tmp); |
|
180 |
case SHAPE_DD : zEdge = 0.0f; |
|
181 |
break; |
|
182 |
case SHAPE_UU : zEdge = mPolygonBands[2*mNumPolygonBands-1]; |
|
183 |
break; |
|
184 |
case SHAPE_DU : zEdge = quot>=0.5f ? mPolygonBands[2*mNumPolygonBands-1] : computeZEdge(1-2*quot); |
|
185 |
break; |
|
186 |
case SHAPE_UD : zEdge = quot<=0.5f ? mPolygonBands[2*mNumPolygonBands-1] : computeZEdge(2*quot-1); |
|
187 |
break; |
|
188 |
default : zEdge = quot<=0.5f ? computeZEdge(1-2*quot) : computeZEdge(2*quot-1); |
|
189 |
break; |
|
160 | 190 |
} |
161 | 191 |
|
162 |
float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz); |
|
192 |
float q = mPolygonBands[2*polyBand]; |
|
193 |
|
|
194 |
float x = q*xEdge; |
|
195 |
float y = q*yEdge; |
|
196 |
float z = q*zEdge + mPolygonBands[2*polyBand+1]; |
|
163 | 197 |
|
164 | 198 |
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB ] = x; |
165 | 199 |
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y; |
166 |
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = mPolygonBands[2*polyBand+1]; |
|
167 |
|
|
168 |
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB ] = vx/len; |
|
169 |
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+1] = vy/len; |
|
170 |
attribs1[VERT1_ATTRIBS*vertex + NOR_ATTRIB+2] = vz/len; |
|
200 |
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = z; |
|
171 | 201 |
|
172 | 202 |
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB ] = x+0.5f; |
173 | 203 |
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f; |
... | ... | |
177 | 207 |
|
178 | 208 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
179 | 209 |
|
180 |
private int createBandStrip(int vertex, int polyBand, int polyVertex, float[] attribs1, float[] attribs2) |
|
210 |
private void addVertexNormal(int vertex, int end1, int end2, float[] attribs1) |
|
211 |
{ |
|
212 |
android.util.Log.e("D", "vertex="+vertex+" end1="+end1+" end2="+end2); |
|
213 |
|
|
214 |
int iv = VERT1_ATTRIBS*vertex + POS_ATTRIB; |
|
215 |
int i1 = VERT1_ATTRIBS*end1 + POS_ATTRIB; |
|
216 |
int i2 = VERT1_ATTRIBS*end2 + POS_ATTRIB; |
|
217 |
|
|
218 |
float vx = attribs1[iv ]; |
|
219 |
float vy = attribs1[iv+1]; |
|
220 |
float vz = attribs1[iv+2]; |
|
221 |
|
|
222 |
float x1 = attribs1[i1 ]; |
|
223 |
float y1 = attribs1[i1+1]; |
|
224 |
float z1 = attribs1[i1+2]; |
|
225 |
|
|
226 |
float x2 = attribs1[i2 ]; |
|
227 |
float y2 = attribs1[i2+1]; |
|
228 |
float z2 = attribs1[i2+2]; |
|
229 |
|
|
230 |
float dx1 = vx-x1; |
|
231 |
float dy1 = vy-y1; |
|
232 |
float dz1 = vz-z1; |
|
233 |
|
|
234 |
float dx2 = vx-x2; |
|
235 |
float dy2 = vy-y2; |
|
236 |
float dz2 = vz-z2; |
|
237 |
|
|
238 |
float cx = dy1*dz2 - dz1*dy2; |
|
239 |
float cy = dz1*dx2 - dx1*dz2; |
|
240 |
float cz = dx1*dy2 - dy1*dx2; |
|
241 |
|
|
242 |
float len = (float)Math.sqrt(cx*cx + cy*cy + cz*cz); |
|
243 |
|
|
244 |
int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB; |
|
245 |
|
|
246 |
attribs1[index ] = cx/len; |
|
247 |
attribs1[index+1] = cy/len; |
|
248 |
attribs1[index+2] = cz/len; |
|
249 |
} |
|
250 |
|
|
251 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
252 |
|
|
253 |
private int createBandStrip(int vertex, int polyBand, int polyVertex, int edgeShape, float[] attribs1, float[] attribs2) |
|
181 | 254 |
{ |
255 |
int initVertex = vertex; |
|
256 |
|
|
182 | 257 |
if( polyVertex==0 ) |
183 | 258 |
{ |
184 |
vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2); |
|
185 |
|
|
186 |
if( polyBand>0 ) |
|
187 |
{ |
|
188 |
vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2); |
|
189 |
} |
|
259 |
vertex = addVertex(vertex,polyBand,0,1,0,edgeShape,attribs1,attribs2); |
|
260 |
if( polyBand>0 ) vertex = addVertex(vertex,polyBand,0,1,0,edgeShape,attribs1,attribs2); |
|
190 | 261 |
} |
191 | 262 |
|
192 | 263 |
boolean specialCase = mNumPolygonBands==2 && polyBand==0 && extraIndex>0; |
... | ... | |
210 | 281 |
quot2 = getQuot(index+1,polyBand , isExtra); |
211 | 282 |
} |
212 | 283 |
|
213 |
vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,quot1,attribs1,attribs2); |
|
214 |
vertex = addVertex(vertex,polyBand ,polyVertex,polyEndVer,quot2,attribs1,attribs2); |
|
284 |
vertex = addVertex(vertex,polyBand+1,polyVertex,polyEndVer,quot1,edgeShape,attribs1,attribs2); |
|
285 |
vertex = addVertex(vertex,polyBand ,polyVertex,polyEndVer,quot2,edgeShape,attribs1,attribs2); |
|
286 |
} |
|
287 |
|
|
288 |
if( polyVertex==0 ) |
|
289 |
{ |
|
290 |
if( polyBand>0 ) addVertexNormal(initVertex,initVertex+3,initVertex+2,attribs1); |
|
291 |
else addVertexNormal(initVertex,initVertex+2,initVertex+1,attribs1); |
|
292 |
} |
|
293 |
else |
|
294 |
{ |
|
295 |
addVertexNormal(initVertex,initVertex-1,initVertex+1,attribs1); |
|
296 |
} |
|
297 |
|
|
298 |
boolean lower = (polyVertex>0 || polyBand>0); |
|
299 |
|
|
300 |
for(int index=initVertex+1; index<vertex-1; index++) |
|
301 |
{ |
|
302 |
addVertexNormal(index,(lower ? index+2 : index-1),index+1,attribs1); |
|
303 |
lower = !lower; |
|
215 | 304 |
} |
216 | 305 |
|
306 |
addVertexNormal(vertex-1,vertex-2,vertex-3,attribs1); |
|
307 |
|
|
217 | 308 |
return vertex; |
218 | 309 |
} |
219 | 310 |
|
311 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
312 |
|
|
313 |
private int computeEdgeShape(int curr) |
|
314 |
{ |
|
315 |
if( mEdgeUp==null || !mEdgeUp[curr] ) return SHAPE_DD; |
|
316 |
|
|
317 |
int prev = (curr==0 ? mNumPolygonVertices-1 : curr-1); |
|
318 |
int next = (curr==mNumPolygonVertices-1 ? 0 : curr+1); |
|
319 |
|
|
320 |
boolean l = mEdgeUp[prev]; |
|
321 |
boolean r = mEdgeUp[next]; |
|
322 |
|
|
323 |
return l ? (r ? SHAPE_UU : SHAPE_UD) : (r ? SHAPE_DU : SHAPE_DUD); |
|
324 |
} |
|
325 |
|
|
220 | 326 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
221 | 327 |
|
222 | 328 |
private void buildGrid(float[] attribs1, float[] attribs2) |
223 | 329 |
{ |
224 | 330 |
int vertex=0; |
225 | 331 |
|
332 |
int[] edgeShape = new int[mNumPolygonVertices]; |
|
333 |
|
|
334 |
for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++) |
|
335 |
edgeShape[polyVertex] = computeEdgeShape(polyVertex); |
|
336 |
|
|
226 | 337 |
for(int polyBand=0; polyBand<mNumPolygonBands-1; polyBand++) |
227 | 338 |
for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++) |
228 | 339 |
{ |
229 |
vertex = createBandStrip(vertex,polyBand,polyVertex,attribs1,attribs2); |
|
340 |
android.util.Log.e("D", "creating strip polyBand="+polyBand+" polyVertex="+polyVertex+" : "+vertex); |
|
341 |
vertex = createBandStrip(vertex,polyBand,polyVertex,edgeShape[polyVertex],attribs1,attribs2); |
|
342 |
android.util.Log.e("D", " "+vertex); |
|
230 | 343 |
} |
231 | 344 |
} |
232 | 345 |
|
... | ... | |
243 | 356 |
* From (1.0,Z[0]) (outer edge, its Z elevation) to (0.0,Z[K]) (the center, |
244 | 357 |
* its elevation). The polygon is split into such concentric bands. |
245 | 358 |
* Must be band[2*i] > band[2*(i+1)] ! |
359 |
* @param edgeUp N booleans - one for each edge; the first connects vertices (0,1) and (2,3); |
|
360 |
* then just like 'verticesXY', i.e. counterclockwise. |
|
361 |
* If this is null, all edges are by default 'down'. |
|
362 |
* 'Down' means that edge's Z is equal to 0; 'up' means that its Z, at least in its |
|
363 |
* middle, is equal to the highest elevation in the middle of the mesh. |
|
364 |
* If the 'previous' edge is also up, then the Z is up horizontally from its middle |
|
365 |
* to the left vertex; else it goes down along the same function it goes along the bands. |
|
366 |
* Same with the right half of the edge. |
|
246 | 367 |
* @param exIndex This and the next parameter describe how to make the mesh denser at the |
247 | 368 |
* polyVertices. If e.g. exIndex=3 and exVertices=2, then 3 triangles of the |
248 | 369 |
* outermost band (and 2 triangles of the next band, and 1 triangle of the third |
... | ... | |
252 | 373 |
* all bands go to. |
253 | 374 |
* @param centerY Y coordinate of the center. |
254 | 375 |
*/ |
255 |
public MeshPolygon(float[] verticesXY, float[] bands, int exIndex, int exVertices, float centerX, float centerY) |
|
376 |
public MeshPolygon(float[] verticesXY, float[] bands, boolean[] edgeUp, int exIndex, int exVertices, float centerX, float centerY)
|
|
256 | 377 |
{ |
257 | 378 |
super(); |
258 | 379 |
|
... | ... | |
260 | 381 |
mPolygonBands = bands; |
261 | 382 |
mNumPolygonVertices= mPolygonVertices.length /2; |
262 | 383 |
mNumPolygonBands = mPolygonBands.length /2; |
384 |
mEdgeUp = edgeUp; |
|
263 | 385 |
extraIndex = exIndex; |
264 | 386 |
extraVertices = exVertices; |
265 | 387 |
|
... | ... | |
273 | 395 |
} |
274 | 396 |
|
275 | 397 |
computeNumberOfVertices(); |
276 |
computeCache(); |
|
398 |
//computeCache();
|
|
277 | 399 |
|
278 | 400 |
float[] attribs1= new float[VERT1_ATTRIBS*numVertices]; |
279 | 401 |
float[] attribs2= new float[VERT2_ATTRIBS*numVertices]; |
... | ... | |
301 | 423 |
|
302 | 424 |
public MeshPolygon(float[] verticesXY, float[] bands, int exIndex, int exVertices) |
303 | 425 |
{ |
304 |
this(verticesXY,bands,exIndex,exVertices,0.0f,0.0f); |
|
426 |
this(verticesXY,bands,null,exIndex,exVertices,0.0f,0.0f);
|
|
305 | 427 |
} |
306 | 428 |
|
307 | 429 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
311 | 433 |
*/ |
312 | 434 |
public MeshPolygon(float[] verticesXY, float[] bands) |
313 | 435 |
{ |
314 |
this(verticesXY,bands,0,0,0.0f,0.0f); |
|
436 |
this(verticesXY,bands,null,0,0,0.0f,0.0f);
|
|
315 | 437 |
} |
316 | 438 |
|
317 | 439 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
build for API 34.