Project

General

Profile

« Previous | Next » 

Revision 72059460

Added by Leszek Koltunski about 4 years ago

MeshFile.

View differences:

src/main/java/org/distorted/examples/meshfile/MeshFileRenderer.java
35 35
import org.distorted.library.effect.VertexEffectMove;
36 36
import org.distorted.library.effect.VertexEffectRotate;
37 37
import org.distorted.library.effect.VertexEffectScale;
38
import org.distorted.library.effect.VertexEffectSink;
39 38
import org.distorted.library.main.DistortedEffects;
40 39
import org.distorted.library.main.DistortedLibrary;
41 40
import org.distorted.library.main.DistortedScreen;
......
44 43
import org.distorted.library.mesh.MeshFile;
45 44
import org.distorted.library.mesh.MeshJoined;
46 45
import org.distorted.library.mesh.MeshPolygon;
47
import org.distorted.library.mesh.MeshTriangle;
48 46
import org.distorted.library.type.DynamicQuat;
49 47
import org.distorted.library.type.Static1D;
50 48
import org.distorted.library.type.Static3D;
......
229 227
                                                                         0xffff0000,
230 228
                                                                         0xff000000 };
231 229
                                   return createTetrahedronTexture(TET_COLORS5);
232
          case  R.raw.dino       : final int[] TET_COLORS7 = new int[] { 0xffffff00,
230
          case  R.raw.dino       : final int[] DINO_COLORS = new int[] { 0xffffff00,
233 231
                                                                         0xffffffff,
234 232
                                                                         0xff0000ff,
235 233
                                                                         0xff00ff00,
236 234
                                                                         0xffff0000,
237
                                                                         0xffb5651d,
238
                                                                         0xff000000 };
239
                                   return createTetrahedronTexture(TET_COLORS7);
235
                                                                         0xffb5651d };
236
                                   return createDinoTexture(DINO_COLORS);
