Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ f804fce3

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

    
82
    Static4D mQuat1, mQuat2;
83
    int mScreenMin;
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

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

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

    
96
      mCurrentScale = DEFAULT_SCALE;
97

    
98
      mQuat1 = new Static4D(0,0,0,1);
99
      mQuat2 = new Static4D(0,0,0,1);
100

    
101
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
102
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
103

    
104
      quatInt1.add(mQuat1);
105
      quatInt2.add(mQuat2);
106

    
107
      VertexEffectDisappear disappear = new VertexEffectDisappear();
108
      disappear.setMeshAssociation(1,-1);
109

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

    
115
      mEffects.apply( disappear );
116

    
117
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
118
      mScreen.showFPS();
119
      }
120

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

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

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

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
//   0 ---> 0
152
//  50 ---> DEFAULT_SCALE
153
// 100 ---> 4*DEFAULT_SCALE
154

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

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

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
    void open(int resourceID)
173
      {
174
      if( mTexture==null ) mTexture = new DistortedTexture();
175
      mTexture.setTexture( createTexture(resourceID) );
176

    
177
      long t1 = System.currentTimeMillis();
178

    
179
      if( resourceID==PROCEDURAL )
180
        {
181
        createMesh();
182
        }
183
      else if( resourceID==POLYGON )
184
        {
185
        createPolygon();
186
        }
187
      else if( resourceID==MULTIGON )
188
        {
189
        createMultigon();
190
        }
191
      else
192
        {
193
        openMesh(resourceID);
194
        }
195

    
196
      long t2 = System.currentTimeMillis();
197

    
198
      mTime = t2-t1;
199

    
200
      mScreen.detachAll();
201
      mScreen.attach(mTexture,mEffects,mMesh);
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
    private Bitmap createTexture(int resourceID)
207
      {
208
      TextureFactory factory = TextureFactory.getInstance();
209

    
210
      float[] vertices;
211
      int[] colors;
212
      float F = 0.5f;
213
      float E = SQ3/2;
214

    
215
      if( resourceID == R.raw.deferredjob ||
216
          resourceID == R.raw.meshjoin    ||
217
          resourceID == PROCEDURAL        ||
218
          resourceID == POLYGON           ||
219
          resourceID == MULTIGON          ||
220
          resourceID == R.raw.predeform    ) return createWhiteTexture();
221

    
222
      if( resourceID == R.raw.cube2       ||
223
          resourceID == R.raw.cube3       ||
224
          resourceID == R.raw.cube4       ||
225
          resourceID == R.raw.cube5        )
226
        {
227
        colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
228
        vertices = new float[] { -F,-F, +F,-F, +F,+F, -F,+F};
229
        return factory.createTexture(vertices,colors,0.10f, 0.10f, true);
230
        }
231
      if( resourceID == R.raw.pyra3       ||
232
          resourceID == R.raw.pyra4       ||
233
          resourceID == R.raw.pyra5        )
234
        {
235
        colors = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
236
        vertices = new float[] { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
237
        return factory.createTexture(vertices,colors,0.05f, 0.05f, true);
238
        }
239
      if( resourceID == R.raw.dino )
240
        {
241
        colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
242
        vertices = new float[] { -F,F/3, 0,-2*F/3, +F,F/3 };
243
        return factory.createTexture(vertices,colors,0.05f, 0.03f, true);
244
        }
245
      if( resourceID == R.raw.skewb )
246
        {
247
        colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
248
        vertices = new float[] { -F+F/4,F/4, F/4,-F+F/4, F/4,F/4};
249
        return factory.createTexture(vertices,colors,0.05f, 0.08f, true);
250
        }
251

    
252
      return null;
253
      }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
    private Bitmap createWhiteTexture()
258
      {
259
      int SIZE = 1;
260
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
261
      Canvas canvas = new Canvas(bitmap);
262

    
263
      Paint paint = new Paint();
264
      paint.setColor(0xffffff55);
265
      paint.setStyle(Paint.Style.FILL);
266
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
267

    
268
      return bitmap;
269
      }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
    private void createPolygon()
274
      {
275
      float A = 0.5f;
276
      float B = 0.04f;
277

    
278
      int extraIndex    = 0;
279
      int extraVertices = 0;
280
      float[] vertices = new float[] { -A,-A, A,-A, A,A, -A,A };
281

    
282
      float C = 2f;
283
      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};
284

    
285
/*
286
      int numBands      = 5;
287
      float[] bands = new float[2*numBands];
288

    
289
      for(int i=0; i<numBands; i++)
290
        {
291
        bands[2*i  ] = 1 + i/(1.0f-numBands);
292
        bands[2*i+1] = B/(numBands-1)*i;
293
        }
294
*/
295
      boolean[] edgesUp = new boolean[] {true,true,false,false};
296
      boolean[] vertsUp = new boolean[] {false,false,false,false};
297

    
298
      mMesh = new MeshPolygon(vertices,bands,edgesUp,vertsUp,extraIndex,extraVertices,0.0f,0.0f);
299
      mMesh.setEffectAssociation(0,0,0);
300
      mMesh.setShowNormals(true);
301
      }
302

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

    
305
    private void createMultigon()
306
      {
307
      float A = 0.5f;
308
      int extraIndex    = 0;
309
      int extraVertices = 0;
310

    
311
      float[] v1 = new float[] {  -A,-A,   A,-A,   A,  A,   -A,  A };
312
      float[] v2 = new float[] {   A,-A, 2*A,-A, 2*A,  A,    A,  A };
313
      float[] v3 = new float[] {-3*A,-A,  -A,-A,  -A,  A, -3*A,  A };
314
      float[] v4 = new float[] {  -A, A,   A, A,   A,3*A,   -A,3*A };
315
      float[] v5 = new float[] {-3*A, A,  -A, A,  -A,3*A, -3*A,3*A };
316

    
317
      float[][] vertices = new float[][] {v1,v2,v3,v4,v5};
318
/*
319
      float[] c1 = new float[] { 0,0 };
320
      float[] c2 = new float[] { 1.5f*A,0 };
321
      float[] c3 = new float[] {-1.5f*A,0 };
322
      float[] c4 = new float[] { 0,1.5f*A };
323
      float[] c5 = new float[] { -1.5f*A,1.5f*A };
324

    
325
      float[][] centers = new float[][] { c1,c2,c3,c4,c5 };
326
*/
327
      float C = 1f;
328
      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};
329

    
330
/*
331
      float B = 0.1f;
332
      int numBands = 7;
333
      float[] bands = new float[2*numBands];
334

    
335
      for(int i=0; i<numBands; i++)
336
        {
337
        bands[2*i  ] = 1 + i/(1.0f-numBands);
338
        bands[2*i+1] = B/(numBands-1)*i;
339
        }
340
*/
341
      mMesh = new MeshMultigon(vertices,bands,extraIndex,extraVertices);
342

    
343
      int numEff = mMesh.getNumEffComponents();
344

    
345
      for(int i=0; i<numEff; i++)
346
        {
347
        mMesh.setEffectAssociation(i, 0, i);
348
        }
349

    
350
     // mMesh.setShowNormals(true);
351
      }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
    private void createMesh()
