Project

General

Profile

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

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

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
import org.distorted.objectlib.main.InitData;
52
import org.distorted.objectlib.main.TwistyObject;
53
import org.distorted.objectlib.objects.*;
54

    
55
import java.io.DataInputStream;
56
import java.io.IOException;
57
import java.io.InputStream;
58

    
59
import javax.microedition.khronos.egl.EGLConfig;
60
import javax.microedition.khronos.opengles.GL10;
61

    
62
import static org.distorted.examples.meshfile.MeshFileActivity.MULTIGON;
63
import static org.distorted.examples.meshfile.MeshFileActivity.POLYGON;
64
import static org.distorted.examples.meshfile.MeshFileActivity.PROCEDURAL;
65
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
66
import static org.distorted.objectlib.main.TwistyObject.MODE_NORM;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

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

    
75
    private final GLSurfaceView mView;
76
    private final Resources mResources;
77
    private final DistortedScreen mScreen;
78
    private final DistortedEffects mEffects;
79
    private final Static3D mScale;
80

    
81
    private DistortedTexture mTexture;
82
    private MeshBase mMesh;
83
    private long mTime;
84
    private float mCurrentScale;
85

    
86
    Static4D mQuat1, mQuat2;
87
    int mScreenMin;
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
    MeshFileRenderer(GLSurfaceView v)
