Project

General

Profile

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

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

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
import static org.distorted.objectlib.main.TwistyObject.MODE_NORM;
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

    
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

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

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

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

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

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

    
116
      mEffects.apply( disappear );
117

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

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

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

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

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

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

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

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

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

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

    
178
      long t1 = System.currentTimeMillis();
179

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

    
197
      long t2 = System.currentTimeMillis();
198

    
199
      mTime = t2-t1;
200

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

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

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

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

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

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

    
253
      return null;
254
      }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

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

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

    
269
      return bitmap;
270
      }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

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

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

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

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

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

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

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

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

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

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

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

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

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

    
344
      int numEff = mMesh.getNumEffComponents();
345

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

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

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
    private void createMesh()
357
      {
358
      float[][] vertices =
359
          {
360
              { 0.5f, 0.5f, 0.5f },
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

    
369
              {-0.5f, 0.0f, 0.5f },
370
              { 0.5f, 0.0f, 0.5f },
371
          };
372

    
373
      int[][][] vertIndices =
374
          {
375
              { {2,3,1,0} },
376
              { {7,6,4,5} },
377
              { {4,0,1,5} },
378
              { {7,3,2,6} },
379
              { {6,2,0,4} },
380
              { {3,7,5,1} }
381
          };
382

    
383
      float height = 0.05f;
384
      int num = 5;
385
      int extraI = 1;
386
      int extraV = 1;
387

    
388
      float[][] bands= { {height,35,0.5f,0.7f,num,extraI,extraV} };
389

    
390
      int[] bandIndices = {0,0,0,0,0,0};
391
      float[] convex = null;
392

    
393
      float[][] corners= { {0.036f,0.12f} };
394
      float[][] centers= { {0.0f, 0.0f, 0.0f} };
395
      int[] ind    = { 0,0,0,0,0,0,0,0,-1,-1 };
396

    
397
      ObjectShape shape = new ObjectShape(vertices, vertIndices);
398
      ObjectFaceShape face = new ObjectFaceShape(bands, bandIndices, convex);
399
      ObjectVertexEffects effects = FactoryCubit.generateVertexEffect(vertices,corners,ind,centers,ind);
400

    
401
      int numFaces = shape.getNumFaces();
402
      int[] outer = new int[numFaces];
403

    
404
      FactoryCubit factory = FactoryCubit.getInstance();
405
      factory.clear();
406
      factory.createNewFaceTransform(shape,outer);
407
      mMesh = factory.createRoundedSolid(shape,face,effects,MESH_NICE,numFaces);
408

    
409
      int numEff = mMesh.getNumEffComponents();
410

    
411
      for(int i=0; i<numEff; i++) mMesh.setEffectAssociation(i, 0, i);
412
      }
413

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415

    
416
    private void openMesh(int resourceID)
417
      {
418
      Context con = mView.getContext();
419
      Resources res = con.getResources();
420
      InputStream is = res.openRawResource(resourceID);
421
      DataInputStream dos = new DataInputStream(is);
422
      mMesh = new MeshFile(dos);
423

    
424
      int numEff = mMesh.getNumEffComponents();
425

    
426
      for(int i=0; i<numEff; i++)
427
        {
428
        mMesh.setEffectAssociation(i, 0, i);
429
        }
430

    
431
      try
432
        {
433
        is.close();
434
        }
435
      catch(IOException e)
436
        {
437
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
438
        }
439
      }
440

    
441
///////////////////////////////////////////////////////////////////////////////////////////////////
442

    
443
    MeshBase getMesh()
444
      {
445
      return mMesh;
446
      }
447

    
448
///////////////////////////////////////////////////////////////////////////////////////////////////
449

    
450
    long getTime()
451
      {
452
      return mTime;
453
      }
454

    
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456

    
457
    int getBytes()
458
      {
459
      if( mMesh instanceof MeshFile )
460
        {
461
        return ((MeshFile)mMesh).getNumBytes();
462
        }
463

    
464
      return 0;
465
      }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

    
469
    int getVertices()
470
      {
471
      return mMesh.getNumVertices();
472
      }
473

    
474
///////////////////////////////////////////////////////////////////////////////////////////////////
475

    
476
    int getEndEffIndex(int component)
477
      {
478
      return mMesh.getLastVertexEff(component);
479
      }
480

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482

    
483
    int getEndTexIndex(int component)
484
      {
485
      return mMesh.getLastVertexTex(component);
486
      }
487

    
488
///////////////////////////////////////////////////////////////////////////////////////////////////
489

    
490
    int getEffComponentNum()
491
      {
492
      return mMesh.getNumEffComponents();
493
      }
494

    
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496

    
497
    int getTexComponentNum()
498
      {
499
      return mMesh.getNumTexComponents();
500
      }
501

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

    
504
    public void distortedException(Exception ex)
505
      {
506
      android.util.Log.e("MeshFile", ex.getMessage() );
507
      }
508

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

    
511
    public InputStream localFile(int fileID)
512
      {
513
      return mResources.openRawResource(fileID);
514
      }
515

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

    
518
    public void logMessage(String message)
519
      {
520
      android.util.Log.e("MeshFile", message );
521
      }
522
}
(2-2/4)