Project

General

Profile

Download (28.3 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 1beffb69

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.examples.meshfile;
21

    
22
import android.content.Context;
23
import android.content.res.Resources;
24
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.opengl.GLSurfaceView;
28

    
29
import org.distorted.examples.R;
30
import org.distorted.library.effect.EffectType;
31
import org.distorted.library.effect.MatrixEffectQuaternion;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectDeform;
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.VertexEffectScale;
38
import org.distorted.library.effect.VertexEffectSink;
39
import org.distorted.library.main.DistortedEffects;
40
import org.distorted.library.main.DistortedLibrary;
41
import org.distorted.library.main.DistortedScreen;
42
import org.distorted.library.main.DistortedTexture;
43
import org.distorted.library.mesh.MeshBase;
44
import org.distorted.library.mesh.MeshFile;
45
import org.distorted.library.mesh.MeshJoined;
46
import org.distorted.library.mesh.MeshPolygon;
47
import org.distorted.library.mesh.MeshTriangle;
48
import org.distorted.library.type.DynamicQuat;
49
import org.distorted.library.type.Static1D;
50
import org.distorted.library.type.Static3D;
51
import org.distorted.library.type.Static4D;
52

    
53
import java.io.DataInputStream;
54
import java.io.IOException;
55
import java.io.InputStream;
56

    
57
import javax.microedition.khronos.egl.EGLConfig;
58
import javax.microedition.khronos.opengles.GL10;
59

    
60
import static org.distorted.examples.meshfile.MeshFileActivity.PROCEDURAL;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
65
{
66
    private final float DEFAULT_SCALE = 0.3f;
67

    
68
    private GLSurfaceView mView;
69
    private DistortedTexture mTexture;
70
    private DistortedScreen mScreen;
71
    private DistortedEffects mEffects;
72
    private Static3D mScale;
73
    private long mTime;
74
    private float mCurrentScale;
75
    private MeshBase mMesh;
76

    
77
    Static4D mQuat1, mQuat2;
78
    int mScreenMin;
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
    MeshFileRenderer(GLSurfaceView v)
83
      {
84
      mView = v;
85
      mScreen = new DistortedScreen();
86
      mScale= new Static3D(1,1,1);
87
      Static3D center=new Static3D(0,0,0);
88

    
89
      mCurrentScale = DEFAULT_SCALE;
90

    
91
      mQuat1 = new Static4D(0,0,0,1);
92
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
93

    
94
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
95
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
96

    
97
      quatInt1.add(mQuat1);
98
      quatInt2.add(mQuat2);
99

    
100
      VertexEffectDisappear disappear = new VertexEffectDisappear();
101
      disappear.setMeshAssociation(1,-1);
102

    
103
      mEffects = new DistortedEffects();
104
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
105
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
106
      mEffects.apply( new MatrixEffectScale(mScale));
107

    
108
      mEffects.apply( disappear );
109

    
110
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
111
      mScreen.showFPS();
112
      }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
   
116
    public void onDrawFrame(GL10 glUnused) 
117
      {
118
      mScreen.render( System.currentTimeMillis() );
119
      }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
    
123
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
124
      {
125
      mScreenMin = Math.min(width, height);
126
      float factor = mCurrentScale*mScreenMin;
127
      mScale.set(factor,factor,factor);
128
      mScreen.resize(width, height);
129
      }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
    
133
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
134
      {
135
      if( mTexture==null ) mTexture = new DistortedTexture();
136

    
137
      VertexEffectDisappear.enable();
138

    
139
      VertexEffectRotate.enable();
140
      VertexEffectDeform.enable();
141

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

    
144
      DistortedLibrary.onCreate(mView.getContext(), this);
145
      }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
    public void distortedException(Exception ex)
150
      {
151
      android.util.Log.e("MeshFile", ex.getMessage() );
152
      }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
//   0 ---> 0
156
//  50 ---> DEFAULT_SCALE
157
// 100 ---> 4*DEFAULT_SCALE
158

    
159
    void setScale(int scale)
160
      {
161
      if( scale<= 50 )
162
        {
163
        mCurrentScale = DEFAULT_SCALE * scale / 50.0f;
164
        }
165
      else
166
        {
167
        mCurrentScale = DEFAULT_SCALE * ( 3*(scale/50.0f) - 2.0f);
168
        }
169

    
170
      float factor = mCurrentScale*mScreenMin;
171
      mScale.set(factor,factor,factor);
172
      }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
    void open(int resourceID)
177
      {
178
      if( mTexture==null ) mTexture = new DistortedTexture();
179
      mTexture.setTexture( createTexture(resourceID) );
180

    
181
      long t1 = System.currentTimeMillis();
182

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

    
192
      long t2 = System.currentTimeMillis();
193

    
194
      mTime = t2-t1;
195

    
196
      mScreen.detachAll();
197
      mScreen.attach(mTexture,mEffects,mMesh);
198
      }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
    private Bitmap createTexture(int resourceID)
203
      {
204
      switch(resourceID)
205
          {
206
          case  R.raw.deferredjob:
207
          case  R.raw.meshjoin   : final int[] TET_COLORS4 = new int[] { 0xffffff00,
208
                                                                         0xff00ff00,
209
                                                                         0xff0000ff,
210
                                                                         0xffff0000 };
211
                                   return createTetrahedronTexture(TET_COLORS4);
212
          case  PROCEDURAL       :
213
          case  R.raw.predeform  : return createGridTexture(3);
214
          case  R.raw.cube2      :
215
          case  R.raw.cube3      :
216
          case  R.raw.cube4      :
217
          case  R.raw.cube5      : final int[] CUBE_COLORS = new int[] { 0xffffff00,
218
                                                                         0xffffffff,
219
                                                                         0xff0000ff,
220
                                                                         0xff00ff00,
221
                                                                         0xffff0000,
222
                                                                         0xffb5651d };
223
                                   return createCubeTexture(CUBE_COLORS);
224
          case  R.raw.pyra3      :
225
          case  R.raw.pyra4      :
226
          case  R.raw.pyra5      : final int[] TET_COLORS5 = new int[] { 0xffffff00,
227
                                                                         0xff00ff00,
228
                                                                         0xff0000ff,
229
                                                                         0xffff0000,
230
                                                                         0xff000000 };
231
                                   return createTetrahedronTexture(TET_COLORS5);
232
          case  R.raw.dino       : final int[] TET_COLORS7 = new int[] { 0xffffff00,
233
                                                                         0xffffffff,
234
                                                                         0xff0000ff,
235
                                                                         0xff00ff00,
236
                                                                         0xffff0000,
237
                                                                         0xffb5651d,
238
                                                                         0xff000000 };
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);
247
          }
248

    
249
      return null;
250
      }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
    private Bitmap createCubeTexture(int[] faceColors)
255
      {
256
      final int FACES=faceColors.length;
257
      int SIZE = 200;
258
      final float R = SIZE*0.10f;
259
      final float M = SIZE*0.05f;
260

    
261
      Bitmap bitmap;
262
      Paint paint = new Paint();
263
      bitmap = Bitmap.createBitmap( (FACES+1)*SIZE, SIZE, Bitmap.Config.ARGB_8888);
264
      Canvas canvas = new Canvas(bitmap);
265

    
266
      paint.setStyle(Paint.Style.FILL);
267
      paint.setColor(0xff000000);
268
      canvas.drawRect(0, 0, (FACES+1)*SIZE, SIZE, paint);
269

    
270
      for(int face=0; face<FACES; face++)
271
        {
272
        paint.setColor(faceColors[face]);
273
        canvas.drawRoundRect( face*SIZE+M, M, (face+1)*SIZE-M, SIZE-M, R, R, paint);
274
        }
275

    
276
      return bitmap;
277
      }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
    private Bitmap createGridTexture(int lines)
282
      {
283
      int SIZE = 200;
284
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
285
      Canvas canvas = new Canvas(bitmap);
286

    
287
      Paint paint = new Paint();
288
      paint.setColor(0xff008800);
289
      paint.setStyle(Paint.Style.FILL);
290
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
291
      paint.setColor(0xffffffff);
292

    
293
      for(int i=0; i<=lines ; i++ )
294
        {
295
        int x = (SIZE*i)/lines;
296
        canvas.drawRect(x-1,   0,  x+1, SIZE, paint);
297
        canvas.drawRect(  0, x-1, SIZE,  x+1, paint);
298
        }
299

    
300
      return bitmap;
301
      }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

    
305
    private Bitmap createTetrahedronTexture(int[] faceColors)
306
      {
307
      final float SQ3 = (float)Math.sqrt(3);
308
      final int FACES=faceColors.length;
309
      int SIZE = 256;
310
      float STROKE = 0.05f*SIZE;
311
      float OFF = STROKE/2 -1;
312
      float OFF2 = 0.5f*SIZE + OFF;
313
      float HEIGHT = SIZE - OFF;
314
      float RADIUS = SIZE/12.0f;
315
      float ARC1_H = 0.2f*SIZE;
316
      float ARC1_W = SIZE*0.5f;
317
      float ARC2_W = 0.153f*SIZE;
318
      float ARC2_H = 0.905f*SIZE;
319
      float ARC3_W = SIZE-ARC2_W;
320

    
321
      float M = SQ3/2;
322
      float D = (M/2 - 0.51f)*SIZE;
323

    
324
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
325
      Canvas canvas = new Canvas(result);
326
      Paint paint = new Paint();
327
      paint.setAntiAlias(true);
328
      paint.setStrokeWidth(STROKE);
329

    
330
      for(int i=0; i<FACES; i++)
331
        {
332
        paint.setColor(faceColors[i]);
333
        paint.setStyle(Paint.Style.FILL);
334

    
335
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
336

    
337
        paint.setColor(0xff000000);
338
        paint.setStyle(Paint.Style.STROKE);
339

    
340
        canvas.drawLine(           i*SIZE, M*HEIGHT+D,  SIZE       +i*SIZE, M*HEIGHT+D, paint);
341
        canvas.drawLine(      OFF +i*SIZE, M*SIZE  +D,       OFF2  +i*SIZE,          D, paint);
342
        canvas.drawLine((SIZE-OFF)+i*SIZE, M*SIZE  +D, (SIZE-OFF2) +i*SIZE,          D, paint);
343

    
344
        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);
345
        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);
346
        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);
