Project

General

Profile

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

examples / src / main / java / org / distorted / examples / singlemesh / SingleMeshRenderer.java @ 9cbe58b0

1 90940caf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 758729b5 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
29 90940caf Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
30
import org.distorted.library.effect.MatrixEffectScale;
31 758729b5 Leszek Koltunski
import org.distorted.library.effect.VertexEffectDeform;
32 90940caf Leszek Koltunski
import org.distorted.library.effect.VertexEffectMove;
33
import org.distorted.library.effect.VertexEffectRotate;
34 758729b5 Leszek Koltunski
import org.distorted.library.effect.VertexEffectSink;
35 90940caf Leszek Koltunski
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
54
{
55 758729b5 Leszek Koltunski
    private static final float DIST = 0.5f;
56
57 9cbe58b0 Leszek Koltunski
    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 758729b5 Leszek Koltunski
    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 51e08404 Leszek Koltunski
    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 758729b5 Leszek Koltunski
    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 90940caf Leszek Koltunski
    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 9cbe58b0 Leszek Koltunski
    private Static3D mAxis;
115 90940caf Leszek Koltunski
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
      Dynamic1D sink = new Dynamic1D(5000,0.0f);
129
      sink.add( new Static1D(0.5f) );
130
      sink.add( new Static1D(2.0f) );
131
132 9cbe58b0 Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);
133
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
134 90940caf Leszek Koltunski
135
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
136
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
137
138
      quatInt1.add(mQuat1);
139
      quatInt2.add(mQuat2);
140
141
      mAngle = new Static1D(0);
142 9cbe58b0 Leszek Koltunski
      mAxis  = new Static3D(1,0,0);
143 90940caf Leszek Koltunski
144
      mAngleDyn = new Dynamic1D(2000,0.5f);
145
      mAngleDyn.add(new Static1D(0));
146
      mAngleDyn.add( mAngle );
147
148 9cbe58b0 Leszek Koltunski
      mRotate = new VertexEffectRotate( mAngleDyn, mAxis, new Static3D(0,0,0) );
149 90940caf Leszek Koltunski
150
      mEffects = new DistortedEffects();
151
      mEffects.apply( mRotate );
152
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
153
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
154
      mEffects.apply( new MatrixEffectScale(mScale));
155
156
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
157
      }
158
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
   
161
    public void onDrawFrame(GL10 glUnused) 
162
      {
163
      mScreen.render( System.currentTimeMillis() );
164
      }
165
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
    
168
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
169
      {
170 5acaa863 Leszek Koltunski
      final float SCALE = 0.3f;
171 90940caf Leszek Koltunski
      mScreenMin = Math.min(width, height);
172
      float factor = SCALE*mScreenMin;
173
      mScale.set(factor,factor,factor);
174
      mScreen.resize(width, height);
175
      }
176
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
    
179
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
180
      {
181
      if( mTexture==null ) mTexture = new DistortedTexture();
182
      mTexture.setTexture( createTexture() );
183
184
      if( mMesh==null ) mMesh = createMesh();
185
186
      mScreen.detachAll();
187
      mScreen.attach(mTexture,mEffects,mMesh);
188
189 758729b5 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX, 15);
190 90940caf Leszek Koltunski
      VertexEffectRotate.enable();
191
192
      try
193
        {
194
        DistortedLibrary.onCreate(mView.getContext());
195
        }
196
      catch(Exception ex)
197
        {
198
        android.util.Log.e("DeferredJob", ex.getMessage() );
199
        }
200
      }
201
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203
204 9cbe58b0 Leszek Koltunski
    void apply(int andAssoc, int axisIndex)
205 90940caf Leszek Koltunski
      {
206 9cbe58b0 Leszek Koltunski
      mRotate.setMeshAssociation(andAssoc,-1);
207
208 90940caf Leszek Koltunski
      mAngle.set(360);
209 9cbe58b0 Leszek Koltunski
210
      mAxis.set0(AXIS[axisIndex].get0());
211
      mAxis.set1(AXIS[axisIndex].get1());
212
      mAxis.set2(AXIS[axisIndex].get2());
213
214 90940caf Leszek Koltunski
      mAngleDyn.resetToBeginning();
215
      }
216
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218
219
    private Bitmap createTexture()
