Project

General

Profile

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

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

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