Project

General

Profile

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

examples / src / main / java / org / distorted / examples / singlemesh / SingleMeshRenderer.java @ 6979a0e0

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.MeshSquare;
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
      VertexEffectRotate.enable();
186

    
187
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
188
      }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

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

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

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

    
203
      mAngle.set(360);
204

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

    
209
      mAngleDyn.resetToBeginning();
210
      }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

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

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

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

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

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

    
240
      return bitmap;
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

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

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

    
260
      MeshBase mesh = new MeshJoined(meshes);
261

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

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

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

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

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

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

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

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

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

    
334
      mesh.mergeEffComponents();
335

    
336
      return mesh;
337
      }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

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

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

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

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

    
359
      MeshBase result = new MeshJoined(cubits);
360

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

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