Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 4bfffdd5

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 1bb5d3b7 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 1bb5d3b7 Leszek Koltunski
import org.distorted.objectlib.helpers.FactoryCubit;
47 0dee575b Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectFaceShape;
48
import org.distorted.objectlib.helpers.ObjectShape;
49 acb2aaae Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectVertexEffects;
50 2f250195 Leszek Koltunski
import org.distorted.objectlib.main.InitData;
51 d32fe3d9 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
52 cfbd047c Leszek Koltunski
import org.distorted.objectlib.objects.*;
53 1bb5d3b7 Leszek Koltunski
54 1af8e143 Leszek Koltunski
import java.io.DataInputStream;
55
import java.io.IOException;
56
import java.io.InputStream;
57
58 42aa970f Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
59
import javax.microedition.khronos.opengles.GL10;
60
61 1bb5d3b7 Leszek Koltunski
import static org.distorted.examples.meshfile.MeshFileActivity.POLYGON;
62 6991ece5 Leszek Koltunski
import static org.distorted.examples.meshfile.MeshFileActivity.PROCEDURAL;
63 baace8e9 Leszek Koltunski
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
64 3e129d0b Leszek Koltunski
import static org.distorted.objectlib.main.TwistyObject.MODE_NORM;
65 6991ece5 Leszek Koltunski
66 42aa970f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68 dc10a48d Leszek Koltunski
class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
69 42aa970f Leszek Koltunski
{
70 7edab735 Leszek Koltunski
    private static final float SQ3 = (float)Math.sqrt(3);
71 16b336db Leszek Koltunski
    private final float DEFAULT_SCALE = 0.3f;
72
73 140f2c4e Leszek Koltunski
    private final GLSurfaceView mView;
74 dc10a48d Leszek Koltunski
    private final Resources mResources;
75 140f2c4e Leszek Koltunski
    private final DistortedScreen mScreen;
76
    private final DistortedEffects mEffects;
77
    private final Static3D mScale;
78 dc10a48d Leszek Koltunski
79
    private DistortedTexture mTexture;
80
    private MeshBase mMesh;
81 48c88f57 Leszek Koltunski
    private long mTime;
82 16b336db Leszek Koltunski
    private float mCurrentScale;
83 42aa970f Leszek Koltunski
84
    Static4D mQuat1, mQuat2;
85
    int mScreenMin;
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
89
    MeshFileRenderer(GLSurfaceView v)
90
      {
91
      mView = v;
92 dc10a48d Leszek Koltunski
      mResources = v.getResources();
93
94 42aa970f Leszek Koltunski
      mScreen = new DistortedScreen();
95
      mScale= new Static3D(1,1,1);
96
      Static3D center=new Static3D(0,0,0);
97
98 16b336db Leszek Koltunski
      mCurrentScale = DEFAULT_SCALE;
99
100 42aa970f Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);
101 fe032f52 Leszek Koltunski
      mQuat2 = new Static4D(0,0,0,1);
102 42aa970f Leszek Koltunski
103
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
104
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
105
106
      quatInt1.add(mQuat1);
107
      quatInt2.add(mQuat2);
108
109 39b925d5 Leszek Koltunski
      VertexEffectDisappear disappear = new VertexEffectDisappear();
110
      disappear.setMeshAssociation(1,-1);
111
112 42aa970f Leszek Koltunski
      mEffects = new DistortedEffects();
113
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
114
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
115
      mEffects.apply( new MatrixEffectScale(mScale));
116
117 39b925d5 Leszek Koltunski
      mEffects.apply( disappear );
118
119 42aa970f Leszek Koltunski
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
120 48c88f57 Leszek Koltunski
      mScreen.showFPS();
121 42aa970f Leszek Koltunski
      }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
   
125
    public void onDrawFrame(GL10 glUnused) 
126
      {
127
      mScreen.render( System.currentTimeMillis() );
128
      }
129
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
    
132
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
133
      {
134
      mScreenMin = Math.min(width, height);
135 16b336db Leszek Koltunski
      float factor = mCurrentScale*mScreenMin;
136 42aa970f Leszek Koltunski
      mScale.set(factor,factor,factor);
137
      mScreen.resize(width, height);
138
      }
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
    
