Project

General

Profile

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

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

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

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

    
79
    Static4D mQuat1, mQuat2;
80
    int mScreenMin;
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

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

    
91
      mCurrentScale = DEFAULT_SCALE;
92

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

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

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

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

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

    
110
      mEffects.apply( disappear );
111

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

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

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

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

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

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

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

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

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

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

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

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

    
179
      long t1 = System.currentTimeMillis();
180

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

    
194
      long t2 = System.currentTimeMillis();
195

    
196
      mTime = t2-t1;
197

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

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

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

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

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

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

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

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

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

    
244
      return null;
245
      }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

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

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

    
260
      return bitmap;
261
      }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

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

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

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

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

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

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

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

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

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

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

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

    
312
      int numEff = mMesh.getNumEffComponents();
313

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

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

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

    
327
      int numEff = mMesh.getNumEffComponents();
328

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

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

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

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

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

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

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

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

    
367
      return 0;
368
      }
369

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

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

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

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

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

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

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

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

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

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