Project

General

Profile

« Previous | Next » 

Revision 6991ece5

Added by Leszek Koltunski about 4 years ago

Add possibility to display also a procedurally created Mesh in MeshFile.

View differences:

src/main/java/org/distorted/examples/TableOfContents.java
86 86

  
87 87
  private enum Application
88 88
    {
89
    MESHFILE          (R.drawable.icon_example_meshfile        , R.string.example_meshfile           , R.string.example_meshfile_subtitle           ,            MeshFileActivity.class),
90

  
91

  
89 92
    MONALISA          (R.drawable.icon_example_monalisa        , R.string.example_monalisa        , R.string.example_monalisa_subtitle        ,         MonaLisaActivity.class),
90 93
    SINK              (R.drawable.icon_example_sink            , R.string.example_sink            , R.string.example_sink_subtitle            ,             SinkActivity.class),
91 94
    BEAN              (R.drawable.icon_example_bean            , R.string.example_bean            , R.string.example_bean_subtitle            ,             BeanActivity.class),
......
126 129
    PREDEFORM         (R.drawable.icon_example_predeform       , R.string.example_predeform           , R.string.example_predeform_subtitle           ,            PredeformActivity.class),
127 130
    DEFERREDJOB       (R.drawable.icon_example_deferredjob     , R.string.example_deferredjob           , R.string.example_deferredjob_subtitle           ,            DeferredJobActivity.class),
128 131
    SINGLEMESH        (R.drawable.icon_example_singlemesh      , R.string.example_singlemesh           , R.string.example_singlemesh_subtitle           ,            SingleMeshActivity.class),
129
    MESHFILE          (R.drawable.icon_example_meshfile        , R.string.example_meshfile           , R.string.example_meshfile_subtitle           ,            MeshFileActivity.class),
130 132
    ;
131 133

  
132 134
    final int icon, title, subtitle;
src/main/java/org/distorted/examples/meshfile/MeshFileActivity.java
34 34

  
35 35
import org.distorted.examples.R;
36 36
import org.distorted.library.main.DistortedLibrary;
37
import org.distorted.library.mesh.MeshFile;
37
import org.distorted.library.mesh.MeshBase;
38 38

  
39 39
///////////////////////////////////////////////////////////////////////////////////////////////////
40 40

  
41 41
public class MeshFileActivity extends Activity implements AdapterView.OnItemSelectedListener,
42 42
                                                          SeekBar.OnSeekBarChangeListener
43 43
{
44
    final static int PROCEDURAL = -1;
45

  
44 46
    private LinearLayout mLayout;
45 47
    private int mResource;
46 48
    private String[] mNames = new String[] { "deferredjob",
......
53 55
                                             "pyra3"      ,
54 56
                                             "pyra4"      ,
55 57
                                             "pyra5"      ,
56
                                             "dino"
58
                                             "dino"       ,
59
                                             "procedural"
57 60
                                           };
58 61

  
59 62
///////////////////////////////////////////////////////////////////////////////////////////////////
......
164 167
          {
165 168
          MeshFileSurfaceView view = findViewById(R.id.meshfileSurfaceView);
166 169
          MeshFileRenderer renderer = view.getRenderer();
167
          MeshFile mesh = renderer.getMesh();
170
          MeshBase mesh = renderer.getMesh();
168 171
          mesh.setEffectAssociation(component,isChecked ? 1:0,component);
169 172
          }
170 173
        }) ;
......
191 194
          case  8: mResource = R.raw.pyra4      ; break;
192 195
          case  9: mResource = R.raw.pyra5      ; break;
193 196
          case 10: mResource = R.raw.dino       ; break;
197
          case 11: mResource = PROCEDURAL       ; break;
194 198
          }
195 199
        }
196 200
      }