220
      {
221 758729b5 Leszek Koltunski
      final int NUM_FACES = 6;
222
      final int TEXTURE_HEIGHT = 200;
223
      final int INTERIOR_COLOR = 0xff000000;
224
      final float R = TEXTURE_HEIGHT*0.10f;
225
      final float M = TEXTURE_HEIGHT*0.05f;
226 90940caf Leszek Koltunski
227 758729b5 Leszek Koltunski
      Bitmap bitmap;
228 90940caf Leszek Koltunski
      Paint paint = new Paint();
229 758729b5 Leszek Koltunski
      bitmap = Bitmap.createBitmap( (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
230
      Canvas canvas = new Canvas(bitmap);
231
232
      paint.setAntiAlias(true);
233
      paint.setTextAlign(Paint.Align.CENTER);
234 90940caf Leszek Koltunski
      paint.setStyle(Paint.Style.FILL);
235
236 758729b5 Leszek Koltunski
      paint.setColor(INTERIOR_COLOR);
237
      canvas.drawRect(0, 0, (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
238
239
      for(int i=0; i<NUM_FACES; i++)
240 90940caf Leszek Koltunski
        {
241
        paint.setColor(FACE_COLORS[i]);
242 758729b5 Leszek Koltunski
        canvas.drawRoundRect( i*TEXTURE_HEIGHT+M, M, (i+1)*TEXTURE_HEIGHT-M, TEXTURE_HEIGHT-M, R, R, paint);
243 90940caf Leszek Koltunski
        }
244
245 758729b5 Leszek Koltunski
      return bitmap;
246 90940caf Leszek Koltunski
      }
247
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249
250 758729b5 Leszek Koltunski
     MeshBase createCubitMesh()
251 90940caf Leszek Koltunski
      {
252 758729b5 Leszek Koltunski
      final int MESHES=6;
253
      int association = 1;
254 90940caf Leszek Koltunski
      MeshBase[] meshes = new MeshRectangles[MESHES];
255 758729b5 Leszek Koltunski
      meshes[0] = new MeshRectangles(10,10);
256
      meshes[0].setEffectAssociation(0,association,0);
257 90940caf Leszek Koltunski
258
      for(int i=1; i<MESHES; i++)
259
        {
260 758729b5 Leszek Koltunski
        association <<=1;
261 90940caf Leszek Koltunski
        meshes[i] = meshes[0].copy(true);
262 758729b5 Leszek Koltunski
        meshes[i].setEffectAssociation(0,association,0);
263 90940caf Leszek Koltunski
        }
264
265 758729b5 Leszek Koltunski
      MeshBase mesh = new MeshJoined(meshes);
266
267
      Static3D axisY   = new Static3D(0,1,0);
268
      Static3D axisX   = new Static3D(1,0,0);
269
      Static3D center  = new Static3D(0,0,0);
270
      Static1D angle90 = new Static1D(90);
271
      Static1D angle180= new Static1D(180);
272
      Static1D angle270= new Static1D(270);
273
274
      float d1 = 1.0f;
275
      float d2 =-0.05f;
276
      float d3 = 0.12f;
277
278
      Static3D dCen0 = new Static3D( d1*(+0.5f), d1*(+0.5f), d1*(+0.5f) );
279
      Static3D dCen1 = new Static3D( d1*(+0.5f), d1*(+0.5f), d1*(-0.5f) );
280
      Static3D dCen2 = new Static3D( d1*(+0.5f), d1*(-0.5f), d1*(+0.5f) );
281
      Static3D dCen3 = new Static3D( d1*(+0.5f), d1*(-0.5f), d1*(-0.5f) );
282
      Static3D dCen4 = new Static3D( d1*(-0.5f), d1*(+0.5f), d1*(+0.5f) );
283
      Static3D dCen5 = new Static3D( d1*(-0.5f), d1*(+0.5f), d1*(-0.5f) );
284
      Static3D dCen6 = new Static3D( d1*(-0.5f), d1*(-0.5f), d1*(+0.5f) );
285
      Static3D dCen7 = new Static3D( d1*(-0.5f), d1*(-0.5f), d1*(-0.5f) );
286
287
      Static3D dVec0 = new Static3D( d2*(+0.5f), d2*(+0.5f), d2*(+0.5f) );
288
      Static3D dVec1 = new Static3D( d2*(+0.5f), d2*(+0.5f), d2*(-0.5f) );
289
      Static3D dVec2 = new Static3D( d2*(+0.5f), d2*(-0.5f), d2*(+0.5f) );
290
      Static3D dVec3 = new Static3D( d2*(+0.5f), d2*(-0.5f), d2*(-0.5f) );
291
      Static3D dVec4 = new Static3D( d2*(-0.5f), d2*(+0.5f), d2*(+0.5f) );
292
      Static3D dVec5 = new Static3D( d2*(-0.5f), d2*(+0.5f), d2*(-0.5f) );
293
      Static3D dVec6 = new Static3D( d2*(-0.5f), d2*(-0.5f), d2*(+0.5f) );
294
      Static3D dVec7 = new Static3D( d2*(-0.5f), d2*(-0.5f), d2*(-0.5f) );
295
296
      Static4D dReg  = new Static4D(0,0,0,d3);
297
      Static1D dRad  = new Static1D(1);
298
299
      VertexEffectMove   effect0 = new VertexEffectMove(new Static3D(0,0,+0.5f));
300
      effect0.setMeshAssociation(63,-1);  // all 6 sides
301
      VertexEffectRotate effect1 = new VertexEffectRotate( angle180, axisX, center );
302
      effect1.setMeshAssociation(32,-1);  // back
303
      VertexEffectRotate effect2 = new VertexEffectRotate( angle90 , axisX, center );
304
      effect2.setMeshAssociation( 8,-1);  // bottom
305
      VertexEffectRotate effect3 = new VertexEffectRotate( angle270, axisX, center );
306
      effect3.setMeshAssociation( 4,-1);  // top
307
      VertexEffectRotate effect4 = new VertexEffectRotate( angle270, axisY, center );
308
      effect4.setMeshAssociation( 2,-1);  // left
309
      VertexEffectRotate effect5 = new VertexEffectRotate( angle90 , axisY, center );
310
      effect5.setMeshAssociation( 1,-1);  // right
311
312
      VertexEffectDeform effect6 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
313
      VertexEffectDeform effect7 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
314
      VertexEffectDeform effect8 = new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
315
      VertexEffectDeform effect9 = new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
316
      VertexEffectDeform effect10= new VertexEffectDeform(dVec4, dRad, dCen4, dReg);
317
      VertexEffectDeform effect11= new VertexEffectDeform(dVec5, dRad, dCen5, dReg);
318
      VertexEffectDeform effect12= new VertexEffectDeform(dVec6, dRad, dCen6, dReg);
319
      VertexEffectDeform effect13= new VertexEffectDeform(dVec7, dRad, dCen7, dReg);
320
321
      VertexEffectSink effect14= new VertexEffectSink( new Static1D(1.5f), center, new Static4D(0,0,0,0.72f) );
322
323
      mesh.apply(effect0);
324
      mesh.apply(effect1);
325
      mesh.apply(effect2);
326
      mesh.apply(effect3);
327
      mesh.apply(effect4);
328
      mesh.apply(effect5);
329
      mesh.apply(effect6);
330
      mesh.apply(effect7);
331
      mesh.apply(effect8);
332
      mesh.apply(effect9);
333
      mesh.apply(effect10);
334
      mesh.apply(effect11);
335
      mesh.apply(effect12);
336
      mesh.apply(effect13);
337
      mesh.apply(effect14);
338
339
      mesh.mergeEffComponents();
340
341
      return mesh;
342
      }
343 90940caf Leszek Koltunski
344 758729b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
345 90940caf Leszek Koltunski
346 758729b5 Leszek Koltunski
    private MeshBase createMesh()
347
      {
348
      final int NUM_CUBITS = CUBIT_MOVES.length;
349
      MeshBase[] cubits = new MeshBase[NUM_CUBITS];
350 90940caf Leszek Koltunski
351 5acaa863 Leszek Koltunski
      cubits[NUM_CUBITS-1] = createCubitMesh();   // NUM_CUBITS-1 (or anything non-zero!)
352 90940caf Leszek Koltunski
353 5acaa863 Leszek Koltunski
      for(int i=0; i<NUM_CUBITS-1; i++)
354 758729b5 Leszek Koltunski
        {
355 5acaa863 Leszek Koltunski
        cubits[i] = cubits[NUM_CUBITS-1].copy(true);
356 758729b5 Leszek Koltunski
        }
357 90940caf Leszek Koltunski
358 758729b5 Leszek Koltunski
      for(int i=0; i<NUM_CUBITS; i++)
359
        {
360
        cubits[i].apply( new MatrixEffectMove(CUBIT_MOVES[i]), 1,0);
361 51e08404 Leszek Koltunski
        cubits[i].setTextureMap(TEXTURE_MAP[i]);
362 758729b5 Leszek Koltunski
        }
363 90940caf Leszek Koltunski
364 758729b5 Leszek Koltunski
      MeshBase result = new MeshJoined(cubits);
365 90940caf Leszek Koltunski
366 9cbe58b0 Leszek Koltunski
      result.setEffectAssociation( 0, (1<<4) + (1<<2) + 1, 0);
367
      result.setEffectAssociation( 1, (1<<4) + (1<<2) + 2, 0);
368
      result.setEffectAssociation( 2, (1<<4) + (2<<2) + 1, 0);
369
      result.setEffectAssociation( 3, (1<<4) + (2<<2) + 2, 0);
370
      result.setEffectAssociation( 4, (2<<4) + (1<<2) + 1, 0);
371
      result.setEffectAssociation( 5, (2<<4) + (1<<2) + 2, 0);
372
      result.setEffectAssociation( 6, (2<<4) + (2<<2) + 1, 0);
373
      result.setEffectAssociation( 7, (2<<4) + (2<<2) + 2, 0);
374
375 758729b5 Leszek Koltunski
      return result;
376 90940caf Leszek Koltunski
      }
377
}