Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 39b925d5

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.effect.VertexEffectDisappear;
33
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.main.DistortedTexture;
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, DistortedLibrary.ExceptionListener
52
{
53
    private final float DEFAULT_SCALE = 0.3f;
54

    
55
    private GLSurfaceView mView;
56
    private DistortedTexture mTexture;
57
    private DistortedScreen mScreen;
58
    private DistortedEffects mEffects;
59
    private Static3D mScale;
60
    private long mTime;
61
    private float mCurrentScale;
62
    private MeshFile mMesh;
63

    
64
    Static4D mQuat1, mQuat2;
65
    int mScreenMin;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

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

    
76
      mCurrentScale = DEFAULT_SCALE;
77

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

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

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

    
87
      VertexEffectDisappear disappear = new VertexEffectDisappear();
88
      disappear.setMeshAssociation(1,-1);
89

    
90
      mEffects = new DistortedEffects();
91
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
92
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
93
      mEffects.apply( new MatrixEffectScale(mScale));
94

    
95
      mEffects.apply( disappear );
96

    
97
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
98
      mScreen.showFPS();
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
   
103
    public void onDrawFrame(GL10 glUnused) 
104
      {
105
      mScreen.render( System.currentTimeMillis() );
106
      }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
111
      {
112
      mScreenMin = Math.min(width, height);
113
      float factor = mCurrentScale*mScreenMin;
114
      mScale.set(factor,factor,factor);
115
      mScreen.resize(width, height);
116
      }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
    
120
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
121
      {
122
      if( mTexture==null ) mTexture = new DistortedTexture();
123

    
124
      VertexEffectDisappear.enable();
125

    
126
      DistortedLibrary.onCreate(mView.getContext(), this);
127
      }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
    public void distortedException(Exception ex)
132
      {
133
      android.util.Log.e("MeshFile", ex.getMessage() );
134
      }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
//   0 ---> 0
138
//  50 ---> DEFAULT_SCALE
139
// 100 ---> 4*DEFAULT_SCALE
140

    
141
    void setScale(int scale)
142
      {
143
      if( scale<= 50 )
144
        {
145
        mCurrentScale = DEFAULT_SCALE * scale / 50.0f;
146
        }
147
      else
148
        {
149
        mCurrentScale = DEFAULT_SCALE * ( 3*(scale/50.0f) - 2.0f);
150
        }
151

    
152
      float factor = mCurrentScale*mScreenMin;
153
      mScale.set(factor,factor,factor);
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
    void open(int resourceID)
159
      {
160
      if( mTexture==null ) mTexture = new DistortedTexture();
161
      mTexture.setTexture( createTexture(resourceID) );
162

    
163
      long t1 = System.currentTimeMillis();
164
      createMesh(resourceID);
165
      long t2 = System.currentTimeMillis();
166

    
167
      mTime = t2-t1;
168

    
169
      mScreen.detachAll();
170
      mScreen.attach(mTexture,mEffects,mMesh);
171
      }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

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

    
206
      return null;
207
      }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
    private Bitmap createCubeTexture(int[] faceColors)
212
      {
213
      final int FACES=faceColors.length;
214
      int SIZE = 200;
215
      final float R = SIZE*0.10f;
216
      final float M = SIZE*0.05f;
217

    
218
      Bitmap bitmap;
219
      Paint paint = new Paint();
220
      bitmap = Bitmap.createBitmap( (FACES+1)*SIZE, SIZE, Bitmap.Config.ARGB_8888);
221
      Canvas canvas = new Canvas(bitmap);
222

    
223
      paint.setStyle(Paint.Style.FILL);
224
      paint.setColor(0xff000000);
225
      canvas.drawRect(0, 0, (FACES+1)*SIZE, SIZE, paint);
226

    
227
      for(int face=0; face<FACES; face++)
228
        {
229
        paint.setColor(faceColors[face]);
230
        canvas.drawRoundRect( face*SIZE+M, M, (face+1)*SIZE-M, SIZE-M, R, R, paint);
231
        }
232

    
233
      return bitmap;
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    private Bitmap createGridTexture(int lines)
239
      {
240
      int SIZE = 200;
241
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
242
      Canvas canvas = new Canvas(bitmap);
243

    
244
      Paint paint = new Paint();
245
      paint.setColor(0xff008800);
246
      paint.setStyle(Paint.Style.FILL);
247
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
248
      paint.setColor(0xffffffff);
249

    
250
      for(int i=0; i<=lines ; i++ )
251
        {
252
        int x = (SIZE*i)/lines;
253
        canvas.drawRect(x-1,   0,  x+1, SIZE, paint);
254
        canvas.drawRect(  0, x-1, SIZE,  x+1, paint);
255
        }
256

    
257
      return bitmap;
258
      }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
    private Bitmap createTetrahedrenTexture(int[] faceColors)
263
      {
264
      final int FACES=faceColors.length;
265
      int SIZE = 200;
266
      float STROKE = 0.05f*SIZE;
267
      float OFF = STROKE/2 -1;
268
      float OFF2 = 0.5f*SIZE + OFF;
269
      float HEIGHT = SIZE - OFF;
270
      float RADIUS = SIZE/12;
271
      float ARC1_H = 0.2f*SIZE;
272
      float ARC1_W = SIZE*0.5f;
273
      float ARC2_W = 0.153f*SIZE;
274
      float ARC2_H = 0.905f*SIZE;
275
      float ARC3_W = SIZE-ARC2_W;
276

    
277
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
278
      Canvas canvas = new Canvas(result);
279
      Paint paint = new Paint();
280
      paint.setAntiAlias(true);
281
      paint.setStrokeWidth(STROKE);
282

    
283
      for(int i=0; i<FACES; i++)
284
        {
285
        paint.setColor(faceColors[i]);
286
        paint.setStyle(Paint.Style.FILL);
287

    
288
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
289

    
290
        paint.setColor(0xff000000);
291
        paint.setStyle(Paint.Style.STROKE);
292

    
293
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
294
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
295
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
296

    
297
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
298
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
299
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
300
        }
301

    
302
      return result;
303
      }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
    private void createMesh(int resourceID)
308
      {
309
      Context con = mView.getContext();
310
      Resources res = con.getResources();
311
      InputStream is = res.openRawResource(resourceID);
312
      DataInputStream dos = new DataInputStream(is);
313
      mMesh = new MeshFile(dos);
314

    
315
      int numEff = mMesh.numEffComponents();
316

    
317
      for(int i=0; i<numEff; i++)
318
        {
319
        mMesh.setEffectAssociation(i, 0, i);
320
        }
321

    
322
      try
323
        {
324
        is.close();
325
        }
326
      catch(IOException e)
327
        {
328
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
329
        }
330
      }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
    MeshFile getMesh()
335
      {
336
      return mMesh;
337
      }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

    
341
    long getTime()
342
      {
343
      return mTime;
344
      }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
    int getBytes()
349
      {
350
      return mMesh.getNumBytes();
351
      }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
    int getVertices()
356
      {
357
      return mMesh.getNumVertices();
358
      }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

    
362
    int getEndEffIndex(int component)
363
      {
364
      return mMesh.getLastVertexEff(component);
365
      }
366

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368

    
369
    int getEndTexIndex(int component)
370
      {
371
      return mMesh.getLastVertexTex(component);
372
      }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375

    
376
    int getEffComponentNum()
377
      {
378
      return mMesh.numEffComponents();
379
      }
380

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382

    
383
    int getTexComponentNum()
384
      {
385
      return mMesh.numTexComponents();
386
      }
387
}
(2-2/3)