Project

General

Profile

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

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

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.Effect;
31
import org.distorted.library.effect.EffectType;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectDisappear;
35
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
import org.distorted.library.mesh.MeshBase;
40
import org.distorted.library.mesh.MeshFile;
41
import org.distorted.library.type.DynamicQuat;
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44

    
45
import java.io.DataInputStream;
46
import java.io.IOException;
47
import java.io.InputStream;
48

    
49
import javax.microedition.khronos.egl.EGLConfig;
50
import javax.microedition.khronos.opengles.GL10;
51

    
52
import static org.distorted.examples.meshfile.MeshFileActivity.PROCEDURAL;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
57
{
58
    private static final float SQ2 = (float)Math.sqrt(2);
59
    private static final float SQ3 = (float)Math.sqrt(3);
60
    private final float DEFAULT_SCALE = 0.3f;
61

    
62
    private final GLSurfaceView mView;
63
    private DistortedTexture mTexture;
64
    private final DistortedScreen mScreen;
65
    private final DistortedEffects mEffects;
66
    private final Static3D mScale;
67
    private long mTime;
68
    private float mCurrentScale;
69
    private MeshBase mMesh;
70

    
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
      mCurrentScale = DEFAULT_SCALE;
84

    
85
      mQuat1 = new Static4D(0,0,0,1);
86
      mQuat2 = new Static4D(0,0,0,1);
87

    
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
      VertexEffectDisappear disappear = new VertexEffectDisappear();
95
      disappear.setMeshAssociation(1,-1);
96

    
97
      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
      mEffects.apply( disappear );
103

    
104
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
105
      mScreen.showFPS();
106
      }
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
      float factor = mCurrentScale*mScreenMin;
121
      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
      Effect.enableEffects(EffectType.VERTEX);
132
      DistortedLibrary.setMax(EffectType.VERTEX, 20);
133
      DistortedLibrary.needTransformFeedback();
134
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
135
      }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
    public void distortedException(Exception ex)
140
      {
141
      android.util.Log.e("MeshFile", ex.getMessage() );
142
      }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
    void open(int resourceID)
167
      {
168
      if( mTexture==null ) mTexture = new DistortedTexture();
169
      mTexture.setTexture( createTexture(resourceID) );
170

    
171
      long t1 = System.currentTimeMillis();
172

    
173
      if( resourceID!=PROCEDURAL )
174
        {
175
        openMesh(resourceID);
176
        }
177
      else
178
        {
179
        createMesh();
180
        }
181

    
182
      long t2 = System.currentTimeMillis();
183

    
184
      mTime = t2-t1;
185

    
186
      mScreen.detachAll();
187
      mScreen.attach(mTexture,mEffects,mMesh);
188
      }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
    private Bitmap createTexture(int resourceID)
193
      {
194
      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
      switch(resourceID)
203
          {
204
          case  R.raw.deferredjob:
205
          case  R.raw.meshjoin   :
206
          case  PROCEDURAL       :
207
          case  R.raw.predeform  : return createWhiteTexture();
208

    
209
          case  R.raw.cube2      :
210
          case  R.raw.cube3      :
211
          case  R.raw.cube4      :
212
          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
          case  R.raw.pyra3      :
216
          case  R.raw.pyra4      :
217
          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

    
221
          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

    
225
          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

    
228
                                   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

    
232
      return null;
233
      }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
    private Bitmap createWhiteTexture()
238
      {
239
      int SIZE = 1;
240
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
241
      Canvas canvas = new Canvas(bitmap);
242

    
243
      Paint paint = new Paint();
244
      paint.setColor(0xffffff55);
245
      paint.setStyle(Paint.Style.FILL);
246
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
247

    
248
      return bitmap;
249
      }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
    private void createMesh()
254
      {
255
      int mode = 0;
256
      double[][] vertices = null;
257
      int[][] vertIndexes = null;
258
      float[][] bands     = null;
259
      int[] bandIndexes   = null;
260
      float[][] corners   = null;
261
      int[] cornerIndexes = null;
262

    
263
      ///// CUBE ////////////////////////////////////////////////////////////////////////////////
264

    
265
      if( mode==0 )
266
        {
267
        vertices = new double[][]
268
          {
269
              { 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
          };
278

    
279
        vertIndexes = new int[][]
280
          {
281
              {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
          };
288

    
289
        bands = new float[][]
290
          {
291
              {0.05f,40,0.5f,0.2f,5,  2,2}
292
          };
293

    
294
        bandIndexes = new int[] { 0,0,0,0,0,0 };
295

    
296
        corners = new float[][]
297
          {
298
              { 0.01f, 0.10f }
299
          };
300

    
301
        cornerIndexes = new int[] { 0,0,0,0,0,0,0,0 };
302
        }
303

    
304
      ///// TETRAHEDRON //////////////////////////////////////////////////////////////////////////
305

    
306
      else if( mode==1 )
307
        {
308
        vertices = new double[][]
309
          {
310
              {-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
          };
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

    
339
      ///// DINO ////////////////////////////////////////////////////////////////////////////////
340

    
341
      else if( mode==2 )
342
        {
343
        vertices = new double[][]
344
          {
345
              {-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
          };
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
              {0.028f,30,0.25f,0.1f,7,  2,5},
362
              {0.001f,30,0.25f,0.1f,7,  1,2}
363
          };
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

    
375
      ///// 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
      FactoryCubit factory = FactoryCubit.getInstance();
417
      mMesh = factory.createRoundedSolid(vertices, vertIndexes, bands, bandIndexes, corners, cornerIndexes);
418

    
419
      int numEff = mMesh.numEffComponents();
420

    
421
      for(int i=0; i<numEff; i++)
422
        {
423
        mMesh.setEffectAssociation(i, 0, i);
424
        }
425
      }
426

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

    
429
    private void openMesh(int resourceID)
430
      {
431
      Context con = mView.getContext();
432
      Resources res = con.getResources();
433
      InputStream is = res.openRawResource(resourceID);
434
      DataInputStream dos = new DataInputStream(is);
435
      mMesh = new MeshFile(dos);
436

    
437
      int numEff = mMesh.numEffComponents();
438

    
439
      for(int i=0; i<numEff; i++)
440
        {
441
        mMesh.setEffectAssociation(i, 0, i);
442
        }
443

    
444
      try
445
        {
446
        is.close();
447
        }
448
      catch(IOException e)
449
        {
450
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
451
        }
452
      }
453

    
454
///////////////////////////////////////////////////////////////////////////////////////////////////
455

    
456
    MeshBase getMesh()
457
      {
458
      return mMesh;
459
      }
460

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

    
463
    long getTime()
464
      {
465
      return mTime;
466
      }
467

    
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469

    
470
    int getBytes()
471
      {
472
      if( mMesh instanceof MeshFile )
473
        {
474
        return ((MeshFile)mMesh).getNumBytes();
475
        }
476

    
477
      return 0;
478
      }
479

    
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481

    
482
    int getVertices()
483
      {
484
      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
      }
514
}
(3-3/5)