Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 7198e5c9

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.MeshFile;
37
import org.distorted.library.type.DynamicQuat;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40

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

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
51
{
52
    private final float DEFAULT_SCALE = 0.3f;
53

    
54
    private GLSurfaceView mView;
55
    private DistortedTexture mTexture;
56
    private DistortedScreen mScreen;
57
    private DistortedEffects mEffects;
58
    private Static3D mScale;
59
    private long mTime;
60
    private float mCurrentScale;
61
    private MeshFile mMesh;
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
      mCurrentScale = DEFAULT_SCALE;
76

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

    
80
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
81
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
82

    
83
      quatInt1.add(mQuat1);
84
      quatInt2.add(mQuat2);
85

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

    
91
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
92
      mScreen.showFPS();
93
      }
94

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

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

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

    
118
      DistortedLibrary.onCreate(mView.getContext(), this);
119
      }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
    public void distortedException(Exception ex)
124
      {
125
      android.util.Log.e("MeshFile", ex.getMessage() );
126
      }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
//   0 ---> 0
130
//  50 ---> DEFAULT_SCALE
131
// 100 ---> 4*DEFAULT_SCALE
132

    
133
    void setScale(int scale)
134
      {
135
      if( scale<= 50 )
136
        {
137
        mCurrentScale = DEFAULT_SCALE * scale / 50.0f;
138
        }
139
      else
140
        {
141
        mCurrentScale = DEFAULT_SCALE * ( 3*(scale/50.0f) - 2.0f);
142
        }
143

    
144
      float factor = mCurrentScale*mScreenMin;
145
      mScale.set(factor,factor,factor);
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
    void open(int resourceID)
151
      {
152
      if( mTexture==null ) mTexture = new DistortedTexture();
153
      mTexture.setTexture( createTexture(resourceID) );
154

    
155
      long t1 = System.currentTimeMillis();
156
      createMesh(resourceID);
157
      long t2 = System.currentTimeMillis();
158

    
159
      mTime = t2-t1;
160

    
161
      mScreen.detachAll();
162
      mScreen.attach(mTexture,mEffects,mMesh);
163
      }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
    private Bitmap createTexture(int resourceID)
168
      {
169
      switch(resourceID)
170
          {
171
          case  R.raw.deferredjob:
172
          case  R.raw.meshjoin   : final int[] TET_COLORS4 = new int[] { 0xffffff00,
173
                                                                         0xff00ff00,
174
                                                                         0xff0000ff,
175
                                                                         0xffff0000 };
176
                                   return createTetrahedrenTexture(TET_COLORS4);
177
          case  R.raw.predeform  : return createGridTexture(3);
178
          case  R.raw.cube2      :
179
          case  R.raw.cube3      :
180
          case  R.raw.cube4      :
181
          case  R.raw.cube5      : final int[] CUBE_COLORS = new int[] { 0xffffff00,
182
                                                                         0xffffffff,
183
                                                                         0xff0000ff,
184
                                                                         0xff00ff00,
185
                                                                         0xffff0000,
186
                                                                         0xffb5651d };
187
                                   return createCubeTexture(CUBE_COLORS);
188
          case  R.raw.pyra3      :
189
          case  R.raw.pyra4      :
190
          case  R.raw.pyra5      : final int[] TET_COLORS5 = new int[] { 0xffffff00,
191
                                                                         0xff00ff00,
192
                                                                         0xff0000ff,
193
                                                                         0xffff0000,
194
                                                                         0xff000000 };
195
                                   return createTetrahedrenTexture(TET_COLORS5);
196
          }
197

    
198
      return null;
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
    private Bitmap createCubeTexture(int[] faceColors)
204
      {
205
      final int FACES=faceColors.length;
206
      int SIZE = 200;
207
      final float R = SIZE*0.10f;
208
      final float M = SIZE*0.05f;
209

    
210
      Bitmap bitmap;
211
      Paint paint = new Paint();
212
      bitmap = Bitmap.createBitmap( (FACES+1)*SIZE, SIZE, Bitmap.Config.ARGB_8888);
213
      Canvas canvas = new Canvas(bitmap);
214

    
215
      paint.setStyle(Paint.Style.FILL);
216
      paint.setColor(0xff000000);
217
      canvas.drawRect(0, 0, (FACES+1)*SIZE, SIZE, paint);
218

    
219
      for(int face=0; face<FACES; face++)
220
        {
221
        paint.setColor(faceColors[face]);
222
        canvas.drawRoundRect( face*SIZE+M, M, (face+1)*SIZE-M, SIZE-M, R, R, paint);
223
        }
224

    
225
      return bitmap;
226
      }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
    private Bitmap createGridTexture(int lines)
231
      {
232
      int SIZE = 200;
233
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
234
      Canvas canvas = new Canvas(bitmap);
235

    
236
      Paint paint = new Paint();
237
      paint.setColor(0xff008800);
238
      paint.setStyle(Paint.Style.FILL);
239
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
240
      paint.setColor(0xffffffff);
241

    
242
      for(int i=0; i<=lines ; i++ )
243
        {
244
        int x = (SIZE*i)/lines;
245
        canvas.drawRect(x-1,   0,  x+1, SIZE, paint);
246
        canvas.drawRect(  0, x-1, SIZE,  x+1, paint);
247
        }
248

    
249
      return bitmap;
250
      }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
    private Bitmap createTetrahedrenTexture(int[] faceColors)
255
      {
256
      final int FACES=faceColors.length;
257
      int SIZE = 200;
258
      float STROKE = 0.05f*SIZE;
259
      float OFF = STROKE/2 -1;
260
      float OFF2 = 0.5f*SIZE + OFF;
261
      float HEIGHT = SIZE - OFF;
262
      float RADIUS = SIZE/12;
263
      float ARC1_H = 0.2f*SIZE;
264
      float ARC1_W = SIZE*0.5f;
265
      float ARC2_W = 0.153f*SIZE;
266
      float ARC2_H = 0.905f*SIZE;
267
      float ARC3_W = SIZE-ARC2_W;
268

    
269
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
270
      Canvas canvas = new Canvas(result);
271
      Paint paint = new Paint();
272
      paint.setAntiAlias(true);
273
      paint.setStrokeWidth(STROKE);
274

    
275
      for(int i=0; i<FACES; i++)
276
        {
277
        paint.setColor(faceColors[i]);
278
        paint.setStyle(Paint.Style.FILL);
279

    
280
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
281

    
282
        paint.setColor(0xff000000);
283
        paint.setStyle(Paint.Style.STROKE);
284

    
285
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
286
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
287
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
288

    
289
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
290
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
291
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
292
        }
293

    
294
      return result;
295
      }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
    private void createMesh(int resourceID)
300
      {
301
      Context con = mView.getContext();
302
      Resources res = con.getResources();
303
      InputStream is = res.openRawResource(resourceID);
304
      DataInputStream dos = new DataInputStream(is);
305
      mMesh = new MeshFile(dos);
306

    
307
      try
308
        {
309
        is.close();
310
        }
311
      catch(IOException e)
312
        {
313
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
314
        }
315
      }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
    long getTime()
320
      {
321
      return mTime;
322
      }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
    int getBytes()
327
      {
328
      return mMesh.getNumBytes();
329
      }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
    int getVertices()
334
      {
335
      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
      }
365
}
(2-2/3)