Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 3e129d0b

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.main.TwistyObject;
50
import org.distorted.objectlib.objects.*;
51

    
52
import java.io.DataInputStream;
53
import java.io.IOException;
54
import java.io.InputStream;
55

    
56
import javax.microedition.khronos.egl.EGLConfig;
57
import javax.microedition.khronos.opengles.GL10;
58

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

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

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

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

    
80
    Static4D mQuat1, mQuat2;
81
    int mScreenMin;
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

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

    
92
      mCurrentScale = DEFAULT_SCALE;
93

    
94
      mQuat1 = new Static4D(0,0,0,1);
95
      mQuat2 = new Static4D(0,0,0,1);
96

    
97
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
98
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
99

    
100
      quatInt1.add(mQuat1);
101
      quatInt2.add(mQuat2);
102

    
103
      VertexEffectDisappear disappear = new VertexEffectDisappear();
104
      disappear.setMeshAssociation(1,-1);
105

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

    
111
      mEffects.apply( disappear );
112

    
113
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
114
      mScreen.showFPS();
115
      }
116

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

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

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

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

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

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
//   0 ---> 0
155
//  50 ---> DEFAULT_SCALE
156
// 100 ---> 4*DEFAULT_SCALE
157

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

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

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

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

    
180
      long t1 = System.currentTimeMillis();
181

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

    
195
      long t2 = System.currentTimeMillis();
196

    
197
      mTime = t2-t1;
198

    
199
      mScreen.detachAll();
200
      mScreen.attach(mTexture,mEffects,mMesh);
201
      }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

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

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

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

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

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

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

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

    
245
      return null;
246
      }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

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

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

    
261
      return bitmap;
262
      }
263

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

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

    
271
      int extraIndex    = 2;
272
      int extraVertices = 2;
273
      int numBands      = 2;
274

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

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

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

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

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

    
298
      int[] numLayers = new int[] {2,3,2};
299
      TwistyObject puzzle = new TwistyCuboid(numLayers,MESH_NICE,MODE_NORM,quat,move,scale,null);
300

    
301
      ObjectShape shape   = puzzle.getObjectShape(variant);
302
      ObjectFaceShape face= puzzle.getObjectFaceShape(variant);
303

    
304
      int[][] indices = shape.getVertIndices();
305
      int numComponents = indices.length;
306
      int[] outer = new int[numComponents];
307

    
308
      FactoryCubit factory = FactoryCubit.getInstance();
309
      factory.clear();
310
      factory.createNewFaceTransform(shape,outer);
311
      mMesh = factory.createRoundedSolid(shape,face,MESH_NICE,numComponents);
312

    
313
      int numEff = mMesh.getNumEffComponents();
314

    
315
      for(int i=0; i<numEff; i++) mMesh.setEffectAssociation(i, 0, i);
316
      }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

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

    
328
      int numEff = mMesh.getNumEffComponents();
329

    
330
      for(int i=0; i<numEff; i++)
331
        {
332
        mMesh.setEffectAssociation(i, 0, i);
333
        }
334

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

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
    MeshBase getMesh()
348
      {
349
      return mMesh;
350
      }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

    
354
    long getTime()
355
      {
356
      return mTime;
357
      }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

    
361
    int getBytes()
362
      {
363
      if( mMesh instanceof MeshFile )
364
        {
365
        return ((MeshFile)mMesh).getNumBytes();
366
        }
367

    
368
      return 0;
369
      }
370

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

    
373
    int getVertices()
374
      {
375
      return mMesh.getNumVertices();
376
      }
377

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

    
380
    int getEndEffIndex(int component)
381
      {
382
      return mMesh.getLastVertexEff(component);
383
      }
384

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

    
387
    int getEndTexIndex(int component)
388
      {
389
      return mMesh.getLastVertexTex(component);
390
      }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
    int getEffComponentNum()
395
      {
396
      return mMesh.getNumEffComponents();
397
      }
398

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

    
401
    int getTexComponentNum()
402
      {
403
      return mMesh.getNumTexComponents();
404
      }
405
}
(2-2/4)