Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 2f250195

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

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

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

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

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

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

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

    
81
    Static4D mQuat1, mQuat2;
82
    int mScreenMin;
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

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

    
93
      mCurrentScale = DEFAULT_SCALE;
94

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

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

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

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

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

    
112
      mEffects.apply( disappear );
113

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

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

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

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

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

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

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

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

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

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

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

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

    
181
      long t1 = System.currentTimeMillis();
182

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

    
196
      long t2 = System.currentTimeMillis();
197

    
198
      mTime = t2-t1;
199

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

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

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

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

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

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

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

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

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

    
246
      return null;
247
      }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

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

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

    
262
      return bitmap;
263
      }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

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

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

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

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

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

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

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

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

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

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

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

    
314
      int numEff = mMesh.getNumEffComponents();
315

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

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

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

    
329
      int numEff = mMesh.getNumEffComponents();
330

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

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

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

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

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

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

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

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

    
369
      return 0;
370
      }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

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

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

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

    
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387

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

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

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

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

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