Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 46a0999e

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.LibraryUser
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 final Resources mResources;
75
    private final DistortedScreen mScreen;
76
    private final DistortedEffects mEffects;
77
    private final Static3D mScale;
78

    
79
    private DistortedTexture mTexture;
80
    private MeshBase mMesh;
81
    private long mTime;
82
    private float mCurrentScale;
83

    
84
    Static4D mQuat1, mQuat2;
85
    int mScreenMin;
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
    MeshFileRenderer(GLSurfaceView v)
90
      {
91
      mView = v;
92
      mResources = v.getResources();
93

    
94
      mScreen = new DistortedScreen();
95
      mScale= new Static3D(1,1,1);
96
      Static3D center=new Static3D(0,0,0);
97

    
98
      mCurrentScale = DEFAULT_SCALE;
99

    
100
      mQuat1 = new Static4D(0,0,0,1);
101
      mQuat2 = new Static4D(0,0,0,1);
102

    
103
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
104
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
105

    
106
      quatInt1.add(mQuat1);
107
      quatInt2.add(mQuat2);
108

    
109
      VertexEffectDisappear disappear = new VertexEffectDisappear();
110
      disappear.setMeshAssociation(1,-1);
111

    
112
      mEffects = new DistortedEffects();
113
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
114
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
115
      mEffects.apply( new MatrixEffectScale(mScale));
116

    
117
      mEffects.apply( disappear );
118

    
119
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
120
      mScreen.showFPS();
121
      }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
   
125
    public void onDrawFrame(GL10 glUnused) 
126
      {
127
      mScreen.render( System.currentTimeMillis() );
128
      }
129

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

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
    
142
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
143
      {
144
      if( mTexture==null ) mTexture = new DistortedTexture();
145

    
146
      Effect.enableEffects(EffectType.VERTEX);
147
      DistortedLibrary.setMax(EffectType.VERTEX, 20);
148
      DistortedLibrary.needTransformFeedback();
149
      DistortedLibrary.onSurfaceCreated(this);
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
      if( resourceID == R.raw.deferredjob ||
214
          resourceID == R.raw.meshjoin    ||
215
          resourceID == PROCEDURAL        ||
216
          resourceID == POLYGON           ||
217
          resourceID == R.raw.predeform    ) return createWhiteTexture();
218

    
219
      if( resourceID == R.raw.cube2       ||
220
          resourceID == R.raw.cube3       ||
221
          resourceID == R.raw.cube4       ||
222
          resourceID == R.raw.cube5        )
223
        {
224
        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
        }
228
      if( resourceID == R.raw.pyra3       ||
229
          resourceID == R.raw.pyra4       ||
230
          resourceID == R.raw.pyra5        )
231
        {
232
        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
      if( resourceID == R.raw.dino )
237
        {
238
        colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
239
        vertices = new float[] { -F,F/3, 0,-2*F/3, +F,F/3 };
240
        return factory.createTexture(vertices,colors,0.05f, 0.03f, true);
241
        }
242
      if( resourceID == R.raw.skewb )
243
        {
244
        colors = new int[] { 0xffffff00, 0xffffffff, 0xff0000ff, 0xff00ff00, 0xffff0000, 0xffb5651d };
245
        vertices = new float[] { -F+F/4,F/4, F/4,-F+F/4, F/4,F/4};
246
        return factory.createTexture(vertices,colors,0.05f, 0.08f, true);
247
        }
248

    
249
      return null;
250
      }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

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

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

    
265
      return bitmap;
266
      }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
    private void createPolygon()
