Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 6a96e571

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 16b336db Leszek Koltunski
    private final float DEFAULT_SCALE = 0.3f;
61
62 140f2c4e Leszek Koltunski
    private final GLSurfaceView mView;
63 42aa970f Leszek Koltunski
    private DistortedTexture mTexture;
64 140f2c4e Leszek Koltunski
    private final DistortedScreen mScreen;
65
    private final DistortedEffects mEffects;
66
    private final Static3D mScale;
67 48c88f57 Leszek Koltunski
    private long mTime;
68 16b336db Leszek Koltunski
    private float mCurrentScale;
69 6991ece5 Leszek Koltunski
    private MeshBase mMesh;
70 42aa970f Leszek Koltunski
71
    Static4D mQuat1, mQuat2;
72
    int mScreenMin;
73
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76
    MeshFileRenderer(GLSurfaceView v)
77
      {
78
      mView = v;
79
      mScreen = new DistortedScreen();
80
      mScale= new Static3D(1,1,1);
81
      Static3D center=new Static3D(0,0,0);
82
83 16b336db Leszek Koltunski
      mCurrentScale = DEFAULT_SCALE;
84
85 42aa970f Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);
86 fe032f52 Leszek Koltunski
      mQuat2 = new Static4D(0,0,0,1);
87 42aa970f Leszek Koltunski
88
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
89
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
90
91
      quatInt1.add(mQuat1);
92
      quatInt2.add(mQuat2);
93
94 39b925d5 Leszek Koltunski
      VertexEffectDisappear disappear = new VertexEffectDisappear();
95
      disappear.setMeshAssociation(1,-1);
96
97 42aa970f Leszek Koltunski
      mEffects = new DistortedEffects();
98
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
99
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
100
      mEffects.apply( new MatrixEffectScale(mScale));
101
102 39b925d5 Leszek Koltunski
      mEffects.apply( disappear );
103
104 42aa970f Leszek Koltunski
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
105 48c88f57 Leszek Koltunski
      mScreen.showFPS();
106 42aa970f Leszek Koltunski
      }
107
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
   
110
    public void onDrawFrame(GL10 glUnused) 
111
      {
112
      mScreen.render( System.currentTimeMillis() );
113
      }
114
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
    
117
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
118
      {
119
      mScreenMin = Math.min(width, height);
120 16b336db Leszek Koltunski
      float factor = mCurrentScale*mScreenMin;
121 42aa970f Leszek Koltunski
      mScale.set(factor,factor,factor);
122
      mScreen.resize(width, height);
123
      }
124
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
    
127
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
128
      {
129
      if( mTexture==null ) mTexture = new DistortedTexture();
130
131 a65604a7 Leszek Koltunski
      Effect.enableEffects(EffectType.VERTEX);
132 04a21ed6 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX, 20);
133 ef02f5e0 Leszek Koltunski
      DistortedLibrary.needTransformFeedback();
134 7c72e798 Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
135 061449ed Leszek Koltunski
      }
136
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
139
    public void distortedException(Exception ex)
140
      {
141
      android.util.Log.e("MeshFile", ex.getMessage() );
142 42aa970f Leszek Koltunski
      }
143
144 16b336db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
145
//   0 ---> 0
146
//  50 ---> DEFAULT_SCALE
147
// 100 ---> 4*DEFAULT_SCALE
148
149
    void setScale(int scale)
150
      {
151
      if( scale<= 50 )
152
        {
153
        mCurrentScale = DEFAULT_SCALE * scale / 50.0f;
154
        }
155
      else
156
        {
157
        mCurrentScale = DEFAULT_SCALE * ( 3*(scale/50.0f) - 2.0f);
158
        }
159
160
      float factor = mCurrentScale*mScreenMin;
161
      mScale.set(factor,factor,factor);
162
      }
163
164 42aa970f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
165
166 1af8e143 Leszek Koltunski
    void open(int resourceID)
167 42aa970f Leszek Koltunski
      {
168 099d8f8b Leszek Koltunski
      if( mTexture==null ) mTexture = new DistortedTexture();
169 8ac4c0bc Leszek Koltunski
      mTexture.setTexture( createTexture(resourceID) );
170
171 acad428e Leszek Koltunski
      long t1 = System.currentTimeMillis();
172 6991ece5 Leszek Koltunski
173
      if( resourceID!=PROCEDURAL )
174
        {
175
        openMesh(resourceID);
176
        }
177
      else
178
        {
179
        createMesh();
180
        }
181
182 acad428e Leszek Koltunski
      long t2 = System.currentTimeMillis();
183
184 48c88f57 Leszek Koltunski
      mTime = t2-t1;
185 42aa970f Leszek Koltunski
186 71c7624f Leszek Koltunski
      mScreen.detachAll();
187 7198e5c9 Leszek Koltunski
      mScreen.attach(mTexture,mEffects,mMesh);
188 42aa970f Leszek Koltunski
      }
