Project

General

Profile

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

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

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 6991ece5 Leszek Koltunski
import org.distorted.library.effect.EffectType;
31 42aa970f Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
32
import org.distorted.library.effect.MatrixEffectScale;
33 6991ece5 Leszek Koltunski
import org.distorted.library.effect.VertexEffectDeform;
34 39b925d5 Leszek Koltunski
import org.distorted.library.effect.VertexEffectDisappear;
35 6991ece5 Leszek Koltunski
import org.distorted.library.effect.VertexEffectRotate;
36 42aa970f Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.main.DistortedTexture;
40 6991ece5 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
41 1af8e143 Leszek Koltunski
import org.distorted.library.mesh.MeshFile;
42 55f20739 Leszek Koltunski
import org.distorted.library.mesh.MeshPolygon;
43 42aa970f Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
44
import org.distorted.library.type.Static3D;
45
import org.distorted.library.type.Static4D;
46
47 1af8e143 Leszek Koltunski
import java.io.DataInputStream;
48
import java.io.IOException;
49
import java.io.InputStream;
50
51 42aa970f Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
52
import javax.microedition.khronos.opengles.GL10;
53
54 6991ece5 Leszek Koltunski
import static org.distorted.examples.meshfile.MeshFileActivity.PROCEDURAL;
55
56 42aa970f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58 061449ed Leszek Koltunski
class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
59 42aa970f Leszek Koltunski
{
60 7edab735 Leszek Koltunski
    private static final float SQ2 = (float)Math.sqrt(2);
61
    private static final float SQ3 = (float)Math.sqrt(3);
62 16b336db Leszek Koltunski
    private final float DEFAULT_SCALE = 0.3f;
63
64 42aa970f Leszek Koltunski
    private GLSurfaceView mView;
65
    private DistortedTexture mTexture;
66
    private DistortedScreen mScreen;
67
    private DistortedEffects mEffects;
68
    private Static3D mScale;
69 48c88f57 Leszek Koltunski
    private long mTime;
70 16b336db Leszek Koltunski
    private float mCurrentScale;
71 6991ece5 Leszek Koltunski
    private MeshBase mMesh;
72 42aa970f Leszek Koltunski
73
    Static4D mQuat1, mQuat2;
74
    int mScreenMin;
75
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
78
    MeshFileRenderer(GLSurfaceView v)
79
      {
80
      mView = v;
81
      mScreen = new DistortedScreen();
82
      mScale= new Static3D(1,1,1);
83
      Static3D center=new Static3D(0,0,0);
84
85 16b336db Leszek Koltunski
      mCurrentScale = DEFAULT_SCALE;
86
87 42aa970f Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);
88 fe032f52 Leszek Koltunski
      mQuat2 = new Static4D(0,0,0,1);
89 42aa970f Leszek Koltunski
90
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
91
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
92
93
      quatInt1.add(mQuat1);
94
      quatInt2.add(mQuat2);
95
96 39b925d5 Leszek Koltunski
      VertexEffectDisappear disappear = new VertexEffectDisappear();
97
      disappear.setMeshAssociation(1,-1);
98
99 42aa970f Leszek Koltunski
      mEffects = new DistortedEffects();
100
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
101
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
102
      mEffects.apply( new MatrixEffectScale(mScale));
103
104 39b925d5 Leszek Koltunski
      mEffects.apply( disappear );
105
106 42aa970f Leszek Koltunski
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
107 48c88f57 Leszek Koltunski
      mScreen.showFPS();
108 42aa970f Leszek Koltunski
      }
109
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
   
112
    public void onDrawFrame(GL10 glUnused) 
113
      {
114
      mScreen.render( System.currentTimeMillis() );
115
      }
116
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
    
119
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
120
      {
121
      mScreenMin = Math.min(width, height);
122 16b336db Leszek Koltunski
      float factor = mCurrentScale*mScreenMin;
123 42aa970f Leszek Koltunski
      mScale.set(factor,factor,factor);
124
      mScreen.resize(width, height);
125
      }
126
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
    
129
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
130
      {
131
      if( mTexture==null ) mTexture = new DistortedTexture();
132
133 39b925d5 Leszek Koltunski
      VertexEffectDisappear.enable();
134
135 6991ece5 Leszek Koltunski
      VertexEffectRotate.enable();
136
      VertexEffectDeform.enable();
137
138 7edab735 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX, 16);
139 6991ece5 Leszek Koltunski
140 7c72e798 Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
141 061449ed Leszek Koltunski
      }
142
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
145
    public void distortedException(Exception ex)
146
      {
147
      android.util.Log.e("MeshFile", ex.getMessage() );
148 42aa970f Leszek Koltunski
      }
149
150 16b336db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 42aa970f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
171
172 1af8e143 Leszek Koltunski
    void open(int resourceID)
173 42aa970f Leszek Koltunski
      {
174 099d8f8b Leszek Koltunski
      if( mTexture==null ) mTexture = new DistortedTexture();
175 8ac4c0bc Leszek Koltunski
      mTexture.setTexture( createTexture(resourceID) );
176
177 acad428e Leszek Koltunski
      long t1 = System.currentTimeMillis();
178 6991ece5 Leszek Koltunski
179
      if( resourceID!=PROCEDURAL )
180
        {
181
        openMesh(resourceID);
182
        }
183
      else
184
        {
185
        createMesh();
186
        }
187
188 acad428e Leszek Koltunski
      long t2 = System.currentTimeMillis();
189
190 48c88f57 Leszek Koltunski
      mTime = t2-t1;
191 42aa970f Leszek Koltunski
192 71c7624f Leszek Koltunski
      mScreen.detachAll();
193 7198e5c9 Leszek Koltunski
      mScreen.attach(mTexture,mEffects,mMesh);
194 42aa970f Leszek Koltunski
      }
