Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 13884c8a

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

    
47
import org.distorted.objectlib.helpers.FactoryCubit;
48
import org.distorted.objectlib.helpers.ObjectFaceShape;
49
import org.distorted.objectlib.helpers.ObjectShape;
50
import org.distorted.objectlib.helpers.ObjectVertexEffects;
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.objectlib.main.TwistyObject.MESH_NICE;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

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

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

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

    
83
    Static4D mQuat1, mQuat2;
84
    int mScreenMin;
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

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

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

    
97
      mCurrentScale = DEFAULT_SCALE;
98
      mNormals = false;
99

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

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

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

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

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

    
117
      mEffects.apply( disappear );
118

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

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

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

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

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

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

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

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

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

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

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

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

    
187
      long t1 = System.currentTimeMillis();
188

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

    
206
      long t2 = System.currentTimeMillis();
207

    
208
      mTime = t2-t1;
209

    
210
      mScreen.detachAll();
211
      mScreen.attach(mTexture,mEffects,mMesh);
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
    private Bitmap createTexture(int resourceID)
217
      {
218
      TextureFactory factory = TextureFactory.getInstance();
219

    
220
      float[] vertices;
221
      int[] colors;
222
      float F = 0.5f;
223
      float E = SQ3/2;
224

    
225
      if( resourceID == R.raw.deferredjob ||
226
          resourceID == R.raw.meshjoin    ||
227
          resourceID == PROCEDURAL        ||
228
          resourceID == POLYGON           ||
229
          resourceID == MULTIGON          ||
230
          resourceID == R.raw.predeform    ) return createWhiteTexture();
231

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

    
262
      return null;
263
      }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
    private Bitmap createWhiteTexture()
268
      {
269
      int SIZE = 1;
270
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
271
      Canvas canvas = new Canvas(bitmap);
272

    
273
      Paint paint = new Paint();
274
      paint.setColor(0xffffff55);
275
      paint.setStyle(Paint.Style.FILL);
276
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
277

    
278
      return bitmap;
279
      }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
    private void createPolygon()
284
      {
285
      float A = 0.5f;
286
      float B = 0.04f;
287

    
288
      int extraIndex    = 0;
289
      int extraVertices = 0;
290
      float[] vertices = new float[] { -A,-A, A,-A, A,A, -A,A };
291

    
292
      float C = 2f;
293
      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};
294

    
295
/*
296
      int numBands      = 5;
297
      float[] bands = new float[2*numBands];
298

    
299
      for(int i=0; i<numBands; i++)
300
        {
301
        bands[2*i  ] = 1 + i/(1.0f-numBands);
302
        bands[2*i+1] = B/(numBands-1)*i;
303
        }
304
*/
305
      boolean[] edgesUp = new boolean[] {true,true,false,false};
306
      boolean[] vertsUp = new boolean[] {false,false,false,false};
307

    
308
      mMesh = new MeshPolygon(vertices,bands,edgesUp,vertsUp,extraIndex,extraVertices,0.0f,0.0f);
309
      mMesh.setEffectAssociation(0,0,0);
310
      }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
    private void createMultigon()
315
      {
316
      float A = 0.5f;
317
      int extraIndex    = 0;
318
      int extraVertices = 0;
319

    
320
      float[] v1 = new float[] {  -A,-A,   A,-A,   A,  A,   -A,  A };
321
      float[] v2 = new float[] {   A,-A, 2*A,-A, 2*A,  A,    A,  A };
322
      float[] v3 = new float[] {-3*A,-A,  -A,-A,  -A,  A, -3*A,  A };
323
      float[] v4 = new float[] {  -A, A,   A, A,   A,3*A,   -A,3*A };
324
      float[] v5 = new float[] {-3*A, A,  -A, A,  -A,3*A, -3*A,3*A };
325

    
326
      float[][] vertices = new float[][] {v1,v2,v3,v4,v5};
327
/*
328
      float[] c1 = new float[] { 0,0 };
329
      float[] c2 = new float[] { 1.5f*A,0 };
330
      float[] c3 = new float[] {-1.5f*A,0 };
331
      float[] c4 = new float[] { 0,1.5f*A };
332
      float[] c5 = new float[] { -1.5f*A,1.5f*A };
333

    
334
      float[][] centers = new float[][] { c1,c2,c3,c4,c5 };
335
*/
336
      float C = 1f;
337
      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};