142
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
143
      {
144
      if( mTexture==null ) mTexture = new DistortedTexture();
145
146 a65604a7 Leszek Koltunski
      Effect.enableEffects(EffectType.VERTEX);
147 04a21ed6 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX, 20);
148 ef02f5e0 Leszek Koltunski
      DistortedLibrary.needTransformFeedback();
149 dc10a48d Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(this);
150 42aa970f Leszek Koltunski
      }
151
152 16b336db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
153
//   0 ---> 0
154
//  50 ---> DEFAULT_SCALE
155
// 100 ---> 4*DEFAULT_SCALE
156
157
    void setScale(int scale)
158
      {
159
      if( scale<= 50 )
160
        {
161
        mCurrentScale = DEFAULT_SCALE * scale / 50.0f;
162
        }
163
      else
164
        {
165
        mCurrentScale = DEFAULT_SCALE * ( 3*(scale/50.0f) - 2.0f);
166
        }
167
168
      float factor = mCurrentScale*mScreenMin;
169
      mScale.set(factor,factor,factor);
170
      }
171
172 42aa970f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
173
174 1af8e143 Leszek Koltunski
    void open(int resourceID)
175 42aa970f Leszek Koltunski
      {
176 099d8f8b Leszek Koltunski
      if( mTexture==null ) mTexture = new DistortedTexture();
177 8ac4c0bc Leszek Koltunski
      mTexture.setTexture( createTexture(resourceID) );
178
179 acad428e Leszek Koltunski
      long t1 = System.currentTimeMillis();
180 6991ece5 Leszek Koltunski
181 1bb5d3b7 Leszek Koltunski
      if( resourceID==PROCEDURAL )
182 6991ece5 Leszek Koltunski
        {
183 1bb5d3b7 Leszek Koltunski
        createMesh();
184
        }
185
      else if( resourceID==POLYGON )
186
        {
187
        createPolygon();
188 6991ece5 Leszek Koltunski
        }
189
      else
190
        {
191 1bb5d3b7 Leszek Koltunski
        openMesh(resourceID);
192 6991ece5 Leszek Koltunski
        }
193
194 acad428e Leszek Koltunski
      long t2 = System.currentTimeMillis();
195
196 48c88f57 Leszek Koltunski
      mTime = t2-t1;
197 42aa970f Leszek Koltunski
198 71c7624f Leszek Koltunski
      mScreen.detachAll();
199 7198e5c9 Leszek Koltunski
      mScreen.attach(mTexture,mEffects,mMesh);
200 42aa970f Leszek Koltunski
      }
201
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203
204 1af8e143 Leszek Koltunski
    private Bitmap createTexture(int resourceID)
