Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 82f297f2

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.VertexEffectDisappear;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedLibrary;
37
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.mesh.MeshBase;
40
import org.distorted.library.mesh.MeshFile;
41
import org.distorted.library.mesh.MeshPolygon;
42
import org.distorted.library.type.DynamicQuat;
43
import org.distorted.library.type.Static3D;
44
import org.distorted.library.type.Static4D;
45

    
46
import org.distorted.objectlib.helpers.FactoryCubit;
47
import org.distorted.objectlib.helpers.ObjectFaceShape;
48
import org.distorted.objectlib.helpers.ObjectShape;
49
import org.distorted.objectlib.helpers.ObjectVertexEffects;
50
import org.distorted.objectlib.main.InitData;
51
import org.distorted.objectlib.main.TwistyObject;
52
import org.distorted.objectlib.objects.*;
53

    
54
import java.io.DataInputStream;
55
import java.io.IOException;
56
import java.io.InputStream;
57

    
58
import javax.microedition.khronos.egl.EGLConfig;
59
import javax.microedition.khronos.opengles.GL10;
60

    
61
import static org.distorted.examples.meshfile.MeshFileActivity.POLYGON;
62
import static org.distorted.examples.meshfile.MeshFileActivity.PROCEDURAL;
63
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
64
import static org.distorted.objectlib.main.TwistyObject.MODE_NORM;
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
69
{
70
    private static final float SQ3 = (float)Math.sqrt(3);
71
    private final float DEFAULT_SCALE = 0.3f;
72

    
73
    private final GLSurfaceView mView;
74
    private DistortedTexture mTexture;
75
    private final DistortedScreen mScreen;
76
    private final DistortedEffects mEffects;
77
    private final Static3D mScale;
78
    private long mTime;
79
    private float mCurrentScale;
80
    private MeshBase mMesh;
81

    
82
    Static4D mQuat1, mQuat2;
83
    int mScreenMin;
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
    MeshFileRenderer(GLSurfaceView v)
88
      {
89
      mView = v;
90
      mScreen = new DistortedScreen();
91
      mScale= new Static3D(1,1,1);
92
      Static3D center=new Static3D(0,0,0);
93

    
94
      mCurrentScale = DEFAULT_SCALE;
95

    
96
      mQuat1 = new Static4D(0,0,0,1);
97
      mQuat2 = new Static4D(0,0,0,1);
98

    
99
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
100
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
101

    
102
      quatInt1.add(mQuat1);
103
      quatInt2.add(mQuat2);
104

    
105
      VertexEffectDisappear disappear = new VertexEffectDisappear();
106
      disappear.setMeshAssociation(1,-1);
107

    
108
      mEffects = new DistortedEffects();
109
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
110
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
111
      mEffects.apply( new MatrixEffectScale(mScale));
112

    
113
      mEffects.apply( disappear );
114

    
115
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
116
      mScreen.showFPS();
117
      }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
   
121
    public void onDrawFrame(GL10 glUnused) 
122
      {
123
      mScreen.render( System.currentTimeMillis() );
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
129
      {
130
      mScreenMin = Math.min(width, height);
131
      float factor = mCurrentScale*mScreenMin;
132
      mScale.set(factor,factor,factor);
133
      mScreen.resize(width, height);
134
      }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
    
138
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
139
      {
140
      if( mTexture==null ) mTexture = new DistortedTexture();
141

    
142
      Effect.enableEffects(EffectType.VERTEX);
143
      DistortedLibrary.setMax(EffectType.VERTEX, 20);
144
      DistortedLibrary.needTransformFeedback();
145
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
    public void distortedException(Exception ex)
151
      {
152
      android.util.Log.e("MeshFile", ex.getMessage() );
153
      }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
//   0 ---> 0
157
//  50 ---> DEFAULT_SCALE
158
// 100 ---> 4*DEFAULT_SCALE
159

    
160
    void setScale(int scale)
161
      {
162
      if( scale<= 50 )
163
        {
164
        mCurrentScale = DEFAULT_SCALE * scale / 50.0f;
165
        }
166
      else
167
        {
168
        mCurrentScale = DEFAULT_SCALE * ( 3*(scale/50.0f) - 2.0f);
169
        }
170

    
171
      float factor = mCurrentScale*mScreenMin;
172
      mScale.set(factor,factor,factor);
173
      }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
    void open(int resourceID)
178
      {
179
      if( mTexture==null ) mTexture = new DistortedTexture();
180
      mTexture.setTexture( createTexture(resourceID) );
181

    
182
      long t1 = System.currentTimeMillis();
183

    
184
      if( resourceID==PROCEDURAL )
185
        {
186
        createMesh();
187
        }
188
      else if( resourceID==POLYGON )
189
        {
190
        createPolygon();
191
        }
192
      else
193
        {
194
        openMesh(resourceID);
195
        }
196

    
197
      long t2 = System.currentTimeMillis();
198

    
199
      mTime = t2-t1;
200

    
201
      mScreen.detachAll();
202
      mScreen.attach(mTexture,mEffects,mMesh);
203
      }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
    private Bitmap createTexture(int resourceID)
208
      {
209
      TextureFactory factory = TextureFactory.getInstance();
210

    
211
      float[] vertices;
212
      int[] colors;
213
      float F = 0.5f;
214
      float E = SQ3/2;
215

    
216
      switch(resourceID)
217
          {
218
          case  R.raw.deferredjob:
219
          case  R.raw.meshjoin   :
220
          case  PROCEDURAL       :
221
          case  POLYGON          :
222
          case  R.raw.predeform  : return createWhiteTexture();
223

    
224
          case  R.raw.cube2      :
225
          case  R.raw.cube3      :
226
          case  R.raw.cube4      :
227
          case  R.raw.cube5      : colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
228
                                   vertices = new float[] { -F,-F, +F,-F, +F,+F, -F,+F};
229
                                   return factory.createTexture(vertices,colors,0.10f, 0.10f, true);
230
          case  R.raw.pyra3      :
231
          case  R.raw.pyra4      :
232
          case  R.raw.pyra5      : colors = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
233
                                   vertices = new float[] { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
234
                                   return factory.createTexture(vertices,colors,0.05f, 0.05f, true);
235

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

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

    
243
                                   vertices = new float[] { -F+F/4,F/4, F/4,-F+F/4, F/4,F/4};
244
                                   return factory.createTexture(vertices,colors,0.05f, 0.08f, true);
245
          }
246

    
247
      return null;
248
      }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
    private Bitmap createWhiteTexture()
253
      {
254
      int SIZE = 1;
255
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
256
      Canvas canvas = new Canvas(bitmap);
257

    
258
      Paint paint = new Paint();
259
      paint.setColor(0xffffff55);
260
      paint.setStyle(Paint.Style.FILL);
261
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
262

    
263
      return bitmap;
264
      }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
    private void createPolygon()
269
      {
270
      float A = 0.5f;
271
      float B = 0.1f;
272

    
273
      int extraIndex    = 2;
274
      int extraVertices = 2;
275
      int numBands      = 2;
276

    
277
      float[] vertices = new float[] { -A,-A, A,-A, A,A, -A,A };
278
      float[] bands = new float[2*numBands];
279

    
280
      for(int i=0; i<numBands; i++)
281
        {
282
        bands[2*i  ] = 1 + i/(1.0f-numBands);
283
        bands[2*i+1] = B/(numBands-1)*i;
284
        }
285

    
286
      mMesh = new MeshPolygon(vertices,bands,extraIndex,extraVertices);
287
      mMesh.setEffectAssociation(0,0,0);
288
      mMesh.setShowNormals(true);
289
      }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
    private void createMesh()
294
      {
295
      Static4D quat = new Static4D(0,0,0,1);
296
      Static3D move = new Static3D(0,0,0);
297
      float scale   = 1.0f;
298
      int variant   = 1;
299

    
300
      InitData data = new InitData(new int[] {2,3,2});
301
      TwistyObject puzzle = new TwistyCuboid(MESH_NICE,MODE_NORM,quat,move,scale,data,null);
302

    
303
      ObjectShape shape           = puzzle.getObjectShape(variant);
304
      ObjectFaceShape face        = puzzle.getObjectFaceShape(variant);
305
      ObjectVertexEffects effects = puzzle.getVertexEffects(variant);
306

    
307
      int[][] indices = shape.getVertIndices();
308
      int numComponents = indices.length;
309
      int[] outer = new int[numComponents];
310

    
311
      FactoryCubit factory = FactoryCubit.getInstance();
312
      factory.clear();
313
      factory.createNewFaceTransform(shape,outer);
314
      mMesh = factory.createRoundedSolid(shape,face,effects,MESH_NICE,numComponents);
315

    
316
      int numEff = mMesh.getNumEffComponents();
317

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

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

    
323
    private void openMesh(int resourceID)
324
      {
325
      Context con = mView.getContext();
326
      Resources res = con.getResources();
327
      InputStream is = res.openRawResource(resourceID);
328
      DataInputStream dos = new DataInputStream(is);
329
      mMesh = new MeshFile(dos);
330

    
331
      int numEff = mMesh.getNumEffComponents();
332

    
333
      for(int i=0; i<numEff; i++)
334
        {
335
        mMesh.setEffectAssociation(i, 0, i);
336
        }
337

    
338
      try
339
        {
340
        is.close();
341
        }
342
      catch(IOException e)
343
        {
344
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
345
        }
346
      }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

    
350
    MeshBase getMesh()
351
      {
352
      return mMesh;
353
      }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

    
357
    long getTime()
358
      {
359
      return mTime;
360
      }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
    int getBytes()
365
      {
366
      if( mMesh instanceof MeshFile )
367
        {
368
        return ((MeshFile)mMesh).getNumBytes();
369
        }
370

    
371
      return 0;
372
      }
373

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

    
376
    int getVertices()
377
      {
378
      return mMesh.getNumVertices();
379
      }
380

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

    
383
    int getEndEffIndex(int component)
384
      {
385
      return mMesh.getLastVertexEff(component);
386
      }
387

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389

    
390
    int getEndTexIndex(int component)
391
      {
392
      return mMesh.getLastVertexTex(component);
393
      }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

    
397
    int getEffComponentNum()
398
      {
399
      return mMesh.getNumEffComponents();
400
      }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

    
404
    int getTexComponentNum()
405
      {
406
      return mMesh.getNumTexComponents();
407
      }
408
}
(2-2/4)