347
        }
348

    
349
      return result;
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

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432

    
433
  private MeshBase createStaticMesh()
434
    {
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 };
455

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

    
460
    for(int i=1; i<MESHES; i++)
461
      {
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]
522

    
523
    result.apply(effect1);
524
    result.apply(effect2);
525
    result.apply(effect3);
526
    result.apply(effect4);
527
    result.apply(effect5);
528
    result.apply(effect6);
529

    
530
    result.apply(effect7);
531
    result.apply(effect8);
532
    result.apply(effect9);
533
    result.apply(effect10);
534

    
535
    result.apply(effect11);
536

    
537
    final float ratio = 0.2f;
538
    final Static4D[] maps = new Static4D[MESHES];
539

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

    
545
    result.setTextureMap(maps,0);
546
    result.setShowNormals(true);
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

    
675
    return result;
676
    }
677

    
678
///////////////////////////////////////////////////////////////////////////////////////////////////
679

    
680
    private void createMesh()
681
      {
682
      mMesh = createStaticMesh();
683

    
684
      int numEff = mMesh.numEffComponents();
685

    
686
      for(int i=0; i<numEff; i++)
687
        {
688
        mMesh.setEffectAssociation(i, 0, i);
689
        }
690
      }
691

    
692
///////////////////////////////////////////////////////////////////////////////////////////////////
693

    
694
    private void openMesh(int resourceID)
