Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 0e93d1a3

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.MeshBandedTriangle;
40
import org.distorted.library.mesh.MeshBase;
41
import org.distorted.library.mesh.MeshFile;
42
import org.distorted.library.mesh.MeshMultigon;
43
import org.distorted.library.mesh.MeshPolygon;
44
import org.distorted.library.type.DynamicQuat;
45
import org.distorted.library.type.Static3D;
46
import org.distorted.library.type.Static4D;
47

    
48
import org.distorted.objectlib.helpers.FactoryCubit;
49
import org.distorted.objectlib.helpers.ObjectFaceShape;
50
import org.distorted.objectlib.helpers.ObjectShape;
51

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

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

    
59
import static org.distorted.examples.meshfile.MeshFileActivity.MULTIGON;
60
import static org.distorted.examples.meshfile.MeshFileActivity.POLYGON;
61
import static org.distorted.examples.meshfile.MeshFileActivity.PROCEDURAL;
62
import static org.distorted.examples.meshfile.MeshFileActivity.TRIANGLE;
63
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
68
{
69
    private static final float SQ3 = (float)Math.sqrt(3);
70
    private final float DEFAULT_SCALE = 0.3f;
71

    
72
    private final GLSurfaceView mView;
73
    private final Resources mResources;
74
    private final DistortedScreen mScreen;
75
    private final DistortedEffects mEffects;
76
    private final Static3D mScale;
77

    
78
    private DistortedTexture mTexture;
79
    private MeshBase mMesh;
80
    private long mTime;
81
    private float mCurrentScale;
82
    private boolean mNormals;
83

    
84
    Static4D mQuat1, mQuat2;
85
    int mScreenMin;
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
    MeshFileRenderer(GLSurfaceView v)
90
      {
91
      mView = v;
92
      mResources = v.getResources();
93

    
94
      mScreen = new DistortedScreen();
95
      mScale= new Static3D(1,1,1);
96
      Static3D center=new Static3D(0,0,0);
97

    
98
      mCurrentScale = DEFAULT_SCALE;
99
      mNormals = false;
100

    
101
      mQuat1 = new Static4D(0,0,0,1);
102
      mQuat2 = new Static4D(0,0,0,1);
103

    
104
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
105
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
106

    
107
      quatInt1.add(mQuat1);
108
      quatInt2.add(mQuat2);
109

    
110
      VertexEffectDisappear disappear = new VertexEffectDisappear();
111
      disappear.setMeshAssociation(1,-1);
112

    
113
      mEffects = new DistortedEffects();
114
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
115
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
116
      mEffects.apply( new MatrixEffectScale(mScale));
117

    
118
      mEffects.apply( disappear );
119

    
120
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
121
      mScreen.showFPS();
122
      }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
   
126
    public void onDrawFrame(GL10 glUnused) 
127
      {
128
      mScreen.render( System.currentTimeMillis() );
129
      }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
    
133
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
134
      {
135
      mScreenMin = Math.min(width, height);
136
      float factor = mCurrentScale*mScreenMin;
137
      mScale.set(factor,factor,factor);
138
      mScreen.resize(width, height);
139
      }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
    
143
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
144
      {
145
      if( mTexture==null ) mTexture = new DistortedTexture();
146

    
147
      Effect.enableEffects(EffectType.VERTEX);
148
      DistortedLibrary.setMax(EffectType.VERTEX, 20);
149
      DistortedLibrary.needTransformFeedback();
150
      DistortedLibrary.onSurfaceCreated(this);
151
      }
152

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

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

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

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
    void flipNormals()
176
      {
177
      mNormals = !mNormals;
178
      if( mMesh!=null ) mMesh.setShowNormals(mNormals);
179
      }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
    void open(int resourceID)
184
      {
185
      if( mTexture==null ) mTexture = new DistortedTexture();
186
      mTexture.setTexture( createTexture(resourceID) );
187

    
188
      long t1 = System.currentTimeMillis();
189

    
190
      if( resourceID==PROCEDURAL )
191
        {
192
        createMesh();
193
        }
194
      else if( resourceID==POLYGON )
195
        {
196
        createPolygon();
197
        }
198
      else if( resourceID==MULTIGON )
199
        {
200
        createMultigon();
201
        }
202
      else if( resourceID==TRIANGLE )
203
        {
204
        createTriangle();
205
        }
206
      else
207
        {
208
        openMesh(resourceID);
209
        }
210

    
211
      long t2 = System.currentTimeMillis();
212

    
213
      mTime = t2-t1;
214

    
215
      mScreen.detachAll();
216
      mScreen.attach(mTexture,mEffects,mMesh);
217
      }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
    private Bitmap createTexture(int resourceID)
222
      {
223
      TextureFactory factory = TextureFactory.getInstance();
224

    
225
      float[] vertices;
226
      int[] colors;
227
      float F = 0.5f;
228
      float E = SQ3/2;
229

    
230
      if( resourceID == R.raw.deferredjob || resourceID == R.raw.meshjoin    ||
231
          resourceID == PROCEDURAL        || resourceID == POLYGON           ||
232
          resourceID == MULTIGON          || resourceID == TRIANGLE          ||
233
          resourceID == R.raw.predeform    ) return createWhiteTexture();
234

    
235
      if( resourceID == R.raw.cube2       ||
236
          resourceID == R.raw.cube3       ||
237
          resourceID == R.raw.cube4       ||
238
          resourceID == R.raw.cube5        )
239
        {
240
        colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
241
        vertices = new float[] { -F,-F, +F,-F, +F,+F, -F,+F};
242
        return factory.createTexture(vertices,colors,0.10f, 0.10f, true);
243
        }
244
      if( resourceID == R.raw.pyra3       ||
245
          resourceID == R.raw.pyra4       ||
246
          resourceID == R.raw.pyra5        )
247
        {
248
        colors = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
249
        vertices = new float[] { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
250
        return factory.createTexture(vertices,colors,0.05f, 0.05f, true);
251
        }
252
      if( resourceID == R.raw.dino )
253
        {
254
        colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
255
        vertices = new float[] { -F,F/3, 0,-2*F/3, +F,F/3 };
256
        return factory.createTexture(vertices,colors,0.05f, 0.03f, true);
257
        }
258
      if( resourceID == R.raw.skewb )
259
        {
260
        colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
261
        vertices = new float[] { -F+F/4,F/4, F/4,-F+F/4, F/4,F/4};
262
        return factory.createTexture(vertices,colors,0.05f, 0.08f, true);
263
        }
264

    
265
      return null;
266
      }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
    private Bitmap createWhiteTexture()
271
      {
272
      int SIZE = 1;
273
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
274
      Canvas canvas = new Canvas(bitmap);
275

    
276
      Paint paint = new Paint();
277
      paint.setColor(0xffffff55);
278
      paint.setStyle(Paint.Style.FILL);
279
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
280

    
281
      return bitmap;
282
      }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
    private void createTriangle()
287
      {
288
      float[] vL = {-1.0f, -0.5f};
289
      float[] vR = { 1.0f, -0.5f};
290
      float[] vT = { 0.0f,  1.0f};
291
      float C = 2f;
292
      float[] bands = { 1.0f, 0.00f*C, /*0.9f, 0.05f*C,*/ 0.8f, 0.06f*C, 0.5f, 0.09f*C, 0.0f, 0.10f*C};
293
      float[] normL = { vL[0]-vT[0], vL[1]-vT[1] };
294
      float[] normR = { vR[0]-vT[0], vR[1]-vT[1] };
295
      int mode = MeshBandedTriangle.MODE_NORMAL;
296
      int extraBands    = 2;
297
      int extraVertices = 1;
298

    
299
      mMesh = new MeshBandedTriangle(vL,vR,vT,bands,normL,normR,mode,extraBands,extraVertices);
300
      mMesh.setEffectAssociation(0,0,0);
301
      }
302

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

    
305
    private void createPolygon()
306
      {
307
      float A = 0.5f;
308
      float B = 0.04f;
309

    
310
      int extraIndex    = 0;
311
      int extraVertices = 0;
312
      float[][] vertices = { {-A,-A}, {A,-A}, {A,A}, {-A,A} };
313

    
314
      float C = 2f;
315
      float[] bands = { 1.0f, 0.00f*C, 0.9f, 0.04f*C,  0.8f, 0.07f*C, 0.5f, 0.09f*C, 0.0f, 0.10f*C};
316
/*
317
      int numBands      = 5;
318
      float[] bands = new float[2*numBands];
319

    
320
      for(int i=0; i<numBands; i++)
321
        {
322
        bands[2*i  ] = 1 + i/(1.0f-numBands);
323
        bands[2*i+1] = B/(numBands-1)*i;
324
        }
325
*/
326
      mMesh = new MeshPolygon(vertices,bands,extraIndex,extraVertices,0.0f,0.0f);
327
      mMesh.setEffectAssociation(0,0,0);
328
      }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
    private float[][] gen(float x, float y)
333
      {
334
      return new float[][] { {x+0.1f,y-0.1f} , {x-0.1f,y-0.1f} , {x-0.1f,y+0.1f} , {x+0.1f,y+0.1f} };
335
      }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

    
339
    private void createMultigon()
340
      {
341
      float A = 0.5f;
342
      int extraIndex    = 0;
343
      int extraVertices = 0;
344

    
345
      float[][][] vertices = new float[19][][];
346

    
347
      vertices[ 0] = gen( 0.4f,-0.4f);
348
      vertices[ 1] = gen( 0.2f,-0.4f);
349
      vertices[ 2] = gen( 0.0f,-0.4f);
350
      vertices[ 3] = gen(-0.2f,-0.4f);
351
      vertices[ 4] = gen(-0.4f,-0.4f);
352
      vertices[ 5] = gen(-0.4f,-0.2f);
353
      vertices[ 6] = gen(-0.4f, 0.0f);
354
      vertices[ 7] = gen(-0.4f, 0.2f);
355
      vertices[ 8] = gen(-0.4f, 0.4f);
356
      vertices[ 9] = gen(-0.2f, 0.4f);
357
      vertices[10] = gen( 0.0f, 0.4f);
358
      vertices[11] = gen( 0.2f, 0.4f);
359
      vertices[12] = gen( 0.4f, 0.4f);
360
      vertices[13] = gen( 0.4f, 0.2f);
361
      vertices[14] = gen( 0.4f, 0.0f);
362
      vertices[15] = gen( 0.4f,-0.2f);
363
      vertices[16] = gen( 0.0f,-0.2f);
364
      vertices[17] = gen( 0.0f, 0.0f);
365
      vertices[18] = gen( 0.0f, 0.2f);
366

    
367
      float C = 0.5f;
368
      float[] bands = new float[] { 1.0f, 0.00f*C, 0.9f, 0.04f*C,  0.8f, 0.07f*C, 0.5f, 0.09f*C, 0.0f, 0.10f*C};
369
/*
370
      float B = 0.1f;
371
      int numBands = 7;
372
      float[] bands = new float[2*numBands];
373

    
374
      for(int i=0; i<numBands; i++)
375
        {
376
        bands[2*i  ] = 1 + i/(1.0f-numBands);
377
        bands[2*i+1] = B/(numBands-1)*i;
378
        }
379
*/
380
      mMesh = new MeshMultigon(vertices,bands,extraIndex,extraVertices);
381

    
382
      int numEff = mMesh.getNumEffComponents();
383

    
384
      for(int i=0; i<numEff; i++)
385
        {
386
        mMesh.setEffectAssociation(i, 0, i);
387
        }
388
      }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391

    
392
    private float[][] createV()
393
      {
394
      return new float[][]
395
              {
396
              {-1.6281155f, -0.2072947f, -3.1770508f },
397
              { 1.2135257f, -1.9635254f, -3.1770515f },
398
              {-0.5427052f, -1.9635254f, -0.33541024f},
399
              {-1.9635254f, -0.7499998f, -1.2135254f },
400
              {-0.75000024f, 1.2135258f, -1.9635254f },
401
              { 1.2135253f,  0.87811553f,-1.4208202f },
402
              {-1.2135255f,  0.4635256f, -0.7499998f },
403
              { 1.2135255f, -1.9635254f, -3.1770515f },
404
              { 2.9697561f, -1.9635254f, -0.33541024f},
405
              { 1.2135255f, -1.9635254f, 0.75f },
406
              { 8.940697E-8f, 1.7881393E-7f, 0.0f },
407
              { 1.2135255f, -0.4635254f, 0.75f },
408
              { 0.0f, 0.0f, 0.0f },
409
              { 1.2135255f, 0.87811553f, -1.4208202f },
410
              { 2.427051f, 0.0f, 0.0f }
411

    
412
              };
413
      }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
    private int[][][] createVI()
418
      {
419
      return new int[][][]
420
              {
421
                {{ 0, 1, 2, 3 }},
422
                {{ 4, 5, 1, 0 }},
423
                {{ 0, 3, 6, 4 }},
424
                {{ 2, 7, 8, 9 }},
425
                {{ 3, 2, 10,6 }, { 2, 9, 11, 12 }},
426
                {{13, 14, 8, 7}},
427
                {{ 6, 10, 5, 4}, { 11, 14, 13, 12}},
428
                {{ 9, 8, 14, 11}}
429
              };
430
      }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

    
434
    private void createMesh()
435
      {
436
      float[][] vertices = createV();
437
      int[][][] vertIndices = createVI();
438

    
439
      int numFaces = vertIndices.length;
440

    
441
      float h = 0.001f;
442
      int angle = 30;
443
      float R = 0.1f;
444
      float S = 0.5f;
445
      int N   = 6;
446
      int exI = 0;
447
      int exV = 0;
448
      float[][] bands  = { {h,angle,R,S,N,exI,exV} };
449

    
450
      int[] bandIndices= new int[numFaces];
451

    
452
      ObjectShape shape = new ObjectShape(vertices, vertIndices);
453
      ObjectFaceShape face = new ObjectFaceShape(bands, bandIndices, null);
454

    
455
      int[] outer = new int[numFaces];
456

    
457
      FactoryCubit factory = FactoryCubit.getInstance();
458
      factory.clear();
459
      factory.createNewFaceTransform(shape,outer);
460
      mMesh = factory.createRoundedSolid(shape,face,null,MESH_NICE,numFaces);
461

    
462
      int numEff = mMesh.getNumEffComponents();
463
      for(int i=0; i<numEff; i++) mMesh.setEffectAssociation(i, 0, i);
464
      }
465

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467

    
468
    private void openMesh(int resourceID)
469
      {
470
      Context con = mView.getContext();
471
      Resources res = con.getResources();
472
      InputStream is = res.openRawResource(resourceID);
473
      DataInputStream dos = new DataInputStream(is);
474
      mMesh = new MeshFile(dos);
475

    
476
      int numEff = mMesh.getNumEffComponents();
477

    
478
      for(int i=0; i<numEff; i++)
479
        {
480
        mMesh.setEffectAssociation(i, 0, i);
481
        }
482

    
483
      try
484
        {
485
        is.close();
486
        }
487
      catch(IOException e)
488
        {
489
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
490
        }
491
      }
492

    
493
///////////////////////////////////////////////////////////////////////////////////////////////////
494

    
495
    MeshBase getMesh()
496
      {
497
      return mMesh;
498
      }
499

    
500
///////////////////////////////////////////////////////////////////////////////////////////////////
501

    
502
    long getTime()
503
      {
504
      return mTime;
505
      }
506

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508

    
509
    int getBytes()
510
      {
511
      if( mMesh instanceof MeshFile )
512
        {
513
        return ((MeshFile)mMesh).getNumBytes();
514
        }
515

    
516
      return 0;
517
      }
518

    
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520

    
521
    int getVertices()
522
      {
523
      return mMesh.getNumVertices();
524
      }
525

    
526
///////////////////////////////////////////////////////////////////////////////////////////////////
527

    
528
    int getEndEffIndex(int component)
529
      {
530
      return mMesh.getLastVertexEff(component);
531
      }
532

    
533
///////////////////////////////////////////////////////////////////////////////////////////////////
534

    
535
    int getEndTexIndex(int component)
536
      {
537
      return mMesh.getLastVertexTex(component);
538
      }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541

    
542
    int getEffComponentNum()
543
      {
544
      return mMesh.getNumEffComponents();
545
      }
546

    
547
///////////////////////////////////////////////////////////////////////////////////////////////////
548

    
549
    int getTexComponentNum()
550
      {
551
      return mMesh.getNumTexComponents();
552
      }
553

    
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555

    
556
    public void distortedException(Exception ex)
557
      {
558
      android.util.Log.e("MeshFile", ex.getMessage() );
559
      }
560

    
561
///////////////////////////////////////////////////////////////////////////////////////////////////
562

    
563
    public InputStream localFile(int fileID)
564
      {
565
      return mResources.openRawResource(fileID);
566
      }
567

    
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569

    
570
    public void logMessage(String message)
571
      {
572
      android.util.Log.e("MeshFile", message );
573
      }
574
}
(2-2/4)