Project

General

Profile

« Previous | Next » 

Revision 1beffb69

Added by Leszek Koltunski over 4 years ago

Procedural Skewb cubits and the Skewb mesh for MeshFile.

View differences:

src/main/java/org/distorted/examples/meshfile/MeshFileActivity.java
45 45

  
46 46
    private LinearLayout mLayout;
47 47
    private int mResource;
48
    private String[] mNames = new String[] { "deferredjob",
48
    private String[] mNames = new String[] { "procedural" ,
49
                                             "deferredjob",
49 50
                                             "meshjoin"   ,
50 51
                                             "predeform"  ,
51 52
                                             "cube2"      ,
......
56 57
                                             "pyra4"      ,
57 58
                                             "pyra5"      ,
58 59
                                             "dino"       ,
59
                                             "procedural"
60
                                             "skewb"
60 61
                                           };
61 62

  
62 63
///////////////////////////////////////////////////////////////////////////////////////////////////
......
183 184
        {
184 185
        switch(pos)
185 186
          {
186
          case  0: mResource = R.raw.deferredjob; break;
187
          case  1: mResource = R.raw.meshjoin   ; break;
188
          case  2: mResource = R.raw.predeform  ; break;
189
          case  3: mResource = R.raw.cube2      ; break;
190
          case  4: mResource = R.raw.cube3      ; break;
191
          case  5: mResource = R.raw.cube4      ; break;
192
          case  6: mResource = R.raw.cube5      ; break;
193
          case  7: mResource = R.raw.pyra3      ; break;
194
          case  8: mResource = R.raw.pyra4      ; break;
195
          case  9: mResource = R.raw.pyra5      ; break;
196
          case 10: mResource = R.raw.dino       ; break;
197
          case 11: mResource = PROCEDURAL       ; break;
187
          case  0: mResource = PROCEDURAL       ; break;
188
          case  1: mResource = R.raw.deferredjob; break;
189
          case  2: mResource = R.raw.meshjoin   ; break;
190
          case  3: mResource = R.raw.predeform  ; break;
191
          case  4: mResource = R.raw.cube2      ; break;
192
          case  5: mResource = R.raw.cube3      ; break;
193
          case  6: mResource = R.raw.cube4      ; break;
194
          case  7: mResource = R.raw.cube5      ; break;
195
          case  8: mResource = R.raw.pyra3      ; break;
196
          case  9: mResource = R.raw.pyra4      ; break;
197
          case 10: mResource = R.raw.pyra5      ; break;
198
          case 11: mResource = R.raw.dino       ; break;
199
          case 12: mResource = R.raw.skewb      ; break;
198 200
          }
199 201
        }
200 202
      }
src/main/java/org/distorted/examples/meshfile/MeshFileRenderer.java
34 34
import org.distorted.library.effect.VertexEffectDisappear;
35 35
import org.distorted.library.effect.VertexEffectMove;
36 36
import org.distorted.library.effect.VertexEffectRotate;
37
import org.distorted.library.effect.VertexEffectScale;
37 38
import org.distorted.library.effect.VertexEffectSink;
38 39
import org.distorted.library.main.DistortedEffects;
39 40
import org.distorted.library.main.DistortedLibrary;
......
43 44
import org.distorted.library.mesh.MeshFile;
44 45
import org.distorted.library.mesh.MeshJoined;
45 46
import org.distorted.library.mesh.MeshPolygon;
47
import org.distorted.library.mesh.MeshTriangle;
46 48
import org.distorted.library.type.DynamicQuat;
47 49
import org.distorted.library.type.Static1D;
48 50
import org.distorted.library.type.Static3D;
......
137 139
      VertexEffectRotate.enable();
138 140
      VertexEffectDeform.enable();
139 141

  
140
      DistortedLibrary.setMax(EffectType.VERTEX, 12);
142
      DistortedLibrary.setMax(EffectType.VERTEX, 14);
141 143

  
142 144
      DistortedLibrary.onCreate(mView.getContext(), this);
143 145
      }
......
207 209
                                                                         0xff0000ff,
208 210
                                                                         0xffff0000 };
209 211
                                   return createTetrahedronTexture(TET_COLORS4);
212
          case  PROCEDURAL       :
210 213
          case  R.raw.predeform  : return createGridTexture(3);
211 214
          case  R.raw.cube2      :
212 215
          case  R.raw.cube3      :
......
218 221
                                                                         0xffff0000,
219 222
                                                                         0xffb5651d };
220 223
                                   return createCubeTexture(CUBE_COLORS);
221
          case  PROCEDURAL       :
222 224
          case  R.raw.pyra3      :
223 225
          case  R.raw.pyra4      :
224 226
          case  R.raw.pyra5      : final int[] TET_COLORS5 = new int[] { 0xffffff00,
......
235 237
                                                                         0xffb5651d,
236 238
                                                                         0xff000000 };
237 239
                                   return createTetrahedronTexture(TET_COLORS7);
240
          case R.raw.skewb       : final int[] SKEW_COLORS = new int[] { 0xffffff00,
241
                                                                         0xffffffff,
242
                                                                         0xff0000ff,
243
                                                                         0xff00ff00,
244
                                                                         0xffff0000,
245
                                                                         0xffb5651d };
246
                                   return createSkewbTexture(SKEW_COLORS);
238 247
          }
239 248

  
240 249
      return null;
......
339 348

  
340 349
      return result;
341 350
      }
351

  
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

  
354
  void createSkewbFaceTexture(Canvas canvas, Paint paint, int[] faceColors, int face, int left, int top, int side)
