Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 1b85f172

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.MeshPolygon;
42
import org.distorted.library.type.DynamicQuat;
43
import org.distorted.library.type.Static3D;
44
import org.distorted.library.type.Static4D;
45

    
46
import java.io.DataInputStream;
47
import java.io.IOException;
48
import java.io.InputStream;
49

    
50
import javax.microedition.khronos.egl.EGLConfig;
51
import javax.microedition.khronos.opengles.GL10;
52

    
53
import static org.distorted.examples.meshfile.MeshFileActivity.PROCEDURAL;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
58
{
59
    private static final float SQ2 = (float)Math.sqrt(2);
60
    private static final float SQ3 = (float)Math.sqrt(3);
61
    private final float DEFAULT_SCALE = 0.3f;
62

    
63
    private final GLSurfaceView mView;
64
    private DistortedTexture mTexture;
65
    private final DistortedScreen mScreen;
66
    private final DistortedEffects mEffects;
67
    private final Static3D mScale;
68
    private long mTime;
69
    private float mCurrentScale;
70
    private MeshBase mMesh;
71

    
72
    Static4D mQuat1, mQuat2;
73
    int mScreenMin;
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
    MeshFileRenderer(GLSurfaceView v)
78
      {
79
      mView = v;
80
      mScreen = new DistortedScreen();
81
      mScale= new Static3D(1,1,1);
82
      Static3D center=new Static3D(0,0,0);
83

    
84
      mCurrentScale = DEFAULT_SCALE;
85

    
86
      mQuat1 = new Static4D(0,0,0,1);
87
      mQuat2 = new Static4D(0,0,0,1);
88

    
89
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
90
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
91

    
92
      quatInt1.add(mQuat1);
93
      quatInt2.add(mQuat2);
94

    
95
      VertexEffectDisappear disappear = new VertexEffectDisappear();
96
      disappear.setMeshAssociation(1,-1);
97

    
98
      mEffects = new DistortedEffects();
99
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
100
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
101
      mEffects.apply( new MatrixEffectScale(mScale));
102

    
103
      mEffects.apply( disappear );
104

    
105
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
106
      mScreen.showFPS();
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
   
111
    public void onDrawFrame(GL10 glUnused) 
112
      {
113
      mScreen.render( System.currentTimeMillis() );
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
    
118
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
119
      {
120
      mScreenMin = Math.min(width, height);
121
      float factor = mCurrentScale*mScreenMin;
122
      mScale.set(factor,factor,factor);
123
      mScreen.resize(width, height);
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
129
      {
130
      if( mTexture==null ) mTexture = new DistortedTexture();
131

    
132
      Effect.enableEffects(EffectType.VERTEX);
133
      DistortedLibrary.setMax(EffectType.VERTEX, 20);
134
      DistortedLibrary.needTransformFeedback();
135
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
136
      }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
    public void distortedException(Exception ex)
141
      {
142
      android.util.Log.e("MeshFile", ex.getMessage() );
143
      }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
//   0 ---> 0
147
//  50 ---> DEFAULT_SCALE
148
// 100 ---> 4*DEFAULT_SCALE
149

    
150
    void setScale(int scale)
151
      {
152
      if( scale<= 50 )
153
        {
154
        mCurrentScale = DEFAULT_SCALE * scale / 50.0f;
155
        }
156
      else
157
        {
158
        mCurrentScale = DEFAULT_SCALE * ( 3*(scale/50.0f) - 2.0f);
159
        }
160

    
161
      float factor = mCurrentScale*mScreenMin;
162
      mScale.set(factor,factor,factor);
163
      }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
    void open(int resourceID)
168
      {
169
      if( mTexture==null ) mTexture = new DistortedTexture();
170
      mTexture.setTexture( createTexture(resourceID) );
171

    
172
      long t1 = System.currentTimeMillis();
173

    
174
      if( resourceID!=PROCEDURAL )
175
        {
176
        openMesh(resourceID);
177
        }
178
      else
179
        {
180
        createMesh();
181
        }
182

    
183
      long t2 = System.currentTimeMillis();
184

    
185
      mTime = t2-t1;
186

    
187
      mScreen.detachAll();
188
      mScreen.attach(mTexture,mEffects,mMesh);
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
    private Bitmap createTexture(int resourceID)
194
      {
195
      TextureFactory factory = TextureFactory.getInstance();
196

    
197
      float[] vertices;
198
      int[] colors;
199
      float F = 0.5f;
200
      float E = SQ3/2;
201
      float G = SQ2/4;
202

    
203
      switch(resourceID)
204
          {
205
          case  R.raw.deferredjob:
206
          case  R.raw.meshjoin   :
207
          case  PROCEDURAL       :
208
          case  R.raw.predeform  : return createWhiteTexture();
209

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

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

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

    
229
                                   vertices = new float[] { -F+F/4,F/4, F/4,-F+F/4, F/4,F/4};
230
                                   return factory.createTexture(vertices,colors,0.05f, 0.08f, true);
231
          }
232

    
233
      return null;
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    private Bitmap createWhiteTexture()
239
      {
240
      int SIZE = 1;
241
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
242
      Canvas canvas = new Canvas(bitmap);
243

    
244
      Paint paint = new Paint();
245
      paint.setColor(0xffffff55);
246
      paint.setStyle(Paint.Style.FILL);
247
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
248

    
249
      return bitmap;
250
      }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  private MeshBase createStaticMesh()
255
    {
256
    final float IVY_D = 0.10f;
257
    final int   IVY_N = 8;
258

    
259
    final float angle = (float)Math.PI/(2*IVY_N);
260
    final float CORR  = 1.0f - IVY_D*SQ2;
261
    final float DIST  = 0.4f;
262
    final float CORR2 = 0.5f;
263
    float[] vertices = new float[2*(IVY_N+1)+6];
264

    
265
    vertices[0] = ( 0.5f      -DIST)*CORR2;
266
    vertices[1] = (-0.5f+IVY_D-DIST)*CORR2;
267
    vertices[2] = ( 0.5f      -DIST)*CORR2;
268
    vertices[3] = ( 0.5f      -DIST)*CORR2;
269
    vertices[4] = (-0.5f+IVY_D-DIST)*CORR2;
270
    vertices[5] = ( 0.5f      -DIST)*CORR2;
271

    
272
    for(int i=0; i<=IVY_N; i++)
273
      {
274
      float ang = (IVY_N-i)*angle;
275
      float sin = (float)Math.sin(ang);
276
      float cos = (float)Math.cos(ang);
277

    
278
      vertices[2*i+6] = (CORR*(cos-0.5f)-DIST)*CORR2;
279
      vertices[2*i+7] = (CORR*(sin-0.5f)-DIST)*CORR2;
280
      }
281

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

    
284
    MeshBase mesh = new MeshPolygon(vertices,bands,0,0);
285
    mesh.setShowNormals(true);
286

    
287
    return mesh;
288
    }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
    private void createMesh()
293
      {
294
      final float[][] vertices = new float[][]
295
          {
296
              { 0.5f, 0.5f, 0.5f },
297
              { 0.5f, 0.5f,-0.5f },
298
              { 0.5f,-0.5f, 0.5f },
299
              { 0.5f,-0.5f,-0.5f },
300
              {-0.5f, 0.5f, 0.5f },
301
              {-0.5f, 0.5f,-0.5f },
302
              {-0.5f,-0.5f, 0.5f },
303
              {-0.5f,-0.5f,-0.5f },
304
          };
305

    
306
      final int[][] vertIndexes = new int[][]
307
          {
308
              {2,3,1,0},   // counterclockwise!
309
              {7,6,4,5},
310
              {4,0,1,5},
311
              {7,3,2,6},
312
              {6,2,0,4},
313
              {3,7,5,1}
314
          };
315

    
316
      final float[][] bands = new float[][]
317
          {
318
              {0.05f,45,0.6f,0.5f,5,  2,2}
319
          };
320

    
321
      final int[] bandIndexes = new int[] { 0,0,0,0,0,0 };
322

    
323
      final float[][] corners = new float[][]
324
          {
325
              { 0.01f, 0.10f }
326
          };
327

    
328
      final int[] cornerIndexes = new int[] { 0,0,0,0,0,0,0,0 };
329

    
330
      FactoryCubit factory = FactoryCubit.getInstance();
331
      mMesh = factory.createRoundedSolid(vertices, vertIndexes, bands, bandIndexes, corners, cornerIndexes);
332

    
333
      int numEff = mMesh.numEffComponents();
334

    
335
      for(int i=0; i<numEff; i++)
336
        {
337
        mMesh.setEffectAssociation(i, 0, i);
338
        }
339
      }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
    private void openMesh(int resourceID)
344
      {
345
      Context con = mView.getContext();
346
      Resources res = con.getResources();
347
      InputStream is = res.openRawResource(resourceID);
348
      DataInputStream dos = new DataInputStream(is);
349
      mMesh = new MeshFile(dos);
350

    
351
      int numEff = mMesh.numEffComponents();
352

    
353
      for(int i=0; i<numEff; i++)
354
        {
355
        mMesh.setEffectAssociation(i, 0, i);
356
        }
357

    
358
      try
359
        {
360
        is.close();
361
        }
362
      catch(IOException e)
363
        {
364
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
365
        }
366
      }
367

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

    
370
    MeshBase getMesh()
371
      {
372
      return mMesh;
373
      }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
    long getTime()
378
      {
379
      return mTime;
380
      }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
    int getBytes()
385
      {
386
      if( mMesh instanceof MeshFile )
387
        {
388
        return ((MeshFile)mMesh).getNumBytes();
389
        }
390

    
391
      return 0;
392
      }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395

    
396
    int getVertices()
397
      {
398
      return mMesh.getNumVertices();
399
      }
400

    
401
///////////////////////////////////////////////////////////////////////////////////////////////////
402

    
403
    int getEndEffIndex(int component)
404
      {
405
      return mMesh.getLastVertexEff(component);
406
      }
407

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409

    
410
    int getEndTexIndex(int component)
411
      {
412
      return mMesh.getLastVertexTex(component);
413
      }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
    int getEffComponentNum()
418
      {
419
      return mMesh.numEffComponents();
420
      }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
    int getTexComponentNum()
425
      {
426
      return mMesh.numTexComponents();
427
      }
428
}
(3-3/5)