271
      {
272
      float A = 0.5f;
273
      float B = 0.04f;
274

    
275
      int extraIndex    = 1;
276
      int extraVertices = 1;
277
      float[] vertices = new float[] { -A,-A, A,-A, A,A, -A,A };
278

    
279
      float C = 0.5f;
280
      float[] bands = new float[] { 1.0f, 0.00f*C, 0.9f, 0.04f*C,  0.8f, 0.07f*C, 0.5f, 0.09f*C, 0.0f, 0.10f*C};
281

    
282
/*
283
      int numBands      = 5;
284
      float[] bands = new float[2*numBands];
285

    
286
      for(int i=0; i<numBands; i++)
287
        {
288
        bands[2*i  ] = 1 + i/(1.0f-numBands);
289
        bands[2*i+1] = B/(numBands-1)*i;
290
        }
291
*/
292
      boolean[] up = new boolean[] {true,false,false,false};
293

    
294
      mMesh = new MeshPolygon(vertices,bands,up,extraIndex,extraVertices,0.0f,0.0f);
295
      mMesh.setEffectAssociation(0,0,0);
296
      mMesh.setShowNormals(true);
297
      }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
    private void createMesh()
302
      {
303
      Static4D quat = new Static4D(0,0,0,1);
304
      Static3D move = new Static3D(0,0,0);
305
      float scale   = 1.0f;
306
      int variant   = 1;
307

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

    
311
      ObjectShape shape           = puzzle.getObjectShape(variant);
312
      ObjectFaceShape face        = puzzle.getObjectFaceShape(variant);
313
      ObjectVertexEffects effects = puzzle.getVertexEffects(variant);
314

    
315
      int[][] indices = shape.getVertIndices();
316
      int numComponents = indices.length;
317
      int[] outer = new int[numComponents];
318

    
319
      FactoryCubit factory = FactoryCubit.getInstance();
320
      factory.clear();
321
      factory.createNewFaceTransform(shape,outer);
322
      mMesh = factory.createRoundedSolid(shape,face,effects,MESH_NICE,numComponents);
323

    
324
      int numEff = mMesh.getNumEffComponents();
325

    
326
      for(int i=0; i<numEff; i++) mMesh.setEffectAssociation(i, 0, i);
327
      }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

    
331
    private void openMesh(int resourceID)
332
      {
333
      Context con = mView.getContext();
334
      Resources res = con.getResources();
335
      InputStream is = res.openRawResource(resourceID);
336
      DataInputStream dos = new DataInputStream(is);
337
      mMesh = new MeshFile(dos);
338

    
339
      int numEff = mMesh.getNumEffComponents();
340

    
341
      for(int i=0; i<numEff; i++)
342
        {
343
        mMesh.setEffectAssociation(i, 0, i);
344
        }
345

    
346
      try
347
        {
348
        is.close();
349
        }
350
      catch(IOException e)
351
        {
352
        android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
353
        }
354
      }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
    MeshBase getMesh()
359
      {
360
      return mMesh;
361
      }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

    
365
    long getTime()
366
      {
367
      return mTime;
368
      }
369

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

    
372
    int getBytes()
373
      {
374
      if( mMesh instanceof MeshFile )
375
        {
376
        return ((MeshFile)mMesh).getNumBytes();
377
        }
378

    
379
      return 0;
380
      }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
    int getVertices()
385
      {
386
      return mMesh.getNumVertices();
387
      }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

    
391
    int getEndEffIndex(int component)
392
      {
393
      return mMesh.getLastVertexEff(component);
394
      }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
    int getEndTexIndex(int component)
399
      {
400
      return mMesh.getLastVertexTex(component);
401
      }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

    
405
    int getEffComponentNum()
406
      {
407
      return mMesh.getNumEffComponents();
408
      }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

    
412
    int getTexComponentNum()
413
      {
414
      return mMesh.getNumTexComponents();
415
      }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
    public void distortedException(Exception ex)
420
      {
421
      android.util.Log.e("MeshFile", ex.getMessage() );
422
      }
423

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
    public InputStream localFile(int fileID)
427
      {
428
      return mResources.openRawResource(fileID);
429
      }
430

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432

    
433
    public void logMessage(String message)
434
      {
435
      android.util.Log.e("MeshFile", message );
436
      }
437
}
(2-2/4)