Project

General

Profile

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

examples / src / main / java / org / distorted / examples / singlemesh / SingleMeshRenderer.java @ dc10a48d

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.singlemesh;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.graphics.Bitmap;
27
import android.graphics.Canvas;
28
import android.graphics.Paint;
29
import android.opengl.GLSurfaceView;
30

    
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectDeform;
35
import org.distorted.library.effect.VertexEffectMove;
36
import org.distorted.library.effect.VertexEffectRotate;
37
import org.distorted.library.effect.VertexEffectSink;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.main.DistortedLibrary;
40
import org.distorted.library.main.DistortedScreen;
41
import org.distorted.library.main.DistortedTexture;
42
import org.distorted.library.mesh.MeshBase;
43
import org.distorted.library.mesh.MeshJoined;
44
import org.distorted.library.mesh.MeshSquare;
45
import org.distorted.library.type.Dynamic1D;
46
import org.distorted.library.type.DynamicQuat;
47
import org.distorted.library.type.Static1D;
48
import org.distorted.library.type.Static3D;
49
import org.distorted.library.type.Static4D;
50

    
51
import java.io.InputStream;
52

    
53
import javax.microedition.khronos.egl.EGLConfig;
54
import javax.microedition.khronos.opengles.GL10;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
class SingleMeshRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
59
{
60
    private static final float DIST = 0.5f;
61

    
62
    private static final Static3D[] AXIS = new Static3D[]
63
         {
64
           new Static3D(1,0,0),
65
           new Static3D(0,1,0),
66
           new Static3D(0,0,1)
67
         };
68

    
69
    private static final int[] FACE_COLORS = new int[]
70
         {
71
           0xffffff00, 0xffffffff,   // (right-YELLOW) (left  -WHITE)
72
           0xff0000ff, 0xff00ff00,   // (top  -BLUE  ) (bottom-GREEN)
73
           0xffff0000, 0xffb5651d    // (front-RED   ) (back  -BROWN)
74
         };
75

    
76
    private static final int NUM_FACES = FACE_COLORS.length;
77

    
78
    private static final Static4D RIG_MAP = new Static4D(0.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
79
    private static final Static4D LEF_MAP = new Static4D(1.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
80
    private static final Static4D TOP_MAP = new Static4D(2.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
81
    private static final Static4D BOT_MAP = new Static4D(3.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
82
    private static final Static4D FRO_MAP = new Static4D(4.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
83
    private static final Static4D BAC_MAP = new Static4D(5.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
84
    private static final Static4D INT_MAP = new Static4D(6.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
85

    
86
    private static final Static4D[][] TEXTURE_MAP = new Static4D[][]
87
         {
88
             {  INT_MAP, LEF_MAP, INT_MAP, BOT_MAP, INT_MAP, BAC_MAP },
89
             {  INT_MAP, LEF_MAP, INT_MAP, BOT_MAP, FRO_MAP, INT_MAP },
90
             {  INT_MAP, LEF_MAP, TOP_MAP, INT_MAP, INT_MAP, BAC_MAP },
91
             {  INT_MAP, LEF_MAP, TOP_MAP, INT_MAP, FRO_MAP, INT_MAP },
92
             {  RIG_MAP, INT_MAP, INT_MAP, BOT_MAP, INT_MAP, BAC_MAP },
93
             {  RIG_MAP, INT_MAP, INT_MAP, BOT_MAP, FRO_MAP, INT_MAP },
94
             {  RIG_MAP, INT_MAP, TOP_MAP, INT_MAP, INT_MAP, BAC_MAP },
95
             {  RIG_MAP, INT_MAP, TOP_MAP, INT_MAP, FRO_MAP, INT_MAP }
96
         };
97

    
98
    private static final Static3D[] CUBIT_MOVES = new Static3D[]
99
         {
100
           new Static3D(-DIST,-DIST,-DIST),
101
           new Static3D(-DIST,-DIST,+DIST),
102
           new Static3D(-DIST,+DIST,-DIST),
103
           new Static3D(-DIST,+DIST,+DIST),
104
           new Static3D(+DIST,-DIST,-DIST),
105
           new Static3D(+DIST,-DIST,+DIST),
106
           new Static3D(+DIST,+DIST,-DIST),
107
           new Static3D(+DIST,+DIST,+DIST),
108
         };
109

    
110
    private final GLSurfaceView mView;
111
    private final Resources mResources;
112
    private final DistortedScreen mScreen;
113
    private final DistortedEffects mEffects;
114
    private final Static3D mScale;
115
    private final VertexEffectRotate mRotate;
116
    private final Dynamic1D mAngleDyn;
117
    private final Static1D mAngle;
118
    private final Static3D mAxis;
119

    
120
    private DistortedTexture mTexture;
121
    private MeshBase mMesh;
122

    
123
    Static4D mQuat1, mQuat2;
124
    int mScreenMin;
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
    SingleMeshRenderer(GLSurfaceView v)
129
      {
130
      mView = v;
131
      mResources = v.getResources();
132

    
133
      mScreen = new DistortedScreen();
134
      mScale= new Static3D(1,1,1);
135
      Static3D center=new Static3D(0,0,0);
136

    
137
      mQuat1 = new Static4D(0,0,0,1);
138
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
139

    
140
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
141
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
142

    
143
      quatInt1.add(mQuat1);
144
      quatInt2.add(mQuat2);
145

    
146
      mAngle = new Static1D(0);
147
      mAxis  = new Static3D(1,0,0);
148

    
149
      mAngleDyn = new Dynamic1D(2000,0.5f);
150
      mAngleDyn.add(new Static1D(0));
151
      mAngleDyn.add( mAngle );
152

    
153
      mRotate = new VertexEffectRotate( mAngleDyn, mAxis, new Static3D(0,0,0) );
154

    
155
      mEffects = new DistortedEffects();
156
      mEffects.apply( mRotate );
157
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
158
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
159
      mEffects.apply( new MatrixEffectScale(mScale));
160

    
161
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
   
166
    public void onDrawFrame(GL10 glUnused) 
167
      {
168
      mScreen.render( System.currentTimeMillis() );
169
      }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
    
173
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
174
      {
175
      final float SCALE = 0.3f;
176
      mScreenMin = Math.min(width, height);
177
      float factor = SCALE*mScreenMin;
178
      mScale.set(factor,factor,factor);
179
      mScreen.resize(width, height);
180
      }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
    
184
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
185
      {
186
      if( mTexture==null ) mTexture = new DistortedTexture();
187
      mTexture.setTexture( createTexture() );
188

    
189
      if( mMesh==null ) mMesh = createMesh();
190

    
191
      mScreen.detachAll();
192
      mScreen.attach(mTexture,mEffects,mMesh);
193

    
194
      VertexEffectRotate.enable();
195

    
196
      DistortedLibrary.onSurfaceCreated(this);
197
      }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
    void apply(int andAssoc, int axisIndex)
202
      {
203
      mRotate.setMeshAssociation(andAssoc,-1);
204

    
205
      mAngle.set(360);
206

    
207
      mAxis.set0(AXIS[axisIndex].get0());
208
      mAxis.set1(AXIS[axisIndex].get1());
209
      mAxis.set2(AXIS[axisIndex].get2());
210

    
211
      mAngleDyn.resetToBeginning();
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
    private Bitmap createTexture()
217
      {
218
      final int NUM_FACES = 6;
219
      final int TEXTURE_HEIGHT = 200;
220
      final int INTERIOR_COLOR = 0xff000000;
221
      final float R = TEXTURE_HEIGHT*0.10f;
222
      final float M = TEXTURE_HEIGHT*0.05f;
223

    
224
      Bitmap bitmap;
225
      Paint paint = new Paint();
226
      bitmap = Bitmap.createBitmap( (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
227
      Canvas canvas = new Canvas(bitmap);
228

    
229
      paint.setAntiAlias(true);
230
      paint.setTextAlign(Paint.Align.CENTER);
231
      paint.setStyle(Paint.Style.FILL);
232

    
233
      paint.setColor(INTERIOR_COLOR);
234
      canvas.drawRect(0, 0, (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
235

    
236
      for(int i=0; i<NUM_FACES; i++)
237
        {
238
        paint.setColor(FACE_COLORS[i]);
239
        canvas.drawRoundRect( i*TEXTURE_HEIGHT+M, M, (i+1)*TEXTURE_HEIGHT-M, TEXTURE_HEIGHT-M, R, R, paint);
240
        }
241

    
242
      return bitmap;
243
      }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
     MeshBase createCubitMesh()
248
      {
249
      final int MESHES=6;
250
      int association = 1;
251
      MeshBase[] meshes = new MeshSquare[MESHES];
252
      meshes[0] = new MeshSquare(10,10);
253
      meshes[0].setEffectAssociation(0,association,0);
254

    
255
      for(int i=1; i<MESHES; i++)
256
        {
257
        association <<=1;
258
        meshes[i] = meshes[0].copy(true);
259
        meshes[i].setEffectAssociation(0,association,0);
260
        }
261

    
262
      MeshBase mesh = new MeshJoined(meshes);
263

    
264
      Static3D axisY   = new Static3D(0,1,0);
265
      Static3D axisX   = new Static3D(1,0,0);
266
      Static3D center  = new Static3D(0,0,0);
267
      Static1D angle90 = new Static1D(90);
268
      Static1D angle180= new Static1D(180);
269
      Static1D angle270= new Static1D(270);
270

    
271
      float d1 = 1.0f;
272
      float d2 =-0.05f;
273
      float d3 = 0.12f;
274

    
275
      Static3D dCen0 = new Static3D( d1*( 0.5f), d1*( 0.5f), d1*( 0.5f) );
276
      Static3D dCen1 = new Static3D( d1*( 0.5f), d1*( 0.5f), d1*(-0.5f) );
277
      Static3D dCen2 = new Static3D( d1*( 0.5f), d1*(-0.5f), d1*( 0.5f) );
278
      Static3D dCen3 = new Static3D( d1*( 0.5f), d1*(-0.5f), d1*(-0.5f) );
279
      Static3D dCen4 = new Static3D( d1*(-0.5f), d1*( 0.5f), d1*( 0.5f) );
280
      Static3D dCen5 = new Static3D( d1*(-0.5f), d1*( 0.5f), d1*(-0.5f) );
281
      Static3D dCen6 = new Static3D( d1*(-0.5f), d1*(-0.5f), d1*( 0.5f) );
282
      Static3D dCen7 = new Static3D( d1*(-0.5f), d1*(-0.5f), d1*(-0.5f) );
283

    
284
      Static3D dVec0 = new Static3D( d2*( 0.5f), d2*( 0.5f), d2*( 0.5f) );
285
      Static3D dVec1 = new Static3D( d2*( 0.5f), d2*( 0.5f), d2*(-0.5f) );
286
      Static3D dVec2 = new Static3D( d2*( 0.5f), d2*(-0.5f), d2*( 0.5f) );
287
      Static3D dVec3 = new Static3D( d2*( 0.5f), d2*(-0.5f), d2*(-0.5f) );
288
      Static3D dVec4 = new Static3D( d2*(-0.5f), d2*( 0.5f), d2*( 0.5f) );
289
      Static3D dVec5 = new Static3D( d2*(-0.5f), d2*( 0.5f), d2*(-0.5f) );
290
      Static3D dVec6 = new Static3D( d2*(-0.5f), d2*(-0.5f), d2*( 0.5f) );
291
      Static3D dVec7 = new Static3D( d2*(-0.5f), d2*(-0.5f), d2*(-0.5f) );
292

    
293
      Static4D dReg  = new Static4D(0,0,0,d3);
294
      Static1D dRad  = new Static1D(1);
295

    
296
      VertexEffectMove   effect0 = new VertexEffectMove(new Static3D(0,0,+0.5f));
297
      effect0.setMeshAssociation(63,-1);  // all 6 sides
298
      VertexEffectRotate effect1 = new VertexEffectRotate( angle180, axisX, center );
299
      effect1.setMeshAssociation(32,-1);  // back
300
      VertexEffectRotate effect2 = new VertexEffectRotate( angle90 , axisX, center );
301
      effect2.setMeshAssociation( 8,-1);  // bottom
302
      VertexEffectRotate effect3 = new VertexEffectRotate( angle270, axisX, center );
303
      effect3.setMeshAssociation( 4,-1);  // top
304
      VertexEffectRotate effect4 = new VertexEffectRotate( angle270, axisY, center );
305
      effect4.setMeshAssociation( 2,-1);  // left
306
      VertexEffectRotate effect5 = new VertexEffectRotate( angle90 , axisY, center );
307
      effect5.setMeshAssociation( 1,-1);  // right
308

    
309
      VertexEffectDeform effect6 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
310
      VertexEffectDeform effect7 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
311
      VertexEffectDeform effect8 = new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
312
      VertexEffectDeform effect9 = new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
313
      VertexEffectDeform effect10= new VertexEffectDeform(dVec4, dRad, dCen4, dReg);
314
      VertexEffectDeform effect11= new VertexEffectDeform(dVec5, dRad, dCen5, dReg);
315
      VertexEffectDeform effect12= new VertexEffectDeform(dVec6, dRad, dCen6, dReg);
316
      VertexEffectDeform effect13= new VertexEffectDeform(dVec7, dRad, dCen7, dReg);
317

    
318
      VertexEffectSink effect14= new VertexEffectSink( new Static1D(1.5f), center, new Static4D(0,0,0,0.72f) );
319

    
320
      mesh.apply(effect0);
321
      mesh.apply(effect1);
322
      mesh.apply(effect2);
323
      mesh.apply(effect3);
324
      mesh.apply(effect4);
325
      mesh.apply(effect5);
326
      mesh.apply(effect6);
327
      mesh.apply(effect7);
328
      mesh.apply(effect8);
329
      mesh.apply(effect9);
330
      mesh.apply(effect10);
331
      mesh.apply(effect11);
332
      mesh.apply(effect12);
333
      mesh.apply(effect13);
334
      mesh.apply(effect14);
335

    
336
      mesh.mergeEffComponents();
337

    
338
      return mesh;
339
      }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
    private MeshBase createMesh()
344
      {
345
      final int NUM_CUBITS = CUBIT_MOVES.length;
346
      MeshBase[] cubits = new MeshBase[NUM_CUBITS];
347

    
348
      cubits[NUM_CUBITS-1] = createCubitMesh();   // NUM_CUBITS-1 (or anything non-zero!)
349

    
350
      for(int i=0; i<NUM_CUBITS-1; i++)
351
        {
352
        cubits[i] = cubits[NUM_CUBITS-1].copy(true);
353
        }
354

    
355
      for(int i=0; i<NUM_CUBITS; i++)
356
        {
357
        cubits[i].apply( new MatrixEffectMove(CUBIT_MOVES[i]), 1,0);
358
        cubits[i].setTextureMap(TEXTURE_MAP[i],0);
359
        }
360

    
361
      MeshBase result = new MeshJoined(cubits);
362

    
363
      result.setEffectAssociation( 0, (1<<4) + (1<<2) + 1, 0);
364
      result.setEffectAssociation( 1, (1<<4) + (1<<2) + 2, 0);
365
      result.setEffectAssociation( 2, (1<<4) + (2<<2) + 1, 0);
366
      result.setEffectAssociation( 3, (1<<4) + (2<<2) + 2, 0);
367
      result.setEffectAssociation( 4, (2<<4) + (1<<2) + 1, 0);
368
      result.setEffectAssociation( 5, (2<<4) + (1<<2) + 2, 0);
369
      result.setEffectAssociation( 6, (2<<4) + (2<<2) + 1, 0);
370
      result.setEffectAssociation( 7, (2<<4) + (2<<2) + 2, 0);
371

    
372
      return result;
373
      }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
    public void distortedException(Exception ex)
378
      {
379
      android.util.Log.e("SingleMesh", ex.getMessage() );
380
      }
381

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

    
384
    public int openGlVersion()
385
      {
386
      Context context = mView.getContext();
387
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
388
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
389
      int glESversion = configurationInfo.reqGlEsVersion;
390
      int major = glESversion >> 16;
391
      int minor = glESversion & 0xff;
392

    
393
      return 100*major + 10*minor;
394
      }
395

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

    
398
    public InputStream localFile(int fileID)
399
      {
400
      return mResources.openRawResource(fileID);
401
      }
402

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

    
405
    public void logMessage(String message)
406
      {
407
      android.util.Log.e("SingleMesh", message );
408
      }
409
}
(2-2/3)