695
      {
696
      Context con = mView.getContext();
697
      Resources res = con.getResources();
698
      InputStream is = res.openRawResource(resourceID);
699
      DataInputStream dos = new DataInputStream(is);
700
      mMesh = new MeshFile(dos);
701

    
702
      int numEff = mMesh.numEffComponents();
703

    
704
      for(int i=0; i<numEff; i++)
705
        {
706
        mMesh.setEffectAssociation(i, 0, i);
707
        }
708

    
709
      try
710
        {
711
        is.close();
712
        }
713
      catch(IOException e)
714
        {
715
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
716
        }
717
      }
718

    
719
///////////////////////////////////////////////////////////////////////////////////////////////////
720

    
721
    MeshBase getMesh()
722
      {
723
      return mMesh;
724
      }
725

    
726
///////////////////////////////////////////////////////////////////////////////////////////////////
727

    
728
    long getTime()
729
      {
730
      return mTime;
731
      }
732

    
733
///////////////////////////////////////////////////////////////////////////////////////////////////
734

    
735
    int getBytes()
736
      {
737
      if( mMesh instanceof MeshFile )
738
        {
739
        return ((MeshFile)mMesh).getNumBytes();
740
        }
741

    
742
      return 0;
743
      }
744

    
745
///////////////////////////////////////////////////////////////////////////////////////////////////
746

    
747
    int getVertices()
748
      {
749
      return mMesh.getNumVertices();
750
      }
751

    
752
///////////////////////////////////////////////////////////////////////////////////////////////////
753

    
754
    int getEndEffIndex(int component)
755
      {
756
      return mMesh.getLastVertexEff(component);
757
      }
758

    
759
///////////////////////////////////////////////////////////////////////////////////////////////////
760

    
761
    int getEndTexIndex(int component)
762
      {
763
      return mMesh.getLastVertexTex(component);
764
      }
765

    
766
///////////////////////////////////////////////////////////////////////////////////////////////////
767

    
768
    int getEffComponentNum()
769
      {
770
      return mMesh.numEffComponents();
771
      }
772

    
773
///////////////////////////////////////////////////////////////////////////////////////////////////
774

    
775
    int getTexComponentNum()
776
      {
777
      return mMesh.numTexComponents();
778
      }
779
}
(2-2/3)