205 42aa970f Leszek Koltunski
      {
206 7edab735 Leszek Koltunski
      TextureFactory factory = TextureFactory.getInstance();
207
208
      float[] vertices;
209
      int[] colors;
210
      float F = 0.5f;
211
      float E = SQ3/2;
212
213 8ac4c0bc Leszek Koltunski
      switch(resourceID)
214
          {
215
          case  R.raw.deferredjob:
216 7edab735 Leszek Koltunski
          case  R.raw.meshjoin   :
217 1beffb69 Leszek Koltunski
          case  PROCEDURAL       :
218 1bb5d3b7 Leszek Koltunski
          case  POLYGON          :
219 7edab735 Leszek Koltunski
          case  R.raw.predeform  : return createWhiteTexture();
220
221 8ac4c0bc Leszek Koltunski
          case  R.raw.cube2      :
222
          case  R.raw.cube3      :
223
          case  R.raw.cube4      :
224 7edab735 Leszek Koltunski
          case  R.raw.cube5      : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
225
                                   vertices = new float[] { -F,-F, +F,-F, +F,+F, -F,+F};
226
                                   return factory.createTexture(vertices,colors,0.10f, 0.10f, true);
227 8ac4c0bc Leszek Koltunski
          case  R.raw.pyra3      :
228
          case  R.raw.pyra4      :
229 7edab735 Leszek Koltunski
          case  R.raw.pyra5      : colors = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
230
                                   vertices = new float[] { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
231
                                   return factory.createTexture(vertices,colors,0.05f, 0.05f, true);
232 8ac4c0bc Leszek Koltunski
233 7edab735 Leszek Koltunski
          case  R.raw.dino       : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
234
                                   vertices = new float[] { -F,F/3, 0,-2*F/3, +F,F/3 };
235
                                   return factory.createTexture(vertices,colors,0.05f, 0.03f, true);
236 8ac4c0bc Leszek Koltunski
237 7edab735 Leszek Koltunski
          case R.raw.skewb       : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
238
                                   //vertices = new float[] { -G,-G, +G,-G, +G,+G, -G,+G };
239 8ac4c0bc Leszek Koltunski
240 7edab735 Leszek Koltunski
                                   vertices = new float[] { -F+F/4,F/4, F/4,-F+F/4, F/4,F/4};
241
                                   return factory.createTexture(vertices,colors,0.05f, 0.08f, true);
242
          }
243 8ac4c0bc Leszek Koltunski
244 7edab735 Leszek Koltunski
      return null;
245 8ac4c0bc Leszek Koltunski
      }
246
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
249 7edab735 Leszek Koltunski
    private Bitmap createWhiteTexture()
250 8ac4c0bc Leszek Koltunski
      {
251 7edab735 Leszek Koltunski
      int SIZE = 1;
252 8ac4c0bc Leszek Koltunski
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
253
      Canvas canvas = new Canvas(bitmap);
254
255
      Paint paint = new Paint();
256 36793c52 Leszek Koltunski
      paint.setColor(0xffffff55);
257 1beffb69 Leszek Koltunski
      paint.setStyle(Paint.Style.FILL);
258 7edab735 Leszek Koltunski
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
259 6991ece5 Leszek Koltunski
260 72059460 Leszek Koltunski
      return bitmap;
261 6991ece5 Leszek Koltunski
      }
262
263 1bb5d3b7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
264
265
    private void createPolygon()
266
      {
267
      float A = 0.5f;
268 4bfffdd5 Leszek Koltunski
      float B = 0.5f;
269 1bb5d3b7 Leszek Koltunski
270 4bfffdd5 Leszek Koltunski
      int extraIndex    = 0;
271
      int extraVertices = 0;
272
      int numBands      = 3;
273 1bb5d3b7 Leszek Koltunski
274
      float[] vertices = new float[] { -A,-A, A,-A, A,A, -A,A };
275
      float[] bands = new float[2*numBands];
276
277
      for(int i=0; i<numBands; i++)
278
        {
279
        bands[2*i  ] = 1 + i/(1.0f-numBands);
280
        bands[2*i+1] = B/(numBands-1)*i;
281
        }
282
283
      mMesh = new MeshPolygon(vertices,bands,extraIndex,extraVertices);
284
      mMesh.setEffectAssociation(0,0,0);
285
      mMesh.setShowNormals(true);
286
      }
287
288 6991ece5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
289
290
    private void createMesh()
291
      {
292 d32fe3d9 Leszek Koltunski
      Static4D quat = new Static4D(0,0,0,1);
293
      Static3D move = new Static3D(0,0,0);
294
      float scale   = 1.0f;
295 cfbd047c Leszek Koltunski
      int variant   = 1;
296 6983badf Leszek Koltunski
297 2f250195 Leszek Koltunski
      InitData data = new InitData(new int[] {2,3,2});
298 82f297f2 Leszek Koltunski
      TwistyObject puzzle = new TwistyCuboid(MESH_NICE,MODE_NORM,quat,move,scale,data,null);
299 3083fab8 Leszek Koltunski
300 acb2aaae Leszek Koltunski
      ObjectShape shape           = puzzle.getObjectShape(variant);
301
      ObjectFaceShape face        = puzzle.getObjectFaceShape(variant);
302
      ObjectVertexEffects effects = puzzle.getVertexEffects(variant);
303 6983badf Leszek Koltunski
304 d32fe3d9 Leszek Koltunski
      int[][] indices = shape.getVertIndices();
305
      int numComponents = indices.length;
306
      int[] outer = new int[numComponents];
307 db9903e8 Leszek Koltunski
308 33647db9 Leszek Koltunski
      FactoryCubit factory = FactoryCubit.getInstance();
309 6983badf Leszek Koltunski
      factory.clear();
310 fa96775c Leszek Koltunski
      factory.createNewFaceTransform(shape,outer);
311 acb2aaae Leszek Koltunski
      mMesh = factory.createRoundedSolid(shape,face,effects,MESH_NICE,numComponents);
312 6983badf Leszek Koltunski
313
      int numEff = mMesh.getNumEffComponents();
314 6991ece5 Leszek Koltunski
315 d32fe3d9 Leszek Koltunski
      for(int i=0; i<numEff; i++) mMesh.setEffectAssociation(i, 0, i);
316 6991ece5 Leszek Koltunski
      }
317 42aa970f Leszek Koltunski
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319
320 6991ece5 Leszek Koltunski
    private void openMesh(int resourceID)
321 42aa970f Leszek Koltunski
      {
322 1af8e143 Leszek Koltunski
      Context con = mView.getContext();
323
      Resources res = con.getResources();
324
      InputStream is = res.openRawResource(resourceID);
325
      DataInputStream dos = new DataInputStream(is);
326 7198e5c9 Leszek Koltunski
      mMesh = new MeshFile(dos);
327 1af8e143 Leszek Koltunski
328 6983badf Leszek Koltunski
      int numEff = mMesh.getNumEffComponents();
329 39b925d5 Leszek Koltunski
330
      for(int i=0; i<numEff; i++)
331
        {
332
        mMesh.setEffectAssociation(i, 0, i);
333
        }
334
335 1af8e143 Leszek Koltunski
      try
336
        {
337
        is.close();
338
        }
339
      catch(IOException e)
340
        {
341 48c88f57 Leszek Koltunski
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
342 1af8e143 Leszek Koltunski
        }
343 42aa970f Leszek Koltunski
      }
344 48c88f57 Leszek Koltunski
345 39b925d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
346
347 6991ece5 Leszek Koltunski
    MeshBase getMesh()
348 39b925d5 Leszek Koltunski
      {
349
      return mMesh;
350
      }
351
352 48c88f57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
353
354
    long getTime()
355
      {
356
      return mTime;
357
      }
358
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360
361
    int getBytes()
362
      {
363 6991ece5 Leszek Koltunski
      if( mMesh instanceof MeshFile )
364
        {
365
        return ((MeshFile)mMesh).getNumBytes();
366
        }
367
368
      return 0;
369 48c88f57 Leszek Koltunski
      }
370
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372
373
    int getVertices()
374
      {
375 7198e5c9 Leszek Koltunski
      return mMesh.getNumVertices();
376
      }
377
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379
380
    int getEndEffIndex(int component)
381
      {
382
      return mMesh.getLastVertexEff(component);
383
      }
384
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386
387
    int getEndTexIndex(int component)
388
      {
389
      return mMesh.getLastVertexTex(component);
390
      }
391
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393
394
    int getEffComponentNum()
395
      {
396 6983badf Leszek Koltunski
      return mMesh.getNumEffComponents();
397 7198e5c9 Leszek Koltunski
      }
398
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400
401
    int getTexComponentNum()
402
      {
403 6983badf Leszek Koltunski
      return mMesh.getNumTexComponents();
404 48c88f57 Leszek Koltunski
      }
405 dc10a48d Leszek Koltunski
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407
408
    public void distortedException(Exception ex)
409
      {
410
      android.util.Log.e("MeshFile", ex.getMessage() );
411
      }
412
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414
415
    public InputStream localFile(int fileID)
416
      {
417
      return mResources.openRawResource(fileID);
418
      }
419
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421
422
    public void logMessage(String message)
423
      {
424
      android.util.Log.e("MeshFile", message );
425
      }
426 42aa970f Leszek Koltunski
}