Project

General

Profile

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

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

1 42aa970f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 1af8e143 Leszek Koltunski
import android.content.Context;
23
import android.content.res.Resources;
24 42aa970f Leszek Koltunski
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.opengl.GLSurfaceView;
28
29 8ac4c0bc Leszek Koltunski
import org.distorted.examples.R;
30 a65604a7 Leszek Koltunski
import org.distorted.library.effect.Effect;
31 6991ece5 Leszek Koltunski
import org.distorted.library.effect.EffectType;
32 42aa970f Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34 39b925d5 Leszek Koltunski
import org.distorted.library.effect.VertexEffectDisappear;
35 42aa970f Leszek Koltunski
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 6991ece5 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
40 1af8e143 Leszek Koltunski
import org.distorted.library.mesh.MeshFile;
41 55f20739 Leszek Koltunski
import org.distorted.library.mesh.MeshPolygon;
42 42aa970f Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
43
import org.distorted.library.type.Static3D;
44
import org.distorted.library.type.Static4D;
45
46 1af8e143 Leszek Koltunski
import java.io.DataInputStream;
47
import java.io.IOException;
48
import java.io.InputStream;
49
50 42aa970f Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
51
import javax.microedition.khronos.opengles.GL10;
52
53 6991ece5 Leszek Koltunski
import static org.distorted.examples.meshfile.MeshFileActivity.PROCEDURAL;
54
55 42aa970f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57 061449ed Leszek Koltunski
class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
58 42aa970f Leszek Koltunski
{
59 7edab735 Leszek Koltunski
    private static final float SQ2 = (float)Math.sqrt(2);
60
    private static final float SQ3 = (float)Math.sqrt(3);
61 16b336db Leszek Koltunski
    private final float DEFAULT_SCALE = 0.3f;
62
63 140f2c4e Leszek Koltunski
    private final GLSurfaceView mView;
64 42aa970f Leszek Koltunski
    private DistortedTexture mTexture;
65 140f2c4e Leszek Koltunski
    private final DistortedScreen mScreen;
66
    private final DistortedEffects mEffects;
67
    private final Static3D mScale;
68 48c88f57 Leszek Koltunski
    private long mTime;
69 16b336db Leszek Koltunski
    private float mCurrentScale;
70 6991ece5 Leszek Koltunski
    private MeshBase mMesh;
71 42aa970f Leszek Koltunski
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 16b336db Leszek Koltunski
      mCurrentScale = DEFAULT_SCALE;
85
86 42aa970f Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);
87 fe032f52 Leszek Koltunski
      mQuat2 = new Static4D(0,0,0,1);
88 42aa970f Leszek Koltunski
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 39b925d5 Leszek Koltunski
      VertexEffectDisappear disappear = new VertexEffectDisappear();
96
      disappear.setMeshAssociation(1,-1);
97
98 42aa970f Leszek Koltunski
      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 39b925d5 Leszek Koltunski
      mEffects.apply( disappear );
104
105 42aa970f Leszek Koltunski
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
106 48c88f57 Leszek Koltunski
      mScreen.showFPS();
107 42aa970f Leszek Koltunski
      }
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 16b336db Leszek Koltunski
      float factor = mCurrentScale*mScreenMin;
122 42aa970f Leszek Koltunski
      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 a65604a7 Leszek Koltunski
      Effect.enableEffects(EffectType.VERTEX);
133 04a21ed6 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX, 20);
134 ef02f5e0 Leszek Koltunski
      DistortedLibrary.needTransformFeedback();
135 7c72e798 Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
136 061449ed Leszek Koltunski
      }
137
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
140
    public void distortedException(Exception ex)
141
      {
142
      android.util.Log.e("MeshFile", ex.getMessage() );
143 42aa970f Leszek Koltunski
      }
144
145 16b336db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 42aa970f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167 1af8e143 Leszek Koltunski
    void open(int resourceID)
168 42aa970f Leszek Koltunski
      {
169 099d8f8b Leszek Koltunski
      if( mTexture==null ) mTexture = new DistortedTexture();
170 8ac4c0bc Leszek Koltunski
      mTexture.setTexture( createTexture(resourceID) );
171
172 acad428e Leszek Koltunski
      long t1 = System.currentTimeMillis();
173 6991ece5 Leszek Koltunski
174
      if( resourceID!=PROCEDURAL )
175
        {
176
        openMesh(resourceID);
177
        }
178
      else
179
        {
180
        createMesh();
181
        }
182
183 acad428e Leszek Koltunski
      long t2 = System.currentTimeMillis();
184
185 48c88f57 Leszek Koltunski
      mTime = t2-t1;
186 42aa970f Leszek Koltunski
187 71c7624f Leszek Koltunski
      mScreen.detachAll();
188 7198e5c9 Leszek Koltunski
      mScreen.attach(mTexture,mEffects,mMesh);
189 42aa970f Leszek Koltunski
      }
190
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
193 1af8e143 Leszek Koltunski
    private Bitmap createTexture(int resourceID)
