Project

General

Profile

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

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

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 A = 2.0f;
359

    
360
      float[][] vertices =
361
          {
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
              {-0.5f,-0.5f,-0.5f },
370

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

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

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

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

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

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

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

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

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

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

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

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

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

    
434
      int numEff = mMesh.getNumEffComponents();
435

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

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

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

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

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459

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

    
465
///////////////////////////////////////////////////////////////////////////////////////////////////
466

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

    
474
      return 0;
475
      }
476

    
477
///////////////////////////////////////////////////////////////////////////////////////////////////
478

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

    
484
///////////////////////////////////////////////////////////////////////////////////////////////////
485

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

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492

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

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499

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

    
505
///////////////////////////////////////////////////////////////////////////////////////////////////
506

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

    
512
///////////////////////////////////////////////////////////////////////////////////////////////////
513

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

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

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

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

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