Project

General

Profile

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

examples / src / main / java / org / distorted / examples / singlemesh / SingleMeshRenderer.java @ 758729b5

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
    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 Static3D[] CUBIT_MOVES = new Static3D[]
65
         {
66
           new Static3D(-DIST,-DIST,-DIST),
67
           new Static3D(-DIST,-DIST,+DIST),
68
           new Static3D(-DIST,+DIST,-DIST),
69
           new Static3D(-DIST,+DIST,+DIST),
70
           new Static3D(+DIST,-DIST,-DIST),
71
           new Static3D(+DIST,-DIST,+DIST),
72
           new Static3D(+DIST,+DIST,-DIST),
73
           new Static3D(+DIST,+DIST,+DIST),
74
         };
75
76 90940caf Leszek Koltunski
    private GLSurfaceView mView;
77
    private DistortedTexture mTexture;
78
    private DistortedScreen mScreen;
79
    private DistortedEffects mEffects;
80
    private Static3D mScale;
81
    private MeshBase mMesh;
82
    private VertexEffectRotate mRotate;
83
    private Dynamic1D mAngleDyn;
84
    private Static1D mAngle;
85
86
    Static4D mQuat1, mQuat2;
87
    int mScreenMin;
88
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91
    SingleMeshRenderer(GLSurfaceView v)
92
      {
93
      mView = v;
94
      mScreen = new DistortedScreen();
95
      mScale= new Static3D(1,1,1);
96
      Static3D center=new Static3D(0,0,0);
97
98
      Dynamic1D sink = new Dynamic1D(5000,0.0f);
99
      sink.add( new Static1D(0.5f) );
100
      sink.add( new Static1D(2.0f) );
101
102
      mQuat1 = new Static4D(0,0,0,1);  // unity
103
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
104
105
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
106
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
107
108
      quatInt1.add(mQuat1);
109
      quatInt2.add(mQuat2);
110
111
      mAngle = new Static1D(0);
112
113
      mAngleDyn = new Dynamic1D(2000,0.5f);
114
      mAngleDyn.add(new Static1D(0));
115
      mAngleDyn.add( mAngle );
116
117
      mRotate = new VertexEffectRotate( mAngleDyn, new Static3D(1,0,0), new Static3D(0,0,0) );
118
119
      mEffects = new DistortedEffects();
120
      mEffects.apply( mRotate );
121
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
122
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
123
      mEffects.apply( new MatrixEffectScale(mScale));
124
125
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
126
      }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
   
130
    public void onDrawFrame(GL10 glUnused) 
131
      {
132
      mScreen.render( System.currentTimeMillis() );
133
      }
134
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
    
137
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
138
      {
139
      final float SCALE = 0.4f;
140
      mScreenMin = Math.min(width, height);
141
      float factor = SCALE*mScreenMin;
142
      mScale.set(factor,factor,factor);
143
      mScreen.resize(width, height);
144
      }
145
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
    
148
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
149
      {
150
      if( mTexture==null ) mTexture = new DistortedTexture();
151
      mTexture.setTexture( createTexture() );
152
153
      if( mMesh==null ) mMesh = createMesh();
154
155
      mScreen.detachAll();
156
      mScreen.attach(mTexture,mEffects,mMesh);
157
158 758729b5 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX, 15);
159 90940caf Leszek Koltunski
      VertexEffectRotate.enable();
160
161
      try
162
        {
163
        DistortedLibrary.onCreate(mView.getContext());
164
        }
165
      catch(Exception ex)
166
        {
167
        android.util.Log.e("DeferredJob", ex.getMessage() );
168
        }
169
      }
170
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
173
    void apply(int number)
174
      {
175
      mRotate.setMeshAssociation(0,number);
176
      mAngle.set(360);
177
      mAngleDyn.resetToBeginning();
178
      }
179
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
182
    private Bitmap createTexture()
