Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 7edab735

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 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 42aa970f Leszek Koltunski
    private GLSurfaceView mView;
64
    private DistortedTexture mTexture;
65
    private DistortedScreen mScreen;
66
    private DistortedEffects mEffects;
67
    private 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
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
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 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 39b925d5 Leszek Koltunski
      VertexEffectDisappear.enable();
133
134 6991ece5 Leszek Koltunski
      VertexEffectRotate.enable();
135
      VertexEffectDeform.enable();
136
137 7edab735 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX, 16);
138 6991ece5 Leszek Koltunski
139 061449ed Leszek Koltunski
      DistortedLibrary.onCreate(mView.getContext(), this);
140
      }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
144
    public void distortedException(Exception ex)
145
      {
146
      android.util.Log.e("MeshFile", ex.getMessage() );
147 42aa970f Leszek Koltunski
      }
148
149 16b336db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
150
//   0 ---> 0
151
//  50 ---> DEFAULT_SCALE
152
// 100 ---> 4*DEFAULT_SCALE
153
154
    void setScale(int scale)
155
      {
156
      if( scale<= 50 )
157
        {
158
        mCurrentScale = DEFAULT_SCALE * scale / 50.0f;
159
        }
160
      else
161
        {
162
        mCurrentScale = DEFAULT_SCALE * ( 3*(scale/50.0f) - 2.0f);
163
        }
164
165
      float factor = mCurrentScale*mScreenMin;
166
      mScale.set(factor,factor,factor);
167
      }
168
169 42aa970f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
170
171 1af8e143 Leszek Koltunski
    void open(int resourceID)
172 42aa970f Leszek Koltunski
      {
173 099d8f8b Leszek Koltunski
      if( mTexture==null ) mTexture = new DistortedTexture();
174 8ac4c0bc Leszek Koltunski
      mTexture.setTexture( createTexture(resourceID) );
175
176 acad428e Leszek Koltunski
      long t1 = System.currentTimeMillis();
177 6991ece5 Leszek Koltunski
178
      if( resourceID!=PROCEDURAL )
179
        {
180
        openMesh(resourceID);
181
        }
182
      else
183
        {
184
        createMesh();
185
        }
186
187 acad428e Leszek Koltunski
      long t2 = System.currentTimeMillis();
188
189 48c88f57 Leszek Koltunski
      mTime = t2-t1;
190 42aa970f Leszek Koltunski
191 71c7624f Leszek Koltunski
      mScreen.detachAll();
192 7198e5c9 Leszek Koltunski
      mScreen.attach(mTexture,mEffects,mMesh);
193 42aa970f Leszek Koltunski
      }
194
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
197 1af8e143 Leszek Koltunski
    private Bitmap createTexture(int resourceID)
