Revision 3083fab8
Added by Leszek Koltunski about 3 years ago
src/main/java/org/distorted/examples/meshfile/FactoryCubit.java | ||
---|---|---|
41 | 41 |
private static final double[] mQuat1 = new double[4]; |
42 | 42 |
private static final double[] mQuat2 = new double[4]; |
43 | 43 |
private static final double[] mQuat3 = new double[4]; |
44 |
private static final double[] mQuat4 = new double[4]; |
|
44 | 45 |
|
45 | 46 |
private static final Static1D RADIUS = new Static1D(1); |
46 | 47 |
|
... | ... | |
49 | 50 |
private static class StickerInfo |
50 | 51 |
{ |
51 | 52 |
double[] vertices; |
52 |
double dx,dy; |
|
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
private static class FaceInfo |
... | ... | |
308 | 308 |
if (y < minY) minY = y; |
309 | 309 |
} |
310 | 310 |
|
311 |
info.scale = Math.max(maxX-minX,maxY-minY); |
|
311 |
minX = minX<0 ? -minX:minX; |
|
312 |
maxX = maxX<0 ? -maxX:maxX; |
|
313 |
minY = minY<0 ? -minY:minY; |
|
314 |
maxY = maxY<0 ? -maxY:maxY; |
|
315 |
|
|
316 |
double max1 = Math.max(minX,minY); |
|
317 |
double max2 = Math.max(maxX,maxY); |
|
318 |
double max3 = Math.max(max1,max2); |
|
319 |
|
|
320 |
info.scale = max3/0.5; |
|
312 | 321 |
|
313 | 322 |
int len = vert3D.length; |
314 | 323 |
StickerInfo sInfo = new StickerInfo(); |
... | ... | |
431 | 440 |
|
432 | 441 |
private double computeCos(double oldX, double oldY, double newX, double newY, double len1, double len2) |
433 | 442 |
{ |
434 |
return (oldX*newX+oldY*newY) / (len1*len2); |
|
443 |
double ret= (oldX*newX+oldY*newY) / (len1*len2); |
|
444 |
if( ret<-1.0 ) return -1.0; |
|
445 |
if( ret> 1.0 ) return 1.0; |
|
446 |
|
|
447 |
return ret; |
|
435 | 448 |
} |
436 | 449 |
|
437 | 450 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
439 | 452 |
|
440 | 453 |
private double computeSin(double oldX, double oldY, double newX, double newY, double len1, double len2) |
441 | 454 |
{ |
442 |
return (newX*oldY-oldX*newY) / (len1*len2); |
|
455 |
double ret= (newX*oldY-oldX*newY) / (len1*len2); |
|
456 |
if( ret<-1.0 ) return -1.0; |
|
457 |
if( ret> 1.0 ) return 1.0; |
|
458 |
|
|
459 |
return ret; |
|
443 | 460 |
} |
444 | 461 |
|
445 | 462 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
480 | 497 |
|
481 | 498 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
482 | 499 |
|
483 |
private boolean isScaledVersionOf(double[] newVert, double[] oldVert, int len) |
|
500 |
private int computeRotatedIndex(int oldVertex, int len, int rotatedVertex, boolean inverted) |
|
501 |
{ |
|
502 |
int v = (rotatedVertex + (inverted? -oldVertex : oldVertex)); |
|
503 |
if( v>=len ) v-=len; |
|
504 |
if( v< 0 ) v+=len; |
|
505 |
|
|
506 |
return v; |
|
507 |
} |
|
508 |
|
|
509 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
510 |
|
|
511 |
private boolean isScaledVersionOf(double[] newVert, double[] oldVert, int len, int vertex, boolean inverted) |
|
484 | 512 |
{ |
485 | 513 |
double EPSILON = 0.001; |
486 | 514 |
double scale = computeScale(newVert,oldVert); |
487 | 515 |
|
488 | 516 |
for(int i=1; i<len; i++) |
489 | 517 |
{ |
490 |
double horz = oldVert[2*i ] - scale*newVert[2*i ]; |
|
491 |
double vert = oldVert[2*i+1] - scale*newVert[2*i+1]; |
|
518 |
int index = computeRotatedIndex(i,len,vertex,inverted); |
|
519 |
|
|
520 |
double horz = oldVert[2*i ] - scale*newVert[2*index ]; |
|
521 |
double vert = oldVert[2*i+1] - scale*newVert[2*index+1]; |
|
492 | 522 |
|
493 | 523 |
if( horz>EPSILON || horz<-EPSILON || vert>EPSILON || vert<-EPSILON ) return false; |
494 | 524 |
} |
... | ... | |
525 | 555 |
double sinHalf = computeSinHalf(cos); |
526 | 556 |
double cosHalf = computeCosHalf(sin,cos); |
527 | 557 |
|
528 |
mQuat2[0] = 0.0f; |
|
529 |
mQuat2[1] = 0.0f; |
|
530 |
mQuat2[2] = sinHalf; |
|
531 |
mQuat2[3] = cosHalf; |
|
558 |
if( flip ) |
|
559 |
{ |
|
560 |
mQuat3[0] = 0.0f; |
|
561 |
mQuat3[1] = 0.0f; |
|
562 |
mQuat3[2] = sinHalf; |
|
563 |
mQuat3[3] = cosHalf; |
|
564 |
|
|
565 |
mQuat4[0] = 1.0; |
|
566 |
mQuat4[1] = 0.0; |
|
567 |
mQuat4[2] = 0.0; |
|
568 |
mQuat4[3] = 0.0; |
|
569 |
|
|
570 |
quatMultiply( mQuat3, mQuat4, mQuat2 ); |
|
571 |
} |
|
572 |
else |
|
573 |
{ |
|
574 |
mQuat2[0] = 0.0f; |
|
575 |
mQuat2[1] = 0.0f; |
|
576 |
mQuat2[2] = sinHalf; |
|
577 |
mQuat2[3] = cosHalf; |
|
578 |
} |
|
532 | 579 |
|
533 | 580 |
quatMultiply( mQuat1, mQuat2, mQuat3 ); |
534 | 581 |
|
... | ... | |
538 | 585 |
info.qw = mQuat3[3]; |
539 | 586 |
} |
540 | 587 |
|
588 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
589 |
|
|
590 |
private void printVert(double[] buffer) |
|
591 |
{ |
|
592 |
int len = buffer.length/2; |
|
593 |
String str = ""; |
|
594 |
|
|
595 |
for(int i=0; i<len; i++) |
|
596 |
{ |
|
597 |
str += (" ("+buffer[2*i]+" , "+buffer[2*i+1]+" ) "); |
|
598 |
} |
|
599 |
|
|
600 |
android.util.Log.d("D", str); |
|
601 |
} |
|
602 |
|
|
541 | 603 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
542 | 604 |
|
543 | 605 |
private boolean foundVertex(FaceInfo info, double[] buffer, int len, double[] newVert, |
... | ... | |
553 | 615 |
|
554 | 616 |
rotateAllVertices(buffer,len,newVert,sin,cos); |
555 | 617 |
|
556 |
if( isScaledVersionOf(buffer,oldVert,len) ) |
|
618 |
if( isScaledVersionOf(buffer,oldVert,len,vertex,inverted) )
|
|
557 | 619 |
{ |
558 | 620 |
double scale = computeScale(oldVert,newVert); |
559 | 621 |
correctInfo(info,scale,sin,cos,oldSticker,inverted); |
... | ... | |
607 | 669 |
return ret; |
608 | 670 |
} |
609 | 671 |
|
610 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
611 |
|
|
612 |
private void centerTextures() |
|
613 |
{ |
|
614 |
int numStickers = mStickerInfo.size(); |
|
615 |
StickerInfo info; |
|
616 |
|
|
617 |
for(int sticker=0; sticker<numStickers; sticker++) |
|
618 |
{ |
|
619 |
double minX = Double.MAX_VALUE; |
|
620 |
double minY = Double.MAX_VALUE; |
|
621 |
double maxX =-Double.MAX_VALUE; |
|
622 |
double maxY =-Double.MAX_VALUE; |
|
623 |
|
|
624 |
info = mStickerInfo.get(sticker); |
|
625 |
double[] vert = info.vertices; |
|
626 |
int numVert = vert.length/2; |
|
627 |
|
|
628 |
for ( int v=0; v<numVert; v++) |
|
629 |
{ |
|
630 |
double x = vert[2*v ]; |
|
631 |
double y = vert[2*v+1]; |
|
632 |
|
|
633 |
if (x < minX) minX = x; |
|
634 |
if (y < minY) minY = y; |
|
635 |
if (x > maxX) maxX = x; |
|
636 |
if (y > maxY) maxY = y; |
|
637 |
} |
|
638 |
|
|
639 |
info.dx = info.dy = 0.0; |
|
640 |
|
|
641 |
if( minX<-0.5 ) info.dx = minX + 0.5; |
|
642 |
if( minY<-0.5 ) info.dy = minY + 0.5; |
|
643 |
if( maxX> 0.5 ) info.dx = maxX - 0.5; |
|
644 |
if( maxY> 0.5 ) info.dy = maxY - 0.5; |
|
645 |
|
|
646 |
for ( int v=0; v<numVert; v++) |
|
647 |
{ |
|
648 |
vert[2*v ] -= info.dx; |
|
649 |
vert[2*v+1] -= info.dy; |
|
650 |
} |
|
651 |
} |
|
652 |
} |
|
653 |
|
|
654 | 672 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
655 | 673 |
|
656 | 674 |
private void prepareFaceInfo( final double[][] vertices, final int[][] indexes) |
... | ... | |
676 | 694 |
|
677 | 695 |
mFaceInfo.add(newInfo); |
678 | 696 |
} |
679 |
|
|
680 |
centerTextures(); |
|
681 | 697 |
} |
682 | 698 |
|
683 | 699 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
815 | 831 |
{ |
816 | 832 |
int assoc = (1<<face); |
817 | 833 |
fInfo = mFaceInfo.get(face); |
818 |
sInfo = mStickerInfo.get(fInfo.sticker); |
|
819 | 834 |
|
820 |
float dx = (float)sInfo.dx; |
|
821 |
float dy = (float)sInfo.dy; |
|
822 | 835 |
float vx = (float)fInfo.vx; |
823 | 836 |
float vy = (float)fInfo.vy; |
824 | 837 |
float vz = (float)fInfo.vz; |
... | ... | |
828 | 841 |
float qz = (float)fInfo.qz; |
829 | 842 |
float qw = (float)fInfo.qw; |
830 | 843 |
|
831 |
Static3D move2D= new Static3D(dx,dy,0.0f); |
|
832 |
Static3D move3D= new Static3D(vx,vy,vz); |
|
833 | 844 |
Static3D scale = new Static3D(sc,sc, fInfo.flip ? -sc : sc); |
845 |
Static3D move3D= new Static3D(vx,vy,vz); |
|
834 | 846 |
Static4D quat = new Static4D(qx,qy,qz,qw); |
835 | 847 |
|
836 |
mesh.apply(new MatrixEffectMove(move2D) ,assoc,-1); |
|
837 | 848 |
mesh.apply(new MatrixEffectScale(scale) ,assoc,-1); |
838 | 849 |
mesh.apply(new MatrixEffectQuaternion(quat,center),assoc,-1); |
839 | 850 |
mesh.apply(new MatrixEffectMove(move3D) ,assoc,-1); |
Also available in: Unified diff
Cubit creation: properly cener the textures. Add Kilominx edge.