Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 55f20739

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
import org.distorted.examples.meshfile.FactoryCubit;
47

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

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

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

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

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

    
74
    Static4D mQuat1, mQuat2;
75
    int mScreenMin;
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

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

    
86
      mCurrentScale = DEFAULT_SCALE;
87

    
88
      mQuat1 = new Static4D(0,0,0,1);
89
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
90

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

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

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

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

    
105
      mEffects.apply( disappear );
106

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

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

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

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

    
134
      VertexEffectDisappear.enable();
135

    
136
      VertexEffectRotate.enable();
137
      VertexEffectDeform.enable();
138

    
139
      DistortedLibrary.setMax(EffectType.VERTEX, 16);
140

    
141
      DistortedLibrary.onCreate(mView.getContext(), this);
142
      }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
    public void distortedException(Exception ex)
147
      {
148
      android.util.Log.e("MeshFile", ex.getMessage() );
149
      }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
//   0 ---> 0
153
//  50 ---> DEFAULT_SCALE
154
// 100 ---> 4*DEFAULT_SCALE
155

    
156
    void setScale(int scale)
157
      {
158
      if( scale<= 50 )
159
        {
160
        mCurrentScale = DEFAULT_SCALE * scale / 50.0f;
161
        }
162
      else
163
        {
164
        mCurrentScale = DEFAULT_SCALE * ( 3*(scale/50.0f) - 2.0f);
165
        }
166

    
167
      float factor = mCurrentScale*mScreenMin;
168
      mScale.set(factor,factor,factor);
169
      }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
    void open(int resourceID)
174
      {
175
      if( mTexture==null ) mTexture = new DistortedTexture();
176
      mTexture.setTexture( createTexture(resourceID) );
177

    
178
      long t1 = System.currentTimeMillis();
179

    
180
      if( resourceID!=PROCEDURAL )
181
        {
182
        openMesh(resourceID);
183
        }
184
      else
185
        {
186
        createMesh();
187
        }
188

    
189
      long t2 = System.currentTimeMillis();
190

    
191
      mTime = t2-t1;
192

    
193
      mScreen.detachAll();
194
      mScreen.attach(mTexture,mEffects,mMesh);
195
      }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
    private Bitmap createTexture(int resourceID)
200
      {
201
      TextureFactory factory = TextureFactory.getInstance();
202

    
203
      float[] vertices;
204
      int[] colors;
205
      float F = 0.5f;
206
      float E = SQ3/2;
207
      float G = SQ2/4;
208

    
209
      switch(resourceID)
210
          {
211
          case  R.raw.deferredjob:
212
          case  R.raw.meshjoin   :
213
          case  PROCEDURAL       :
214
          case  R.raw.predeform  : return createWhiteTexture();
215

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

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

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

    
235
                                   vertices = new float[] { -F+F/4,F/4, F/4,-F+F/4, F/4,F/4};
236
                                   return factory.createTexture(vertices,colors,0.05f, 0.08f, true);
237
          }
238

    
239
      return null;
240
      }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
    private Bitmap createWhiteTexture()
245
      {
246
      int SIZE = 1;
247
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
248
      Canvas canvas = new Canvas(bitmap);
249

    
250
      Paint paint = new Paint();
251
      paint.setColor(0xffffff55);
252
      paint.setStyle(Paint.Style.FILL);
253
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
254

    
255
      return bitmap;
256
      }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  private MeshBase createStaticMesh()
261
    {
262
    /*
263
    MeshBase triangle = new MeshTriangle(5);
264

    
265
    VertexEffectMove move = new VertexEffectMove( new Static3D(1,0,0) );
266
    triangle.apply(move);
267

    
268
    triangle.mergeEffComponents();
269

    
270
    VertexEffectScale scale = new VertexEffectScale( new Static3D(2,1,1) );
271
    triangle.apply(scale);
272

    
273
    return triangle;
274
     */
275
/*
276
    final float DIST  = 0.2f;
277
    float[] vertices = new float[6];
278

    
279
    vertices[0] =  0.5f-DIST;
280
    vertices[1] = -0.5f-DIST;
281

    
282
    vertices[2] =  0.5f-DIST;
283
    vertices[3] =  0.5f-DIST;
284

    
285
    vertices[4] = -0.5f-DIST;
286
    vertices[5] =  0.5f-DIST;
287

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

    
290
    MeshBase mesh = new MeshPolygon(vertices,bands0,0,0);
291
    mesh.setShowNormals(true);
292

    
293
    return mesh;
294
    */
295

    
296
    final float IVY_D = 0.10f;
297
    final int   IVY_N = 8;
298

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

    
305
    vertices[0] = ( 0.5f      -DIST)*CORR2;
306
    vertices[1] = (-0.5f+IVY_D-DIST)*CORR2;
307
    vertices[2] = ( 0.5f      -DIST)*CORR2;
308
    vertices[3] = ( 0.5f      -DIST)*CORR2;
309
    vertices[4] = (-0.5f+IVY_D-DIST)*CORR2;
310
    vertices[5] = ( 0.5f      -DIST)*CORR2;
311

    
312
    for(int i=0; i<=IVY_N; i++)
313
      {
314
      float ang = (IVY_N-i)*angle;
315
      float sin = (float)Math.sin(ang);
316
      float cos = (float)Math.cos(ang);
317

    
318
      vertices[2*i+6] = (CORR*(cos-0.5f)-DIST)*CORR2;
319
      vertices[2*i+7] = (CORR*(sin-0.5f)-DIST)*CORR2;
320
      }
321

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

    
324
    MeshBase mesh = new MeshPolygon(vertices,bands,0,0);
325
    mesh.setShowNormals(true);
326

    
327
    return mesh;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
    private void createMesh()
333
      {
334
      mMesh = createStaticMesh();
335

    
336
      int numEff = mMesh.numEffComponents();
337

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

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

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

    
354
      int numEff = mMesh.numEffComponents();
355

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

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

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

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

    
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

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

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

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

    
394
      return 0;
395
      }
396

    
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398

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

    
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405

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

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412

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

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

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

    
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426

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