Project

General

Profile

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

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

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.Effect;
31
import org.distorted.library.effect.EffectType;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffect;
35
import org.distorted.library.effect.VertexEffectDeform;
36
import org.distorted.library.effect.VertexEffectDisappear;
37
import org.distorted.library.effect.VertexEffectRotate;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.main.DistortedLibrary;
40
import org.distorted.library.main.DistortedScreen;
41
import org.distorted.library.main.DistortedTexture;
42
import org.distorted.library.mesh.MeshBase;
43
import org.distorted.library.mesh.MeshFile;
44
import org.distorted.library.mesh.MeshPolygon;
45
import org.distorted.library.type.DynamicQuat;
46
import org.distorted.library.type.Static3D;
47
import org.distorted.library.type.Static4D;
48

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

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

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

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

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

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

    
75
    Static4D mQuat1, mQuat2;
76
    int mScreenMin;
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

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

    
87
      mCurrentScale = DEFAULT_SCALE;
88

    
89
      mQuat1 = new Static4D(0,0,0,1);
90
      mQuat2 = new Static4D(0,0,0,1);
91

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

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

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

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

    
106
      mEffects.apply( disappear );
107

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

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

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

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

    
135
      Effect.enableEffects(EffectType.VERTEX);
136
      DistortedLibrary.setMax(EffectType.VERTEX, 10);
137
      DistortedLibrary.needTransformFeedback();
138
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
139
      }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

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

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

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

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

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

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

    
175
      long t1 = System.currentTimeMillis();
176

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

    
186
      long t2 = System.currentTimeMillis();
187

    
188
      mTime = t2-t1;
189

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

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

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

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

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

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

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

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

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

    
236
      return null;
237
      }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

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

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

    
252
      return bitmap;
253
      }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

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

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

    
265
    triangle.mergeEffComponents();
266

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

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

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

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

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

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

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

    
290
    return mesh;
291
    */
292

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

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

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

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

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

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

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

    
324
    return mesh;
325
    }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
    private void createMesh()
330
      {
331
      int[] dimensions = new int[] {1,2,3};
332

    
333
      FactoryCubit factory = FactoryCubit.getInstance();
334
      mMesh = factory.createCuboidMesh(dimensions);
335

    
336
      //mMesh = createStaticMesh();
337

    
338
      int numEff = mMesh.numEffComponents();
339

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

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

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

    
356
      int numEff = mMesh.numEffComponents();
357

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

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

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374

    
375
    MeshBase getMesh()
376
      {
377
      return mMesh;
378
      }
379

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

    
382
    long getTime()
383
      {
384
      return mTime;
385
      }
386

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

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

    
396
      return 0;
397
      }
398

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

    
401
    int getVertices()
402
      {
403
      return mMesh.getNumVertices();
404
      }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
    int getEndEffIndex(int component)
409
      {
410
      return mMesh.getLastVertexEff(component);
411
      }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
    int getEndTexIndex(int component)
416
      {
417
      return mMesh.getLastVertexTex(component);
418
      }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

    
422
    int getEffComponentNum()
423
      {
424
      return mMesh.numEffComponents();
425
      }
426

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

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