194 42aa970f Leszek Koltunski
      {
195 7edab735 Leszek Koltunski
      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 8ac4c0bc Leszek Koltunski
      switch(resourceID)
204
          {
205
          case  R.raw.deferredjob:
206 7edab735 Leszek Koltunski
          case  R.raw.meshjoin   :
207 1beffb69 Leszek Koltunski
          case  PROCEDURAL       :
208 7edab735 Leszek Koltunski
          case  R.raw.predeform  : return createWhiteTexture();
209
210 8ac4c0bc Leszek Koltunski
          case  R.raw.cube2      :
211
          case  R.raw.cube3      :
212
          case  R.raw.cube4      :
213 7edab735 Leszek Koltunski
          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 8ac4c0bc Leszek Koltunski
          case  R.raw.pyra3      :
217
          case  R.raw.pyra4      :
218 7edab735 Leszek Koltunski
          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 8ac4c0bc Leszek Koltunski
222 7edab735 Leszek Koltunski
          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 8ac4c0bc Leszek Koltunski
226 7edab735 Leszek Koltunski
          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 8ac4c0bc Leszek Koltunski
229 7edab735 Leszek Koltunski
                                   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 8ac4c0bc Leszek Koltunski
233 7edab735 Leszek Koltunski
      return null;
234 8ac4c0bc Leszek Koltunski
      }
235
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
238 7edab735 Leszek Koltunski
    private Bitmap createWhiteTexture()
239 8ac4c0bc Leszek Koltunski
      {
240 7edab735 Leszek Koltunski
      int SIZE = 1;
241 8ac4c0bc Leszek Koltunski
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
242
      Canvas canvas = new Canvas(bitmap);
243
244
      Paint paint = new Paint();
245 36793c52 Leszek Koltunski
      paint.setColor(0xffffff55);
246 1beffb69 Leszek Koltunski
      paint.setStyle(Paint.Style.FILL);
247 7edab735 Leszek Koltunski
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
248 6991ece5 Leszek Koltunski
249 72059460 Leszek Koltunski
      return bitmap;
250 6991ece5 Leszek Koltunski
      }
251
252 72059460 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
253 6991ece5 Leszek Koltunski
254 72059460 Leszek Koltunski
  private MeshBase createStaticMesh()
255
    {
256 55f20739 Leszek Koltunski
    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 6991ece5 Leszek Koltunski
    }
289
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291
292
    private void createMesh()
293
      {
294 33647db9 Leszek Koltunski
      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 140f2c4e Leszek Koltunski
              {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 33647db9 Leszek Koltunski
          };
315
316
      final float[][] bands = new float[][]
317
          {
318 140f2c4e Leszek Koltunski
              {0.05f,45,0.6f,0.5f,5,  2,2}
319 33647db9 Leszek Koltunski
          };
320 a65604a7 Leszek Koltunski
321 33647db9 Leszek Koltunski
      final int[] bandIndexes = new int[] { 0,0,0,0,0,0 };
322 1c41c4c9 Leszek Koltunski
323 1b85f172 Leszek Koltunski
      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 33647db9 Leszek Koltunski
      FactoryCubit factory = FactoryCubit.getInstance();
331 1b85f172 Leszek Koltunski
      mMesh = factory.createRoundedSolid(vertices, vertIndexes, bands, bandIndexes, corners, cornerIndexes);
332 6991ece5 Leszek Koltunski
333
      int numEff = mMesh.numEffComponents();
334
335
      for(int i=0; i<numEff; i++)
336
        {
337
        mMesh.setEffectAssociation(i, 0, i);
338
        }
339
      }
340 42aa970f Leszek Koltunski
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342
343 6991ece5 Leszek Koltunski
    private void openMesh(int resourceID)
344 42aa970f Leszek Koltunski
      {
345 1af8e143 Leszek Koltunski
      Context con = mView.getContext();
346
      Resources res = con.getResources();
347
      InputStream is = res.openRawResource(resourceID);
348
      DataInputStream dos = new DataInputStream(is);
349 7198e5c9 Leszek Koltunski
      mMesh = new MeshFile(dos);
350 1af8e143 Leszek Koltunski
351 39b925d5 Leszek Koltunski
      int numEff = mMesh.numEffComponents();
352
353
      for(int i=0; i<numEff; i++)
354
        {
355
        mMesh.setEffectAssociation(i, 0, i);
356
        }
357
358 1af8e143 Leszek Koltunski
      try
359
        {
360
        is.close();
361
        }
362
      catch(IOException e)
363
        {
364 48c88f57 Leszek Koltunski
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
365 1af8e143 Leszek Koltunski
        }
366 42aa970f Leszek Koltunski
      }
367 48c88f57 Leszek Koltunski
368 39b925d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
369
370 6991ece5 Leszek Koltunski
    MeshBase getMesh()
371 39b925d5 Leszek Koltunski
      {
372
      return mMesh;
373
      }
374
375 48c88f57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
376
377
    long getTime()
378
      {
379
      return mTime;
380
      }
381
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383
384
    int getBytes()
385
      {
386 6991ece5 Leszek Koltunski
      if( mMesh instanceof MeshFile )
387
        {
388
        return ((MeshFile)mMesh).getNumBytes();
389
        }
390
391
      return 0;
392 48c88f57 Leszek Koltunski
      }
393
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395
396
    int getVertices()
397
      {
398 7198e5c9 Leszek Koltunski
      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 48c88f57 Leszek Koltunski
      }
428 42aa970f Leszek Koltunski
}