198 42aa970f Leszek Koltunski
      {
199 7edab735 Leszek Koltunski
      TextureFactory factory = TextureFactory.getInstance();
200
201
      float[] vertices;
202
      int[] colors;
203
      float F = 0.5f;
204
      float E = SQ3/2;
205
      float G = SQ2/4;
206
207 8ac4c0bc Leszek Koltunski
      switch(resourceID)
208
          {
209
          case  R.raw.deferredjob:
210 7edab735 Leszek Koltunski
          case  R.raw.meshjoin   :
211 1beffb69 Leszek Koltunski
          case  PROCEDURAL       :
212 7edab735 Leszek Koltunski
          case  R.raw.predeform  : return createWhiteTexture();
213
214 8ac4c0bc Leszek Koltunski
          case  R.raw.cube2      :
215
          case  R.raw.cube3      :
216
          case  R.raw.cube4      :
217 7edab735 Leszek Koltunski
          case  R.raw.cube5      : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
218
                                   vertices = new float[] { -F,-F, +F,-F, +F,+F, -F,+F};
219
                                   return factory.createTexture(vertices,colors,0.10f, 0.10f, true);
220 8ac4c0bc Leszek Koltunski
          case  R.raw.pyra3      :
221
          case  R.raw.pyra4      :
222 7edab735 Leszek Koltunski
          case  R.raw.pyra5      : colors = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
223
                                   vertices = new float[] { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
224
                                   return factory.createTexture(vertices,colors,0.05f, 0.05f, true);
225 8ac4c0bc Leszek Koltunski
226 7edab735 Leszek Koltunski
          case  R.raw.dino       : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
227
                                   vertices = new float[] { -F,F/3, 0,-2*F/3, +F,F/3 };
228
                                   return factory.createTexture(vertices,colors,0.05f, 0.03f, true);
229 8ac4c0bc Leszek Koltunski
230 7edab735 Leszek Koltunski
          case R.raw.skewb       : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
231
                                   //vertices = new float[] { -G,-G, +G,-G, +G,+G, -G,+G };
232 8ac4c0bc Leszek Koltunski
233 7edab735 Leszek Koltunski
                                   vertices = new float[] { -F+F/4,F/4, F/4,-F+F/4, F/4,F/4};
234
                                   return factory.createTexture(vertices,colors,0.05f, 0.08f, true);
235
          }
236 8ac4c0bc Leszek Koltunski
237 7edab735 Leszek Koltunski
      return null;
238 8ac4c0bc Leszek Koltunski
      }
239
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
242 7edab735 Leszek Koltunski
    private Bitmap createWhiteTexture()
243 8ac4c0bc Leszek Koltunski
      {
244 7edab735 Leszek Koltunski
      int SIZE = 1;
245 8ac4c0bc Leszek Koltunski
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
246
      Canvas canvas = new Canvas(bitmap);
247
248
      Paint paint = new Paint();
249
      paint.setColor(0xffffffff);
250 1beffb69 Leszek Koltunski
      paint.setStyle(Paint.Style.FILL);
251 7edab735 Leszek Koltunski
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
252 6991ece5 Leszek Koltunski
253 72059460 Leszek Koltunski
      return bitmap;
254 6991ece5 Leszek Koltunski
      }
255
256 72059460 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
257 6991ece5 Leszek Koltunski
258 72059460 Leszek Koltunski
  private MeshBase createStaticMesh()
259
    {
260 7edab735 Leszek Koltunski
    CubitFactory factory = CubitFactory.getInstance();
261
    return factory.createSkewbFaceMesh();
262 6991ece5 Leszek Koltunski
    }
263
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
266
    private void createMesh()
267
      {
268
      mMesh = createStaticMesh();
269
270
      int numEff = mMesh.numEffComponents();
271
272
      for(int i=0; i<numEff; i++)
273
        {
274
        mMesh.setEffectAssociation(i, 0, i);
275
        }
276
      }
277 42aa970f Leszek Koltunski
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279
280 6991ece5 Leszek Koltunski
    private void openMesh(int resourceID)
281 42aa970f Leszek Koltunski
      {
282 1af8e143 Leszek Koltunski
      Context con = mView.getContext();
283
      Resources res = con.getResources();
284
      InputStream is = res.openRawResource(resourceID);
285
      DataInputStream dos = new DataInputStream(is);
286 7198e5c9 Leszek Koltunski
      mMesh = new MeshFile(dos);
287 1af8e143 Leszek Koltunski
288 39b925d5 Leszek Koltunski
      int numEff = mMesh.numEffComponents();
289
290
      for(int i=0; i<numEff; i++)
291
        {
292
        mMesh.setEffectAssociation(i, 0, i);
293
        }
294
295 1af8e143 Leszek Koltunski
      try
296
        {
297
        is.close();
298
        }
299
      catch(IOException e)
300
        {
301 48c88f57 Leszek Koltunski
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
302 1af8e143 Leszek Koltunski
        }
303 42aa970f Leszek Koltunski
      }
304 48c88f57 Leszek Koltunski
305 39b925d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
306
307 6991ece5 Leszek Koltunski
    MeshBase getMesh()
308 39b925d5 Leszek Koltunski
      {
309
      return mMesh;
310
      }
311
312 48c88f57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
313
314
    long getTime()
315
      {
316
      return mTime;
317
      }
318
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320
321
    int getBytes()
322
      {
323 6991ece5 Leszek Koltunski
      if( mMesh instanceof MeshFile )
324
        {
325
        return ((MeshFile)mMesh).getNumBytes();
326
        }
327
328
      return 0;
329 48c88f57 Leszek Koltunski
      }
330
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332
333
    int getVertices()
334
      {
335 7198e5c9 Leszek Koltunski
      return mMesh.getNumVertices();
336
      }
337
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339
340
    int getEndEffIndex(int component)
341
      {
342
      return mMesh.getLastVertexEff(component);
343
      }
344
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346
347
    int getEndTexIndex(int component)
348
      {
349
      return mMesh.getLastVertexTex(component);
350
      }
351
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353
354
    int getEffComponentNum()
355
      {
356
      return mMesh.numEffComponents();
357
      }
358
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360
361
    int getTexComponentNum()
362
      {
363
      return mMesh.numTexComponents();
364 48c88f57 Leszek Koltunski
      }
365 42aa970f Leszek Koltunski
}