356
      {
357
      float A = 2.0f;
358

    
359
      float[][] vertices =
360
          {
361
              { 0.5f, 0.5f, 0.5f },
362
              { 0.5f, 0.5f,-0.5f },
363
              { 0.5f,-0.5f, 0.5f },
364
              { 0.5f,-0.5f,-0.5f },
365
              {-0.5f, 0.5f, 0.5f },
366
              {-0.5f, 0.5f,-0.5f },
367
              {-0.5f,-0.5f, 0.5f },
368
              {-0.5f,-0.5f,-0.5f },
369

    
370
              { 0.5f,   -A, 0.5f },
371
              { 0.5f,   -A,-0.5f },
372
              {-0.5f,   -A, 0.5f },
373
              {-0.5f,   -A,-0.5f },
374

    
375
              {   -A, 0.5f, 0.5f },
376
              {   -A, 0.5f,-0.5f },
377
              {   -A,-0.5f, 0.5f },
378
              {   -A,-0.5f,-0.5f },
379
          };
380

    
381
      int[][][] vertIndices =
382
          {
383
              { { 8, 9, 1, 0} },
384
              { {15,14,12,13} },
385
              { {11,10, 6, 7} },
386
              { {12, 0, 1,13} },
387
              { {11, 9, 8,10} },
388
              { {15, 7, 6,14} },
389
              { {14, 6, 4,12}, { 6, 2, 0, 4}, {10, 8, 2, 6} },
390
              { { 3, 7, 5, 1}, { 9,11, 7, 3}, { 7,15,13, 5} }
391
          };
392

    
393
      float height = 0.02f;
394
      int num = 5;
395
      int extraI = 1;
396
      int extraV = 1;
397

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

    
400
      int[] bandIndices = {0,0,0,0,0,0,0,0};
401
      float[] convex = null;
402

    
403
      float[][] corners= { {0.036f,0.12f} };
404
      float[][] centers= { {0.0f, 0.0f, 0.0f} };
405
      int[] ind    = { 0,0,-1,-1,-1,-1,-1,-1, 0,0,0,0,0,0,0,0 };
406

    
407
      ObjectShape shape = new ObjectShape(vertices, vertIndices);
408
      ObjectFaceShape face = new ObjectFaceShape(bands, bandIndices, convex);
409
      ObjectVertexEffects effects = FactoryCubit.generateVertexEffect(vertices,corners,ind,centers,ind);
410

    
411
      int numFaces = shape.getNumFaces();
412
      int[] outer = new int[numFaces];
413

    
414
      FactoryCubit factory = FactoryCubit.getInstance();
415
      factory.clear();
416
      factory.createNewFaceTransform(shape,outer);
417
      mMesh = factory.createRoundedSolid(shape,face,effects,MESH_NICE,numFaces);
418

    
419
      int numEff = mMesh.getNumEffComponents();
420
      for(int i=0; i<numEff; i++) mMesh.setEffectAssociation(i, 0, i);
421
      }