92
      {
93
      mView = v;
94
      mResources = v.getResources();
95

    
96
      mScreen = new DistortedScreen();
97
      mScale= new Static3D(1,1,1);
98
      Static3D center=new Static3D(0,0,0);
99

    
100
      mCurrentScale = DEFAULT_SCALE;
101

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

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

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

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

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

    
119
      mEffects.apply( disappear );
120

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

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

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

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

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

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

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

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

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
    void open(int resourceID)
177
      {
178
      if( mTexture==null ) mTexture = new DistortedTexture();
179
      mTexture.setTexture( createTexture(resourceID) );
180

    
181
      long t1 = System.currentTimeMillis();
182

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

    
200
      long t2 = System.currentTimeMillis();
201

    
202
      mTime = t2-t1;
203

    
204
      mScreen.detachAll();
205
      mScreen.attach(mTexture,mEffects,mMesh);
206
      }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
    private Bitmap createTexture(int resourceID)
211
      {
212
      TextureFactory factory = TextureFactory.getInstance();
213

    
214
      float[] vertices;
215
      int[] colors;
216
      float F = 0.5f;
217
      float E = SQ3/2;
218

    
219
      if( resourceID == R.raw.deferredjob ||
220
          resourceID == R.raw.meshjoin    ||
221
          resourceID == PROCEDURAL        ||
222
          resourceID == POLYGON           ||
223
          resourceID == MULTIGON          ||
224
          resourceID == R.raw.predeform    ) return createWhiteTexture();
225

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

    
256
      return null;
257
      }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
    private Bitmap createWhiteTexture()
262
      {
263
      int SIZE = 1;
264
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
265
      Canvas canvas = new Canvas(bitmap);
266

    
267
      Paint paint = new Paint();
268
      paint.setColor(0xffffff55);
269
      paint.setStyle(Paint.Style.FILL);
270
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
271

    
272
      return bitmap;
273
      }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
    private void createPolygon()
278
      {
279
      float A = 0.5f;
280
      float B = 0.04f;
281

    
282
      int extraIndex    = 1;
283
      int extraVertices = 1;
284
      float[] vertices = new float[] { -A,-A, A,-A, A,A, -A,A };
285

    
286
      float C = 2f;
287
      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};
288

    
289
/*
290
      int numBands      = 5;
291
      float[] bands = new float[2*numBands];
292

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

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

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308

    
309
    private void createMultigon()
310
      {
311
      float A = 0.5f;
312
      int extraIndex    = 0;
313
      int extraVertices = 0;
314

    
315
      float[] v1 = new float[] {  -A,-A,   A,-A,   A,  A,   -A,  A };
316
      float[] v2 = new float[] {   A,-A, 2*A,-A, 2*A,  A,    A,  A };
317
      float[] v3 = new float[] {-3*A,-A,  -A,-A,  -A,  A, -3*A,  A };
318
      float[] v4 = new float[] {  -A, A,   A, A,   A,3*A,   -A,3*A };
319
      float[] v5 = new float[] {-3*A, A,  -A, A,  -A,3*A, -3*A,3*A };
320

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

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

    
334
/*
335
      float B = 0.1f;
336
      int numBands = 7;
337
      float[] bands = new float[2*numBands];
338

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

    
347
      int numEff = mMesh.getNumEffComponents();
348

    
349
      for(int i=0; i<numEff; i++)
350
        {
351
        mMesh.setEffectAssociation(i, 0, i);
352
        }
353

    
354
     // mMesh.setShowNormals(true);
355
      }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
    private void createMesh()
360
      {
361
      Static4D quat = new Static4D(0,0,0,1);
362
      Static3D move = new Static3D(0,0,0);
363
      float scale   = 1.0f;
364
      int variant   = 1;
365

    
366
      InitData data = new InitData(new int[] {2,3,2});
367
      TwistyObject puzzle = new TwistyCuboid(MESH_NICE,MODE_NORM,quat,move,scale,data,null);
368

    
369
      ObjectShape shape           = puzzle.getObjectShape(variant);
370
      ObjectFaceShape face        = puzzle.getObjectFaceShape(variant);
371
      ObjectVertexEffects effects = puzzle.getVertexEffects(variant);
372

    
373
      int[][] indices = shape.getVertIndices();
374
      int numComponents = indices.length;
375
      int[] outer = new int[numComponents];
376

    
377
      FactoryCubit factory = FactoryCubit.getInstance();
378
      factory.clear();
379
      factory.createNewFaceTransform(shape,outer);
380
      mMesh = factory.createRoundedSolid(shape,face,effects,MESH_NICE,numComponents);
381

    
382
      int numEff = mMesh.getNumEffComponents();
383

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

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

    
389
    private void openMesh(int resourceID)
390
      {
391
      Context con = mView.getContext();
392
      Resources res = con.getResources();
393
      InputStream is = res.openRawResource(resourceID);
394
      DataInputStream dos = new DataInputStream(is);
395
      mMesh = new MeshFile(dos);
396

    
397
      int numEff = mMesh.getNumEffComponents();
398

    
399
      for(int i=0; i<numEff; i++)
400
        {
401
        mMesh.setEffectAssociation(i, 0, i);
402
        }
403

    
404
      try
405
        {
406
        is.close();
407
        }
408
      catch(IOException e)
409
        {
410
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
411
        }
412
      }
413

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

    
416
    MeshBase getMesh()
417
      {
418
      return mMesh;
419
      }
420

    
421
///////////////////////////////////////////////////////////////////////////////////////////////////
422

    
423
    long getTime()
424
      {
425
      return mTime;
426
      }
427

    
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429

    
430
    int getBytes()
431
      {
432
      if( mMesh instanceof MeshFile )
433
        {
434
        return ((MeshFile)mMesh).getNumBytes();
435
        }
436

    
437
      return 0;
438
      }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441

    
442
    int getVertices()
443
      {
444
      return mMesh.getNumVertices();
445
      }
446

    
447
///////////////////////////////////////////////////////////////////////////////////////////////////
448

    
449
    int getEndEffIndex(int component)
450
      {
451
      return mMesh.getLastVertexEff(component);
452
      }
453

    
454
///////////////////////////////////////////////////////////////////////////////////////////////////
455

    
456
    int getEndTexIndex(int component)
457
      {
458
      return mMesh.getLastVertexTex(component);
459
      }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462

    
463
    int getEffComponentNum()
464
      {
465
      return mMesh.getNumEffComponents();
466
      }
467

    
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469

    
470
    int getTexComponentNum()
471
      {
472
      return mMesh.getNumTexComponents();
473
      }
474

    
475
///////////////////////////////////////////////////////////////////////////////////////////////////
476

    
477
    public void distortedException(Exception ex)
478
      {
479
      android.util.Log.e("MeshFile", ex.getMessage() );
480
      }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
    public InputStream localFile(int fileID)
485
      {
486
      return mResources.openRawResource(fileID);
487
      }
488

    
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490

    
491
    public void logMessage(String message)
492
      {
493
      android.util.Log.e("MeshFile", message );
494
      }
495
}
(2-2/4)