355
    {
356
    final int COLORS = 6;
357
    final float SQ2 = (float)Math.sqrt(2);
358

  
359
    if( face<COLORS )
360
      {
361
      float STROKE = 0.035f*side;
362
      float L= left+0.125f*side;
363
      float H= 0.375f*side;
364
      float LEN = 0.5f*side;
365

  
366
      paint.setAntiAlias(true);
367
      paint.setStrokeWidth(STROKE);
368
      paint.setColor(faceColors[face]);
369
      paint.setStyle(Paint.Style.FILL);
370

  
371
      canvas.drawRect(left,top,left+side,top+side,paint);
372

  
373
      paint.setColor(0xff000000);
374
      paint.setStyle(Paint.Style.STROKE);
375

  
376
      canvas.drawLine( L    , H,  L+LEN, H    , paint);
377
      canvas.drawLine( L    , H,  L+LEN, H+LEN, paint);
378
      canvas.drawLine( L+LEN, H,  L+LEN, H+LEN, paint);
379

  
380
      float S1 = 0.125f*side;
381
      float S2 = 0.070f*side;
382
      float X  = 0.7f*S2;
383

  
384
      float LA = left+0.625f*side;
385
      float RA = left+0.125f*side;
386
      float TA = 0.375f*side;
387
      float BA = 0.875f*side;
388

  
389
      canvas.drawArc( LA-S1, TA     , LA     , TA+S1, 270, 90, false, paint);
390
      canvas.drawArc( RA+X , TA     , RA+X+S2, TA+S2, 135,135, false, paint);
391
      canvas.drawArc( LA-S2, BA-X-S2, LA     , BA-X ,   0,135, false, paint);
392
      }
393
    else
394
      {
395
      final float R = (SQ2/2)*side*0.10f;
396
      final float M = side*(0.5f-SQ2/4+0.018f);
397

  
398
      paint.setColor(faceColors[face-COLORS]);
399
      paint.setStyle(Paint.Style.FILL);
400
      canvas.drawRoundRect( left+M, top+M, left+side-M, top+side-M, R, R, paint);
401
      }
402
    }
403

  
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405

  
406
    private Bitmap createSkewbTexture(int[] faceColors)
407
      {
408
      final int TEXTURE_HEIGHT = 256;
409
      final int NUM_TEXTURES = 2*6;
410
      Bitmap bitmap;
411

  
412
      Paint paint = new Paint();
413
      bitmap = Bitmap.createBitmap( (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
414
      Canvas canvas = new Canvas(bitmap);
415

  
416
      paint.setAntiAlias(true);
417
      paint.setTextAlign(Paint.Align.CENTER);
418
      paint.setStyle(Paint.Style.FILL);
419

  
420
      paint.setColor(0xff000000);
421
      canvas.drawRect(0, 0, (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
422

  
423
      for(int i=0; i<NUM_TEXTURES; i++)
424
        {
425
        createSkewbFaceTexture(canvas, paint, faceColors, i, i*TEXTURE_HEIGHT, 0, TEXTURE_HEIGHT);
426
        }
427

  
428
      return bitmap;
429
      }
430

  
342 431
///////////////////////////////////////////////////////////////////////////////////////////////////
343 432

  
344 433
  private MeshBase createStaticMesh()
345 434
    {
435
    /*
346 436
    final float SQ2 = (float)Math.sqrt(2);
347 437
    final float SQ3 = (float)Math.sqrt(3);
348 438
    final float angleFaces = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
......
455 545
    result.setTextureMap(maps,0);
456 546
    result.setShowNormals(true);
457 547

  
548
    return result;
549
     */
550
    final float SQ2 = (float)Math.sqrt(2);
551
    final float SQ3 = (float)Math.sqrt(3);
552

  
553
    final int FACES_PER_CUBIT=6;
554

  
555
    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};
560

  
561
    float[] bands0 = { 1.0f    , 0,
562
                       1.0f-2*D, D*0.25f,
563
                       1.0f-4*D, D*0.35f,
564
                       1.0f-8*D, D*0.6f,
565
                       0.60f   , D*1.0f,
566
                       0.30f   , D*1.375f,
567
                       0.0f    , D*1.4f };
568

  
569
    MeshBase[] meshes = new MeshBase[FACES_PER_CUBIT];
570

  
571
    meshes[0] = new MeshPolygon(vertices0, bands0, 3, 3);
572
    meshes[0].setEffectAssociation(0,1,0);
573
    meshes[1] = meshes[0].copy(true);
574
    meshes[1].setEffectAssociation(0,2,0);
575
    meshes[2] = meshes[0].copy(true);
576
    meshes[2].setEffectAssociation(0,4,0);
577

  
578
    float[] vertices1 = { 0,0, F,0, F/2,(SQ3/2)*F };
579
    float[] bands1 = { 1.0f, 0.0f, 0.0f, 0.0f };
580

  
581
    meshes[3] = new MeshPolygon(vertices1,bands1,0,0);
582
    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

  
588
    MeshBase result = new MeshJoined(meshes);
589

  
590
    Static3D axisX  = new Static3D(1,0,0);
591
    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
656

  
657
    result.apply(effect0);
658
    result.apply(effect1);
659
    result.apply(effect2);
660
    result.apply(effect3);
661
    result.apply(effect4);
662
    result.apply(effect5);
663
    result.apply(effect6);
664
    result.apply(effect7);
665
    result.apply(effect8);
666
    result.apply(effect9);
667
    result.apply(effect10);
668

  
669
    result.apply(effect11);
670
    result.apply(effect12);
671
    result.apply(effect13);
672

  
673
//result.setShowNormals(true);
674

  
458 675
    return result;
459 676
    }
460 677

  

Also available in: Unified diff