338

    
339
/*
340
      float B = 0.1f;
341
      int numBands = 7;
342
      float[] bands = new float[2*numBands];
343

    
344
      for(int i=0; i<numBands; i++)
345
        {
346
        bands[2*i  ] = 1 + i/(1.0f-numBands);
347
        bands[2*i+1] = B/(numBands-1)*i;
348
        }
349
*/
350
      mMesh = new MeshMultigon(vertices,bands,extraIndex,extraVertices);
351

    
352
      int numEff = mMesh.getNumEffComponents();
353

    
354
      for(int i=0; i<numEff; i++)
355
        {
356
        mMesh.setEffectAssociation(i, 0, i);
357
        }
358
      }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

    
362
    private void createMesh()
363
      {
364
      /*
365
      float A = 2.0f;
366

    
367
      float[][] vertices =
368
          {
369
              { 0.5f, 0.5f, 0.5f },
370
              { 0.5f, 0.5f,-0.5f },
371
              { 0.5f,-0.5f, 0.5f },
372
              { 0.5f,-0.5f,-0.5f },
373
              {-0.5f, 0.5f, 0.5f },
374
              {-0.5f, 0.5f,-0.5f },
375
              {-0.5f,-0.5f, 0.5f },
376
              {-0.5f,-0.5f,-0.5f },
377

    
378
              { 0.5f,   -A, 0.5f },
379
              { 0.5f,   -A,-0.5f },
380
              {-0.5f,   -A, 0.5f },
381
              {-0.5f,   -A,-0.5f },
382

    
383
              {   -A, 0.5f, 0.5f },
384
              {   -A, 0.5f,-0.5f },
385
              {   -A,-0.5f, 0.5f },
386
              {   -A,-0.5f,-0.5f },
387
          };
388

    
389
      int[][][] vertIndices =
390
          {
391
              { { 8, 9, 1, 0} },
392
              { {15,14,12,13} },
393
              { {11,10, 6, 7} },
394
              { {12, 0, 1,13} },
395
              { {11, 9, 8,10} },
396
              { {15, 7, 6,14} },
397
              { {14, 6, 4,12}, { 6, 2, 0, 4}, {10, 8, 2, 6} },
398
              { { 3, 7, 5, 1}, { 9,11, 7, 3}, { 7,15,13, 5} }
399
          };
400

    
401
      float height = 0.02f;
402
      int num = 5;
403
      int extraI = 1;
404
      int extraV = 1;
405

    
406
      float[][] bands= { {height,25,0.3f,0.5f,num,extraI,extraV} };
407

    
408
      int[] bandIndices = {0,0,0,0,0,0,0,0};
409
      float[] convex = null;
410

    
411
      float[][] corners= { {0.036f,0.12f} };
412
      float[][] centers= { {0.0f, 0.0f, 0.0f} };
413
      int[] ind    = { 0,0,-1,-1,-1,-1,-1,-1, 0,0,0,0,0,0,0,0 };
414
      */
415
      float[][] vertices = { {-1.5f, 0.0f, 0.0f},{ 1.5f, 0.0f, 0.0f},{ 0.0f,-1.5f, 0.0f},{ 0.0f, 0.0f,-1.5f},{0,0,0} };
416
      int[][][] vertIndices = { {{2,1,4},{4,0,2}},{{3,0,4},{4,1,3}},{{2,3,1}},{{3,2,0}} };
417
      float h1 = 0.035f;
418
      float h2 = 0.010f;
419
      int angle = 30;
420
      float R = 0.1f;
421
      float S = 0.5f;
422
      int N   = 6;
423
      int exI = 0;
424
      int exV = 0;
425
      float[][] bands  = { {h1,angle,R,S,N,exI,exV}, {h2,angle,R,S,N,exI,exV} };
426
      int[] bandIndices= { 0,0,1,1 };
427
      float[] convex = null;
428
      float[][] corners   = { {0.07f,0.40f}, {0.05f,0.30f} };
429
      int[] cornerIndices = { 0,0,1,1,-1 };
430
      float[][] centers   = { {0.0f, -0.75f, -0.75f} };
431
      int[] centerIndices = { 0,0,0,0,-1 };
432

    
433
      ObjectShape shape = new ObjectShape(vertices, vertIndices);
434
      ObjectFaceShape face = new ObjectFaceShape(bands, bandIndices, convex);
435
      ObjectVertexEffects effects = FactoryCubit.generateVertexEffect(vertices,corners,cornerIndices,centers,centerIndices);
436

    
437
      int numFaces = shape.getNumFaces();
438
      int[] outer = new int[numFaces];
439

    
440
      FactoryCubit factory = FactoryCubit.getInstance();
441
      factory.clear();
442
      factory.createNewFaceTransform(shape,outer);
443
      mMesh = factory.createRoundedSolid(shape,face,effects,MESH_NICE,numFaces);
444

    
445
      int numEff = mMesh.getNumEffComponents();
446
      for(int i=0; i<numEff; i++) mMesh.setEffectAssociation(i, 0, i);
447
      }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
    private void openMesh(int resourceID)
