Revision d3287c14
Added by Leszek Koltunski over 1 year 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 float NOT_DONE_YET = -1000;
|
|
36 |
private static final int NUM_CACHE = 20;
|
|
37 | 37 |
private static final int SHAPE_DD = 0; |
38 | 38 |
private static final int SHAPE_DU = 1; |
39 | 39 |
private static final int SHAPE_UD = 2; |
... | ... | |
50 | 50 |
private int numVertices; |
51 | 51 |
private int extraIndex, extraVertices; |
52 | 52 |
|
53 |
private float[] mBandQuot;
|
|
53 |
private float[] mCurveCache;
|
|
54 | 54 |
|
55 | 55 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
56 | 56 |
// polygonVertices>=3 , polygonBands>=2 |
... | ... | |
74 | 74 |
|
75 | 75 |
private void computeCache() |
76 | 76 |
{ |
77 |
mBandQuot = new float[mNumPolygonBands]; |
|
77 |
mCurveCache = new float[NUM_CACHE]; |
|
78 |
float[] tmpD = new float[mNumPolygonBands+1]; |
|
79 |
float[] tmpX = new float[mNumPolygonBands+1]; |
|
78 | 80 |
|
79 |
int next, prev; |
|
81 |
for(int i=1; i<mNumPolygonBands; i++) |
|
82 |
{ |
|
83 |
tmpD[i] = (mPolygonBands[2*i-1]-mPolygonBands[2*i+1]) / (mPolygonBands[2*i]-mPolygonBands[2*i-2]); |
|
84 |
tmpX[i] = 1.0f - (mPolygonBands[2*i]+mPolygonBands[2*i-2])/2; |
|
85 |
} |
|
86 |
|
|
87 |
tmpD[0] = tmpD[1]; |
|
88 |
tmpD[mNumPolygonBands] = tmpD[mNumPolygonBands-1]; |
|
89 |
tmpX[0] = 0.0f; |
|
90 |
tmpX[mNumPolygonBands] = 1.0f; |
|
80 | 91 |
|
81 |
for(int band=0; band<mNumPolygonBands; band++) |
|
92 |
int prev = 0; |
|
93 |
int next = 0; |
|
94 |
|
|
95 |
for(int i=0; i<NUM_CACHE-1; i++) |
|
82 | 96 |
{ |
83 |
next = (band==mNumPolygonBands-1 ? band : band+1); |
|
84 |
prev = (band== 0 ? band : band-1); |
|
97 |
float x = i/(NUM_CACHE-1.0f); |
|
85 | 98 |
|
86 |
mBandQuot[band] = (mPolygonBands[2*prev+1]-mPolygonBands[2*next+1]) / (mPolygonBands[2*next]-mPolygonBands[2*prev]); |
|
99 |
if( x>=tmpX[next] ) |
|
100 |
{ |
|
101 |
prev = next; |
|
102 |
while( next<=mNumPolygonBands && x>=tmpX[next] ) next++; |
|
103 |
} |
|
104 |
|
|
105 |
if( next>prev ) |
|
106 |
{ |
|
107 |
float t = (x-tmpX[prev]) / (tmpX[next]-tmpX[prev]); |
|
108 |
mCurveCache[i] = t*(tmpD[next]-tmpD[prev]) + tmpD[prev]; |
|
109 |
} |
|
110 |
else |
|
111 |
{ |
|
112 |
mCurveCache[i] = tmpD[next]; |
|
113 |
} |
|
87 | 114 |
} |
115 |
|
|
116 |
mCurveCache[NUM_CACHE-1] = tmpD[mNumPolygonBands]; |
|
88 | 117 |
} |
89 | 118 |
|
90 | 119 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
160 | 189 |
return 0.0f; |
161 | 190 |
} |
162 | 191 |
|
192 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
193 |
|
|
194 |
private float derivative(float x) |
|
195 |
{ |
|
196 |
if( x>=1.0f ) |
|
197 |
{ |
|
198 |
return mCurveCache[NUM_CACHE-1]; |
|
199 |
} |
|
200 |
else |
|
201 |
{ |
|
202 |
float tmp = x*(NUM_CACHE-1); |
|
203 |
int i1 = (int)tmp; |
|
204 |
int i2 = i1+1; |
|
205 |
return (tmp-i1)*(mCurveCache[i2]-mCurveCache[i1]) + mCurveCache[i1]; |
|
206 |
} |
|
207 |
} |
|
208 |
|
|
209 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
210 |
|
|
211 |
private void doNormals(float[] attribs1, int index, float quot, int edgeShape, |
|
212 |
float xEdge, float yEdge, float zEdge, int polyBand) |
|
213 |
{ |
|
214 |
if( ( quot<=0.5f && (edgeShape==SHAPE_UD || edgeShape==SHAPE_UU) ) || |
|
215 |
( quot> 0.5f && (edgeShape==SHAPE_DU || edgeShape==SHAPE_UU) ) ) |
|
216 |
{ |
|
217 |
attribs1[index ] = 0; |
|
218 |
attribs1[index+1] = 0; |
|
219 |
attribs1[index+2] = 1; |
|
220 |
} |
|
221 |
else |
|
222 |
{ |
|
223 |
float t = mPolygonBands[2*mNumPolygonBands-1]; |
|
224 |
float x = 1.0f - mPolygonBands[2*polyBand]; |
|
225 |
float d = derivative(x); |
|
226 |
|
|
227 |
d *= ((t-zEdge)/t); |
|
228 |
|
|
229 |
float vx = d*xEdge; |
|
230 |
float vy = d*yEdge; |
|
231 |
float vz = xEdge*xEdge + yEdge*yEdge; |
|
232 |
float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz); |
|
233 |
|
|
234 |
attribs1[index ] = vx/len; |
|
235 |
attribs1[index+1] = vy/len; |
|
236 |
attribs1[index+2] = vz/len; |
|
237 |
} |
|
238 |
} |
|
239 |
|
|
163 | 240 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
164 | 241 |
|
165 | 242 |
private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot, |
... | ... | |
176 | 253 |
float yEdge = Yfirst + quot*(Ylast-Yfirst); |
177 | 254 |
float zEdge; |
178 | 255 |
|
256 |
float q = mPolygonBands[2*polyBand]; |
|
257 |
float o = mPolygonBands[2*polyBand+1]; |
|
258 |
float t = mPolygonBands[2*mNumPolygonBands-1]; |
|
259 |
|
|
179 | 260 |
switch(edgeShape) |
180 | 261 |
{ |
181 |
case SHAPE_DD : zEdge = 0.0f; |
|
182 |
break; |
|
183 |
case SHAPE_UU : zEdge = mPolygonBands[2*mNumPolygonBands-1]; |
|
184 |
break; |
|
185 |
case SHAPE_DU : zEdge = quot>=0.5f ? mPolygonBands[2*mNumPolygonBands-1] : computeZEdge(1-2*quot); |
|
186 |
break; |
|
187 |
case SHAPE_UD : zEdge = quot<=0.5f ? mPolygonBands[2*mNumPolygonBands-1] : computeZEdge(2*quot-1); |
|
188 |
break; |
|
189 |
default : zEdge = quot<=0.5f ? computeZEdge(1-2*quot) : computeZEdge(2*quot-1); |
|
190 |
break; |
|
262 |
case SHAPE_DD : zEdge = 0.0f; break; |
|
263 |
case SHAPE_UU : zEdge = t; break; |
|
264 |
case SHAPE_DU : zEdge = quot>=0.5f ? t : computeZEdge(1-2*quot); break; |
|
265 |
case SHAPE_UD : zEdge = quot<=0.5f ? t : computeZEdge(2*quot-1); break; |
|
266 |
default : zEdge = quot<=0.5f ? computeZEdge(1-2*quot) : computeZEdge(2*quot-1); break; |
|
191 | 267 |
} |
192 | 268 |
|
193 |
float q = mPolygonBands[2*polyBand]; |
|
194 |
|
|
195 | 269 |
float x = q*xEdge; |
196 | 270 |
float y = q*yEdge; |
197 |
float z = q*zEdge + mPolygonBands[2*polyBand+1];
|
|
271 |
float z = o + (t-o)*(zEdge/t);
|
|
198 | 272 |
|
199 | 273 |
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB ] = x; |
200 | 274 |
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y; |
201 | 275 |
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = z; |
202 | 276 |
|
203 | 277 |
int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB; |
204 |
|
|
205 |
if( quot==0.0f || quot==1.0f ) |
|
206 |
{ |
|
207 |
float vx = mBandQuot[polyBand]*xEdge; |
|
208 |
float vy = mBandQuot[polyBand]*yEdge; |
|
209 |
float vz = xEdge*xEdge + yEdge*yEdge; |
|
210 |
float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz); |
|
211 |
|
|
212 |
attribs1[index ] = vx/len; |
|
213 |
attribs1[index+1] = vy/len; |
|
214 |
attribs1[index+2] = vz/len; |
|
215 |
} |
|
216 |
else |
|
217 |
{ |
|
218 |
attribs1[index ] = NOT_DONE_YET; |
|
219 |
} |
|
278 |
doNormals(attribs1,index, quot, edgeShape, xEdge, yEdge, zEdge, polyBand); |
|
220 | 279 |
|
221 | 280 |
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB ] = x+0.5f; |
222 | 281 |
attribs2[VERT2_ATTRIBS*vertex + TEX_ATTRIB+1] = y+0.5f; |
... | ... | |
224 | 283 |
return vertex+1; |
225 | 284 |
} |
226 | 285 |
|
227 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
228 |
|
|
229 |
private void addVertexNormal(int vertex, int end1, int end2, float[] attribs1) |
|
230 |
{ |
|
231 |
// android.util.Log.e("D", "vertex="+vertex+" end1="+end1+" end2="+end2); |
|
232 |
|
|
233 |
int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB; |
|
234 |
|
|
235 |
if( attribs1[index] == NOT_DONE_YET) |
|
236 |
{ |
|
237 |
int iv = VERT1_ATTRIBS*vertex + POS_ATTRIB; |
|
238 |
int i1 = VERT1_ATTRIBS*end1 + POS_ATTRIB; |
|
239 |
int i2 = VERT1_ATTRIBS*end2 + POS_ATTRIB; |
|
240 |
|
|
241 |
float vx = attribs1[iv ]; |
|
242 |
float vy = attribs1[iv+1]; |
|
243 |
float vz = attribs1[iv+2]; |
|
244 |
|
|
245 |
float x1 = attribs1[i1 ]; |
|
246 |
float y1 = attribs1[i1+1]; |
|
247 |
float z1 = attribs1[i1+2]; |
|
248 |
|
|
249 |
float x2 = attribs1[i2 ]; |
|
250 |
float y2 = attribs1[i2+1]; |
|
251 |
float z2 = attribs1[i2+2]; |
|
252 |
|
|
253 |
float dx1 = vx-x1; |
|
254 |
float dy1 = vy-y1; |
|
255 |
float dz1 = vz-z1; |
|
256 |
|
|
257 |
float dx2 = vx-x2; |
|
258 |
float dy2 = vy-y2; |
|
259 |
float dz2 = vz-z2; |
|
260 |
|
|
261 |
float cx = dy1*dz2 - dz1*dy2; |
|
262 |
float cy = dz1*dx2 - dx1*dz2; |
|
263 |
float cz = dx1*dy2 - dy1*dx2; |
|
264 |
|
|
265 |
float len = (float)Math.sqrt(cx*cx + cy*cy + cz*cz); |
|
266 |
|
|
267 |
attribs1[index ] = cx/len; |
|
268 |
attribs1[index+1] = cy/len; |
|
269 |
attribs1[index+2] = cz/len; |
|
270 |
} |
|
271 |
} |
|
272 |
|
|
273 | 286 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
274 | 287 |
|
275 | 288 |
private int createBandStrip(int vertex, int polyBand, int polyVertex, int edgeShape, float[] attribs1, float[] attribs2) |
276 | 289 |
{ |
277 |
int initVertex = vertex; |
|
278 |
|
|
279 | 290 |
if( polyVertex==0 ) |
280 | 291 |
{ |
281 | 292 |
vertex = addVertex(vertex,polyBand,0,1,0,edgeShape,attribs1,attribs2); |
... | ... | |
307 | 318 |
vertex = addVertex(vertex,polyBand ,polyVertex,polyEndVer,quot2,edgeShape,attribs1,attribs2); |
308 | 319 |
} |
309 | 320 |
|
310 |
if( polyVertex==0 ) |
|
311 |
{ |
|
312 |
if( polyBand>0 ) addVertexNormal(initVertex,initVertex+3,initVertex+2,attribs1); |
|
313 |
else addVertexNormal(initVertex,initVertex+2,initVertex+1,attribs1); |
|
314 |
} |
|
315 |
else |
|
316 |
{ |
|
317 |
addVertexNormal(initVertex,initVertex-1,initVertex+1,attribs1); |
|
318 |
} |
|
319 |
|
|
320 |
boolean lower = (polyVertex>0 || polyBand>0); |
|
321 |
|
|
322 |
for(int index=initVertex+1; index<vertex-1; index++) |
|
323 |
{ |
|
324 |
addVertexNormal(index,(lower ? index+2 : index-1),index+1,attribs1); |
|
325 |
lower = !lower; |
|
326 |
} |
|
327 |
|
|
328 |
addVertexNormal(vertex-1,vertex-2,vertex-3,attribs1); |
|
329 |
|
|
330 | 321 |
return vertex; |
331 | 322 |
} |
332 | 323 |
|
Also available in: Unified diff
MeshPolygon: support for an arbitrary subset of edges being 'up' (i.e. buildup to MeshMultigon)