Project

General

Profile

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

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

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.EffectType;
31
import org.distorted.library.effect.MatrixEffectQuaternion;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectDeform;
34
import org.distorted.library.effect.VertexEffectDisappear;
35
import org.distorted.library.effect.VertexEffectRotate;
36
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
import org.distorted.library.mesh.MeshBase;
41
import org.distorted.library.mesh.MeshFile;
42
import org.distorted.library.mesh.MeshPolygon;
43
import org.distorted.library.type.DynamicQuat;
44
import org.distorted.library.type.Static3D;
45
import org.distorted.library.type.Static4D;
46

    
47
import java.io.DataInputStream;
48
import java.io.IOException;
49
import java.io.InputStream;
50

    
51
import javax.microedition.khronos.egl.EGLConfig;
52
import javax.microedition.khronos.opengles.GL10;
53

    
54
import static org.distorted.examples.meshfile.MeshFileActivity.PROCEDURAL;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
59
{
60
    private static final float SQ2 = (float)Math.sqrt(2);
61
    private static final float SQ3 = (float)Math.sqrt(3);
62
    private final float DEFAULT_SCALE = 0.3f;
63

    
64
    private GLSurfaceView mView;
65
    private DistortedTexture mTexture;
66
    private DistortedScreen mScreen;
67
    private DistortedEffects mEffects;
68
    private Static3D mScale;
69
    private long mTime;
70
    private float mCurrentScale;
71
    private MeshBase mMesh;
72

    
73
    Static4D mQuat1, mQuat2;
74
    int mScreenMin;
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
    MeshFileRenderer(GLSurfaceView v)
79
      {
80
      mView = v;
81
      mScreen = new DistortedScreen();
82
      mScale= new Static3D(1,1,1);
83
      Static3D center=new Static3D(0,0,0);
84

    
85
      mCurrentScale = DEFAULT_SCALE;
86

    
87
      mQuat1 = new Static4D(0,0,0,1);
88
      mQuat2 = new Static4D(0,0,0,1);
89

    
90
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
91
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
92

    
93
      quatInt1.add(mQuat1);
94
      quatInt2.add(mQuat2);
95

    
96
      VertexEffectDisappear disappear = new VertexEffectDisappear();
97
      disappear.setMeshAssociation(1,-1);
98

    
99
      mEffects = new DistortedEffects();
100
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
101
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
102
      mEffects.apply( new MatrixEffectScale(mScale));
103

    
104
      mEffects.apply( disappear );
105

    
106
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
107
      mScreen.showFPS();
108
      }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
   
112
    public void onDrawFrame(GL10 glUnused) 
113
      {
114
      mScreen.render( System.currentTimeMillis() );
115
      }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
    
119
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
120
      {
121
      mScreenMin = Math.min(width, height);
122
      float factor = mCurrentScale*mScreenMin;
123
      mScale.set(factor,factor,factor);
124
      mScreen.resize(width, height);
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
    
129
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
130
      {
131
      if( mTexture==null ) mTexture = new DistortedTexture();
132

    
133
      VertexEffectDisappear.enable();
134
      VertexEffectRotate.enable();
135
      VertexEffectDeform.enable();
136

    
137
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
138
      }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
    public void distortedException(Exception ex)
143
      {
144
      android.util.Log.e("MeshFile", ex.getMessage() );
145
      }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
//   0 ---> 0
149
//  50 ---> DEFAULT_SCALE
150
// 100 ---> 4*DEFAULT_SCALE
151

    
152
    void setScale(int scale)
153
      {
154
      if( scale<= 50 )
155
        {
156
        mCurrentScale = DEFAULT_SCALE * scale / 50.0f;
157
        }
158
      else
159
        {
160
        mCurrentScale = DEFAULT_SCALE * ( 3*(scale/50.0f) - 2.0f);
161
        }
162

    
163
      float factor = mCurrentScale*mScreenMin;
164
      mScale.set(factor,factor,factor);
165
      }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
    void open(int resourceID)
170
      {
171
      if( mTexture==null ) mTexture = new DistortedTexture();
172
      mTexture.setTexture( createTexture(resourceID) );
173

    
174
      long t1 = System.currentTimeMillis();
175

    
176
      if( resourceID!=PROCEDURAL )
177
        {
178
        openMesh(resourceID);
179
        }
180
      else
181
        {
182
        createMesh();
183
        }
184

    
185
      long t2 = System.currentTimeMillis();
186

    
187
      mTime = t2-t1;
188

    
189
      mScreen.detachAll();
190
      mScreen.attach(mTexture,mEffects,mMesh);
191
      }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
    private Bitmap createTexture(int resourceID)
196
      {
197
      TextureFactory factory = TextureFactory.getInstance();
198

    
199
      float[] vertices;
200
      int[] colors;
201
      float F = 0.5f;
202
      float E = SQ3/2;
203
      float G = SQ2/4;
204

    
205
      switch(resourceID)
206
          {
207
          case  R.raw.deferredjob:
208
          case  R.raw.meshjoin   :
209
          case  PROCEDURAL       :
210
          case  R.raw.predeform  : return createWhiteTexture();
211

    
212
          case  R.raw.cube2      :
213
          case  R.raw.cube3      :
214
          case  R.raw.cube4      :
215
          case  R.raw.cube5      : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
216
                                   vertices = new float[] { -F,-F, +F,-F, +F,+F, -F,+F};
217
                                   return factory.createTexture(vertices,colors,0.10f, 0.10f, true);
218
          case  R.raw.pyra3      :
219
          case  R.raw.pyra4      :
220
          case  R.raw.pyra5      : colors = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
221
                                   vertices = new float[] { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
222
                                   return factory.createTexture(vertices,colors,0.05f, 0.05f, true);
223

    
224
          case  R.raw.dino       : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
225
                                   vertices = new float[] { -F,F/3, 0,-2*F/3, +F,F/3 };
226
                                   return factory.createTexture(vertices,colors,0.05f, 0.03f, true);
227

    
228
          case R.raw.skewb       : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
229
                                   //vertices = new float[] { -G,-G, +G,-G, +G,+G, -G,+G };
230

    
231
                                   vertices = new float[] { -F+F/4,F/4, F/4,-F+F/4, F/4,F/4};
232
                                   return factory.createTexture(vertices,colors,0.05f, 0.08f, true);
233
          }
234

    
235
      return null;
236
      }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
    private Bitmap createWhiteTexture()
241
      {
242
      int SIZE = 1;
243
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
244
      Canvas canvas = new Canvas(bitmap);
245

    
246
      Paint paint = new Paint();
247
      paint.setColor(0xffffff55);
248
      paint.setStyle(Paint.Style.FILL);
249
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
250

    
251
      return bitmap;
252
      }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  private MeshBase createStaticMesh()
257
    {
258
    /*
259
    MeshBase triangle = new MeshTriangle(5);
260

    
261
    VertexEffectMove move = new VertexEffectMove( new Static3D(1,0,0) );
262
    triangle.apply(move);
263

    
264
    triangle.mergeEffComponents();
265

    
266
    VertexEffectScale scale = new VertexEffectScale( new Static3D(2,1,1) );
267
    triangle.apply(scale);
268

    
269
    return triangle;
270
     */
271
/*
272
    final float DIST  = 0.2f;
273
    float[] vertices = new float[6];
274

    
275
    vertices[0] =  0.5f-DIST;
276
    vertices[1] = -0.5f-DIST;
277

    
278
    vertices[2] =  0.5f-DIST;
279
    vertices[3] =  0.5f-DIST;
280

    
281
    vertices[4] = -0.5f-DIST;
282
    vertices[5] =  0.5f-DIST;
283

    
284
    float[] bands0 = new float[] {1.0f, 0.0f, 0.5f, 0.03f, 0.0f, 0.05f};
285

    
286
    MeshBase mesh = new MeshPolygon(vertices,bands0,0,0);
287
    mesh.setShowNormals(true);
288

    
289
    return mesh;
290
    */
291

    
292
    final float IVY_D = 0.10f;
293
    final int   IVY_N = 8;
294

    
295
    final float angle = (float)Math.PI/(2*IVY_N);
296
    final float CORR  = 1.0f - IVY_D*SQ2;
297
    final float DIST  = 0.4f;
298
    final float CORR2 = 0.5f;
299
    float[] vertices = new float[2*(IVY_N+1)+6];
300

    
301
    vertices[0] = ( 0.5f      -DIST)*CORR2;
302
    vertices[1] = (-0.5f+IVY_D-DIST)*CORR2;
303
    vertices[2] = ( 0.5f      -DIST)*CORR2;
304
    vertices[3] = ( 0.5f      -DIST)*CORR2;
305
    vertices[4] = (-0.5f+IVY_D-DIST)*CORR2;
306
    vertices[5] = ( 0.5f      -DIST)*CORR2;
307

    
308
    for(int i=0; i<=IVY_N; i++)
309
      {
310
      float ang = (IVY_N-i)*angle;
311
      float sin = (float)Math.sin(ang);
312
      float cos = (float)Math.cos(ang);
313

    
314
      vertices[2*i+6] = (CORR*(cos-0.5f)-DIST)*CORR2;
315
      vertices[2*i+7] = (CORR*(sin-0.5f)-DIST)*CORR2;
316
      }
317

    
318
    float[] bands = new float[] {1.0f, 0.0f, 0.5f, 0.03f, 0.0f, 0.05f};
319

    
320
    MeshBase mesh = new MeshPolygon(vertices,bands,0,0);
321
    mesh.setShowNormals(true);
322

    
323
    return mesh;
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
    private void createMesh()
329
      {
330
      FactoryCubit factory = FactoryCubit.getInstance();
331
      mMesh = factory.createMegaminxCornerMesh();
332

    
333
      //mMesh = createStaticMesh();
334

    
335
      int numEff = mMesh.numEffComponents();
336

    
337
      for(int i=0; i<numEff; i++)
338
        {
339
        mMesh.setEffectAssociation(i, 0, i);
340
        }
341
      }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

    
345
    private void openMesh(int resourceID)
346
      {
347
      Context con = mView.getContext();
348
      Resources res = con.getResources();
349
      InputStream is = res.openRawResource(resourceID);
350
      DataInputStream dos = new DataInputStream(is);
351
      mMesh = new MeshFile(dos);
352

    
353
      int numEff = mMesh.numEffComponents();
354

    
355
      for(int i=0; i<numEff; i++)
356
        {
357
        mMesh.setEffectAssociation(i, 0, i);
358
        }
359

    
360
      try
361
        {
362
        is.close();
363
        }
364
      catch(IOException e)
365
        {
366
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
367
        }
368
      }
369

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

    
372
    MeshBase getMesh()
373
      {
374
      return mMesh;
375
      }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
    long getTime()
380
      {
381
      return mTime;
382
      }
383

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

    
386
    int getBytes()
387
      {
388
      if( mMesh instanceof MeshFile )
389
        {
390
        return ((MeshFile)mMesh).getNumBytes();
391
        }
392

    
393
      return 0;
394
      }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
    int getVertices()
399
      {
400
      return mMesh.getNumVertices();
401
      }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

    
405
    int getEndEffIndex(int component)
406
      {
407
      return mMesh.getLastVertexEff(component);
408
      }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

    
412
    int getEndTexIndex(int component)
413
      {
414
      return mMesh.getLastVertexTex(component);
415
      }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
    int getEffComponentNum()
420
      {
421
      return mMesh.numEffComponents();
422
      }
423

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
    int getTexComponentNum()
427
      {
428
      return mMesh.numTexComponents();
429
      }
430
}
(3-3/5)