src/main/java/org/distorted/examples/meshfile/MeshFileRenderer.java
27 27
import android.opengl.GLSurfaceView;
28 28

  
29 29
import org.distorted.examples.R;
30
import org.distorted.library.effect.EffectType;
30 31
import org.distorted.library.effect.MatrixEffectQuaternion;
31 32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectDeform;
32 34
import org.distorted.library.effect.VertexEffectDisappear;
35
import org.distorted.library.effect.VertexEffectMove;
36
import org.distorted.library.effect.VertexEffectRotate;
37
import org.distorted.library.effect.VertexEffectSink;
33 38
import org.distorted.library.main.DistortedEffects;
34 39
import org.distorted.library.main.DistortedLibrary;
35 40
import org.distorted.library.main.DistortedScreen;
36 41
import org.distorted.library.main.DistortedTexture;
42
import org.distorted.library.mesh.MeshBase;
37 43
import org.distorted.library.mesh.MeshFile;
44
import org.distorted.library.mesh.MeshJoined;
45
import org.distorted.library.mesh.MeshPolygon;
38 46
import org.distorted.library.type.DynamicQuat;
47
import org.distorted.library.type.Static1D;
39 48
import org.distorted.library.type.Static3D;
40 49
import org.distorted.library.type.Static4D;
41 50

  
......
46 55
import javax.microedition.khronos.egl.EGLConfig;
47 56
import javax.microedition.khronos.opengles.GL10;
48 57

  
58
import static org.distorted.examples.meshfile.MeshFileActivity.PROCEDURAL;
59

  
49 60
///////////////////////////////////////////////////////////////////////////////////////////////////
50 61

  
51 62
class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
......
59 70
    private Static3D mScale;
60 71
    private long mTime;
61 72
    private float mCurrentScale;
62
    private MeshFile mMesh;
73
    private MeshBase mMesh;
63 74

  
64 75
    Static4D mQuat1, mQuat2;
65 76
    int mScreenMin;
......
123 134

  
124 135
      VertexEffectDisappear.enable();
125 136

  
137
      VertexEffectRotate.enable();
138
      VertexEffectDeform.enable();
139

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

  
126 142
      DistortedLibrary.onCreate(mView.getContext(), this);
127 143
      }
128 144

  
......
161 177
      mTexture.setTexture( createTexture(resourceID) );
162 178

  
163 179
      long t1 = System.currentTimeMillis();
164
      createMesh(resourceID);
180

  
181
      if( resourceID!=PROCEDURAL )
182
        {
183
        openMesh(resourceID);
184
        }
185
      else
186
        {
187
        createMesh();
188
        }
189

  
165 190
      long t2 = System.currentTimeMillis();
166 191

  
167 192
      mTime = t2-t1;
......
193 218
                                                                         0xffff0000,
194 219
                                                                         0xffb5651d };
195 220
                                   return createCubeTexture(CUBE_COLORS);
221
          case  PROCEDURAL       :
196 222
          case  R.raw.pyra3      :
197 223
          case  R.raw.pyra4      :
198 224
          case  R.raw.pyra5      : final int[] TET_COLORS5 = new int[] { 0xffffff00,
......
269 295

  
270 296
    private Bitmap createTetrahedronTexture(int[] faceColors)
271 297
      {
298
      final float SQ3 = (float)Math.sqrt(3);
272 299
      final int FACES=faceColors.length;
273
      int SIZE = 200;
300
      int SIZE = 256;
274 301
      float STROKE = 0.05f*SIZE;
275 302
      float OFF = STROKE/2 -1;
276 303
      float OFF2 = 0.5f*SIZE + OFF;
277 304
      float HEIGHT = SIZE - OFF;
278
      float RADIUS = SIZE/12;
305
      float RADIUS = SIZE/12.0f;
279 306
      float ARC1_H = 0.2f*SIZE;
280 307
      float ARC1_W = SIZE*0.5f;
281 308
      float ARC2_W = 0.153f*SIZE;
282 309
      float ARC2_H = 0.905f*SIZE;
283 310
      float ARC3_W = SIZE-ARC2_W;
284 311

  
312
      float M = SQ3/2;
313
      float D = (M/2 - 0.51f)*SIZE;
314

  
285 315
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
286 316
      Canvas canvas = new Canvas(result);
287 317
      Paint paint = new Paint();
......
298 328
        paint.setColor(0xff000000);
299 329
        paint.setStyle(Paint.Style.STROKE);
300 330

  
301
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
302
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
303
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
331
        canvas.drawLine(           i*SIZE, M*HEIGHT+D,  SIZE       +i*SIZE, M*HEIGHT+D, paint);
332
        canvas.drawLine(      OFF +i*SIZE, M*SIZE  +D,       OFF2  +i*SIZE,          D, paint);
333
        canvas.drawLine((SIZE-OFF)+i*SIZE, M*SIZE  +D, (SIZE-OFF2) +i*SIZE,          D, paint);
304 334

  
305
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
306
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
307
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
335
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, M*(ARC1_H-RADIUS)+D, ARC1_W+RADIUS+i*SIZE, M*(ARC1_H+RADIUS)+D, 225, 90, false, paint);
336
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, M*(ARC2_H-RADIUS)+D, ARC2_W+RADIUS+i*SIZE, M*(ARC2_H+RADIUS)+D, 105, 90, false, paint);
337
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, M*(ARC2_H-RADIUS)+D, ARC3_W+RADIUS+i*SIZE, M*(ARC2_H+RADIUS)+D, 345, 90, false, paint);
308 338
        }
