Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 96a6db6c

1 42aa970f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 1af8e143 Leszek Koltunski
import android.content.Context;
23
import android.content.res.Resources;
24 42aa970f Leszek Koltunski
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.opengl.GLSurfaceView;
28
29 8ac4c0bc Leszek Koltunski
import org.distorted.examples.R;
30 a65604a7 Leszek Koltunski
import org.distorted.library.effect.Effect;
31 6991ece5 Leszek Koltunski
import org.distorted.library.effect.EffectType;
32 42aa970f Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34 39b925d5 Leszek Koltunski
import org.distorted.library.effect.VertexEffectDisappear;
35 42aa970f Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedLibrary;
37
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.main.DistortedTexture;
39 6991ece5 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
40 1af8e143 Leszek Koltunski
import org.distorted.library.mesh.MeshFile;
41 42aa970f Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44
45 1af8e143 Leszek Koltunski
import java.io.DataInputStream;
46
import java.io.IOException;
47
import java.io.InputStream;
48
49 42aa970f Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
50
import javax.microedition.khronos.opengles.GL10;
51
52 6991ece5 Leszek Koltunski
import static org.distorted.examples.meshfile.MeshFileActivity.PROCEDURAL;
53
54 42aa970f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 061449ed Leszek Koltunski
class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
57 42aa970f Leszek Koltunski
{
58 7edab735 Leszek Koltunski
    private static final float SQ2 = (float)Math.sqrt(2);
59
    private static final float SQ3 = (float)Math.sqrt(3);
60 db9903e8 Leszek Koltunski
    private static final float SQ5 = (float)Math.sqrt(5);
61 16b336db Leszek Koltunski
    private final float DEFAULT_SCALE = 0.3f;
62
63 140f2c4e Leszek Koltunski
    private final GLSurfaceView mView;
64 42aa970f Leszek Koltunski
    private DistortedTexture mTexture;
65 140f2c4e Leszek Koltunski
    private final DistortedScreen mScreen;
66
    private final DistortedEffects mEffects;
67
    private final Static3D mScale;
68 48c88f57 Leszek Koltunski
    private long mTime;
69 16b336db Leszek Koltunski
    private float mCurrentScale;
70 6991ece5 Leszek Koltunski
    private MeshBase mMesh;
71 42aa970f Leszek Koltunski
72
    Static4D mQuat1, mQuat2;
73
    int mScreenMin;
74
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
77
    MeshFileRenderer(GLSurfaceView v)
78
      {
79
      mView = v;
80
      mScreen = new DistortedScreen();
81
      mScale= new Static3D(1,1,1);
82
      Static3D center=new Static3D(0,0,0);
83
84 16b336db Leszek Koltunski
      mCurrentScale = DEFAULT_SCALE;
85
86 42aa970f Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);
87 fe032f52 Leszek Koltunski
      mQuat2 = new Static4D(0,0,0,1);
88 42aa970f Leszek Koltunski
89
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
90
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
91
92
      quatInt1.add(mQuat1);
93
      quatInt2.add(mQuat2);
94
95 39b925d5 Leszek Koltunski
      VertexEffectDisappear disappear = new VertexEffectDisappear();
96
      disappear.setMeshAssociation(1,-1);
97
98 42aa970f Leszek Koltunski
      mEffects = new DistortedEffects();
99
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
100
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
101
      mEffects.apply( new MatrixEffectScale(mScale));
102
103 39b925d5 Leszek Koltunski
      mEffects.apply( disappear );
104
105 42aa970f Leszek Koltunski
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
106 48c88f57 Leszek Koltunski
      mScreen.showFPS();
107 42aa970f Leszek Koltunski
      }
108
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
   
111
    public void onDrawFrame(GL10 glUnused) 
112
      {
113
      mScreen.render( System.currentTimeMillis() );
114
      }
115
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
    
118
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
119
      {
120
      mScreenMin = Math.min(width, height);
121 16b336db Leszek Koltunski
      float factor = mCurrentScale*mScreenMin;
122 42aa970f Leszek Koltunski
      mScale.set(factor,factor,factor);
123
      mScreen.resize(width, height);
124
      }
125
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
129
      {
130
      if( mTexture==null ) mTexture = new DistortedTexture();
131
132 a65604a7 Leszek Koltunski
      Effect.enableEffects(EffectType.VERTEX);
133 04a21ed6 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX, 20);
134 ef02f5e0 Leszek Koltunski
      DistortedLibrary.needTransformFeedback();
135 7c72e798 Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
136 061449ed Leszek Koltunski
      }
137
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
140
    public void distortedException(Exception ex)
141
      {
142
      android.util.Log.e("MeshFile", ex.getMessage() );
143 42aa970f Leszek Koltunski
      }
144
145 16b336db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
146
//   0 ---> 0
147
//  50 ---> DEFAULT_SCALE
148
// 100 ---> 4*DEFAULT_SCALE
149
150
    void setScale(int scale)