189
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191
192 1af8e143 Leszek Koltunski
    private Bitmap createTexture(int resourceID)
193 42aa970f Leszek Koltunski
      {
194 7edab735 Leszek Koltunski
      TextureFactory factory = TextureFactory.getInstance();
195
196
      float[] vertices;
197
      int[] colors;
198
      float F = 0.5f;
199
      float E = SQ3/2;
200
      float G = SQ2/4;
201
202 8ac4c0bc Leszek Koltunski
      switch(resourceID)
203
          {
204
          case  R.raw.deferredjob:
205 7edab735 Leszek Koltunski
          case  R.raw.meshjoin   :
206 1beffb69 Leszek Koltunski
          case  PROCEDURAL       :
207 7edab735 Leszek Koltunski
          case  R.raw.predeform  : return createWhiteTexture();
208
209 8ac4c0bc Leszek Koltunski
          case  R.raw.cube2      :
210
          case  R.raw.cube3      :
211
          case  R.raw.cube4      :
212 7edab735 Leszek Koltunski
          case  R.raw.cube5      : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
213
                                   vertices = new float[] { -F,-F, +F,-F, +F,+F, -F,+F};
214
                                   return factory.createTexture(vertices,colors,0.10f, 0.10f, true);
215 8ac4c0bc Leszek Koltunski
          case  R.raw.pyra3      :
216
          case  R.raw.pyra4      :
217 7edab735 Leszek Koltunski
          case  R.raw.pyra5      : colors = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
218
                                   vertices = new float[] { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
219
                                   return factory.createTexture(vertices,colors,0.05f, 0.05f, true);
220 8ac4c0bc Leszek Koltunski
221 7edab735 Leszek Koltunski
          case  R.raw.dino       : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
222
                                   vertices = new float[] { -F,F/3, 0,-2*F/3, +F,F/3 };
223
                                   return factory.createTexture(vertices,colors,0.05f, 0.03f, true);
224 8ac4c0bc Leszek Koltunski
225 7edab735 Leszek Koltunski
          case R.raw.skewb       : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
226
                                   //vertices = new float[] { -G,-G, +G,-G, +G,+G, -G,+G };
227 8ac4c0bc Leszek Koltunski
228 7edab735 Leszek Koltunski
                                   vertices = new float[] { -F+F/4,F/4, F/4,-F+F/4, F/4,F/4};
229
                                   return factory.createTexture(vertices,colors,0.05f, 0.08f, true);
230
          }
231 8ac4c0bc Leszek Koltunski
232 7edab735 Leszek Koltunski
      return null;
233 8ac4c0bc Leszek Koltunski
      }
234
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236
237 7edab735 Leszek Koltunski
    private Bitmap createWhiteTexture()
238 8ac4c0bc Leszek Koltunski
      {
239 7edab735 Leszek Koltunski
      int SIZE = 1;
240 8ac4c0bc Leszek Koltunski
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
241
      Canvas canvas = new Canvas(bitmap);
242
243
      Paint paint = new Paint();
244 36793c52 Leszek Koltunski
      paint.setColor(0xffffff55);
245 1beffb69 Leszek Koltunski
      paint.setStyle(Paint.Style.FILL);
246 7edab735 Leszek Koltunski
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
247 6991ece5 Leszek Koltunski
248 72059460 Leszek Koltunski
      return bitmap;
249 6991ece5 Leszek Koltunski
      }
250
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252
253
    private void createMesh()
254
      {
255 6a96e571 Leszek Koltunski
      int mode = 0;
256 4d883a92 Leszek Koltunski
      double[][] vertices = null;
257 28f4aba0 Leszek Koltunski
      int[][] vertIndexes = null;
258
      float[][] bands     = null;
259
      int[] bandIndexes   = null;
260
      float[][] corners   = null;
261
      int[] cornerIndexes = null;
262
263 4d883a92 Leszek Koltunski
      ///// CUBE ////////////////////////////////////////////////////////////////////////////////
264
265
      if( mode==0 )
266 28f4aba0 Leszek Koltunski
        {
267 4d883a92 Leszek Koltunski
        vertices = new double[][]
268 33647db9 Leszek Koltunski
          {
269 4d883a92 Leszek Koltunski
              { 0.5, 0.5, 0.5 },
270
              { 0.5, 0.5,-0.5 },
271
              { 0.5,-0.5, 0.5 },
272
              { 0.5,-0.5,-0.5 },
273
              {-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 33647db9 Leszek Koltunski
          };
278
279 28f4aba0 Leszek Koltunski
        vertIndexes = new int[][]
280 33647db9 Leszek Koltunski
          {
281 140f2c4e Leszek Koltunski
              {2,3,1,0},   // counterclockwise!
282
              {7,6,4,5},
283
              {4,0,1,5},
284
              {7,3,2,6},
285
              {6,2,0,4},
286
              {3,7,5,1}
287 33647db9 Leszek Koltunski
          };
288
289 28f4aba0 Leszek Koltunski
        bands = new float[][]
290 33647db9 Leszek Koltunski
          {
291 6a96e571 Leszek Koltunski
              {0.05f,40,0.5f,0.2f,5,  2,2}
292 33647db9 Leszek Koltunski
          };
293 a65604a7 Leszek Koltunski
294 28f4aba0 Leszek Koltunski
        bandIndexes = new int[] { 0,0,0,0,0,0 };
295 1c41c4c9 Leszek Koltunski
296 28f4aba0 Leszek Koltunski
        corners = new float[][]
297 1b85f172 Leszek Koltunski
          {
298
              { 0.01f, 0.10f }
299
          };
300
301 28f4aba0 Leszek Koltunski
        cornerIndexes = new int[] { 0,0,0,0,0,0,0,0 };
302
        }
303 4d883a92 Leszek Koltunski
304
      ///// TETRAHEDRON //////////////////////////////////////////////////////////////////////////
305
306
      else if( mode==1 )
307 28f4aba0 Leszek Koltunski
        {
308 4d883a92 Leszek Koltunski
        vertices = new double[][]
309 28f4aba0 Leszek Koltunski
          {
310 4d883a92 Leszek Koltunski
              {-0.5, SQ2/4, 0.0},
311
              { 0.5, SQ2/4, 0.0},
312
              { 0.0,-SQ2/4, 0.5},
313
              { 0.0,-SQ2/4,-0.5}
314 28f4aba0 Leszek Koltunski
          };
315
316
        vertIndexes = new int[][]
317
          {
318
              {2,1,0},   // counterclockwise!
319
              {2,3,1},
320
              {3,2,0},
321
              {3,0,1}
322
          };
323
324
        bands = new float[][]
325
          {
326
              {0.05f,30,0.6f,0.5f,5,  2,2}
327
          };
328
329
        bandIndexes = new int[] { 0,0,0,0 };
330
331
        corners = new float[][]
332
          {
333
              { 0.02f, 0.10f }
334
          };
335
336
        cornerIndexes = new int[] { 0,0,0,0 };
337
        }
338 4d883a92 Leszek Koltunski
339
      ///// DINO ////////////////////////////////////////////////////////////////////////////////
340
341
      else if( mode==2 )
342 28f4aba0 Leszek Koltunski
        {
343 4d883a92 Leszek Koltunski
        vertices = new double[][]
344 28f4aba0 Leszek Koltunski
          {
345 4d883a92 Leszek Koltunski
              {-0.5, 0.0, 0.0},
346
              { 0.5, 0.0, 0.0},
347
              { 0.0,-0.5, 0.0},
348
              { 0.0, 0.0,-0.5}
349 28f4aba0 Leszek Koltunski
          };
350
351
        vertIndexes = new int[][]
352
          {
353
              {2,1,0},   // counterclockwise!
354
              {2,3,1},
355
              {3,2,0},
356
              {3,0,1}
357
          };
358
359
        bands = new float[][]
360
          {
361 6a96e571 Leszek Koltunski
              {0.028f,30,0.25f,0.1f,7,  2,5},
362
              {0.001f,30,0.25f,0.1f,7,  1,2}
363 28f4aba0 Leszek Koltunski
          };
364
365
        bandIndexes = new int[] { 0,1,1,0 };
366
367
        corners = new float[][]
368
          {
369
              { 0.01f, 0.04f }
370
          };
371
372
        cornerIndexes = new int[] { 0,0,0,0 };
373
        }
374 1b85f172 Leszek Koltunski
375 6a96e571 Leszek Koltunski
      ///// OCTAHEDRON ////////////////////////////////////////////////////////////////////////////
376
377
      else if( mode==3 )
378
        {
379
        vertices = new double[][]
380
          {
381
              { 0.5,   0.0, 0.5},
382
              { 0.5,   0.0,-0.5},
383
              {-0.5,   0.0,-0.5},
384
              {-0.5,   0.0, 0.5},
385
              { 0.0, SQ2/2, 0.0},
386
              { 0.0,-SQ2/2, 0.0},
387
          };
388
389
        vertIndexes = new int[][]
390
          {
391
              {3,0,4},   // counterclockwise!
392
              {0,1,4},
393
              {1,2,4},
394
              {2,3,4},
395
              {5,0,3},
396
              {5,1,0},
397
              {5,2,1},
398
              {5,3,2}
399
          };
400
401
        bands = new float[][]
402
          {
403
             {0.05f,17,0.5f,0.2f,5,  2,2}
404
          };
405
406
        bandIndexes = new int[] { 0,0,0,0,0,0,0,0 };
407
408
        corners = new float[][]
409
          {
410
              { 0.03f, 0.12f }
411
          };
412
413
        cornerIndexes = new int[] { 0,0,0,0,0,0 };
414
        }
415
416 33647db9 Leszek Koltunski
      FactoryCubit factory = FactoryCubit.getInstance();
417 1b85f172 Leszek Koltunski
      mMesh = factory.createRoundedSolid(vertices, vertIndexes, bands, bandIndexes, corners, cornerIndexes);
418 6991ece5 Leszek Koltunski
419
      int numEff = mMesh.numEffComponents();
420
421
      for(int i=0; i<numEff; i++)
422
        {
423
        mMesh.setEffectAssociation(i, 0, i);
424
        }
425
      }
426 42aa970f Leszek Koltunski
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428
429 6991ece5 Leszek Koltunski
    private void openMesh(int resourceID)
430 42aa970f Leszek Koltunski
      {
431 1af8e143 Leszek Koltunski
      Context con = mView.getContext();
432
      Resources res = con.getResources();
433
      InputStream is = res.openRawResource(resourceID);
434
      DataInputStream dos = new DataInputStream(is);
435 7198e5c9 Leszek Koltunski
      mMesh = new MeshFile(dos);
436 1af8e143 Leszek Koltunski
437 39b925d5 Leszek Koltunski
      int numEff = mMesh.numEffComponents();
438
439
      for(int i=0; i<numEff; i++)
440
        {
441
        mMesh.setEffectAssociation(i, 0, i);
442
        }
443
444 1af8e143 Leszek Koltunski
      try
445
        {
446
        is.close();
447
        }
448
      catch(IOException e)
449
        {
450 48c88f57 Leszek Koltunski
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
451 1af8e143 Leszek Koltunski
        }
452 42aa970f Leszek Koltunski
      }
453 48c88f57 Leszek Koltunski
454 39b925d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
455
456 6991ece5 Leszek Koltunski
    MeshBase getMesh()
457 39b925d5 Leszek Koltunski
      {
458
      return mMesh;
459
      }
460
461 48c88f57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
462
463
    long getTime()
464
      {
465
      return mTime;
466
      }
467
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469
470
    int getBytes()
471
      {
472 6991ece5 Leszek Koltunski
      if( mMesh instanceof MeshFile )
473
        {
474
        return ((MeshFile)mMesh).getNumBytes();
475
        }
476
477
      return 0;
478 48c88f57 Leszek Koltunski
      }
479
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481
482
    int getVertices()
483
      {
484 7198e5c9 Leszek Koltunski
      return mMesh.getNumVertices();
485
      }
486
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488
489
    int getEndEffIndex(int component)
490
      {
491
      return mMesh.getLastVertexEff(component);
492
      }
493
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495
496
    int getEndTexIndex(int component)
497
      {
498
      return mMesh.getLastVertexTex(component);
499
      }
500
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502
503
    int getEffComponentNum()
504
      {
505
      return mMesh.numEffComponents();
506
      }
507
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509
510
    int getTexComponentNum()
511
      {
512
      return mMesh.numTexComponents();
513 48c88f57 Leszek Koltunski
      }
514 42aa970f Leszek Koltunski
}