Revision e529379e
Added by Leszek Koltunski over 2 years ago
| src/main/java/org/distorted/library/mesh/MeshPolygon.java | ||
|---|---|---|
| 20 | 20 |
|
| 21 | 21 |
package org.distorted.library.mesh; |
| 22 | 22 |
|
| 23 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 24 |
|
|
| 25 | 23 |
import org.distorted.library.main.DistortedLibrary; |
| 26 | 24 |
|
| 25 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 27 | 26 |
/** |
| 28 | 27 |
* Create a polygon of any shape and varying elevations from the edges towards the center. |
| 29 | 28 |
* <p> |
| ... | ... | |
| 34 | 33 |
public class MeshPolygon extends MeshBase |
| 35 | 34 |
{
|
| 36 | 35 |
private static final int NUM_CACHE = 20; |
| 37 |
private static final int SHAPE_DD = 0; |
|
| 38 |
private static final int SHAPE_DU = 1; |
|
| 39 |
private static final int SHAPE_UD = 2; |
|
| 40 |
private static final int SHAPE_UU = 3; |
|
| 41 |
private static final int SHAPE_DUD= 4; |
|
| 42 | 36 |
|
| 43 | 37 |
private float[] mPolygonVertices; |
| 44 | 38 |
private int mNumPolygonVertices; |
| 45 | 39 |
private float[] mPolygonBands; |
| 46 | 40 |
private int mNumPolygonBands; |
| 47 |
private boolean[] mEdgeUp; |
|
| 48 |
private boolean[] mVertUp; |
|
| 49 | 41 |
|
| 50 | 42 |
private int remainingVert; |
| 51 | 43 |
private int numVertices; |
| 52 |
private int extraIndex, extraVertices;
|
|
| 44 |
private int extraBand, extraVertices;
|
|
| 53 | 45 |
|
| 54 | 46 |
private float[] mCurveCache; |
| 55 |
private float mVecX, mVecY; |
|
| 56 |
private int[] mEdgeShape; |
|
| 57 | 47 |
|
| 58 | 48 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 59 | 49 |
// polygonVertices>=3 , polygonBands>=2 |
| 60 | 50 |
|
| 61 | 51 |
private void computeNumberOfVertices() |
| 62 | 52 |
{
|
| 63 |
if( mNumPolygonBands==2 && extraIndex>0 )
|
|
| 53 |
if( mNumPolygonBands==2 && extraBand>0 )
|
|
| 64 | 54 |
{
|
| 65 |
numVertices = 1 + 2*mNumPolygonVertices*(1+extraIndex+2*extraVertices);
|
|
| 55 |
numVertices = 1 + 2*mNumPolygonVertices*(1+extraBand+2*extraVertices);
|
|
| 66 | 56 |
} |
| 67 | 57 |
else |
| 68 | 58 |
{
|
| 69 | 59 |
numVertices = (mNumPolygonVertices*mNumPolygonBands+2)*(mNumPolygonBands-1) - 1; |
| 70 |
numVertices+= 2*mNumPolygonVertices*(2*extraIndex*extraVertices);
|
|
| 60 |
numVertices+= 2*mNumPolygonVertices*(2*extraBand*extraVertices);
|
|
| 71 | 61 |
} |
| 72 | 62 |
|
| 73 | 63 |
remainingVert = numVertices; |
| ... | ... | |
| 123 | 113 |
|
| 124 | 114 |
private float getSpecialQuot(int index) |
| 125 | 115 |
{
|
| 126 |
int num = 1 + extraIndex + 2*extraVertices;
|
|
| 116 |
int num = 1 + extraBand + 2*extraVertices;
|
|
| 127 | 117 |
int change1 = extraVertices+1; |
| 128 | 118 |
int change2 = num-change1; |
| 129 |
float quot = 1.0f/(extraIndex+1);
|
|
| 119 |
float quot = 1.0f/(extraBand+1);
|
|
| 130 | 120 |
|
| 131 | 121 |
if( index<change1 ) return index*quot/change1; |
| 132 | 122 |
else if( index>change2 ) return 1-quot + (index-change2)*quot/change1; |
| ... | ... | |
| 143 | 133 |
{
|
| 144 | 134 |
if( isExtra ) |
| 145 | 135 |
{
|
| 146 |
int extra = extraIndex-band+extraVertices;
|
|
| 136 |
int extra = extraBand-band+extraVertices;
|
|
| 147 | 137 |
|
| 148 | 138 |
if( index < extra ) |
| 149 | 139 |
{
|
| 150 |
float quot = ((float)extraIndex-band)/(extra*num);
|
|
| 140 |
float quot = ((float)extraBand-band)/(extra*num);
|
|
| 151 | 141 |
return index*quot; |
| 152 | 142 |
} |
| 153 | 143 |
else if( index > num+2*extraVertices-extra ) |
| 154 | 144 |
{
|
| 155 |
float quot = ((float)extraIndex-band)/(extra*num);
|
|
| 156 |
return (1.0f-((float)extraIndex-band)/num) + (index-num-2*extraVertices+extra)*quot;
|
|
| 145 |
float quot = ((float)extraBand-band)/(extra*num);
|
|
| 146 |
return (1.0f-((float)extraBand-band)/num) + (index-num-2*extraVertices+extra)*quot;
|
|
| 157 | 147 |
} |
| 158 | 148 |
else |
| 159 | 149 |
{
|
| ... | ... | |
| 167 | 157 |
return 1.0f; |
| 168 | 158 |
} |
| 169 | 159 |
|
| 170 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 171 |
|
|
| 172 |
private float computeZEdge(float quot) |
|
| 173 |
{
|
|
| 174 |
if( quot>=1.0f ) return 0.0f; |
|
| 175 |
|
|
| 176 |
for(int band=1; band<mNumPolygonBands; band++) |
|
| 177 |
{
|
|
| 178 |
float curr = mPolygonBands[2*band]; |
|
| 179 |
|
|
| 180 |
if( curr<=quot ) |
|
| 181 |
{
|
|
| 182 |
float prev = mPolygonBands[2*band-2]; |
|
| 183 |
float prevH= mPolygonBands[2*band-1]; |
|
| 184 |
float currH= mPolygonBands[2*band+1]; |
|
| 185 |
|
|
| 186 |
float A = (prev-quot)/(prev-curr); |
|
| 187 |
|
|
| 188 |
return A*currH + (1-A)*prevH; |
|
| 189 |
} |
|
| 190 |
} |
|
| 191 |
|
|
| 192 |
return 0.0f; |
|
| 193 |
} |
|
| 194 |
|
|
| 195 | 160 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 196 | 161 |
|
| 197 | 162 |
private float derivative(float x) |
| ... | ... | |
| 212 | 177 |
} |
| 213 | 178 |
} |
| 214 | 179 |
|
| 215 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 216 |
|
|
| 217 |
private void figureOutNormalVector2D(int edgeShape, int polyVertex, int polyEndVer, float quot, float xEdge, float yEdge ) |
|
| 218 |
{
|
|
| 219 |
if( edgeShape==SHAPE_DD ) |
|
| 220 |
{
|
|
| 221 |
if( quot<0.5f ) |
|
| 222 |
{
|
|
| 223 |
int p = polyVertex>0 ? polyVertex-1 : mNumPolygonVertices-1; |
|
| 224 |
int prvShape = mEdgeShape[p]; |
|
| 225 |
|
|
| 226 |
switch(prvShape) |
|
| 227 |
{
|
|
| 228 |
case SHAPE_DD : mVecX = xEdge; |
|
| 229 |
mVecY = yEdge; |
|
| 230 |
break; |
|
| 231 |
case SHAPE_UD : |
|
| 232 |
case SHAPE_DUD: float a = 2*quot; |
|
| 233 |
float xCurr= mPolygonVertices[2*polyVertex ]; |
|
| 234 |
float yCurr= mPolygonVertices[2*polyVertex+1]; |
|
| 235 |
float xPrev= mPolygonVertices[2*p ]; |
|
| 236 |
float yPrev= mPolygonVertices[2*p+1]; |
|
| 237 |
float xVec = xCurr-xPrev; |
|
| 238 |
float yVec = yCurr-yPrev; |
|
| 239 |
|
|
| 240 |
mVecX = a*xEdge + (1-a)*xVec; |
|
| 241 |
mVecY = a*yEdge + (1-a)*yVec; |
|
| 242 |
|
|
| 243 |
break; |
|
| 244 |
default: throw new RuntimeException("figureOutNormalVector2D: impossible1: "+prvShape);
|
|
| 245 |
} |
|
| 246 |
} |
|
| 247 |
else |
|
| 248 |
{
|
|
| 249 |
int n = polyEndVer==mNumPolygonVertices-1 ? 0 : polyEndVer+1; |
|
| 250 |
int nxtShape = mEdgeShape[polyEndVer]; |
|
| 251 |
|
|
| 252 |
switch(nxtShape) |
|
| 253 |
{
|
|
| 254 |
case SHAPE_DD : mVecX = xEdge; |
|
| 255 |
mVecY = yEdge; |
|
| 256 |
break; |
|
| 257 |
case SHAPE_DU : |
|
| 258 |
case SHAPE_DUD: float a = 2-2*quot; |
|
| 259 |
float xCurr= mPolygonVertices[2*polyEndVer ]; |
|
| 260 |
float yCurr= mPolygonVertices[2*polyEndVer+1]; |
|
| 261 |
float xNext= mPolygonVertices[2*n ]; |
|
| 262 |
float yNext= mPolygonVertices[2*n+1]; |
|
| 263 |
float xVec = xCurr-xNext; |
|
| 264 |
float yVec = yCurr-yNext; |
|
| 265 |
|
|
| 266 |
mVecX = a*xEdge + (1-a)*xVec; |
|
| 267 |
mVecY = a*yEdge + (1-a)*yVec; |
|
| 268 |
break; |
|
| 269 |
default: throw new RuntimeException("figureOutNormalVector2D: impossible2: "+nxtShape);
|
|
| 270 |
} |
|
| 271 |
} |
|
| 272 |
} |
|
| 273 |
else if( edgeShape==SHAPE_UU || (quot>=0.5f && edgeShape==SHAPE_DU) || (quot<0.5f && edgeShape==SHAPE_UD) ) |
|
| 274 |
{
|
|
| 275 |
mVecX = 1; |
|
| 276 |
mVecY = 0; |
|
| 277 |
} |
|
| 278 |
else |
|
| 279 |
{
|
|
| 280 |
float dx = mPolygonVertices[2*polyVertex ] - mPolygonVertices[2*polyEndVer ]; |
|
| 281 |
float dy = mPolygonVertices[2*polyVertex+1] - mPolygonVertices[2*polyEndVer+1]; |
|
| 282 |
|
|
| 283 |
if( quot<0.5 ) |
|
| 284 |
{
|
|
| 285 |
mVecX = dx; |
|
| 286 |
mVecY = dy; |
|
| 287 |
} |
|
| 288 |
else |
|
| 289 |
{
|
|
| 290 |
mVecX = -dx; |
|
| 291 |
mVecY = -dy; |
|
| 292 |
} |
|
| 293 |
} |
|
| 294 |
} |
|
| 295 |
|
|
| 296 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 297 |
|
|
| 298 |
private float figureOutDerivative(int edgeShape, int polyBand, float quot) |
|
| 299 |
{
|
|
| 300 |
if( edgeShape==SHAPE_DD ) |
|
| 301 |
{
|
|
| 302 |
return derivative(1-mPolygonBands[2*polyBand]); |
|
| 303 |
} |
|
| 304 |
if( edgeShape==SHAPE_UU || (quot>=0.5f && edgeShape==SHAPE_DU) || (quot<0.5f && edgeShape==SHAPE_UD) ) |
|
| 305 |
{
|
|
| 306 |
return 0; |
|
| 307 |
} |
|
| 308 |
|
|
| 309 |
float x = 1-mPolygonBands[2*polyBand]; |
|
| 310 |
float q = quot>=0.5f ? 2-2*quot : 2*quot; |
|
| 311 |
return derivative((1-x)*q+x); |
|
| 312 |
} |
|
| 313 |
|
|
| 314 | 180 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 315 | 181 |
|
| 316 | 182 |
private int addVertex(int vertex, int polyBand, int polyVertex, int polyEndVer, float quot, |
| ... | ... | |
| 325 | 191 |
|
| 326 | 192 |
float xEdge = Xfirst + quot*(Xlast-Xfirst); |
| 327 | 193 |
float yEdge = Yfirst + quot*(Ylast-Yfirst); |
| 328 |
float zEdge; |
|
| 329 | 194 |
|
| 330 | 195 |
float q = mPolygonBands[2*polyBand]; |
| 331 |
float o = mPolygonBands[2*polyBand+1]; |
|
| 332 |
float t = mPolygonBands[2*mNumPolygonBands-1]; |
|
| 333 |
|
|
| 334 |
int shape = mEdgeShape[polyVertex]; |
|
| 335 |
|
|
| 336 |
switch(shape) |
|
| 337 |
{
|
|
| 338 |
case SHAPE_DD : zEdge = 0.0f; break; |
|
| 339 |
case SHAPE_UU : zEdge = t; break; |
|
| 340 |
case SHAPE_DU : zEdge = quot>=0.5f ? t : computeZEdge(1-2*quot); break; |
|
| 341 |
case SHAPE_UD : zEdge = quot<=0.5f ? t : computeZEdge(2*quot-1); break; |
|
| 342 |
default : zEdge = quot<=0.5f ? computeZEdge(1-2*quot) : computeZEdge(2*quot-1); break; |
|
| 343 |
} |
|
| 344 |
|
|
| 345 | 196 |
float x = q*xEdge; |
| 346 | 197 |
float y = q*yEdge; |
| 347 |
float z = o + (t-o)*(zEdge/t);
|
|
| 198 |
float z = mPolygonBands[2*polyBand+1];
|
|
| 348 | 199 |
|
| 349 | 200 |
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB ] = x; |
| 350 | 201 |
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+1] = y; |
| 351 | 202 |
attribs1[VERT1_ATTRIBS*vertex + POS_ATTRIB+2] = z; |
| 352 | 203 |
|
| 353 |
figureOutNormalVector2D(shape, polyVertex, polyEndVer, quot, xEdge, yEdge); |
|
| 354 |
float d = figureOutDerivative(shape,polyBand,quot); |
|
| 204 |
float d = derivative(1-mPolygonBands[2*polyBand]); |
|
| 355 | 205 |
|
| 356 |
float vx = d*mVecX;
|
|
| 357 |
float vy = d*mVecY;
|
|
| 358 |
float vz = mVecX*mVecX + mVecY*mVecY;
|
|
| 206 |
float vx = d*xEdge;
|
|
| 207 |
float vy = d*yEdge;
|
|
| 208 |
float vz = xEdge*xEdge + yEdge*yEdge;
|
|
| 359 | 209 |
float len = (float)Math.sqrt(vx*vx + vy*vy + vz*vz); |
| 360 | 210 |
|
| 361 | 211 |
int index = VERT1_ATTRIBS*vertex + NOR_ATTRIB; |
| ... | ... | |
| 379 | 229 |
if( polyBand>0 ) vertex = addVertex(vertex,polyBand,0,1,0,attribs1,attribs2); |
| 380 | 230 |
} |
| 381 | 231 |
|
| 382 |
boolean specialCase = mNumPolygonBands==2 && polyBand==0 && extraIndex>0;
|
|
| 383 |
boolean isExtra = polyBand<extraIndex;
|
|
| 384 |
int numPairs = specialCase ? extraIndex+1 : mNumPolygonBands-1-polyBand;
|
|
| 232 |
boolean specialCase = mNumPolygonBands==2 && polyBand==0 && extraBand>0;
|
|
| 233 |
boolean isExtra = polyBand<extraBand;
|
|
| 234 |
int numPairs = specialCase ? extraBand+1 : mNumPolygonBands-1-polyBand;
|
|
| 385 | 235 |
if( isExtra ) numPairs += 2*extraVertices; |
| 386 | 236 |
|
| 387 | 237 |
int polyEndVer = polyVertex==mNumPolygonVertices-1 ? 0 : polyVertex+1; |
| ... | ... | |
| 407 | 257 |
return vertex; |
| 408 | 258 |
} |
| 409 | 259 |
|
| 410 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 411 |
|
|
| 412 |
private int computeEdgeShape(int curr) |
|
| 413 |
{
|
|
| 414 |
if( mEdgeUp==null || !mEdgeUp[curr] ) return SHAPE_DD; |
|
| 415 |
|
|
| 416 |
int prev = (curr==0 ? mNumPolygonVertices-1 : curr-1); |
|
| 417 |
int next = (curr==mNumPolygonVertices-1 ? 0 : curr+1); |
|
| 418 |
|
|
| 419 |
boolean rd = ( !mEdgeUp[next] || mVertUp==null || !mVertUp[next] ); |
|
| 420 |
boolean ld = ( !mEdgeUp[prev] || mVertUp==null || !mVertUp[curr] ); |
|
| 421 |
|
|
| 422 |
return rd ? (ld ? SHAPE_DUD : SHAPE_UD) : (ld ? SHAPE_DU : SHAPE_UU); |
|
| 423 |
} |
|
| 424 |
|
|
| 425 | 260 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 426 | 261 |
|
| 427 | 262 |
private void buildGrid(float[] attribs1, float[] attribs2) |
| 428 | 263 |
{
|
| 429 | 264 |
int vertex=0; |
| 430 | 265 |
|
| 431 |
mEdgeShape = new int[mNumPolygonVertices]; |
|
| 432 |
|
|
| 433 |
for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++) |
|
| 434 |
mEdgeShape[polyVertex] = computeEdgeShape(polyVertex); |
|
| 435 |
|
|
| 436 | 266 |
for(int polyBand=0; polyBand<mNumPolygonBands-1; polyBand++) |
| 437 | 267 |
for(int polyVertex=0; polyVertex<mNumPolygonVertices; polyVertex++) |
| 438 | 268 |
vertex = createBandStrip(vertex,polyBand,polyVertex,attribs1,attribs2); |
| ... | ... | |
| 451 | 281 |
* From (1.0,Z[0]) (outer edge, its Z elevation) to (0.0,Z[K]) (the center, |
| 452 | 282 |
* its elevation). The polygon is split into such concentric bands. |
| 453 | 283 |
* Must be band[2*i] > band[2*(i+1)] ! |
| 454 |
* @param edgeUp N booleans - one for each edge; the first connects vertices (0,1) and (2,3); |
|
| 455 |
* then just like 'verticesXY', i.e. counterclockwise. |
|
| 456 |
* If this is null, all edges are by default 'down'. |
|
| 457 |
* 'Down' means that edge's Z is equal to 0; 'up' means that its Z, at least in its |
|
| 458 |
* middle, is equal to the highest elevation in the middle of the mesh. |
|
| 459 |
* If the 'previous' edge is also up, then the Z is up horizontally from its middle |
|
| 460 |
* to the left vertex; else it goes down along the same function it goes along the bands. |
|
| 461 |
* Same with the right half of the edge. |
|
| 462 |
* @param vertUp N booleans - one for each vertex. The first is about vertex 0, then vertex 1, etc. |
|
| 463 |
* If this is null or false, the vertex is down; otheerwise - it is 'up'. |
|
| 464 |
* This is taken into account only in one case: if both edges the vertex is the end of |
|
| 465 |
* are 'up', then the vertex can be 'up' or 'down'; otherwise - if at least one of |
|
| 466 |
* those edges is 'down' - then the vertex must be 'down' as well and this is ignored. |
|
| 467 |
* @param exIndex This and the next parameter describe how to make the mesh denser at the |
|
| 284 |
* @param exBand This and the next parameter describe how to make the mesh denser at the |
|
| 468 | 285 |
* polyVertices. If e.g. exIndex=3 and exVertices=2, then 3 triangles of the |
| 469 | 286 |
* outermost band (and 2 triangles of the next band, and 1 triangle of the third |
| 470 | 287 |
* band) get denser - the 3 triangles become 3+2 = 5. |
| ... | ... | |
| 473 | 290 |
* all bands go to. |
| 474 | 291 |
* @param centerY Y coordinate of the center. |
| 475 | 292 |
*/ |
| 476 |
public MeshPolygon(float[] verticesXY, float[] bands, boolean[] edgeUp, boolean[] vertUp, int exIndex, int exVertices, float centerX, float centerY)
|
|
| 293 |
public MeshPolygon(float[] verticesXY, float[] bands, int exBand, int exVertices, float centerX, float centerY)
|
|
| 477 | 294 |
{
|
| 478 | 295 |
super(); |
| 479 | 296 |
|
| ... | ... | |
| 481 | 298 |
mPolygonBands = bands; |
| 482 | 299 |
mNumPolygonVertices= mPolygonVertices.length /2; |
| 483 | 300 |
mNumPolygonBands = mPolygonBands.length /2; |
| 484 |
mEdgeUp = edgeUp; |
|
| 485 |
mVertUp = vertUp; |
|
| 486 |
extraIndex = exIndex; |
|
| 301 |
extraBand = exBand; |
|
| 487 | 302 |
extraVertices = exVertices; |
| 488 | 303 |
|
| 489 | 304 |
if( centerX!=0.0f || centerY!=0.0f ) |
| ... | ... | |
| 524 | 339 |
|
| 525 | 340 |
public MeshPolygon(float[] verticesXY, float[] bands, int exIndex, int exVertices) |
| 526 | 341 |
{
|
| 527 |
this(verticesXY,bands,null,null,exIndex,exVertices,0.0f,0.0f);
|
|
| 342 |
this(verticesXY,bands,exIndex,exVertices,0.0f,0.0f); |
|
| 528 | 343 |
} |
| 529 | 344 |
|
| 530 | 345 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 534 | 349 |
*/ |
| 535 | 350 |
public MeshPolygon(float[] verticesXY, float[] bands) |
| 536 | 351 |
{
|
| 537 |
this(verticesXY,bands,null,null,0,0,0.0f,0.0f);
|
|
| 352 |
this(verticesXY,bands,0,0,0.0f,0.0f); |
|
| 538 | 353 |
} |
| 539 | 354 |
|
| 540 | 355 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
We no longer need to support 'edgesUp' in MeshPolygons.