151
      {
152
      if( scale<= 50 )
153
        {
154
        mCurrentScale = DEFAULT_SCALE * scale / 50.0f;
155
        }
156
      else
157
        {
158
        mCurrentScale = DEFAULT_SCALE * ( 3*(scale/50.0f) - 2.0f);
159
        }
160
161
      float factor = mCurrentScale*mScreenMin;
162
      mScale.set(factor,factor,factor);
163
      }
164
165 42aa970f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167 1af8e143 Leszek Koltunski
    void open(int resourceID)
168 42aa970f Leszek Koltunski
      {
169 099d8f8b Leszek Koltunski
      if( mTexture==null ) mTexture = new DistortedTexture();
170 8ac4c0bc Leszek Koltunski
      mTexture.setTexture( createTexture(resourceID) );
171
172 acad428e Leszek Koltunski
      long t1 = System.currentTimeMillis();
173 6991ece5 Leszek Koltunski
174
      if( resourceID!=PROCEDURAL )
175
        {
176
        openMesh(resourceID);
177
        }
178
      else
179
        {
180
        createMesh();
181
        }
182
183 acad428e Leszek Koltunski
      long t2 = System.currentTimeMillis();
184
185 48c88f57 Leszek Koltunski
      mTime = t2-t1;
186 42aa970f Leszek Koltunski
187 71c7624f Leszek Koltunski
      mScreen.detachAll();
188 7198e5c9 Leszek Koltunski
      mScreen.attach(mTexture,mEffects,mMesh);
189 42aa970f Leszek Koltunski
      }
190
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
193 1af8e143 Leszek Koltunski
    private Bitmap createTexture(int resourceID)
194 42aa970f Leszek Koltunski
      {
195 7edab735 Leszek Koltunski
      TextureFactory factory = TextureFactory.getInstance();
196
197
      float[] vertices;
198
      int[] colors;
199
      float F = 0.5f;
200
      float E = SQ3/2;
201
      float G = SQ2/4;
202
203 8ac4c0bc Leszek Koltunski
      switch(resourceID)
204
          {
205
          case  R.raw.deferredjob:
206 7edab735 Leszek Koltunski
          case  R.raw.meshjoin   :
207 1beffb69 Leszek Koltunski
          case  PROCEDURAL       :
208 7edab735 Leszek Koltunski
          case  R.raw.predeform  : return createWhiteTexture();
209
210 8ac4c0bc Leszek Koltunski
          case  R.raw.cube2      :
211
          case  R.raw.cube3      :
212
          case  R.raw.cube4      :
213 7edab735 Leszek Koltunski
          case  R.raw.cube5      : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
214
                                   vertices = new float[] { -F,-F, +F,-F, +F,+F, -F,+F};
215
                                   return factory.createTexture(vertices,colors,0.10f, 0.10f, true);
216 8ac4c0bc Leszek Koltunski
          case  R.raw.pyra3      :
217
          case  R.raw.pyra4      :
218 7edab735 Leszek Koltunski
          case  R.raw.pyra5      : colors = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
219
                                   vertices = new float[] { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
220
                                   return factory.createTexture(vertices,colors,0.05f, 0.05f, true);
221 8ac4c0bc Leszek Koltunski
222 7edab735 Leszek Koltunski
          case  R.raw.dino       : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
223
                                   vertices = new float[] { -F,F/3, 0,-2*F/3, +F,F/3 };
224
                                   return factory.createTexture(vertices,colors,0.05f, 0.03f, true);
225 8ac4c0bc Leszek Koltunski
226 7edab735 Leszek Koltunski
          case R.raw.skewb       : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
227
                                   //vertices = new float[] { -G,-G, +G,-G, +G,+G, -G,+G };
228 8ac4c0bc Leszek Koltunski
229 7edab735 Leszek Koltunski
                                   vertices = new float[] { -F+F/4,F/4, F/4,-F+F/4, F/4,F/4};
230
                                   return factory.createTexture(vertices,colors,0.05f, 0.08f, true);
231
          }
232 8ac4c0bc Leszek Koltunski
233 7edab735 Leszek Koltunski
      return null;
234 8ac4c0bc Leszek Koltunski
      }
235
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
238 7edab735 Leszek Koltunski
    private Bitmap createWhiteTexture()
239 8ac4c0bc Leszek Koltunski
      {
240 7edab735 Leszek Koltunski
      int SIZE = 1;
241 8ac4c0bc Leszek Koltunski
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
242
      Canvas canvas = new Canvas(bitmap);
243
244
      Paint paint = new Paint();
245 36793c52 Leszek Koltunski
      paint.setColor(0xffffff55);
246 1beffb69 Leszek Koltunski
      paint.setStyle(Paint.Style.FILL);
247 7edab735 Leszek Koltunski
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
248 6991ece5 Leszek Koltunski
249 72059460 Leszek Koltunski
      return bitmap;
250 6991ece5 Leszek Koltunski
      }
251
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253
254
    private void createMesh()
255
      {
256 96a6db6c Leszek Koltunski
      int mode = 10;
257 6983badf Leszek Koltunski
      int numComponents = 0;
258 4d883a92 Leszek Koltunski
      double[][] vertices = null;
259 28f4aba0 Leszek Koltunski
      int[][] vertIndexes = null;
260
      float[][] bands     = null;
261
      int[] bandIndexes   = null;
262
      float[][] corners   = null;
263
      int[] cornerIndexes = null;
264 6983badf Leszek Koltunski
      float[][] centers   = null;
265
      int[] centerIndexes = null;
266 28f4aba0 Leszek Koltunski
267 4d883a92 Leszek Koltunski
      ///// CUBE ////////////////////////////////////////////////////////////////////////////////
268
269
      if( mode==0 )
270 28f4aba0 Leszek Koltunski
        {
271 4d883a92 Leszek Koltunski
        vertices = new double[][]
272 33647db9 Leszek Koltunski
          {
273 4d883a92 Leszek Koltunski
              { 0.5, 0.5, 0.5 },
274
              { 0.5, 0.5,-0.5 },
275
              { 0.5,-0.5, 0.5 },
276
              { 0.5,-0.5,-0.5 },
277
              {-0.5, 0.5, 0.5 },
278
              {-0.5, 0.5,-0.5 },
279
              {-0.5,-0.5, 0.5 },
280
              {-0.5,-0.5,-0.5 },
281 33647db9 Leszek Koltunski
          };
282
283 28f4aba0 Leszek Koltunski
        vertIndexes = new int[][]
284 33647db9 Leszek Koltunski
          {
285 140f2c4e Leszek Koltunski
              {2,3,1,0},   // counterclockwise!
286
              {7,6,4,5},
287
              {4,0,1,5},
288
              {7,3,2,6},
289
              {6,2,0,4},
290
              {3,7,5,1}
291 33647db9 Leszek Koltunski
          };
292
293 28f4aba0 Leszek Koltunski
        bands = new float[][]
294 33647db9 Leszek Koltunski
          {
295 6a96e571 Leszek Koltunski
              {0.05f,40,0.5f,0.2f,5,  2,2}
296 33647db9 Leszek Koltunski
          };
297 a65604a7 Leszek Koltunski
298 28f4aba0 Leszek Koltunski
        bandIndexes = new int[] { 0,0,0,0,0,0 };
299 1c41c4c9 Leszek Koltunski
300 28f4aba0 Leszek Koltunski
        corners = new float[][]
301 1b85f172 Leszek Koltunski
          {
302
              { 0.01f, 0.10f }
303
          };
304
305 28f4aba0 Leszek Koltunski
        cornerIndexes = new int[] { 0,0,0,0,0,0,0,0 };
306 6983badf Leszek Koltunski
307
        centers = new float[][]
308
          {
309
              { 0.0f, 0.0f, 0.0f }
310
          };
311
312
        centerIndexes = new int[] { 0,0,0,0,0,0,0,0 };
313
314
        numComponents = 8;
315 28f4aba0 Leszek Koltunski
        }
316 4d883a92 Leszek Koltunski
317
      ///// TETRAHEDRON //////////////////////////////////////////////////////////////////////////
318
319
      else if( mode==1 )
320 28f4aba0 Leszek Koltunski
        {
321 4d883a92 Leszek Koltunski
        vertices = new double[][]
322 28f4aba0 Leszek Koltunski
          {
323 4d883a92 Leszek Koltunski
              {-0.5, SQ2/4, 0.0},
324
              { 0.5, SQ2/4, 0.0},
325
              { 0.0,-SQ2/4, 0.5},
326
              { 0.0,-SQ2/4,-0.5}
327 28f4aba0 Leszek Koltunski
          };
328
329
        vertIndexes = new int[][]
330
          {
331
              {2,1,0},   // counterclockwise!
332
              {2,3,1},
333
              {3,2,0},
334
              {3,0,1}
335
          };
336
337
        bands = new float[][]
338
          {
339
              {0.05f,30,0.6f,0.5f,5,  2,2}
340
          };
341
342
        bandIndexes = new int[] { 0,0,0,0 };
343
344
        corners = new float[][]
345
          {
346
              { 0.02f, 0.10f }
347
          };
348
349
        cornerIndexes = new int[] { 0,0,0,0 };
350 6983badf Leszek Koltunski
351
        centers = new float[][]
352
          {
353
              { 0.0f, 0.0f, 0.0f }
354
          };
355
356
        centerIndexes = new int[] { 0,0,0,0 };
357
358
        numComponents = 4;
359 28f4aba0 Leszek Koltunski
        }
360 4d883a92 Leszek Koltunski
361
      ///// DINO ////////////////////////////////////////////////////////////////////////////////
362
363
      else if( mode==2 )
364 28f4aba0 Leszek Koltunski
        {
365 4d883a92 Leszek Koltunski
        vertices = new double[][]
366 28f4aba0 Leszek Koltunski
          {
367 4d883a92 Leszek Koltunski
              {-0.5, 0.0, 0.0},
368
              { 0.5, 0.0, 0.0},
369
              { 0.0,-0.5, 0.0},
370
              { 0.0, 0.0,-0.5}
371 28f4aba0 Leszek Koltunski
          };
372
373
        vertIndexes = new int[][]
374
          {
375
              {2,1,0},   // counterclockwise!
376
              {2,3,1},
377
              {3,2,0},
378
              {3,0,1}
379
          };
380
381
        bands = new float[][]
382
          {
383 6a96e571 Leszek Koltunski
              {0.028f,30,0.25f,0.1f,7,  2,5},
384
              {0.001f,30,0.25f,0.1f,7,  1,2}
385 28f4aba0 Leszek Koltunski
          };
386
387
        bandIndexes = new int[] { 0,1,1,0 };
388
389
        corners = new float[][]
390
          {
391 6983badf Leszek Koltunski
              { 0.02f, 0.04f }
392 28f4aba0 Leszek Koltunski
          };
393
394
        cornerIndexes = new int[] { 0,0,0,0 };
395 6983badf Leszek Koltunski
396
        centers = new float[][]
397
          {
398
              { 0.0f,-0.25f,-0.25f }
399
          };
400
401
        centerIndexes = new int[] { 0,0,0,0 };
402
403
        numComponents = 4;
404 28f4aba0 Leszek Koltunski
        }
405 1b85f172 Leszek Koltunski
406 6a96e571 Leszek Koltunski
      ///// OCTAHEDRON ////////////////////////////////////////////////////////////////////////////
407
408
      else if( mode==3 )
409
        {
410
        vertices = new double[][]
411
          {
412
              { 0.5,   0.0, 0.5},
413
              { 0.5,   0.0,-0.5},
414
              {-0.5,   0.0,-0.5},
415
              {-0.5,   0.0, 0.5},
416
              { 0.0, SQ2/2, 0.0},
417
              { 0.0,-SQ2/2, 0.0},
418
          };
419
420
        vertIndexes = new int[][]
421
          {
422
              {3,0,4},   // counterclockwise!
423
              {0,1,4},
424
              {1,2,4},
425
              {2,3,4},
426
              {5,0,3},
427
              {5,1,0},
428
              {5,2,1},
429
              {5,3,2}
430
          };
431
432
        bands = new float[][]
433
          {
434
             {0.05f,17,0.5f,0.2f,5,  2,2}
435
          };
436
437
        bandIndexes = new int[] { 0,0,0,0,0,0,0,0 };
438
439
        corners = new float[][]
440
          {
441
              { 0.03f, 0.12f }
442
          };
443
444
        cornerIndexes = new int[] { 0,0,0,0,0,0 };
445 6983badf Leszek Koltunski
446
        centers = new float[][]
447
          {
448
              { 0.0f, 0.0f, 0.0f }
449
          };
450
451
        centerIndexes = new int[] { 0,0,0,0,0,0 };
452
453
        numComponents = 8;
454 6a96e571 Leszek Koltunski
        }
455
456 3083fab8 Leszek Koltunski
      ///// KILOMINX EDGE ////////////////////////////////////////////////////////////////////////
457
458
      else if( mode==4 )
459
        {
460
        double SQ5  = Math.sqrt(5);
461
        double SIN18= (SQ5-1)/4;
462
        double COS18= 0.25*Math.sqrt(10.0+2.0*SQ5);
463
        double H = 1.0;
464
        double L = 2.0;
465
        double X = H*Math.sqrt((5+SQ5)/10);
466
        double Y = H*Math.sqrt((5-SQ5)/10);
467
        double D = H*SIN18/COS18;
468
469
        vertices = new double[][]
470
          {
471
              { 0.0,   Y, L/2},
472
              {   X, 0.0, L/2},
473
              { 0.0,  -Y, L/2},
474
              {  -X, 0.0, L/2},
475
              { 0.0,  Y, -L/2 +D},
476
              {   X, 0.0,-L/2   },
477
              { 0.0, -Y, -L/2-D },
478
              {  -X, 0.0,-L/2   }
479
          };
480
481
        vertIndexes = new int[][]
482
          {
483
              {3,2,1,0},   // counterclockwise!
484
              {0,1,5,4},
485
              {3,0,4,7},
486
              {2,1,5,6},
487
              {3,2,6,7},
488
              {4,5,6,7}
489
          };
490
491
        bands = new float[][]
492
          {
493
             {0.04f,13,(float)(L/3),(float)L/8, 5,2,3},
494
             {0.00f, 0,(float)(L/2),(float)L/4, 5,2,3}
495
          };
496
497
        bandIndexes = new int[] { 1,0,0,1,1,1 };
498
499
        corners = new float[][]
500
          {
501 6983badf Leszek Koltunski
              { 0.04f, 0.12f }
502
          };
503
504
        cornerIndexes = new int[] { 0,-1,-1,-1,0,-1,-1,-1 };
505
506
        centers = new float[][]
507
          {
508
              { 0.0f, 0.0f, 0.0f }
509
          };
510
511
        centerIndexes = new int[] { 0,-1,-1,-1,0,-1,-1,-1 };
512
513
        numComponents = 6;
514
        }
515
516
      ///// IVY_CORNER ////////////////////////////////////////////////////////////////////////
517
518
      else if( mode==5 )
519
        {
520
        int IVY_N = 3;
521
        final float IVY_D = 0.003f;
522
        final double angle = Math.PI/(2*IVY_N);
523
        final double CORR  = 1.0 - 2*IVY_D;
524
525
        vertices = new double[3*IVY_N+7][3];
526
        vertIndexes = new int[6][IVY_N+4];
527
528
        vertices[0][0] = 0.0;
529
        vertices[0][1] = 0.0;
530
        vertices[0][2] = 0.0;
531
        vertices[1][0] =-1.0;
532
        vertices[1][1] = 0.0;
533
        vertices[1][2] = 0.0;
534
        vertices[2][0] = 0.0;
535
        vertices[2][1] =-1.0;
536
        vertices[2][2] = 0.0;
537
        vertices[3][0] = 0.0;
538
        vertices[3][1] = 0.0;
539
        vertices[3][2] =-1.0;
540
541
        vertIndexes[0][0] = 2;
542
        vertIndexes[0][1] = 0;
543
        vertIndexes[0][2] = 1;
544
        vertIndexes[3][0] = 2;
545
        vertIndexes[3][1] = 0;
546
        vertIndexes[3][2] = 1;
547
548
        vertIndexes[1][0] = 3;
549
        vertIndexes[1][1] = 0;
550
        vertIndexes[1][2] = 2;
551
        vertIndexes[4][0] = 3;
552
        vertIndexes[4][1] = 0;
553
        vertIndexes[4][2] = 2;
554
555
        vertIndexes[2][0] = 1;
556
        vertIndexes[2][1] = 0;
557
        vertIndexes[2][2] = 3;
558
        vertIndexes[5][0] = 1;
559
        vertIndexes[5][1] = 0;
560
        vertIndexes[5][2] = 3;
561
562
        int N1 = 4;
563
        int N2 = N1 + IVY_N + 1;
564
        int N3 = N2 + IVY_N + 1;
565
566
        for(int i=0; i<=IVY_N; i++)
567
          {
568
          double cos1 = Math.cos((IVY_N-i)*angle);
569
          double sin1 = Math.sin((IVY_N-i)*angle);
570
          double cos2 = Math.cos((      i)*angle);
571
          double sin2 = Math.sin((      i)*angle);
572
573
          vertices[N1+i][0] = CORR*(cos1-0.5) - 0.5;
574
          vertices[N1+i][1] = CORR*(sin1-0.5) - 0.5;
575
          vertices[N1+i][2] = 0.0;
576
577
          vertices[N2+i][0] = 0.0;
578
          vertices[N2+i][1] = CORR*(sin2-0.5) - 0.5;
579
          vertices[N2+i][2] = CORR*(cos2-0.5) - 0.5;
580
581
          vertices[N3+i][0] = CORR*(cos2-0.5) - 0.5;
582
          vertices[N3+i][1] = 0.0;
583
          vertices[N3+i][2] = CORR*(sin2-0.5) - 0.5;
584
585
          vertIndexes[0][i+3] = N1 + i;
586
          vertIndexes[1][i+3] = N2 + i;
587
          vertIndexes[2][i+3] = N3 + i;
588
          vertIndexes[3][i+3] = N1 + i;
589
          vertIndexes[4][i+3] = N2 + i;
590
          vertIndexes[5][i+3] = N3 + i;
591
          }
592
593
        bands = new float[][]
594
          {
595
             {+0.012f,20,0.2f,0.5f,7,1,2},
596
             {-0.100f,20,0.2f,0.0f,2,1,2}
597
          };
598
599
        bandIndexes = new int[] { 0,0,0,1,1,1 };
600
601
        corners = new float[][]
602
          {
603
              { 0.04f, 0.12f }
604
          };
605
606
        cornerIndexes = new int[3*IVY_N+7];
607
608
        centers = new float[][]
609
          {
610
              {-0.5f,-0.5f,-0.5f}
611 3083fab8 Leszek Koltunski
          };
612
613 6983badf Leszek Koltunski
        centerIndexes = new int[3*IVY_N+7];
614
615
        for(int i=0; i<4; i++)
616
          {
617
          cornerIndexes[i] = 0;
618
          centerIndexes[i] = 0;
619
          }
620
        for(int i=4; i<3*IVY_N+7; i++)
621
          {
622
          cornerIndexes[i] = -1;
623
          centerIndexes[i] = -1;
624
          }
625
626
        numComponents = 6;
627
        }
628
629
      ///// IVY_FACE ////////////////////////////////////////////////////////////////////////
630
631
      else if( mode==6 )
632
        {
633
        int IVY_N = 6;
634
        final float IVY_D = 0.003f;
635
        final double angle = Math.PI/(2*IVY_N);
636
        final double CORR  = 1.0 - 2*IVY_D;
637
638
        vertices   = new double[2*IVY_N][3];
639
        vertIndexes= new int[2][2*IVY_N];
640
641
        for(int i=0; i<IVY_N; i++)
642
          {
643
          double cos = Math.cos(i*angle);
644
          double sin = Math.sin(i*angle);
645
646
          vertices[i      ][0] = CORR*(0.5-cos);
647
          vertices[i      ][1] = CORR*(0.5-sin);
648
          vertices[i      ][2] = 0.0;
649
          vertices[i+IVY_N][0] = CORR*(cos-0.5);
650
          vertices[i+IVY_N][1] = CORR*(sin-0.5);
651
          vertices[i+IVY_N][2] = 0.0;
652
653
          vertIndexes[0][i      ] = i;
654
          vertIndexes[0][i+IVY_N] = i+IVY_N;
655
          vertIndexes[1][i      ] = i;
656
          vertIndexes[1][i+IVY_N] = i+IVY_N;
657
          }
658
659
        bands = new float[][]
660
          {
661
             {+0.012f,20,0.2f,0.5f,7,1,2},
662
             {-0.100f,20,0.2f,0.0f,2,1,2},
663
          };
664
665
        bandIndexes = new int[] { 0,1 };
666
667
        corners = new float[][]
668
          {
669
             {0.03f,0.10f}
670
          };
671
672
        centers = new float[][]
673
          {
674
              {-0.0f,-0.0f,-0.5f}
675
          };
676
677
        cornerIndexes = new int[2*IVY_N];
678
        centerIndexes = new int[2*IVY_N];
679
680
        for(int i=0; i<2*IVY_N; i++)
681
          {
682
          cornerIndexes[i] = -1;
683
          centerIndexes[i] = -1;
684
          }
685
686
        cornerIndexes[0    ] = 0;
687
        cornerIndexes[IVY_N] = 0;
688
        centerIndexes[0    ] = 0;
689
        centerIndexes[IVY_N] = 0;
690
691
        numComponents = 2;
692 3083fab8 Leszek Koltunski
        }
693
694 db9903e8 Leszek Koltunski
      ///// SKEWB Ultimate SMALL  /////////////////////////////////////////////////////////////
695
696
      else if( mode==7 )
697
        {
698
        double S = (SQ5+1)/4;
699
700
        vertices = new double[][]
701
          {
702
              { 0.0       ,  0.0      , 0.0       },
703
              { -0.5*S    , 0.5*S+0.25, -0.25     },
704
              {-0.25      , -S/2      , (-2*S-1)/4},
705
              { 0.5*S+0.25, 0.25      , -S/2      },
706
              { 0.0       , 0.5       , -S-0.5    },
707
              { 0.0       , 0.5       , 0.0       },
708
              { -0.5*S    ,-0.5*S+0.25, -0.25     },
709
              {  0.5*S    ,-0.5*S+0.25, -0.25     }
710
          };
711
712
        vertIndexes = new int[][]
713
          {
714
              {6,0,5,1},   // counterclockwise!
715
              {0,7,3,5},
716
              {0,6,2,7},
717
              {4,3,5,1},
718
              {4,2,7,3},
719
              {4,1,6,2},
720
          };
721
722
        bands = new float[][]
723
          {
724
             {0.04f,17,0.5f,0.2f,5,  2,2},
725
             {0.01f, 1,0.5f,0.2f,5,  2,2}
726
          };
727
728
        bandIndexes = new int[] { 0,0,0,1,1,1 };
729
730
        corners = new float[][]
731
          {
732
              { 0.013f, 0.08f }
733
          };
734
735
        cornerIndexes = new int[] { 0, 0, 0, 0,-1,0,0,0 };
736
737
        centers = new float[][]
738
          {
739
              { 0.0f,-0.5f, (float)(-S-0.5) }
740
          };
741
742
        centerIndexes = new int[] { 0,0,0,0,0,0,0,0 };
743
744
        numComponents = 8;
745
        }
746
747
      ///// SKEWB Ultimate BIG ///////////////////////////////////////////////////////////////
748
749
      else if( mode==8 )
750
        {
751
        double S = (SQ5+1)/4;
752
753
        vertices = new double[][]
754
          {
755
              {-S/2     ,-S/2+0.25,     0.25},
756
              { S/2     , S/2-0.25,    -0.25},
757
              {-S       ,     0.00,     0.00},
758
              {     0.25, S/2     ,-S/2-0.25},
759
              {-S/2     ,-S/2-0.25,     0.25},
760
              { S/2+0.25,    -0.25,-S/2     },
761
              {-S       ,    -0.50,     0.00},
762
              {     0.50,     0.00,-S       },
763
              {-S  +0.25, S/2     ,-S/2-0.25},
764
              {     0.25,-S/2-0.50,-S/2+0.25},
765
              {-S/2     ,-S/2-0.25,-S  -0.25}
766
          };
767
768
        vertIndexes = new int[][]
769
          {
770
              {0,1,3,8,2},   // counterclockwise!
771
              {0,4,9,5,1},
772
              { 0,2,6,4},
773
              { 1,5,7,3},
774
              {10,9,4,6},
775
              {10,9,5,7},
776
              {10,8,3,7},
777
              {10,8,2,6}
778
          };
779
780
        bands = new float[][]
781
          {
782
             {0.04f,17,0.5f,0.2f,5,  2,2},
783
             {0.04f,17,0.5f,0.2f,5,  2,2},
784
             {0.01f, 1,0.5f,0.2f,5,  2,2}
785
          };
786
787
        bandIndexes = new int[] { 0,0,1,1,2,2,2,2 };
788
789
        corners = new float[][]
790
          {
791
              { 0.013f, 0.08f }
792
          };
793
794
        cornerIndexes = new int[] { 0,0,0,0,0,0,0,0,0,0,-1 };
795
796
        centers = new float[][]
797
          {
798
              { (float)(-S/2), 0.25f, (float)(-S/2-0.5) }
799
          };
800
801
        centerIndexes = new int[] { 0,0,0,0,0,0,0,0,0,0,0 };
802
803
        numComponents = 8;
804
        }
805
806 96a6db6c Leszek Koltunski
      ///// SQUARE-1 MIDDLE ///////////////////////////////////////////////////////////////
807
808
      else if( mode==9 )
809
        {
810
        final float X = 3*(2-SQ3)/2;
811
812
        vertices = new double[][]
813
          {
814
              { -1.5-X, 0.5, 1.5 },
815
              {    0.0, 0.5, 1.5 },
816
              {    0.0, 0.5,-1.5 },
817
              { -1.5+X, 0.5,-1.5 },
818
              { -1.5-X,-0.5, 1.5 },
819
              {    0.0,-0.5, 1.5 },
820
              {    0.0,-0.5,-1.5 },
821
              { -1.5+X,-0.5,-1.5 }
822
          };
823
824
        vertIndexes = new int[][]
825
          {
826
              {0,1,2,3},   // counterclockwise!
827
              {4,5,6,7},
828
              {4,5,1,0},
829
              {5,6,2,1},
830
              {6,7,3,2},
831
              {7,4,0,3}
832
          };
833
834
        bands = new float[][]
835
          {
836
             {0.040f,35,0.8f,1.0f,5,2,1},
837
             {0.020f,35,0.8f,1.0f,5,2,1},
838
             {0.001f,35,0.8f,1.0f,5,2,1}
839
          };
840
841
        bandIndexes = new int[] { 2,2,1,1,0,2 };
842
843
        corners = new float[][]
844
          {
845
              {0.04f,0.05f}
846
          };
847
848
        cornerIndexes = new int[] { 0,0,0,0,0,0,0,0 };
849
850
        centers = new float[][]
851
          {
852
              { -0.75f, 0.0f, 0.0f}
853
          };
854
855
        centerIndexes = new int[] { 0,0,0,0,0,0,0,0 };
856
857
        numComponents = 6;
858
        }
859
860
      ///// SQUARE-1 EDGE ///////////////////////////////////////////////////////////////
861
862
      else if( mode==10 )
863
        {
864
        final float X = 3*(2-SQ3)/2;
865
866
        vertices = new double[][]
867
          {
868
              { -X, 0.5, 0.0 },
869
              { +X, 0.5, 0.0 },
870
              {0.0, 0.5,-1.5 },
871
              { -X,-0.5, 0.0 },
872
              { +X,-0.5, 0.0 },
873
              {0.0,-0.5,-1.5 },
874
          };
875
876
        vertIndexes = new int[][]
877
          {
878
              {0,1,2},   // counterclockwise!
879
              {3,4,5},
880
              {3,4,1,0},
881
              {4,5,2,1},
882
              {5,3,0,2}
883
          };
884
885
        bands = new float[][]
886
          {
887
            {0.038f,35,0.5f,0.9f, 5,2,1},
888
            {0.001f,35,0.5f,0.9f, 5,2,1}
889
          };
890
891
        bandIndexes = new int[]  { 0,1,0,1,1 };
892
893
        corners = new float[][]
894
          {
895
             {0.06f,0.20f}
896
          };
897
898
        cornerIndexes = new int[] { 0,0,-1,0,0,-1 };
899
900
        centers = new float[][]
901
          {
902
              { 0.0f, 0.0f,-0.5f}
903
          };
904
905
        centerIndexes = new int[] { 0,0,-1,0,0,-1 };
906
907
        numComponents = 6;
908
        }
909
910
      ///// SQUARE-1 CORNER ///////////////////////////////////////////////////////////////
911
912
      else if( mode==11 )
913
        {
914
        final float X = 3*(2-SQ3)/2;
915
916
        vertices = new double[][]
917
          {
918
              { X-1.5, 0.5,  0.0 },
919
              {   0.0, 0.5,  0.0 },
920
              {   0.0, 0.5,X-1.5 },
921
              {  -1.5, 0.5, -1.5 },
922
              { X-1.5,-0.5,  0.0 },
923
              {   0.0,-0.5,  0.0 },
924
              {   0.0,-0.5,X-1.5 },
925
              {  -1.5,-0.5, -1.5 }
926
          };
927
928
        vertIndexes = new int[][]
929
          {
930
              {0,1,2,3},   // counterclockwise!
931
              {4,5,6,7},
932
              {4,5,1,0},
933
              {5,6,2,1},
934
              {7,4,0,3},
935
              {6,7,3,2}
936
          };
937
938
        bands = new float[][]
939
          {
940
            {0.038f,35,0.9f,1.0f, 5,2,1},
941
            {0.001f,35,0.9f,1.0f, 5,2,1}
942
          };
943
944
        bandIndexes = new int[]  { 0,1,0,0,1,1 };
945
946
        corners = new float[][]
947
          {
948
            {0.08f,0.20f}
949
          };
950
951
        cornerIndexes = new int[] { 0,0,0,-1,0,0,0,-1 };
952
953
        centers = new float[][]
954
          {
955
             { -0.5f, 0.0f,-0.5f}
956
          };
957
958
        centerIndexes = new int[] { -1,0,-1,-1,-1,0,-1,-1 };
959
960
        numComponents = 6;
961
        }
962
963 db9903e8 Leszek Koltunski
      ///// END DEFINITIONS /////////////////////////////////////////////////////////////////
964
965 33647db9 Leszek Koltunski
      FactoryCubit factory = FactoryCubit.getInstance();
966 6991ece5 Leszek Koltunski
967 6983badf Leszek Koltunski
      factory.clear();
968
      factory.createNewFaceTransform(vertices,vertIndexes);
969
      mMesh = factory.createRoundedSolid(vertices, vertIndexes,
970
                                         bands, bandIndexes,
971
                                         corners, cornerIndexes,
972
                                         centers, centerIndexes,
973
                                         numComponents );
974
975
      int numEff = mMesh.getNumEffComponents();
976 6991ece5 Leszek Koltunski
977
      for(int i=0; i<numEff; i++)
978
        {
979
        mMesh.setEffectAssociation(i, 0, i);
980
        }
981
      }
982 42aa970f Leszek Koltunski
983
///////////////////////////////////////////////////////////////////////////////////////////////////
984
985 6991ece5 Leszek Koltunski
    private void openMesh(int resourceID)
986 42aa970f Leszek Koltunski
      {
987 1af8e143 Leszek Koltunski
      Context con = mView.getContext();
988
      Resources res = con.getResources();
989
      InputStream is = res.openRawResource(resourceID);
990
      DataInputStream dos = new DataInputStream(is);
991 7198e5c9 Leszek Koltunski
      mMesh = new MeshFile(dos);
992 1af8e143 Leszek Koltunski
993 6983badf Leszek Koltunski
      int numEff = mMesh.getNumEffComponents();
994 39b925d5 Leszek Koltunski
995
      for(int i=0; i<numEff; i++)
996
        {
997
        mMesh.setEffectAssociation(i, 0, i);
998
        }
999
1000 1af8e143 Leszek Koltunski
      try
1001
        {
1002
        is.close();
1003
        }
1004
      catch(IOException e)
1005
        {
1006 48c88f57 Leszek Koltunski
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
1007 1af8e143 Leszek Koltunski
        }
1008 42aa970f Leszek Koltunski
      }
1009 48c88f57 Leszek Koltunski
1010 39b925d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1011
1012 6991ece5 Leszek Koltunski
    MeshBase getMesh()
1013 39b925d5 Leszek Koltunski
      {
1014
      return mMesh;
1015
      }
1016
1017 48c88f57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1018
1019
    long getTime()
1020
      {
1021
      return mTime;
1022
      }
1023
1024
///////////////////////////////////////////////////////////////////////////////////////////////////
1025
1026
    int getBytes()
1027
      {
1028 6991ece5 Leszek Koltunski
      if( mMesh instanceof MeshFile )
1029
        {
1030
        return ((MeshFile)mMesh).getNumBytes();
1031
        }
1032
1033
      return 0;
1034 48c88f57 Leszek Koltunski
      }
1035
1036
///////////////////////////////////////////////////////////////////////////////////////////////////
1037
1038
    int getVertices()
1039
      {
1040 7198e5c9 Leszek Koltunski
      return mMesh.getNumVertices();
1041
      }
1042
1043
///////////////////////////////////////////////////////////////////////////////////////////////////
1044
1045
    int getEndEffIndex(int component)
1046
      {
1047
      return mMesh.getLastVertexEff(component);
1048
      }
1049
1050
///////////////////////////////////////////////////////////////////////////////////////////////////
1051
1052
    int getEndTexIndex(int component)
1053
      {
1054
      return mMesh.getLastVertexTex(component);
1055
      }
1056
1057
///////////////////////////////////////////////////////////////////////////////////////////////////
1058
1059
    int getEffComponentNum()
1060
      {
1061 6983badf Leszek Koltunski
      return mMesh.getNumEffComponents();
1062 7198e5c9 Leszek Koltunski
      }
1063
1064
///////////////////////////////////////////////////////////////////////////////////////////////////
1065
1066
    int getTexComponentNum()
1067
      {
1068 6983badf Leszek Koltunski
      return mMesh.getNumTexComponents();
1069 48c88f57 Leszek Koltunski
      }
1070 42aa970f Leszek Koltunski
}