Revision d6994cc6
Added by Leszek Koltunski almost 9 years ago
| src/main/java/org/distorted/library/MeshCubes.java | ||
|---|---|---|
| 64 | 64 |
} |
| 65 | 65 |
|
| 66 | 66 |
private int mCols, mRows; |
| 67 |
private short[][] mCubes;
|
|
| 67 |
private int[][] mCubes;
|
|
| 68 | 68 |
private ArrayList<Edge> mEdges = new ArrayList<>(); |
| 69 | 69 |
|
| 70 | 70 |
private int remainingVert; |
| ... | ... | |
| 161 | 161 |
|
| 162 | 162 |
return ret; |
| 163 | 163 |
} |
| 164 |
*/
|
|
| 164 |
*/ |
|
| 165 | 165 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 166 | 166 |
/* |
| 167 | 167 |
private static String debug(Edge e) |
| ... | ... | |
| 194 | 194 |
mCols = cols; |
| 195 | 195 |
mRows = desc.length()/cols; |
| 196 | 196 |
|
| 197 |
mCubes = new short[mRows][mCols];
|
|
| 197 |
mCubes = new int[mRows][mCols];
|
|
| 198 | 198 |
|
| 199 | 199 |
for(int j=0; j<mCols; j++) |
| 200 | 200 |
for(int i=0; i<mRows; i++) |
| 201 |
mCubes[i][j] = (short)(desc.charAt(i*mCols+j) == '1' ? 1:0); |
|
| 202 |
|
|
| 201 |
mCubes[i][j] = (desc.charAt(i*mCols+j) == '1' ? 1:0); |
|
| 202 |
|
|
| 203 |
buildBoundingVert(frontOnly); |
|
| 204 |
|
|
| 203 | 205 |
markRegions(); |
| 204 | 206 |
dataLength = computeDataLength(frontOnly); |
| 205 | 207 |
|
| 206 | 208 |
remainingVert = dataLength; |
| 207 |
buildBoundingVert(mCubes,frontOnly); |
|
| 208 | 209 |
} |
| 209 | 210 |
} |
| 210 | 211 |
|
| ... | ... | |
| 219 | 220 |
|
| 220 | 221 |
if( cols>0 && rows>0 ) |
| 221 | 222 |
{
|
| 222 |
mCubes = new short[mRows][mCols];
|
|
| 223 |
mCubes = new int[mRows][mCols];
|
|
| 223 | 224 |
|
| 224 | 225 |
for(int j=0; j<mCols; j++) |
| 225 | 226 |
for(int i=0; i<mRows; i++) |
| 226 |
mCubes[i][j] = (short)1;
|
|
| 227 |
mCubes[i][j] = 1; |
|
| 227 | 228 |
|
| 228 | 229 |
markRegions(); |
| 229 | 230 |
dataLength = computeDataLength(frontOnly); |
| ... | ... | |
| 236 | 237 |
|
| 237 | 238 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 238 | 239 |
|
| 239 |
private float retLeftmost(short[][] cubes, int row)
|
|
| 240 |
private float retRightmost(int row)
|
|
| 240 | 241 |
{
|
| 242 |
if( row==0 ) |
|
| 243 |
{
|
|
| 244 |
for(int i=mCols-1; i>=0; i--) |
|
| 245 |
if( mCubes[0][i]==1 ) |
|
| 246 |
return (float)(i+1); |
|
| 247 |
} |
|
| 248 |
else if(row==mRows) |
|
| 249 |
{
|
|
| 250 |
for(int i=mCols-1; i>=0; i--) |
|
| 251 |
if( mCubes[mRows-1][i]==1 ) |
|
| 252 |
return (float)(i+1); |
|
| 253 |
} |
|
| 254 |
else |
|
| 255 |
{
|
|
| 256 |
for(int i=mCols-1; i>=0; i--) |
|
| 257 |
if( mCubes[row][i]==1 || mCubes[row-1][i]==1 ) |
|
| 258 |
return (float)(i+1); |
|
| 259 |
} |
|
| 241 | 260 |
|
| 242 | 261 |
return 0.0f; |
| 243 | 262 |
} |
| 244 | 263 |
|
| 245 | 264 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 246 | 265 |
|
| 247 |
private float retRightmost(short[][] cubes, int row)
|
|
| 266 |
private float retLeftmost(int row)
|
|
| 248 | 267 |
{
|
| 249 | 268 |
if( row==0 ) |
| 250 | 269 |
{
|
| 251 | 270 |
for(int i=0; i<mCols; i++) |
| 252 |
{
|
|
| 253 |
|
|
| 254 |
} |
|
| 271 |
if( mCubes[0][i]==1 ) |
|
| 272 |
return (float)i; |
|
| 255 | 273 |
} |
| 256 | 274 |
else if(row==mRows) |
| 257 | 275 |
{
|
| 258 | 276 |
for(int i=0; i<mCols; i++) |
| 259 |
{
|
|
| 260 |
|
|
| 261 |
} |
|
| 277 |
if( mCubes[mRows-1][i]==1 ) |
|
| 278 |
return (float)i; |
|
| 262 | 279 |
} |
| 263 | 280 |
else |
| 264 | 281 |
{
|
| 265 | 282 |
for(int i=0; i<mCols; i++) |
| 266 |
{
|
|
| 267 |
|
|
| 268 |
} |
|
| 283 |
if( mCubes[row][i]==1 || mCubes[row-1][i]==1 ) |
|
| 284 |
return (float)i; |
|
| 269 | 285 |
} |
| 270 | 286 |
|
| 271 |
return 0.0f;
|
|
| 287 |
return (float)mCols;
|
|
| 272 | 288 |
} |
| 273 | 289 |
|
| 274 | 290 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 275 | 291 |
|
| 276 |
private int addLeftmost(float[] temp, short[][] cubes, int row, int index)
|
|
| 292 |
private int addLeftmost(float[] temp, int row, int index) |
|
| 277 | 293 |
{
|
| 278 |
float x2 = retLeftmost(cubes,row);
|
|
| 279 |
float y2 = row/mRows - 0.5f;
|
|
| 294 |
float x2 = retLeftmost(row)/mCols - 0.5f;
|
|
| 295 |
float y2 = (float)row /mRows - 0.5f;
|
|
| 280 | 296 |
|
| 281 | 297 |
if( index>1 ) |
| 282 | 298 |
{
|
| ... | ... | |
| 285 | 301 |
float x1 = temp[2*index-2]; |
| 286 | 302 |
float y1 = temp[2*index-1]; |
| 287 | 303 |
|
| 288 |
if( (x0-x2)*(y0-y1) <= (x0-x1)*(y0-y2) )
|
|
| 304 |
while( (x0-x2)*(y0-y1) <= (x0-x1)*(y0-y2) )
|
|
| 289 | 305 |
{
|
| 290 |
temp[2*index-2] = x2; |
|
| 291 |
temp[2*index-1] = y2; |
|
| 292 |
return index; |
|
| 306 |
if( --index>1 ) |
|
| 307 |
{
|
|
| 308 |
x1 = x0; |
|
| 309 |
y1 = y0; |
|
| 310 |
x0 = temp[2*index-4]; |
|
| 311 |
y0 = temp[2*index-3]; |
|
| 312 |
} |
|
| 313 |
else break; |
|
| 293 | 314 |
} |
| 294 | 315 |
} |
| 295 | 316 |
|
| 296 |
temp[2*index+0] = x2;
|
|
| 317 |
temp[2*index ] = x2;
|
|
| 297 | 318 |
temp[2*index+1] = y2; |
| 298 | 319 |
return index+1; |
| 299 | 320 |
} |
| 300 | 321 |
|
| 301 | 322 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 302 | 323 |
|
| 303 |
private int addRightmost(float[] temp, short[][] cubes, int row, int index)
|
|
| 324 |
private int addRightmost(float[] temp, int row, int index) |
|
| 304 | 325 |
{
|
| 305 |
float x2 = retRightmost(cubes,row);
|
|
| 306 |
float y2 = row/mRows - 0.5f;
|
|
| 326 |
float x2 = retRightmost(row)/mCols - 0.5f;
|
|
| 327 |
float y2 = (float)row /mRows - 0.5f;
|
|
| 307 | 328 |
|
| 308 | 329 |
if( index>1 ) |
| 309 | 330 |
{
|
| ... | ... | |
| 312 | 333 |
float x1 = temp[2*index-2]; |
| 313 | 334 |
float y1 = temp[2*index-1]; |
| 314 | 335 |
|
| 315 |
if( (x0-x2)*(y0-y1) >= (x0-x1)*(y0-y2) )
|
|
| 336 |
while( (x0-x2)*(y0-y1) >= (x0-x1)*(y0-y2) )
|
|
| 316 | 337 |
{
|
| 317 |
temp[2*index-2] = x2; |
|
| 318 |
temp[2*index-1] = y2; |
|
| 319 |
return index; |
|
| 338 |
if( --index>1 ) |
|
| 339 |
{
|
|
| 340 |
x1 = x0; |
|
| 341 |
y1 = y0; |
|
| 342 |
x0 = temp[2*index-4]; |
|
| 343 |
y0 = temp[2*index-3]; |
|
| 344 |
} |
|
| 345 |
else break; |
|
| 320 | 346 |
} |
| 321 | 347 |
} |
| 322 | 348 |
|
| 323 |
temp[2*index+0] = x2;
|
|
| 349 |
temp[2*index ] = x2;
|
|
| 324 | 350 |
temp[2*index+1] = y2; |
| 325 | 351 |
return index+1; |
| 326 | 352 |
} |
| ... | ... | |
| 329 | 355 |
// fill up the mBoundingVert array with the smallest set of vertices which form the same convex hull |
| 330 | 356 |
// like the whole thing. |
| 331 | 357 |
|
| 332 |
private void buildBoundingVert(short[][] cubes, boolean frontOnly)
|
|
| 358 |
private void buildBoundingVert(boolean frontOnly) |
|
| 333 | 359 |
{
|
| 334 | 360 |
int lVert=0, rVert=0; |
| 335 |
float[] tempL = new float[2*(mRows+1)]; |
|
| 336 |
float[] tempR = new float[2*(mRows+1)]; |
|
| 361 |
int firstRow=0, lastRow=mRows-1; |
|
| 337 | 362 |
|
| 338 |
for(int i=0; i<=mRows; i++) |
|
| 363 |
for(int i=0; i<mRows; i++) |
|
| 364 |
for(int j=0; j<mCols; j++) |
|
| 365 |
if(mCubes[i][j] !=0 ) |
|
| 366 |
{
|
|
| 367 |
firstRow = i; |
|
| 368 |
i=mRows; |
|
| 369 |
j=mCols; |
|
| 370 |
} |
|
| 371 |
|
|
| 372 |
for(int i=mRows-1; i>=0; i--) |
|
| 373 |
for(int j=0; j<mCols; j++) |
|
| 374 |
if(mCubes[i][j] !=0 ) |
|
| 375 |
{
|
|
| 376 |
lastRow = i; |
|
| 377 |
i=-1; |
|
| 378 |
j=mCols; |
|
| 379 |
} |
|
| 380 |
|
|
| 381 |
//android.util.Log.d("cubes", "first :"+firstRow+" last: "+lastRow);
|
|
| 382 |
|
|
| 383 |
int numLines = lastRow-firstRow+2; |
|
| 384 |
|
|
| 385 |
float[] tempL = new float[2*numLines]; |
|
| 386 |
float[] tempR = new float[2*numLines]; |
|
| 387 |
|
|
| 388 |
for(int i=firstRow; i<=lastRow+1; i++) |
|
| 339 | 389 |
{
|
| 340 |
lVert = addLeftmost (tempL,cubes,i,lVert);
|
|
| 341 |
rVert = addRightmost(tempR,cubes,i,rVert);
|
|
| 390 |
lVert = addLeftmost (tempL,i,lVert); |
|
| 391 |
rVert = addRightmost(tempR,i,rVert); |
|
| 342 | 392 |
} |
| 343 | 393 |
|
| 394 |
//android.util.Log.d("cubes", "LEFT :"+debug(tempL,2));
|
|
| 395 |
//android.util.Log.d("cubes", "RIGHT:"+debug(tempR,2));
|
|
| 396 |
|
|
| 344 | 397 |
int numVert = lVert+rVert; |
| 345 | 398 |
|
| 346 | 399 |
mBoundingVert = new float[ numVert*(frontOnly ? 3:6) ]; |
| ... | ... | |
| 368 | 421 |
mBoundingVert[3*(i+numVert)+2] = BACKZ; |
| 369 | 422 |
} |
| 370 | 423 |
} |
| 424 |
|
|
| 425 |
//android.util.Log.d("cubes", "VERT:"+debug(mBoundingVert,3));
|
|
| 371 | 426 |
} |
| 372 | 427 |
|
| 373 | 428 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 451 | 506 |
|
| 452 | 507 |
private void markRegion(short newVal, int row, int col) |
| 453 | 508 |
{
|
| 454 |
short val = mCubes[row][col];
|
|
| 509 |
int val = mCubes[row][col];
|
|
| 455 | 510 |
mCubes[row][col] = newVal; |
| 456 | 511 |
|
| 457 | 512 |
if( row>0 && mCubes[row-1][col ]==val ) markRegion(newVal, row-1, col ); |
| ... | ... | |
| 560 | 615 |
|
| 561 | 616 |
private int buildFrontBackGrid(boolean front, int vertex, float[] position, float[] normal, float[] texture) |
| 562 | 617 |
{
|
| 563 |
short last, current;
|
|
| 618 |
int last, current;
|
|
| 564 | 619 |
boolean seenLand=false; |
| 565 | 620 |
boolean lastBlockIsNE = false; |
| 566 | 621 |
boolean currentBlockIsNE; |
Also available in: Unified diff
Correct construction of boundingVertices in MeshCubes.