452
      {
453
      Context con = mView.getContext();
454
      Resources res = con.getResources();
455
      InputStream is = res.openRawResource(resourceID);
456
      DataInputStream dos = new DataInputStream(is);
457
      mMesh = new MeshFile(dos);
458

    
459
      int numEff = mMesh.getNumEffComponents();
460

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

    
466
      try
467
        {
468
        is.close();
469
        }
470
      catch(IOException e)
471
        {
472
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
473
        }
474
      }
475

    
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477

    
478
    MeshBase getMesh()
479
      {
480
      return mMesh;
481
      }
482

    
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484

    
485
    long getTime()
486
      {
487
      return mTime;
488
      }
489

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491

    
492
    int getBytes()
493
      {
494
      if( mMesh instanceof MeshFile )
495
        {
496
        return ((MeshFile)mMesh).getNumBytes();
497
        }
498

    
499
      return 0;
500
      }
501

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

    
504
    int getVertices()
505
      {
506
      return mMesh.getNumVertices();
507
      }
508

    
509
///////////////////////////////////////////////////////////////////////////////////////////////////
510

    
511
    int getEndEffIndex(int component)
512
      {
513
      return mMesh.getLastVertexEff(component);
514
      }
515

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517

    
518
    int getEndTexIndex(int component)
519
      {
520
      return mMesh.getLastVertexTex(component);
521
      }
522

    
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524

    
525
    int getEffComponentNum()
526
      {
527
      return mMesh.getNumEffComponents();
528
      }
529

    
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531

    
532
    int getTexComponentNum()
533
      {
534
      return mMesh.getNumTexComponents();
535
      }
536

    
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538

    
539
    public void distortedException(Exception ex)
540
      {
541
      android.util.Log.e("MeshFile", ex.getMessage() );
542
      }
543

    
544
///////////////////////////////////////////////////////////////////////////////////////////////////
545

    
546
    public InputStream localFile(int fileID)
547
      {
548
      return mResources.openRawResource(fileID);
549
      }
550

    
551
///////////////////////////////////////////////////////////////////////////////////////////////////
552

    
553
    public void logMessage(String message)
554
      {
555
      android.util.Log.e("MeshFile", message );
556
      }
557
}
(2-2/4)