Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import android.content.Context;
23
import android.content.res.Resources;
24
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.opengl.GLSurfaceView;
28

    
29
import org.distorted.examples.R;
30
import org.distorted.library.effect.MatrixEffectQuaternion;
31
import org.distorted.library.effect.MatrixEffectScale;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedLibrary;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.mesh.MeshBase;
37
import org.distorted.library.mesh.MeshFile;
38
import org.distorted.library.type.DynamicQuat;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41

    
42
import java.io.DataInputStream;
43
import java.io.IOException;
44
import java.io.InputStream;
45

    
46
import javax.microedition.khronos.egl.EGLConfig;
47
import javax.microedition.khronos.opengles.GL10;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class MeshFileRenderer implements GLSurfaceView.Renderer
52
{
53
    private GLSurfaceView mView;
54
    private DistortedTexture mTexture;
55
    private DistortedScreen mScreen;
56
    private DistortedEffects mEffects;
57
    private Static3D mScale;
58
    private MeshBase mMesh;
59
    private long mTime;
60
    private int mBytes;
61
    private int mVertices;
62

    
63
    Static4D mQuat1, mQuat2;
64
    int mScreenMin;
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
    MeshFileRenderer(GLSurfaceView v)
69
      {
70
      mView = v;
71
      mScreen = new DistortedScreen();
72
      mScale= new Static3D(1,1,1);
73
      Static3D center=new Static3D(0,0,0);
74

    
75
      mQuat1 = new Static4D(0,0,0,1);
76
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
77

    
78
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
79
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
80

    
81
      quatInt1.add(mQuat1);
82
      quatInt2.add(mQuat2);
83

    
84
      mEffects = new DistortedEffects();
85
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
86
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
87
      mEffects.apply( new MatrixEffectScale(mScale));
88

    
89
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
90
      mScreen.showFPS();
91
      }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
   
95
    public void onDrawFrame(GL10 glUnused) 
96
      {
97
      mScreen.render( System.currentTimeMillis() );
98
      }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
    
102
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
103
      {
104
      final float SCALE = 0.3f;
105
      mScreenMin = Math.min(width, height);
106
      float factor = SCALE*mScreenMin;
107
      mScale.set(factor,factor,factor);
108
      mScreen.resize(width, height);
109
      }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
    
113
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
114
      {
115
      if( mTexture==null ) mTexture = new DistortedTexture();
116

    
117
      try
118
        {
119
        DistortedLibrary.onCreate(mView.getContext());
120
        }
121
      catch(Exception ex)
122
        {
123
        android.util.Log.e("MeshFile", ex.getMessage() );
124
        }
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
    void open(int resourceID)
130
      {
131
      mTexture.setTexture( createTexture(resourceID) );
132

    
133
      long t1 = System.currentTimeMillis();
134
      mMesh = createMesh(resourceID);
135
      long t2 = System.currentTimeMillis();
136

    
137
      mTime = t2-t1;
138

    
139
      mScreen.detachAll();
140
      mScreen.attach(mTexture,mEffects,mMesh);
141
      }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
    private Bitmap createTexture(int resourceID)
146
      {
147
      switch(resourceID)
148
          {
149
          case  R.raw.deferredjob:
150
          case  R.raw.meshjoin   : final int[] TET_COLORS4 = new int[] { 0xffffff00,
151
                                                                         0xff00ff00,
152
                                                                         0xff0000ff,
153
                                                                         0xffff0000 };
154
                                   return createTetrahedrenTexture(TET_COLORS4);
155
          case  R.raw.predeform  : return createGridTexture(3);
156
          case  R.raw.cube2      :
157
          case  R.raw.cube3      :
158
          case  R.raw.cube4      :
159
          case  R.raw.cube5      : final int[] CUBE_COLORS = new int[] { 0xffffff00,
160
                                                                         0xffffffff,
161
                                                                         0xff0000ff,
162
                                                                         0xff00ff00,
163
                                                                         0xffff0000,
164
                                                                         0xffb5651d };
165
                                   return createCubeTexture(CUBE_COLORS);
166
          case  R.raw.pyra3      :
167
          case  R.raw.pyra4      :
168
          case  R.raw.pyra5      : final int[] TET_COLORS5 = new int[] { 0xffffff00,
169
                                                                         0xff00ff00,
170
                                                                         0xff0000ff,
171
                                                                         0xffff0000,
172
                                                                         0xff000000 };
173
                                   return createTetrahedrenTexture(TET_COLORS5);
174
          }
175

    
176
      return null;
177
      }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
    private Bitmap createCubeTexture(int[] faceColors)
182
      {
183
      final int FACES=faceColors.length;
184
      int SIZE = 200;
185
      final float R = SIZE*0.10f;
186
      final float M = SIZE*0.05f;
187

    
188
      Bitmap bitmap;
189
      Paint paint = new Paint();
190
      bitmap = Bitmap.createBitmap( (FACES+1)*SIZE, SIZE, Bitmap.Config.ARGB_8888);
191
      Canvas canvas = new Canvas(bitmap);
192

    
193
      paint.setStyle(Paint.Style.FILL);
194
      paint.setColor(0xff000000);
195
      canvas.drawRect(0, 0, (FACES+1)*SIZE, SIZE, paint);
196

    
197
      for(int face=0; face<FACES; face++)
198
        {
199
        paint.setColor(faceColors[face]);
200
        canvas.drawRoundRect( face*SIZE+M, M, (face+1)*SIZE-M, SIZE-M, R, R, paint);
201
        }
202

    
203
      return bitmap;
204
      }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
    private Bitmap createGridTexture(int lines)
209
      {
210
      int SIZE = 200;
211
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
212
      Canvas canvas = new Canvas(bitmap);
213

    
214
      Paint paint = new Paint();
215
      paint.setColor(0xff008800);
216
      paint.setStyle(Paint.Style.FILL);
217
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
218
      paint.setColor(0xffffffff);
219

    
220
      for(int i=0; i<=lines ; i++ )
221
        {
222
        int x = (SIZE*i)/lines;
223
        canvas.drawRect(x-1,   0,  x+1, SIZE, paint);
224
        canvas.drawRect(  0, x-1, SIZE,  x+1, paint);
225
        }
226

    
227
      return bitmap;
228
      }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
    private Bitmap createTetrahedrenTexture(int[] faceColors)
233
      {
234
      final int FACES=faceColors.length;
235
      int SIZE = 200;
236
      float STROKE = 0.05f*SIZE;
237
      float OFF = STROKE/2 -1;
238
      float OFF2 = 0.5f*SIZE + OFF;
239
      float HEIGHT = SIZE - OFF;
240
      float RADIUS = SIZE/12;
241
      float ARC1_H = 0.2f*SIZE;
242
      float ARC1_W = SIZE*0.5f;
243
      float ARC2_W = 0.153f*SIZE;
244
      float ARC2_H = 0.905f*SIZE;
245
      float ARC3_W = SIZE-ARC2_W;
246

    
247
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
248
      Canvas canvas = new Canvas(result);
249
      Paint paint = new Paint();
250
      paint.setAntiAlias(true);
251
      paint.setStrokeWidth(STROKE);
252

    
253
      for(int i=0; i<FACES; i++)
254
        {
255
        paint.setColor(faceColors[i]);
256
        paint.setStyle(Paint.Style.FILL);
257

    
258
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
259

    
260
        paint.setColor(0xff000000);
261
        paint.setStyle(Paint.Style.STROKE);
262

    
263
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
264
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
265
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
266

    
267
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
268
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
269
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
270
        }
271

    
272
      return result;
273
      }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
    private MeshBase createMesh(int resourceID)
278
      {
279
      Context con = mView.getContext();
280
      Resources res = con.getResources();
281
      InputStream is = res.openRawResource(resourceID);
282
      DataInputStream dos = new DataInputStream(is);
283
      MeshFile mesh = new MeshFile(dos);
284

    
285
      mBytes = mesh.getNumBytes();
286
      mVertices = mesh.getNumVertices();
287

    
288
      try
289
        {
290
        is.close();
291
        }
292
      catch(IOException e)
293
        {
294
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
295
        }
296

    
297
      return mesh;
298
      }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
    long getTime()
303
      {
304
      return mTime;
305
      }
306

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308

    
309
    int getBytes()
310
      {
311
      return mBytes;
312
      }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
    int getVertices()
317
      {
318
      return mVertices;
319
      }
320
}
(2-2/3)