195
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
198 1af8e143 Leszek Koltunski
    private Bitmap createTexture(int resourceID)
199 42aa970f Leszek Koltunski
      {
200 7edab735 Leszek Koltunski
      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 8ac4c0bc Leszek Koltunski
      switch(resourceID)
209
          {
210
          case  R.raw.deferredjob:
211 7edab735 Leszek Koltunski
          case  R.raw.meshjoin   :
212 1beffb69 Leszek Koltunski
          case  PROCEDURAL       :
213 7edab735 Leszek Koltunski
          case  R.raw.predeform  : return createWhiteTexture();
214
215 8ac4c0bc Leszek Koltunski
          case  R.raw.cube2      :
216
          case  R.raw.cube3      :
217
          case  R.raw.cube4      :
218 7edab735 Leszek Koltunski
          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 8ac4c0bc Leszek Koltunski
          case  R.raw.pyra3      :
222
          case  R.raw.pyra4      :
223 7edab735 Leszek Koltunski
          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 8ac4c0bc Leszek Koltunski
227 7edab735 Leszek Koltunski
          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 8ac4c0bc Leszek Koltunski
231 7edab735 Leszek Koltunski
          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 8ac4c0bc Leszek Koltunski
234 7edab735 Leszek Koltunski
                                   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 8ac4c0bc Leszek Koltunski
238 7edab735 Leszek Koltunski
      return null;
239 8ac4c0bc Leszek Koltunski
      }
240
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242
243 7edab735 Leszek Koltunski
    private Bitmap createWhiteTexture()
244 8ac4c0bc Leszek Koltunski
      {
245 7edab735 Leszek Koltunski
      int SIZE = 1;
246 8ac4c0bc Leszek Koltunski
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
247
      Canvas canvas = new Canvas(bitmap);
248
249
      Paint paint = new Paint();
250 36793c52 Leszek Koltunski
      paint.setColor(0xffffff55);
251 1beffb69 Leszek Koltunski
      paint.setStyle(Paint.Style.FILL);
252 7edab735 Leszek Koltunski
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
253 6991ece5 Leszek Koltunski
254 72059460 Leszek Koltunski
      return bitmap;
255 6991ece5 Leszek Koltunski
      }
256
257 72059460 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
258 6991ece5 Leszek Koltunski
259 72059460 Leszek Koltunski
  private MeshBase createStaticMesh()
260
    {
261 55f20739 Leszek Koltunski
    /*
262 36793c52 Leszek Koltunski
    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 55f20739 Leszek Koltunski
     */
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 6991ece5 Leszek Koltunski
    }
328
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330
331
    private void createMesh()
332
      {
333 1c41c4c9 Leszek Koltunski
      FactoryCubit factory = FactoryCubit.getInstance();
334 fe032f52 Leszek Koltunski
      mMesh = factory.createMinxCornerMesh();
335 1c41c4c9 Leszek Koltunski
336
      //mMesh = createStaticMesh();
337 6991ece5 Leszek Koltunski
338
      int numEff = mMesh.numEffComponents();
339
340
      for(int i=0; i<numEff; i++)
341
        {
342
        mMesh.setEffectAssociation(i, 0, i);
343
        }
344
      }
345 42aa970f Leszek Koltunski
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347
348 6991ece5 Leszek Koltunski
    private void openMesh(int resourceID)
349 42aa970f Leszek Koltunski
      {
350 1af8e143 Leszek Koltunski
      Context con = mView.getContext();
351
      Resources res = con.getResources();
352
      InputStream is = res.openRawResource(resourceID);
353
      DataInputStream dos = new DataInputStream(is);
354 7198e5c9 Leszek Koltunski
      mMesh = new MeshFile(dos);
355 1af8e143 Leszek Koltunski
356 39b925d5 Leszek Koltunski
      int numEff = mMesh.numEffComponents();
357
358
      for(int i=0; i<numEff; i++)
359
        {
360
        mMesh.setEffectAssociation(i, 0, i);
361
        }
362
363 1af8e143 Leszek Koltunski
      try
364
        {
365
        is.close();
366
        }
367
      catch(IOException e)
368
        {
369 48c88f57 Leszek Koltunski
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
370 1af8e143 Leszek Koltunski
        }
371 42aa970f Leszek Koltunski
      }
372 48c88f57 Leszek Koltunski
373 39b925d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
374
375 6991ece5 Leszek Koltunski
    MeshBase getMesh()
376 39b925d5 Leszek Koltunski
      {
377
      return mMesh;
378
      }
379
380 48c88f57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
381
382
    long getTime()
383
      {
384
      return mTime;
385
      }
386
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388
389
    int getBytes()
390
      {
391 6991ece5 Leszek Koltunski
      if( mMesh instanceof MeshFile )
392
        {
393
        return ((MeshFile)mMesh).getNumBytes();
394
        }
395
396
      return 0;
397 48c88f57 Leszek Koltunski
      }
398
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400
401
    int getVertices()
402
      {
403 7198e5c9 Leszek Koltunski
      return mMesh.getNumVertices();
404
      }
405
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407
408
    int getEndEffIndex(int component)
409
      {
410
      return mMesh.getLastVertexEff(component);
411
      }
412
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414
415
    int getEndTexIndex(int component)
416
      {
417
      return mMesh.getLastVertexTex(component);
418
      }
419
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421
422
    int getEffComponentNum()
423
      {
424
      return mMesh.numEffComponents();
425
      }
426
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428
429
    int getTexComponentNum()
430
      {
431
      return mMesh.numTexComponents();
432 48c88f57 Leszek Koltunski
      }
433 42aa970f Leszek Koltunski
}