Project

General

Profile

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

examples / src / main / java / org / distorted / examples / singlemesh / SingleMeshRenderer.java @ 51e08404

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
54
{
55
    private static final float DIST = 0.5f;
56

    
57
    private static final int[] FACE_COLORS = new int[]
58
         {
59
           0xffffff00, 0xffffffff,   // (right-YELLOW) (left  -WHITE)
60
           0xff0000ff, 0xff00ff00,   // (top  -BLUE  ) (bottom-GREEN)
61
           0xffff0000, 0xffb5651d    // (front-RED   ) (back  -BROWN)
62
         };
63

    
64
    private static final int NUM_FACES = FACE_COLORS.length;
65

    
66
    private static final Static4D RIG_MAP = new Static4D(0.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
67
    private static final Static4D LEF_MAP = new Static4D(1.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
68
    private static final Static4D TOP_MAP = new Static4D(2.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
69
    private static final Static4D BOT_MAP = new Static4D(3.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
70
    private static final Static4D FRO_MAP = new Static4D(4.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
71
    private static final Static4D BAC_MAP = new Static4D(5.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
72
    private static final Static4D INT_MAP = new Static4D(6.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
73

    
74
    private static final Static4D[][] TEXTURE_MAP = new Static4D[][]
75
         {
76
             {  INT_MAP, LEF_MAP, INT_MAP, BOT_MAP, INT_MAP, BAC_MAP },
77
             {  INT_MAP, LEF_MAP, INT_MAP, BOT_MAP, FRO_MAP, INT_MAP },
78
             {  INT_MAP, LEF_MAP, TOP_MAP, INT_MAP, INT_MAP, BAC_MAP },
79
             {  INT_MAP, LEF_MAP, TOP_MAP, INT_MAP, FRO_MAP, INT_MAP },
80
             {  RIG_MAP, INT_MAP, INT_MAP, BOT_MAP, INT_MAP, BAC_MAP },
81
             {  RIG_MAP, INT_MAP, INT_MAP, BOT_MAP, FRO_MAP, INT_MAP },
82
             {  RIG_MAP, INT_MAP, TOP_MAP, INT_MAP, INT_MAP, BAC_MAP },
83
             {  RIG_MAP, INT_MAP, TOP_MAP, INT_MAP, FRO_MAP, INT_MAP }
84
         };
85

    
86
    private static final Static3D[] CUBIT_MOVES = new Static3D[]
87
         {
88
           new Static3D(-DIST,-DIST,-DIST),
89
           new Static3D(-DIST,-DIST,+DIST),
90
           new Static3D(-DIST,+DIST,-DIST),
91
           new Static3D(-DIST,+DIST,+DIST),
92
           new Static3D(+DIST,-DIST,-DIST),
93
           new Static3D(+DIST,-DIST,+DIST),
94
           new Static3D(+DIST,+DIST,-DIST),
95
           new Static3D(+DIST,+DIST,+DIST),
96
         };
97

    
98
    private GLSurfaceView mView;
99
    private DistortedTexture mTexture;
100
    private DistortedScreen mScreen;
101
    private DistortedEffects mEffects;
102
    private Static3D mScale;
103
    private MeshBase mMesh;
104
    private VertexEffectRotate mRotate;
105
    private Dynamic1D mAngleDyn;
106
    private Static1D mAngle;
107

    
108
    Static4D mQuat1, mQuat2;
109
    int mScreenMin;
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
    SingleMeshRenderer(GLSurfaceView v)
114
      {
115
      mView = v;
116
      mScreen = new DistortedScreen();
117
      mScale= new Static3D(1,1,1);
118
      Static3D center=new Static3D(0,0,0);
119

    
120
      Dynamic1D sink = new Dynamic1D(5000,0.0f);
121
      sink.add( new Static1D(0.5f) );
122
      sink.add( new Static1D(2.0f) );
123

    
124
      mQuat1 = new Static4D(0,0,0,1);  // unity
125
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
126

    
127
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
128
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
129

    
130
      quatInt1.add(mQuat1);
131
      quatInt2.add(mQuat2);
132

    
133
      mAngle = new Static1D(0);
134

    
135
      mAngleDyn = new Dynamic1D(2000,0.5f);
136
      mAngleDyn.add(new Static1D(0));
137
      mAngleDyn.add( mAngle );
138

    
139
      mRotate = new VertexEffectRotate( mAngleDyn, new Static3D(1,0,0), new Static3D(0,0,0) );
140

    
141
      mEffects = new DistortedEffects();
142
      mEffects.apply( mRotate );
143
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
144
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
145
      mEffects.apply( new MatrixEffectScale(mScale));
146

    
147
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
148
      }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
   
152
    public void onDrawFrame(GL10 glUnused) 
153
      {
154
      mScreen.render( System.currentTimeMillis() );
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
    
159
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
160
      {
161
      final float SCALE = 0.3f;
162
      mScreenMin = Math.min(width, height);
163
      float factor = SCALE*mScreenMin;
164
      mScale.set(factor,factor,factor);
165
      mScreen.resize(width, height);
166
      }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
    
170
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
171
      {
172
      if( mTexture==null ) mTexture = new DistortedTexture();
173
      mTexture.setTexture( createTexture() );
174

    
175
      if( mMesh==null ) mMesh = createMesh();
176

    
177
      mScreen.detachAll();
178
      mScreen.attach(mTexture,mEffects,mMesh);
179

    
180
      DistortedLibrary.setMax(EffectType.VERTEX, 15);
181
      VertexEffectRotate.enable();
182

    
183
      try
184
        {
185
        DistortedLibrary.onCreate(mView.getContext());
186
        }
187
      catch(Exception ex)
188
        {
189
        android.util.Log.e("DeferredJob", ex.getMessage() );
190
        }
191
      }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
    void apply(int number)
196
      {
197
      mRotate.setMeshAssociation(0,number);
198
      mAngle.set(360);
199
      mAngleDyn.resetToBeginning();
200
      }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
    private Bitmap createTexture()
205
      {
206
      final int NUM_FACES = 6;
207
      final int TEXTURE_HEIGHT = 200;
208
      final int INTERIOR_COLOR = 0xff000000;
209
      final float R = TEXTURE_HEIGHT*0.10f;
210
      final float M = TEXTURE_HEIGHT*0.05f;
211

    
212
      Bitmap bitmap;
213
      Paint paint = new Paint();
214
      bitmap = Bitmap.createBitmap( (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
215
      Canvas canvas = new Canvas(bitmap);
216

    
217
      paint.setAntiAlias(true);
218
      paint.setTextAlign(Paint.Align.CENTER);
219
      paint.setStyle(Paint.Style.FILL);
220

    
221
      paint.setColor(INTERIOR_COLOR);
222
      canvas.drawRect(0, 0, (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
223

    
224
      for(int i=0; i<NUM_FACES; i++)
225
        {
226
        paint.setColor(FACE_COLORS[i]);
227
        canvas.drawRoundRect( i*TEXTURE_HEIGHT+M, M, (i+1)*TEXTURE_HEIGHT-M, TEXTURE_HEIGHT-M, R, R, paint);
228
        }
229

    
230
      return bitmap;
231
      }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
     MeshBase createCubitMesh()
236
      {
237
      final int MESHES=6;
238
      int association = 1;
239
      MeshBase[] meshes = new MeshRectangles[MESHES];
240
      meshes[0] = new MeshRectangles(10,10);
241
      meshes[0].setEffectAssociation(0,association,0);
242

    
243
      for(int i=1; i<MESHES; i++)
244
        {
245
        association <<=1;
246
        meshes[i] = meshes[0].copy(true);
247
        meshes[i].setEffectAssociation(0,association,0);
248
        }
249

    
250
      MeshBase mesh = new MeshJoined(meshes);
251

    
252
      Static3D axisY   = new Static3D(0,1,0);
253
      Static3D axisX   = new Static3D(1,0,0);
254
      Static3D center  = new Static3D(0,0,0);
255
      Static1D angle90 = new Static1D(90);
256
      Static1D angle180= new Static1D(180);
257
      Static1D angle270= new Static1D(270);
258

    
259
      float d1 = 1.0f;
260
      float d2 =-0.05f;
261
      float d3 = 0.12f;
262

    
263
      Static3D dCen0 = new Static3D( d1*(+0.5f), d1*(+0.5f), d1*(+0.5f) );
264
      Static3D dCen1 = new Static3D( d1*(+0.5f), d1*(+0.5f), d1*(-0.5f) );
265
      Static3D dCen2 = new Static3D( d1*(+0.5f), d1*(-0.5f), d1*(+0.5f) );
266
      Static3D dCen3 = new Static3D( d1*(+0.5f), d1*(-0.5f), d1*(-0.5f) );
267
      Static3D dCen4 = new Static3D( d1*(-0.5f), d1*(+0.5f), d1*(+0.5f) );
268
      Static3D dCen5 = new Static3D( d1*(-0.5f), d1*(+0.5f), d1*(-0.5f) );
269
      Static3D dCen6 = new Static3D( d1*(-0.5f), d1*(-0.5f), d1*(+0.5f) );
270
      Static3D dCen7 = new Static3D( d1*(-0.5f), d1*(-0.5f), d1*(-0.5f) );
271

    
272
      Static3D dVec0 = new Static3D( d2*(+0.5f), d2*(+0.5f), d2*(+0.5f) );
273
      Static3D dVec1 = new Static3D( d2*(+0.5f), d2*(+0.5f), d2*(-0.5f) );
274
      Static3D dVec2 = new Static3D( d2*(+0.5f), d2*(-0.5f), d2*(+0.5f) );
275
      Static3D dVec3 = new Static3D( d2*(+0.5f), d2*(-0.5f), d2*(-0.5f) );
276
      Static3D dVec4 = new Static3D( d2*(-0.5f), d2*(+0.5f), d2*(+0.5f) );
277
      Static3D dVec5 = new Static3D( d2*(-0.5f), d2*(+0.5f), d2*(-0.5f) );
278
      Static3D dVec6 = new Static3D( d2*(-0.5f), d2*(-0.5f), d2*(+0.5f) );
279
      Static3D dVec7 = new Static3D( d2*(-0.5f), d2*(-0.5f), d2*(-0.5f) );
280

    
281
      Static4D dReg  = new Static4D(0,0,0,d3);
282
      Static1D dRad  = new Static1D(1);
283

    
284
      VertexEffectMove   effect0 = new VertexEffectMove(new Static3D(0,0,+0.5f));
285
      effect0.setMeshAssociation(63,-1);  // all 6 sides
286
      VertexEffectRotate effect1 = new VertexEffectRotate( angle180, axisX, center );
287
      effect1.setMeshAssociation(32,-1);  // back
288
      VertexEffectRotate effect2 = new VertexEffectRotate( angle90 , axisX, center );
289
      effect2.setMeshAssociation( 8,-1);  // bottom
290
      VertexEffectRotate effect3 = new VertexEffectRotate( angle270, axisX, center );
291
      effect3.setMeshAssociation( 4,-1);  // top
292
      VertexEffectRotate effect4 = new VertexEffectRotate( angle270, axisY, center );
293
      effect4.setMeshAssociation( 2,-1);  // left
294
      VertexEffectRotate effect5 = new VertexEffectRotate( angle90 , axisY, center );
295
      effect5.setMeshAssociation( 1,-1);  // right
296

    
297
      VertexEffectDeform effect6 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
298
      VertexEffectDeform effect7 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
299
      VertexEffectDeform effect8 = new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
300
      VertexEffectDeform effect9 = new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
301
      VertexEffectDeform effect10= new VertexEffectDeform(dVec4, dRad, dCen4, dReg);
302
      VertexEffectDeform effect11= new VertexEffectDeform(dVec5, dRad, dCen5, dReg);
303
      VertexEffectDeform effect12= new VertexEffectDeform(dVec6, dRad, dCen6, dReg);
304
      VertexEffectDeform effect13= new VertexEffectDeform(dVec7, dRad, dCen7, dReg);
305

    
306
      VertexEffectSink effect14= new VertexEffectSink( new Static1D(1.5f), center, new Static4D(0,0,0,0.72f) );
307

    
308
      mesh.apply(effect0);
309
      mesh.apply(effect1);
310
      mesh.apply(effect2);
311
      mesh.apply(effect3);
312
      mesh.apply(effect4);
313
      mesh.apply(effect5);
314
      mesh.apply(effect6);
315
      mesh.apply(effect7);
316
      mesh.apply(effect8);
317
      mesh.apply(effect9);
318
      mesh.apply(effect10);
319
      mesh.apply(effect11);
320
      mesh.apply(effect12);
321
      mesh.apply(effect13);
322
      mesh.apply(effect14);
323

    
324
      mesh.mergeEffComponents();
325

    
326
      return mesh;
327
      }
328

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

    
331
    private MeshBase createMesh()
332
      {
333
      final int NUM_CUBITS = CUBIT_MOVES.length;
334
      MeshBase[] cubits = new MeshBase[NUM_CUBITS];
335

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

    
338
      for(int i=0; i<NUM_CUBITS-1; i++)
339
        {
340
        cubits[i] = cubits[NUM_CUBITS-1].copy(true);
341
        }
342

    
343
      for(int i=0; i<NUM_CUBITS; i++)
344
        {
345
        cubits[i].apply( new MatrixEffectMove(CUBIT_MOVES[i]), 1,0);
346
        cubits[i].setTextureMap(TEXTURE_MAP[i]);
347
        }
348

    
349
      MeshBase result = new MeshJoined(cubits);
350

    
351
      return result;
352
      }
353
}
(2-2/3)