Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 33647db9

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.VertexEffect;
35
import org.distorted.library.effect.VertexEffectDeform;
36
import org.distorted.library.effect.VertexEffectDisappear;
37
import org.distorted.library.effect.VertexEffectRotate;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.main.DistortedLibrary;
40
import org.distorted.library.main.DistortedScreen;
41
import org.distorted.library.main.DistortedTexture;
42
import org.distorted.library.mesh.MeshBase;
43
import org.distorted.library.mesh.MeshFile;
44
import org.distorted.library.mesh.MeshPolygon;
45
import org.distorted.library.type.DynamicQuat;
46
import org.distorted.library.type.Static3D;
47
import org.distorted.library.type.Static4D;
48

    
49
import java.io.DataInputStream;
50
import java.io.IOException;
51
import java.io.InputStream;
52

    
53
import javax.microedition.khronos.egl.EGLConfig;
54
import javax.microedition.khronos.opengles.GL10;
55

    
56
import static org.distorted.examples.meshfile.MeshFileActivity.PROCEDURAL;
57
import static org.distorted.examples.meshfile.FactoryCubit.COS18;
58
import static org.distorted.examples.meshfile.FactoryCubit.SIN18;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
63
{
64
    private static final float SQ2 = (float)Math.sqrt(2);
65
    private static final float SQ3 = (float)Math.sqrt(3);
66
    private final float DEFAULT_SCALE = 0.3f;
67

    
68
    private GLSurfaceView mView;
69
    private DistortedTexture mTexture;
70
    private DistortedScreen mScreen;
71
    private DistortedEffects mEffects;
72
    private Static3D mScale;
73
    private long mTime;
74
    private float mCurrentScale;
75
    private MeshBase mMesh;
76

    
77
    Static4D mQuat1, mQuat2;
78
    int mScreenMin;
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
    MeshFileRenderer(GLSurfaceView v)
83
      {
84
      mView = v;
85
      mScreen = new DistortedScreen();
86
      mScale= new Static3D(1,1,1);
87
      Static3D center=new Static3D(0,0,0);
88

    
89
      mCurrentScale = DEFAULT_SCALE;
90

    
91
      mQuat1 = new Static4D(0,0,0,1);
92
      mQuat2 = new Static4D(0,0,0,1);
93

    
94
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
95
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
96

    
97
      quatInt1.add(mQuat1);
98
      quatInt2.add(mQuat2);
99

    
100
      VertexEffectDisappear disappear = new VertexEffectDisappear();
101
      disappear.setMeshAssociation(1,-1);
102

    
103
      mEffects = new DistortedEffects();
104
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
105
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
106
      mEffects.apply( new MatrixEffectScale(mScale));
107

    
108
      mEffects.apply( disappear );
109

    
110
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
111
      mScreen.showFPS();
112
      }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
   
116
    public void onDrawFrame(GL10 glUnused) 
117
      {
118
      mScreen.render( System.currentTimeMillis() );
119
      }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
    
123
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
124
      {
125
      mScreenMin = Math.min(width, height);
126
      float factor = mCurrentScale*mScreenMin;
127
      mScale.set(factor,factor,factor);
128
      mScreen.resize(width, height);
129
      }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
    
133
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
134
      {
135
      if( mTexture==null ) mTexture = new DistortedTexture();
136

    
137
      Effect.enableEffects(EffectType.VERTEX);
138
      DistortedLibrary.setMax(EffectType.VERTEX, 20);
139
      DistortedLibrary.needTransformFeedback();
140
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
141
      }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
    public void distortedException(Exception ex)
146
      {
147
      android.util.Log.e("MeshFile", ex.getMessage() );
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
        openMesh(resourceID);
182
        }
183
      else
184
        {
185
        createMesh();
186
        }
187

    
188
      long t2 = System.currentTimeMillis();
189

    
190
      mTime = t2-t1;
191

    
192
      mScreen.detachAll();
193
      mScreen.attach(mTexture,mEffects,mMesh);
194
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
    private Bitmap createTexture(int resourceID)
199
      {
200
      TextureFactory factory = TextureFactory.getInstance();
201

    
202
      float[] vertices;
203
      int[] colors;
204
      float F = 0.5f;
205
      float E = SQ3/2;
206
      float G = SQ2/4;
207

    
208
      switch(resourceID)
209
          {
210
          case  R.raw.deferredjob:
211
          case  R.raw.meshjoin   :
212
          case  PROCEDURAL       :
213
          case  R.raw.predeform  : return createWhiteTexture();
214

    
215
          case  R.raw.cube2      :
216
          case  R.raw.cube3      :
217
          case  R.raw.cube4      :
218
          case  R.raw.cube5      : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
219
                                   vertices = new float[] { -F,-F, +F,-F, +F,+F, -F,+F};
220
                                   return factory.createTexture(vertices,colors,0.10f, 0.10f, true);
221
          case  R.raw.pyra3      :
222
          case  R.raw.pyra4      :
223
          case  R.raw.pyra5      : colors = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
224
                                   vertices = new float[] { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
225
                                   return factory.createTexture(vertices,colors,0.05f, 0.05f, true);
226

    
227
          case  R.raw.dino       : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
228
                                   vertices = new float[] { -F,F/3, 0,-2*F/3, +F,F/3 };
229
                                   return factory.createTexture(vertices,colors,0.05f, 0.03f, true);
230

    
231
          case R.raw.skewb       : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
232
                                   //vertices = new float[] { -G,-G, +G,-G, +G,+G, -G,+G };
233

    
234
                                   vertices = new float[] { -F+F/4,F/4, F/4,-F+F/4, F/4,F/4};
235
                                   return factory.createTexture(vertices,colors,0.05f, 0.08f, true);
236
          }
237

    
238
      return null;
239
      }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
    private Bitmap createWhiteTexture()
244
      {
245
      int SIZE = 1;
246
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
247
      Canvas canvas = new Canvas(bitmap);
248

    
249
      Paint paint = new Paint();
250
      paint.setColor(0xffffff55);
251
      paint.setStyle(Paint.Style.FILL);
252
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
253

    
254
      return bitmap;
255
      }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  private MeshBase createStaticMesh()
260
    {
261
    /*
262
    MeshBase triangle = new MeshTriangle(5);
263

    
264
    VertexEffectMove move = new VertexEffectMove( new Static3D(1,0,0) );
265
    triangle.apply(move);
266

    
267
    triangle.mergeEffComponents();
268

    
269
    VertexEffectScale scale = new VertexEffectScale( new Static3D(2,1,1) );
270
    triangle.apply(scale);
271

    
272
    return triangle;
273
     */
274
/*
275
    final float DIST  = 0.2f;
276
    float[] vertices = new float[6];
277

    
278
    vertices[0] =  0.5f-DIST;
279
    vertices[1] = -0.5f-DIST;
280

    
281
    vertices[2] =  0.5f-DIST;
282
    vertices[3] =  0.5f-DIST;
283

    
284
    vertices[4] = -0.5f-DIST;
285
    vertices[5] =  0.5f-DIST;
286

    
287
    float[] bands0 = new float[] {1.0f, 0.0f, 0.5f, 0.03f, 0.0f, 0.05f};
288

    
289
    MeshBase mesh = new MeshPolygon(vertices,bands0,0,0);
290
    mesh.setShowNormals(true);
291

    
292
    return mesh;
293
    */
294

    
295
    final float IVY_D = 0.10f;
296
    final int   IVY_N = 8;
297

    
298
    final float angle = (float)Math.PI/(2*IVY_N);
299
    final float CORR  = 1.0f - IVY_D*SQ2;
300
    final float DIST  = 0.4f;
301
    final float CORR2 = 0.5f;
302
    float[] vertices = new float[2*(IVY_N+1)+6];
303

    
304
    vertices[0] = ( 0.5f      -DIST)*CORR2;
305
    vertices[1] = (-0.5f+IVY_D-DIST)*CORR2;
306
    vertices[2] = ( 0.5f      -DIST)*CORR2;
307
    vertices[3] = ( 0.5f      -DIST)*CORR2;
308
    vertices[4] = (-0.5f+IVY_D-DIST)*CORR2;
309
    vertices[5] = ( 0.5f      -DIST)*CORR2;
310

    
311
    for(int i=0; i<=IVY_N; i++)
312
      {
313
      float ang = (IVY_N-i)*angle;
314
      float sin = (float)Math.sin(ang);
315
      float cos = (float)Math.cos(ang);
316

    
317
      vertices[2*i+6] = (CORR*(cos-0.5f)-DIST)*CORR2;
318
      vertices[2*i+7] = (CORR*(sin-0.5f)-DIST)*CORR2;
319
      }
320

    
321
    float[] bands = new float[] {1.0f, 0.0f, 0.5f, 0.03f, 0.0f, 0.05f};
322

    
323
    MeshBase mesh = new MeshPolygon(vertices,bands,0,0);
324
    mesh.setShowNormals(true);
325

    
326
    return mesh;
327
    }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

    
331
    private void createMesh()
332
      {
333
      final float[][] vertices = new float[][]
334
          {
335
              { 0.5f, 0.5f, 0.5f },
336
              { 0.5f, 0.5f,-0.5f },
337
              { 0.5f,-0.5f, 0.5f },
338
              { 0.5f,-0.5f,-0.5f },
339
              {-0.5f, 0.5f, 0.5f },
340
              {-0.5f, 0.5f,-0.5f },
341
              {-0.5f,-0.5f, 0.5f },
342
              {-0.5f,-0.5f,-0.5f },
343
          };
344

    
345
      final int[][] vertIndexes = new int[][]
346
          {
347
              {0,1,3,2},
348
              {5,4,6,7},
349
              {5,1,0,4},
350
              {6,2,3,7},
351
              {4,0,2,6},
352
              {1,5,7,3}
353
          };
354

    
355
      final float[][] bands = new float[][]
356
          {
357
              {0.048f,35,0.5f,0.7f,5,  2,2}
358
          };
359

    
360
      final int[] bandIndexes = new int[] { 0,0,0,0,0,0 };
361

    
362
      FactoryCubit factory = FactoryCubit.getInstance();
363
      mMesh = factory.createRoundedSolid(vertices, vertIndexes, bands, bandIndexes);
364

    
365
      int numEff = mMesh.numEffComponents();
366

    
367
      for(int i=0; i<numEff; i++)
368
        {
369
        mMesh.setEffectAssociation(i, 0, i);
370
        }
371
      }
372

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374

    
375
    private void openMesh(int resourceID)
376
      {
377
      Context con = mView.getContext();
378
      Resources res = con.getResources();
379
      InputStream is = res.openRawResource(resourceID);
380
      DataInputStream dos = new DataInputStream(is);
381
      mMesh = new MeshFile(dos);
382

    
383
      int numEff = mMesh.numEffComponents();
384

    
385
      for(int i=0; i<numEff; i++)
386
        {
387
        mMesh.setEffectAssociation(i, 0, i);
388
        }
389

    
390
      try
391
        {
392
        is.close();
393
        }
394
      catch(IOException e)
395
        {
396
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
397
        }
398
      }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
    MeshBase getMesh()
403
      {
404
      return mMesh;
405
      }
406

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408

    
409
    long getTime()
410
      {
411
      return mTime;
412
      }
413

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

    
416
    int getBytes()
417
      {
418
      if( mMesh instanceof MeshFile )
419
        {
420
        return ((MeshFile)mMesh).getNumBytes();
421
        }
422

    
423
      return 0;
424
      }
425

    
426
///////////////////////////////////////////////////////////////////////////////////////////////////
427

    
428
    int getVertices()
429
      {
430
      return mMesh.getNumVertices();
431
      }
432

    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434

    
435
    int getEndEffIndex(int component)
436
      {
437
      return mMesh.getLastVertexEff(component);
438
      }
439

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

    
442
    int getEndTexIndex(int component)
443
      {
444
      return mMesh.getLastVertexTex(component);
445
      }
446

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

    
449
    int getEffComponentNum()
450
      {
451
      return mMesh.numEffComponents();
452
      }
453

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

    
456
    int getTexComponentNum()
457
      {
458
      return mMesh.numTexComponents();
459
      }
460
}
(3-3/5)