422

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

    
425
    private void openMesh(int resourceID)
426
      {
427
      Context con = mView.getContext();
428
      Resources res = con.getResources();
429
      InputStream is = res.openRawResource(resourceID);
430
      DataInputStream dos = new DataInputStream(is);
431
      mMesh = new MeshFile(dos);
432

    
433
      int numEff = mMesh.getNumEffComponents();
434

    
435
      for(int i=0; i<numEff; i++)
436
        {
437
        mMesh.setEffectAssociation(i, 0, i);
438
        }
439

    
440
      try
441
        {
442
        is.close();
443
        }
444
      catch(IOException e)
445
        {
446
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
447
        }
448
      }
449

    
450
///////////////////////////////////////////////////////////////////////////////////////////////////
451

    
452
    MeshBase getMesh()
453
      {
454
      return mMesh;
455
      }
456

    
457
///////////////////////////////////////////////////////////////////////////////////////////////////
458

    
459
    long getTime()
460
      {
461
      return mTime;
462
      }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465

    
466
    int getBytes()
467
      {
468
      if( mMesh instanceof MeshFile )
469
        {
470
        return ((MeshFile)mMesh).getNumBytes();
471
        }
472

    
473
      return 0;
474
      }
475

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

    
478
    int getVertices()
479
      {
480
      return mMesh.getNumVertices();
481
      }
482

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

    
485
    int getEndEffIndex(int component)
486
      {
487
      return mMesh.getLastVertexEff(component);
488
      }
489

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

    
492
    int getEndTexIndex(int component)
493
      {
494
      return mMesh.getLastVertexTex(component);
495
      }
496

    
497
///////////////////////////////////////////////////////////////////////////////////////////////////
498

    
499
    int getEffComponentNum()
500
      {
501
      return mMesh.getNumEffComponents();
502
      }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

    
506
    int getTexComponentNum()
507
      {
508
      return mMesh.getNumTexComponents();
509
      }
510

    
511
///////////////////////////////////////////////////////////////////////////////////////////////////
512

    
513
    public void distortedException(Exception ex)
514
      {
515
      android.util.Log.e("MeshFile", ex.getMessage() );
516
      }
517

    
518
///////////////////////////////////////////////////////////////////////////////////////////////////
519

    
520
    public InputStream localFile(int fileID)
521
      {
522
      return mResources.openRawResource(fileID);
523
      }
524

    
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526

    
527
    public void logMessage(String message)
528
      {
529
      android.util.Log.e("MeshFile", message );
530
      }
531
}
(2-2/4)