Project

General

Profile

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

examples / src / main / java / org / distorted / examples / singlemesh / SingleMeshRenderer.java @ 061449ed

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.graphics.Bitmap;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25
import android.opengl.GLSurfaceView;
26

    
27
import org.distorted.library.effect.EffectType;
28
import org.distorted.library.effect.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectQuaternion;
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectDeform;
32
import org.distorted.library.effect.VertexEffectMove;
33
import org.distorted.library.effect.VertexEffectRotate;
34
import org.distorted.library.effect.VertexEffectSink;
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.MeshJoined;
41
import org.distorted.library.mesh.MeshRectangles;
42
import org.distorted.library.type.Dynamic1D;
43
import org.distorted.library.type.DynamicQuat;
44
import org.distorted.library.type.Static1D;
45
import org.distorted.library.type.Static3D;
46
import org.distorted.library.type.Static4D;
47

    
48
import javax.microedition.khronos.egl.EGLConfig;
49
import javax.microedition.khronos.opengles.GL10;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
class SingleMeshRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
54
{
55
    private static final float DIST = 0.5f;
56

    
57
    private static Static3D[] AXIS = new Static3D[]
58
         {
59
           new Static3D(1,0,0),
60
           new Static3D(0,1,0),
61
           new Static3D(0,0,1)
62
         };
63

    
64
    private static final int[] FACE_COLORS = new int[]
65
         {
66
           0xffffff00, 0xffffffff,   // (right-YELLOW) (left  -WHITE)
67
           0xff0000ff, 0xff00ff00,   // (top  -BLUE  ) (bottom-GREEN)
68
           0xffff0000, 0xffb5651d    // (front-RED   ) (back  -BROWN)
69
         };
70

    
71
    private static final int NUM_FACES = FACE_COLORS.length;
72

    
73
    private static final Static4D RIG_MAP = new Static4D(0.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
74
    private static final Static4D LEF_MAP = new Static4D(1.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
75
    private static final Static4D TOP_MAP = new Static4D(2.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
76
    private static final Static4D BOT_MAP = new Static4D(3.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
77
    private static final Static4D FRO_MAP = new Static4D(4.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
78
    private static final Static4D BAC_MAP = new Static4D(5.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
79
    private static final Static4D INT_MAP = new Static4D(6.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
80

    
81
    private static final Static4D[][] TEXTURE_MAP = new Static4D[][]
82
         {
83
             {  INT_MAP, LEF_MAP, INT_MAP, BOT_MAP, INT_MAP, BAC_MAP },
84
             {  INT_MAP, LEF_MAP, INT_MAP, BOT_MAP, FRO_MAP, INT_MAP },
85
             {  INT_MAP, LEF_MAP, TOP_MAP, INT_MAP, INT_MAP, BAC_MAP },
86
             {  INT_MAP, LEF_MAP, TOP_MAP, INT_MAP, FRO_MAP, INT_MAP },
87
             {  RIG_MAP, INT_MAP, INT_MAP, BOT_MAP, INT_MAP, BAC_MAP },
88
             {  RIG_MAP, INT_MAP, INT_MAP, BOT_MAP, FRO_MAP, INT_MAP },
89
             {  RIG_MAP, INT_MAP, TOP_MAP, INT_MAP, INT_MAP, BAC_MAP },
90
             {  RIG_MAP, INT_MAP, TOP_MAP, INT_MAP, FRO_MAP, INT_MAP }
91
         };
92

    
93
    private static final Static3D[] CUBIT_MOVES = new Static3D[]
94
         {
95
           new Static3D(-DIST,-DIST,-DIST),
96
           new Static3D(-DIST,-DIST,+DIST),
97
           new Static3D(-DIST,+DIST,-DIST),
98
           new Static3D(-DIST,+DIST,+DIST),
99
           new Static3D(+DIST,-DIST,-DIST),
100
           new Static3D(+DIST,-DIST,+DIST),
101
           new Static3D(+DIST,+DIST,-DIST),
102
           new Static3D(+DIST,+DIST,+DIST),
103
         };
104

    
105
    private GLSurfaceView mView;
106
    private DistortedTexture mTexture;
107
    private DistortedScreen mScreen;
108
    private DistortedEffects mEffects;
109
    private Static3D mScale;
110
    private MeshBase mMesh;
111
    private VertexEffectRotate mRotate;
112
    private Dynamic1D mAngleDyn;
113
    private Static1D mAngle;
114
    private Static3D mAxis;
115

    
116
    Static4D mQuat1, mQuat2;
117
    int mScreenMin;
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
    SingleMeshRenderer(GLSurfaceView v)
122
      {
123
      mView = v;
124
      mScreen = new DistortedScreen();
125
      mScale= new Static3D(1,1,1);
126
      Static3D center=new Static3D(0,0,0);
127

    
128
      mQuat1 = new Static4D(0,0,0,1);
129
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
130

    
131
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
132
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
133

    
134
      quatInt1.add(mQuat1);
135
      quatInt2.add(mQuat2);
136

    
137
      mAngle = new Static1D(0);
138
      mAxis  = new Static3D(1,0,0);
139

    
140
      mAngleDyn = new Dynamic1D(2000,0.5f);
141
      mAngleDyn.add(new Static1D(0));
142
      mAngleDyn.add( mAngle );
143

    
144
      mRotate = new VertexEffectRotate( mAngleDyn, mAxis, new Static3D(0,0,0) );
145

    
146
      mEffects = new DistortedEffects();
147
      mEffects.apply( mRotate );
148
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
149
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
150
      mEffects.apply( new MatrixEffectScale(mScale));
151

    
152
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
153
      }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
   
157
    public void onDrawFrame(GL10 glUnused) 
158
      {
159
      mScreen.render( System.currentTimeMillis() );
160
      }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
    
164
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
165
      {
166
      final float SCALE = 0.3f;
167
      mScreenMin = Math.min(width, height);
168
      float factor = SCALE*mScreenMin;
169
      mScale.set(factor,factor,factor);
170
      mScreen.resize(width, height);
171
      }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
    
175
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
176
      {
177
      if( mTexture==null ) mTexture = new DistortedTexture();
178
      mTexture.setTexture( createTexture() );
179

    
180
      if( mMesh==null ) mMesh = createMesh();
181

    
182
      mScreen.detachAll();
183
      mScreen.attach(mTexture,mEffects,mMesh);
184

    
185
      DistortedLibrary.setMax(EffectType.VERTEX, 15);
186
      VertexEffectRotate.enable();
187

    
188
      DistortedLibrary.onCreate(mView.getContext(), this);
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
    public void distortedException(Exception ex)
194
      {
195
      android.util.Log.e("SingleMesh", ex.getMessage() );
196
      }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

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

    
204
      mAngle.set(360);
205

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

    
210
      mAngleDyn.resetToBeginning();
211
      }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

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

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

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

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

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

    
241
      return bitmap;
242
      }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

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

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

    
261
      MeshBase mesh = new MeshJoined(meshes);
262

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

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

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

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

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

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

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

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

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

    
335
      mesh.mergeEffComponents();
336

    
337
      return mesh;
338
      }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

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

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

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

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

    
360
      MeshBase result = new MeshJoined(cubits);
361

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

    
371
      return result;
372
      }
373
}
(2-2/3)