240 237
          case R.raw.skewb       : final int[] SKEW_COLORS = new int[] { 0xffffff00,
241 238
                                                                         0xffffffff,
242 239
                                                                         0xff0000ff,
......
430 427

  
431 428
///////////////////////////////////////////////////////////////////////////////////////////////////
432 429

  
433
  private MeshBase createStaticMesh()
430
  void createDinoFaceTexture(Canvas canvas, Paint paint, int[] faceColors, int face, int left, int top, int side)
434 431
    {
435
    /*
436
    final float SQ2 = (float)Math.sqrt(2);
437
    final float SQ3 = (float)Math.sqrt(3);
438
    final float angleFaces = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
439
    final int MESHES=4;
440

  
441
    int association = 1;
442
    MeshBase[] meshes;
443

  
444
    float D = 0.0005f;
445
    float E = SQ3/2 - 3*D*SQ2;
446
    float F = 0.5f - D*SQ2*SQ3;
447

  
448
    float[] vertices = { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
449
    float[] bands = new float[] {    1.0f    ,-D,
450
                                     1.0f  -D,-D*0.80f,
451
                                     1.0f-2*D,-D*0.65f,
452
                                     1.0f-4*D,+D*0.10f,
453
                                     0.50f, 0.035f,
454
                                     0.0f, 0.040f };
432
    float STROKE = 0.04f*side;
433
    float L= left;
434
    float H= 0.333f*side;
435
    float LEN = 0.5f*side;
436

  
437
    paint.setAntiAlias(true);
438
    paint.setStrokeWidth(STROKE);
439
    paint.setColor(faceColors[face]);
440
    paint.setStyle(Paint.Style.FILL);
441

  
442
    canvas.drawRect(left,top,left+side,top+side,paint);
443

  
444
    paint.setColor(0xff000000);
445
    paint.setStyle(Paint.Style.STROKE);
446

  
447
    canvas.drawLine( L      , H,  L+2*LEN, H    , paint);
448
    canvas.drawLine( L      , H,  L+  LEN, H+LEN, paint);
449
    canvas.drawLine( L+2*LEN, H,  L+  LEN, H+LEN, paint);
450

  
451
    float S1 = 0.150f*side;
452
    float S2 = 0.090f*side;
453
    float X  = 0.7f*S2;
454
    float Y  = 0.2f*S1;
455

  
456
    float LA = left+0.500f*side;
457
    float RA = left;
458
    float TA = 0.333f*side;
459
    float BA = 0.833f*side;
460

  
461
    canvas.drawArc( RA+X        , TA     , RA+X+S2  , TA+S2, 135,135, false, paint);
462
    canvas.drawArc( RA+side-S2-X, TA     , RA+side-X, TA+S2, 270,135, false, paint);
463
    canvas.drawArc( LA-S1/2     , BA-S1-Y, LA+S1/2  , BA-Y ,  45, 90, false, paint);
464
    }
455 465

  
456
    meshes = new MeshPolygon[MESHES];
457
    meshes[0] = new MeshPolygon(vertices, bands, 2,2);
458
    meshes[0].setEffectAssociation(0,association,0);
466
///////////////////////////////////////////////////////////////////////////////////////////////////
459 467

  
460
    for(int i=1; i<MESHES; i++)
468
    private Bitmap createDinoTexture(int[] faceColors)
461 469
      {
462
      association <<= 1;
463
      meshes[i] = meshes[0].copy(true);
464
      meshes[i].setEffectAssociation(0,association,0);
465
      }
466

  
467
    MeshBase result = new MeshJoined(meshes);
468

  
469
    Static3D a0 = new Static3D(         0,        1,       0 );
470
    Static3D a1 = new Static3D(         0,  -1.0f/3, 2*SQ2/3 );
471
    Static3D a2 = new Static3D(-SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 );
472
    Static3D a3 = new Static3D( SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 );
473

  
474
    float tetraHeight = SQ2*SQ3/3;
475
    float d1 = (0.75f-2*SQ2*D)*tetraHeight;
476
    float d2 =-0.06f*tetraHeight;
477
    float d3 = 0.05f*tetraHeight;
478
    float d4 = 0.70f*tetraHeight;
479
    float d5 = 1.2f;
480

  
481
    Static3D dCen0 = new Static3D( d1*a0.get0(), d1*a0.get1(), d1*a0.get2() );
482
    Static3D dCen1 = new Static3D( d1*a1.get0(), d1*a1.get1(), d1*a1.get2() );
483
    Static3D dCen2 = new Static3D( d1*a2.get0(), d1*a2.get1(), d1*a2.get2() );
484
    Static3D dCen3 = new Static3D( d1*a3.get0(), d1*a3.get1(), d1*a3.get2() );
485

  
486
    Static3D dVec0 = new Static3D( d2*a0.get0(), d2*a0.get1(), d2*a0.get2() );
487
    Static3D dVec1 = new Static3D( d2*a1.get0(), d2*a1.get1(), d2*a1.get2() );
488
    Static3D dVec2 = new Static3D( d2*a2.get0(), d2*a2.get1(), d2*a2.get2() );
489
    Static3D dVec3 = new Static3D( d2*a3.get0(), d2*a3.get1(), d2*a3.get2() );
490

  
491
    Static4D dReg  = new Static4D(0,0,0,d3);
492
    Static1D dRad  = new Static1D(1);
493
    Static3D center= new Static3D(0,0,0);
494
    Static4D sReg  = new Static4D(0,0,0,d4);
495
    Static1D sink  = new Static1D(d5);
496

  
497
    Static1D angle  = new Static1D(angleFaces);
498
    Static3D axis1  = new Static3D(  -1, 0,      0);
499
    Static3D axis2  = new Static3D(0.5f, 0, -SQ3/2);
500
    Static3D axis3  = new Static3D(0.5f, 0, +SQ3/2);
501
    Static3D center1= new Static3D(0,-SQ3*SQ2/12,-SQ3/6);
502
    Static3D center2= new Static3D(0,-SQ3*SQ2/12,+SQ3/3);
503

  
504
    VertexEffectRotate  effect1 = new VertexEffectRotate( new Static1D(90), new Static3D(1,0,0), center );
505
    VertexEffectMove    effect2 = new VertexEffectMove  ( new Static3D(0,-SQ3*SQ2/12,0) );
506
    VertexEffectRotate  effect3 = new VertexEffectRotate( new Static1D(180), new Static3D(0,0,1), center1 );
507
    VertexEffectRotate  effect4 = new VertexEffectRotate( angle, axis1, center1 );
508
    VertexEffectRotate  effect5 = new VertexEffectRotate( angle, axis2, center2 );
509
    VertexEffectRotate  effect6 = new VertexEffectRotate( angle, axis3, center2 );
510

  
511
    VertexEffectDeform  effect7 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
512
    VertexEffectDeform  effect8 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
513
    VertexEffectDeform  effect9 = new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
514
    VertexEffectDeform  effect10= new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
515

  
516
    VertexEffectSink    effect11= new VertexEffectSink(sink,center, sReg);
517

  
518
    effect3.setMeshAssociation(14,-1);  // apply to mesh[1], [2] and [3]
519
    effect4.setMeshAssociation( 2,-1);  // apply only to mesh[1]
520
    effect5.setMeshAssociation( 4,-1);  // apply only to mesh[2]
521
    effect6.setMeshAssociation( 8,-1);  // apply only to mesh[3]
470
      final int TEXTURE_HEIGHT = 256;
471
      final int NUM_TEXTURES = 6;
472
      Bitmap bitmap;
522 473

  
523
    result.apply(effect1);
524
    result.apply(effect2);
525
    result.apply(effect3);
526
    result.apply(effect4);
527
    result.apply(effect5);
528
    result.apply(effect6);
474
      Paint paint = new Paint();
475
      bitmap = Bitmap.createBitmap( (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
476
      Canvas canvas = new Canvas(bitmap);
529 477

  
530
    result.apply(effect7);
531
    result.apply(effect8);
532
    result.apply(effect9);
533
    result.apply(effect10);
478
      paint.setAntiAlias(true);
479
      paint.setTextAlign(Paint.Align.CENTER);
480
      paint.setStyle(Paint.Style.FILL);
534 481

  
535
    result.apply(effect11);
482
      paint.setColor(0xff000000);
483
      canvas.drawRect(0, 0, (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
536 484

  
537
    final float ratio = 0.2f;
538
    final Static4D[] maps = new Static4D[MESHES];
485
      for(int i=0; i<NUM_TEXTURES; i++)
486
        {
487
        createDinoFaceTexture(canvas, paint, faceColors, i, i*TEXTURE_HEIGHT, 0, TEXTURE_HEIGHT);
488
        }
539 489

  
540
    for(int mesh=0; mesh<MESHES; mesh++)
541
      {
542
      maps[mesh] = new Static4D( mesh*ratio, 0.0f, ratio, 1.0f);
490
      return bitmap;
543 491
      }
544 492

  
545
    result.setTextureMap(maps,0);
546
    result.setShowNormals(true);
493
///////////////////////////////////////////////////////////////////////////////////////////////////
547 494

  
548
    return result;
549
     */
495
  private MeshBase createStaticMesh()
496
    {
550 497
    final float SQ2 = (float)Math.sqrt(2);
551 498
    final float SQ3 = (float)Math.sqrt(3);
499
    final float ANGLE_FACES = (float)((180/Math.PI)*(Math.atan(SQ2)));
552 500

  
553
    final int FACES_PER_CUBIT=6;
501
    final int MESHES=4;
554 502

  
555 503
    float D = 0.02f;
556
    float E = 0.5f;
557
    float F = SQ2/2;
558

  
559
    float[] vertices0 = { -E+E/4,E/4, E/4,-E+E/4, E/4,E/4};
504
    float E = 0.5f*SQ2;
505
    float F = 0.5f;
560 506

  
561 507
    float[] bands0 = { 1.0f    , 0,
562 508
                       1.0f-2*D, D*0.25f,
......
566 512
                       0.30f   , D*1.375f,
567 513
                       0.0f    , D*1.4f };
568 514

  
569
    MeshBase[] meshes = new MeshBase[FACES_PER_CUBIT];
515
    float[] vertices0 = { -F,F/3, 0,-2*F/3, +F,F/3 };
570 516

  
571
    meshes[0] = new MeshPolygon(vertices0, bands0, 3, 3);
517
    MeshBase[] meshes = new MeshPolygon[MESHES];
518
    meshes[0] = new MeshPolygon(vertices0, bands0, 2, 5);
572 519
    meshes[0].setEffectAssociation(0,1,0);
573 520
    meshes[1] = meshes[0].copy(true);
574 521
    meshes[1].setEffectAssociation(0,2,0);
575
    meshes[2] = meshes[0].copy(true);
576
    meshes[2].setEffectAssociation(0,4,0);
577 522

  
578
    float[] vertices1 = { 0,0, F,0, F/2,(SQ3/2)*F };
579
    float[] bands1 = { 1.0f, 0.0f, 0.0f, 0.0f };
523
    float[] bands1 = { 1.0f    , 0,
524
                       0.50f   , 0,
525
                       0.0f    , 0 };
526

  
527
    float[] vertices1 = { -E/2,-E*(SQ3/6), E/2,-E*(SQ3/6), 0,E*(SQ3/3) };
580 528

  
581
    meshes[3] = new MeshPolygon(vertices1,bands1,0,0);
529
    meshes[2] = new MeshPolygon(vertices1, bands1, 1, 2);
530
    meshes[2].setEffectAssociation(0,4,0);
531
    meshes[3] = meshes[2].copy(true);
582 532
    meshes[3].setEffectAssociation(0,8,0);
583
    meshes[4] = meshes[3].copy(true);
584
    meshes[4].setEffectAssociation(0,16,0);
585
    meshes[5] = meshes[3].copy(true);
586
    meshes[5].setEffectAssociation(0,32,0);
587 533

  
588 534
    MeshBase result = new MeshJoined(meshes);
589 535

  
536
    Static3D a0 = new Static3D(     0,-3*F,    0 );
537
    Static3D a1 = new Static3D(     0,   0, -3*F );
538
    Static3D a2 = new Static3D(  -3*F,   0,    0 );
539
    Static3D a3 = new Static3D(  +3*F,   0,    0 );
540

  
541
    Static3D v0 = new Static3D(     0,-3*F/2, 3*F/2 );
542
    Static3D v1 = new Static3D(     0, 3*F/2,-3*F/2 );
543
    Static3D v2 = new Static3D(  -3*F, 3*F/2, 3*F/2 );
544
    Static3D v3 = new Static3D(  +3*F, 3*F/2, 3*F/2 );
545

  
546
    float d1 = 1.0f;
547
    float d2 =-0.10f;
548
    float d3 =-0.10f;
549
    float d4 = 0.40f;
550

  
551
    Static3D dCen0 = new Static3D( d1*a0.get0(), d1*a0.get1(), d1*a0.get2() );
552
    Static3D dCen1 = new Static3D( d1*a1.get0(), d1*a1.get1(), d1*a1.get2() );
553
    Static3D dCen2 = new Static3D( d1*a2.get0(), d1*a2.get1(), d1*a2.get2() );
554
    Static3D dCen3 = new Static3D( d1*a3.get0(), d1*a3.get1(), d1*a3.get2() );
555

  
556
    Static3D dVec0 = new Static3D( d3*v0.get0(), d3*v0.get1(), d3*v0.get2() );
557
    Static3D dVec1 = new Static3D( d3*v1.get0(), d3*v1.get1(), d3*v1.get2() );
558
    Static3D dVec2 = new Static3D( d2*v2.get0(), d2*v2.get1(), d2*v2.get2() );
559
    Static3D dVec3 = new Static3D( d2*v3.get0(), d2*v3.get1(), d2*v3.get2() );
560

  
561
    Static4D dReg  = new Static4D(0,0,0,d4);
562
    Static1D dRad  = new Static1D(1);
563

  
564
    Static1D angle1 = new Static1D(+ANGLE_FACES);
565
    Static1D angle2 = new Static1D(-ANGLE_FACES);
566

  
590 567
    Static3D axisX  = new Static3D(1,0,0);
591 568
    Static3D axisY  = new Static3D(0,1,0);
592
    Static3D axis0  = new Static3D(-SQ2/2,0,SQ2/2);
593
    Static3D axis1  = new Static3D(+SQ3/3,+SQ3/3,+SQ3/3);
594
    Static1D angle1 = new Static1D(+90);
595
    Static1D angle2 = new Static1D(-90);
596
    Static1D angle3 = new Static1D(-15);
597
    Static1D angle4 = new Static1D((float)((180.0f/Math.PI)*Math.acos(SQ3/3)));
598
    Static1D angle5 = new Static1D(120);
599
    Static1D angle6 = new Static1D(240);
600
    Static3D center1= new Static3D(0,0,0);
601
    Static3D center2= new Static3D(-0.5f,-0.5f,-0.5f);
602
    Static3D move1  = new Static3D(-E/4,-E/4,0);
603
    Static3D move2  = new Static3D(-0.5f,-0.5f,-0.5f);
604

  
605
    float d0 =-0.04f;
606
    float d1 = 0.04f;
607
    float r0 = 0.15f;
608
    float r1 = 0.10f;
609

  
610
    Static3D vec0   = new Static3D(d0*(+SQ3/3),d0*(+SQ3/3),d0*(+SQ3/3));
611
    Static3D vec1   = new Static3D(d1*(+SQ3/3),d1*(-SQ3/3),d1*(-SQ3/3));
612
    Static3D vec2   = new Static3D(d1*(-SQ3/3),d1*(+SQ3/3),d1*(-SQ3/3));
613
    Static3D vec3   = new Static3D(d1*(-SQ3/3),d1*(-SQ3/3),d1*(+SQ3/3));
614

  
615
    Static1D radius = new Static1D(0.5f);
616

  
617
    Static3D cent0  = new Static3D( 0.0f, 0.0f, 0.0f);
618
    Static3D cent1  = new Static3D(-0.5f, 0.0f, 0.0f);
619
    Static3D cent2  = new Static3D( 0.0f,-0.5f, 0.0f);
620
    Static3D cent3  = new Static3D( 0.0f, 0.0f,-0.5f);
621

  
622
    Static4D reg0   = new Static4D(0,0,0,r0);
623
    Static4D reg1   = new Static4D(0,0,0,r1);
624

  
625
    VertexEffectMove   effect0 = new VertexEffectMove(move1);
626
    VertexEffectScale  effect1 = new VertexEffectScale(new Static3D(1,1,-1));
627
    VertexEffectRotate effect2 = new VertexEffectRotate(angle1,axisX,center1);
628
    VertexEffectRotate effect3 = new VertexEffectRotate(angle2,axisY,center1);
629
    VertexEffectMove   effect4 = new VertexEffectMove(move2);
630
    VertexEffectRotate effect5 = new VertexEffectRotate(angle1,axisX,center2);
631
    VertexEffectRotate effect6 = new VertexEffectRotate(angle3,axisY,center2);
632
    VertexEffectRotate effect7 = new VertexEffectRotate(angle4,axis0,center2);
633
    VertexEffectRotate effect8 = new VertexEffectRotate(angle5,axis1,center2);
634
    VertexEffectRotate effect9 = new VertexEffectRotate(angle6,axis1,center2);
635

  
636
    VertexEffectDeform effect10= new VertexEffectDeform(vec0,radius,cent0,reg0);
637
    VertexEffectDeform effect11= new VertexEffectDeform(vec1,radius,cent1,reg1);
638
    VertexEffectDeform effect12= new VertexEffectDeform(vec2,radius,cent2,reg1);
639
    VertexEffectDeform effect13= new VertexEffectDeform(vec3,radius,cent3,reg1);
640

  
641
    effect0.setMeshAssociation( 7,-1);  // meshes 0,1,2
642
    effect1.setMeshAssociation( 6,-1);  // meshes 1,2
643
    effect2.setMeshAssociation( 2,-1);  // mesh 1
644
    effect3.setMeshAssociation( 4,-1);  // mesh 2
645
    effect4.setMeshAssociation(56,-1);  // meshes 3,4,5
646
    effect5.setMeshAssociation(56,-1);  // meshes 3,4,5
647
    effect6.setMeshAssociation(56,-1);  // meshes 3,4,5
648
    effect7.setMeshAssociation(56,-1);  // meshes 3,4,5
649
    effect8.setMeshAssociation(16,-1);  // mesh 4
650
    effect9.setMeshAssociation(32,-1);  // mesh 5
651

  
652
    effect10.setMeshAssociation(63,-1); // all meshes
653
    effect11.setMeshAssociation(63,-1); // all meshes
654
    effect12.setMeshAssociation(63,-1); // all meshes
655
    effect13.setMeshAssociation(63,-1); // all meshes
569
    Static3D axisZ  = new Static3D(0,-1,1);
570

  
571
    Static3D center0= new Static3D(0,0,0);
572
    Static3D center1= new Static3D(0,-3*F,0);
573

  
574
    VertexEffectScale   effect0 = new VertexEffectScale ( new Static3D(3,3,3) );
575
    VertexEffectMove    effect1 = new VertexEffectMove  ( new Static3D(0,-F,0) );
576
    VertexEffectRotate  effect2 = new VertexEffectRotate( new Static1D(90), axisX, center0 );
577
    VertexEffectScale   effect3 = new VertexEffectScale ( new Static3D(1,-1,1) );
578
    VertexEffectMove    effect4 = new VertexEffectMove  ( new Static3D(3*E/2,E*(SQ3/2)-3*F,0) );
579
    VertexEffectRotate  effect5 = new VertexEffectRotate( new Static1D(+90), axisY, center1 );
580
    VertexEffectScale   effect6 = new VertexEffectScale ( new Static3D(-1,1,1) );
581
    VertexEffectRotate  effect7 = new VertexEffectRotate( new Static1D( 45), axisX, center1 );
582
    VertexEffectRotate  effect8 = new VertexEffectRotate( angle1           , axisZ, center1 );
583
    VertexEffectRotate  effect9 = new VertexEffectRotate( angle2           , axisZ, center1 );
584

  
585
    VertexEffectDeform  effect10= new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
586
    VertexEffectDeform  effect11= new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
587
    VertexEffectDeform  effect12= new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
588
    VertexEffectDeform  effect13= new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
589

  
590
    effect0.setMeshAssociation(15,-1);  // apply to meshes 0,1,2,3
591
    effect1.setMeshAssociation( 3,-1);  // apply to meshes 0,1
592
    effect2.setMeshAssociation( 2,-1);  // apply to mesh 1
593
    effect3.setMeshAssociation( 2,-1);  // apply to mesh 0
594
    effect4.setMeshAssociation(12,-1);  // apply to meshes 2,3
595
    effect5.setMeshAssociation(12,-1);  // apply to meshes 2,3
596
    effect6.setMeshAssociation( 8,-1);  // apply to mesh 3
597
    effect7.setMeshAssociation(12,-1);  // apply to meshes 2,3
598
    effect8.setMeshAssociation( 4,-1);  // apply to mesh 2
599
    effect9.setMeshAssociation( 8,-1);  // apply to mesh 3
600
    effect10.setMeshAssociation(15,-1); // apply to meshes 0,1,2,3
601
    effect11.setMeshAssociation(15,-1); // apply to meshes 0,1,2,3
602
    effect12.setMeshAssociation(15,-1); // apply to meshes 0,1,2,3
603
    effect13.setMeshAssociation(15,-1); // apply to meshes 0,1,2,3
656 604

  
657 605
    result.apply(effect0);
658 606
    result.apply(effect1);
......
665 613
    result.apply(effect8);
666 614
    result.apply(effect9);
667 615
    result.apply(effect10);
668

  
669 616
    result.apply(effect11);
670 617
    result.apply(effect12);
671 618
    result.apply(effect13);
672 619

  
673
//result.setShowNormals(true);
674

  
675 620
    return result;
676 621
    }
677 622

  

Also available in: Unified diff