Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 7edab735

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.type.DynamicQuat;
43
import org.distorted.library.type.Static3D;
44
import org.distorted.library.type.Static4D;
45

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

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

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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

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

    
72
    Static4D mQuat1, mQuat2;
73
    int mScreenMin;
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

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

    
84
      mCurrentScale = DEFAULT_SCALE;
85

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

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

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

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

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

    
103
      mEffects.apply( disappear );
104

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

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

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

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

    
132
      VertexEffectDisappear.enable();
133

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

    
137
      DistortedLibrary.setMax(EffectType.VERTEX, 16);
138

    
139
      DistortedLibrary.onCreate(mView.getContext(), this);
140
      }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

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

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

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

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

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

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

    
176
      long t1 = System.currentTimeMillis();
177

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

    
187
      long t2 = System.currentTimeMillis();
188

    
189
      mTime = t2-t1;
190

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

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

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

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

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

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

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

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

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

    
237
      return null;
238
      }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

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

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

    
253
      return bitmap;
254
      }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  private MeshBase createStaticMesh()
259
    {
260
    CubitFactory factory = CubitFactory.getInstance();
261
    return factory.createSkewbFaceMesh();
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
    private void createMesh()
267
      {
268
      mMesh = createStaticMesh();
269

    
270
      int numEff = mMesh.numEffComponents();
271

    
272
      for(int i=0; i<numEff; i++)
273
        {
274
        mMesh.setEffectAssociation(i, 0, i);
275
        }
276
      }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
    private void openMesh(int resourceID)
281
      {
282
      Context con = mView.getContext();
283
      Resources res = con.getResources();
284
      InputStream is = res.openRawResource(resourceID);
285
      DataInputStream dos = new DataInputStream(is);
286
      mMesh = new MeshFile(dos);
287

    
288
      int numEff = mMesh.numEffComponents();
289

    
290
      for(int i=0; i<numEff; i++)
291
        {
292
        mMesh.setEffectAssociation(i, 0, i);
293
        }
294

    
295
      try
296
        {
297
        is.close();
298
        }
299
      catch(IOException e)
300
        {
301
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
302
        }
303
      }
304

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

    
307
    MeshBase getMesh()
308
      {
309
      return mMesh;
310
      }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
    long getTime()
315
      {
316
      return mTime;
317
      }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

    
321
    int getBytes()
322
      {
323
      if( mMesh instanceof MeshFile )
324
        {
325
        return ((MeshFile)mMesh).getNumBytes();
326
        }
327

    
328
      return 0;
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
}
(3-3/5)