183
      {
184 758729b5 Leszek Koltunski
      final int NUM_FACES = 6;
185
      final int TEXTURE_HEIGHT = 200;
186
      final int INTERIOR_COLOR = 0xff000000;
187
      final float R = TEXTURE_HEIGHT*0.10f;
188
      final float M = TEXTURE_HEIGHT*0.05f;
189 90940caf Leszek Koltunski
190 758729b5 Leszek Koltunski
      Bitmap bitmap;
191 90940caf Leszek Koltunski
      Paint paint = new Paint();
192 758729b5 Leszek Koltunski
      bitmap = Bitmap.createBitmap( (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
193
      Canvas canvas = new Canvas(bitmap);
194
195
      paint.setAntiAlias(true);
196
      paint.setTextAlign(Paint.Align.CENTER);
197 90940caf Leszek Koltunski
      paint.setStyle(Paint.Style.FILL);
198
199 758729b5 Leszek Koltunski
      paint.setColor(INTERIOR_COLOR);
200
      canvas.drawRect(0, 0, (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
201
202
      for(int i=0; i<NUM_FACES; i++)
203 90940caf Leszek Koltunski
        {
204
        paint.setColor(FACE_COLORS[i]);
205 758729b5 Leszek Koltunski
        canvas.drawRoundRect( i*TEXTURE_HEIGHT+M, M, (i+1)*TEXTURE_HEIGHT-M, TEXTURE_HEIGHT-M, R, R, paint);
206 90940caf Leszek Koltunski
        }
207
208 758729b5 Leszek Koltunski
      return bitmap;
209 90940caf Leszek Koltunski
      }
210
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
213 758729b5 Leszek Koltunski
     MeshBase createCubitMesh()
214 90940caf Leszek Koltunski
      {
215 758729b5 Leszek Koltunski
      final int MESHES=6;
216
      int association = 1;
217 90940caf Leszek Koltunski
      MeshBase[] meshes = new MeshRectangles[MESHES];
218 758729b5 Leszek Koltunski
      meshes[0] = new MeshRectangles(10,10);
219
      meshes[0].setEffectAssociation(0,association,0);
220 90940caf Leszek Koltunski
221
      for(int i=1; i<MESHES; i++)
222
        {
223 758729b5 Leszek Koltunski
        association <<=1;
224 90940caf Leszek Koltunski
        meshes[i] = meshes[0].copy(true);
225 758729b5 Leszek Koltunski
        meshes[i].setEffectAssociation(0,association,0);
226 90940caf Leszek Koltunski
        }
227
228 758729b5 Leszek Koltunski
      MeshBase mesh = new MeshJoined(meshes);
229
230
      Static3D axisY   = new Static3D(0,1,0);
231
      Static3D axisX   = new Static3D(1,0,0);
232
      Static3D center  = new Static3D(0,0,0);
233
      Static1D angle90 = new Static1D(90);
234
      Static1D angle180= new Static1D(180);
235
      Static1D angle270= new Static1D(270);
236
237
      float d1 = 1.0f;
238
      float d2 =-0.05f;
239
      float d3 = 0.12f;
240
241
      Static3D dCen0 = new Static3D( d1*(+0.5f), d1*(+0.5f), d1*(+0.5f) );
242
      Static3D dCen1 = new Static3D( d1*(+0.5f), d1*(+0.5f), d1*(-0.5f) );
243
      Static3D dCen2 = new Static3D( d1*(+0.5f), d1*(-0.5f), d1*(+0.5f) );
244
      Static3D dCen3 = new Static3D( d1*(+0.5f), d1*(-0.5f), d1*(-0.5f) );
245
      Static3D dCen4 = new Static3D( d1*(-0.5f), d1*(+0.5f), d1*(+0.5f) );
246
      Static3D dCen5 = new Static3D( d1*(-0.5f), d1*(+0.5f), d1*(-0.5f) );
247
      Static3D dCen6 = new Static3D( d1*(-0.5f), d1*(-0.5f), d1*(+0.5f) );
248
      Static3D dCen7 = new Static3D( d1*(-0.5f), d1*(-0.5f), d1*(-0.5f) );
249
250
      Static3D dVec0 = new Static3D( d2*(+0.5f), d2*(+0.5f), d2*(+0.5f) );
251
      Static3D dVec1 = new Static3D( d2*(+0.5f), d2*(+0.5f), d2*(-0.5f) );
252
      Static3D dVec2 = new Static3D( d2*(+0.5f), d2*(-0.5f), d2*(+0.5f) );
253
      Static3D dVec3 = new Static3D( d2*(+0.5f), d2*(-0.5f), d2*(-0.5f) );
254
      Static3D dVec4 = new Static3D( d2*(-0.5f), d2*(+0.5f), d2*(+0.5f) );
255
      Static3D dVec5 = new Static3D( d2*(-0.5f), d2*(+0.5f), d2*(-0.5f) );
256
      Static3D dVec6 = new Static3D( d2*(-0.5f), d2*(-0.5f), d2*(+0.5f) );
257
      Static3D dVec7 = new Static3D( d2*(-0.5f), d2*(-0.5f), d2*(-0.5f) );
258
259
      Static4D dReg  = new Static4D(0,0,0,d3);
260
      Static1D dRad  = new Static1D(1);
261
262
      VertexEffectMove   effect0 = new VertexEffectMove(new Static3D(0,0,+0.5f));
263
      effect0.setMeshAssociation(63,-1);  // all 6 sides
264
      VertexEffectRotate effect1 = new VertexEffectRotate( angle180, axisX, center );
265
      effect1.setMeshAssociation(32,-1);  // back
266
      VertexEffectRotate effect2 = new VertexEffectRotate( angle90 , axisX, center );
267
      effect2.setMeshAssociation( 8,-1);  // bottom
268
      VertexEffectRotate effect3 = new VertexEffectRotate( angle270, axisX, center );
269
      effect3.setMeshAssociation( 4,-1);  // top
270
      VertexEffectRotate effect4 = new VertexEffectRotate( angle270, axisY, center );
271
      effect4.setMeshAssociation( 2,-1);  // left
272
      VertexEffectRotate effect5 = new VertexEffectRotate( angle90 , axisY, center );
273
      effect5.setMeshAssociation( 1,-1);  // right
274
275
      VertexEffectDeform effect6 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
276
      VertexEffectDeform effect7 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
277
      VertexEffectDeform effect8 = new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
278
      VertexEffectDeform effect9 = new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
279
      VertexEffectDeform effect10= new VertexEffectDeform(dVec4, dRad, dCen4, dReg);
280
      VertexEffectDeform effect11= new VertexEffectDeform(dVec5, dRad, dCen5, dReg);
281
      VertexEffectDeform effect12= new VertexEffectDeform(dVec6, dRad, dCen6, dReg);
282
      VertexEffectDeform effect13= new VertexEffectDeform(dVec7, dRad, dCen7, dReg);
283
284
      VertexEffectSink effect14= new VertexEffectSink( new Static1D(1.5f), center, new Static4D(0,0,0,0.72f) );
285
286
      mesh.apply(effect0);
287
      mesh.apply(effect1);
288
      mesh.apply(effect2);
289
      mesh.apply(effect3);
290
      mesh.apply(effect4);
291
      mesh.apply(effect5);
292
      mesh.apply(effect6);
293
      mesh.apply(effect7);
294
      mesh.apply(effect8);
295
      mesh.apply(effect9);
296
      mesh.apply(effect10);
297
      mesh.apply(effect11);
298
      mesh.apply(effect12);
299
      mesh.apply(effect13);
300
      mesh.apply(effect14);
301
302
      mesh.mergeEffComponents();
303
304
      return mesh;
305
      }
306 90940caf Leszek Koltunski
307 758729b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
308 90940caf Leszek Koltunski
309 758729b5 Leszek Koltunski
    private MeshBase createMesh()
310
      {
311
      final int NUM_CUBITS = CUBIT_MOVES.length;
312
      MeshBase[] cubits = new MeshBase[NUM_CUBITS];
313 90940caf Leszek Koltunski
314 758729b5 Leszek Koltunski
      cubits[0] = createCubitMesh();
315 90940caf Leszek Koltunski
316 758729b5 Leszek Koltunski
      for(int i=1; i<NUM_CUBITS; i++)
317
        {
318
        cubits[i] = cubits[0].copy(true);
319
        }
320 90940caf Leszek Koltunski
321 758729b5 Leszek Koltunski
      for(int i=0; i<NUM_CUBITS; i++)
322
        {
323
        cubits[i].apply( new MatrixEffectMove(CUBIT_MOVES[i]), 1,0);
324
        }
325 90940caf Leszek Koltunski
326 758729b5 Leszek Koltunski
      MeshBase result = new MeshJoined(cubits);
327 90940caf Leszek Koltunski
328 758729b5 Leszek Koltunski
      return result;
329 90940caf Leszek Koltunski
      }
330
}