309 339

  
310 340
      return result;
311 341
      }
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

  
344
  private MeshBase createStaticMesh()
345
    {
346
    final float SQ2 = (float)Math.sqrt(2);
347
    final float SQ3 = (float)Math.sqrt(3);
348
    final float angleFaces = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
349
    final int MESHES=4;
350

  
351
    int association = 1;
352
    MeshBase[] meshes;
353

  
354
    float D = 0.0003f;
355
    float E = SQ3/2 - 3*D*SQ2;
356
    float F = 0.5f - D*SQ2*SQ3;
357

  
358
    float[] vertices = { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
359
    float[] bands = new float[] {    1.0f    ,-D,
360
                                     1.0f  -D,-D*0.80f,
361
                                     1.0f-2*D,-D*0.65f,
362
                                     1.0f-4*D,+D*0.10f,
363
                                     0.50f, 0.035f,
364
                                     0.0f, 0.040f };
365

  
366
    meshes = new MeshPolygon[MESHES];
367
    meshes[0] = new MeshPolygon(vertices, bands, 2,2);
368
    meshes[0].setEffectAssociation(0,association,0);
369

  
370
    for(int i=1; i<MESHES; i++)
371
      {
372
      association <<= 1;
373
      meshes[i] = meshes[0].copy(true);
374
      meshes[i].setEffectAssociation(0,association,0);
375
      }
376

  
377
    MeshBase result = new MeshJoined(meshes);
378

  
379
    Static3D a0 = new Static3D(         0,        1,       0 );
380
    Static3D a1 = new Static3D(         0,  -1.0f/3, 2*SQ2/3 );
381
    Static3D a2 = new Static3D(-SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 );
382
    Static3D a3 = new Static3D( SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 );
383

  
384
    float tetraHeight = SQ2*SQ3/3;
385
    float d1 = (0.75f-2*SQ2*D)*tetraHeight;
386
    float d2 =-0.06f*tetraHeight;
387
    float d3 = 0.05f*tetraHeight;
388
    float d4 = 0.70f*tetraHeight;
389
    float d5 = 1.2f;
390

  
391
    Static3D dCen0 = new Static3D( d1*a0.get0(), d1*a0.get1(), d1*a0.get2() );
392
    Static3D dCen1 = new Static3D( d1*a1.get0(), d1*a1.get1(), d1*a1.get2() );
393
    Static3D dCen2 = new Static3D( d1*a2.get0(), d1*a2.get1(), d1*a2.get2() );
394
    Static3D dCen3 = new Static3D( d1*a3.get0(), d1*a3.get1(), d1*a3.get2() );
395

  
396
    Static3D dVec0 = new Static3D( d2*a0.get0(), d2*a0.get1(), d2*a0.get2() );
397
    Static3D dVec1 = new Static3D( d2*a1.get0(), d2*a1.get1(), d2*a1.get2() );
398
    Static3D dVec2 = new Static3D( d2*a2.get0(), d2*a2.get1(), d2*a2.get2() );
399
    Static3D dVec3 = new Static3D( d2*a3.get0(), d2*a3.get1(), d2*a3.get2() );
400

  
401
    Static4D dReg  = new Static4D(0,0,0,d3);
402
    Static1D dRad  = new Static1D(1);
403
    Static3D center= new Static3D(0,0,0);
404
    Static4D sReg  = new Static4D(0,0,0,d4);
405
    Static1D sink  = new Static1D(d5);
406

  
407
    Static1D angle  = new Static1D(angleFaces);
408
    Static3D axis1  = new Static3D(  -1, 0,      0);
409
    Static3D axis2  = new Static3D(0.5f, 0, -SQ3/2);
410
    Static3D axis3  = new Static3D(0.5f, 0, +SQ3/2);
411
    Static3D center1= new Static3D(0,-SQ3*SQ2/12,-SQ3/6);
412
    Static3D center2= new Static3D(0,-SQ3*SQ2/12,+SQ3/3);
413

  
414
    VertexEffectRotate  effect1 = new VertexEffectRotate( new Static1D(90), new Static3D(1,0,0), center );
415
    VertexEffectMove    effect2 = new VertexEffectMove  ( new Static3D(0,-SQ3*SQ2/12,0) );
416
    VertexEffectRotate  effect3 = new VertexEffectRotate( new Static1D(180), new Static3D(0,0,1), center1 );
417
    VertexEffectRotate  effect4 = new VertexEffectRotate( angle, axis1, center1 );
418
    VertexEffectRotate  effect5 = new VertexEffectRotate( angle, axis2, center2 );
419
    VertexEffectRotate  effect6 = new VertexEffectRotate( angle, axis3, center2 );
420

  
421
    VertexEffectDeform  effect7 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
422
    VertexEffectDeform  effect8 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
423
    VertexEffectDeform  effect9 = new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
424
    VertexEffectDeform  effect10= new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
425

  
426
    VertexEffectSink    effect11= new VertexEffectSink(sink,center, sReg);
427

  
428
    effect3.setMeshAssociation(14,-1);  // apply to mesh[1], [2] and [3]
429
    effect4.setMeshAssociation( 2,-1);  // apply only to mesh[1]
430
    effect5.setMeshAssociation( 4,-1);  // apply only to mesh[2]
431
    effect6.setMeshAssociation( 8,-1);  // apply only to mesh[3]
432

  
433
    result.apply(effect1);
434
    result.apply(effect2);
435
    result.apply(effect3);
436
    result.apply(effect4);
437
    result.apply(effect5);
438
    result.apply(effect6);
439

  
440
    result.apply(effect7);
441
    result.apply(effect8);
442
    result.apply(effect9);
443
    result.apply(effect10);
444

  
445
    result.apply(effect11);
446

  
447
    final float ratio = 0.2f;
448
    final Static4D[] maps = new Static4D[MESHES];
449

  
450
    for(int mesh=0; mesh<MESHES; mesh++)
451
      {
452
      maps[mesh] = new Static4D( mesh*ratio, 0.0f, ratio, 1.0f);
453
      }
454

  
455
    result.setTextureMap(maps,0);
456
    result.setShowNormals(true);
457

  
458
    return result;
459
    }
460

  
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462

  
463
    private void createMesh()
464
      {
465
      mMesh = createStaticMesh();
466

  
467
      int numEff = mMesh.numEffComponents();
468

  
469
      for(int i=0; i<numEff; i++)
470
        {
471
        mMesh.setEffectAssociation(i, 0, i);
472
        }
473
      }
312 474

  
313 475
///////////////////////////////////////////////////////////////////////////////////////////////////
314 476

  
315
    private void createMesh(int resourceID)
477
    private void openMesh(int resourceID)
316 478
      {
317 479
      Context con = mView.getContext();
318 480
      Resources res = con.getResources();
......
339 501

  
340 502
///////////////////////////////////////////////////////////////////////////////////////////////////
341 503

  
342
    MeshFile getMesh()
504
    MeshBase getMesh()
343 505
      {
344 506
      return mMesh;
345 507
      }
......
355 517

  
356 518
    int getBytes()
357 519
      {
358
      return mMesh.getNumBytes();
520
      if( mMesh instanceof MeshFile )
521
        {
522
        return ((MeshFile)mMesh).getNumBytes();
523
        }
524

  
525
      return 0;
359 526
      }
360 527

  
361 528
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff