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.app.ActivityManager;
|
23
|
import android.content.Context;
|
24
|
import android.content.pm.ConfigurationInfo;
|
25
|
import android.content.res.Resources;
|
26
|
import android.graphics.Bitmap;
|
27
|
import android.graphics.Canvas;
|
28
|
import android.graphics.Paint;
|
29
|
import android.opengl.GLSurfaceView;
|
30
|
|
31
|
import org.distorted.library.effect.MatrixEffectMove;
|
32
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
33
|
import org.distorted.library.effect.MatrixEffectScale;
|
34
|
import org.distorted.library.effect.VertexEffectDeform;
|
35
|
import org.distorted.library.effect.VertexEffectMove;
|
36
|
import org.distorted.library.effect.VertexEffectRotate;
|
37
|
import org.distorted.library.effect.VertexEffectSink;
|
38
|
import org.distorted.library.main.DistortedEffects;
|
39
|
import org.distorted.library.main.DistortedLibrary;
|
40
|
import org.distorted.library.main.DistortedScreen;
|
41
|
import org.distorted.library.main.DistortedTexture;
|
42
|
import org.distorted.library.mesh.MeshBase;
|
43
|
import org.distorted.library.mesh.MeshJoined;
|
44
|
import org.distorted.library.mesh.MeshSquare;
|
45
|
import org.distorted.library.type.Dynamic1D;
|
46
|
import org.distorted.library.type.DynamicQuat;
|
47
|
import org.distorted.library.type.Static1D;
|
48
|
import org.distorted.library.type.Static3D;
|
49
|
import org.distorted.library.type.Static4D;
|
50
|
|
51
|
import java.io.InputStream;
|
52
|
|
53
|
import javax.microedition.khronos.egl.EGLConfig;
|
54
|
import javax.microedition.khronos.opengles.GL10;
|
55
|
|
56
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
|
58
|
class SingleMeshRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
|
59
|
{
|
60
|
private static final float DIST = 0.5f;
|
61
|
|
62
|
private static final Static3D[] AXIS = new Static3D[]
|
63
|
{
|
64
|
new Static3D(1,0,0),
|
65
|
new Static3D(0,1,0),
|
66
|
new Static3D(0,0,1)
|
67
|
};
|
68
|
|
69
|
private static final int[] FACE_COLORS = new int[]
|
70
|
{
|
71
|
0xffffff00, 0xffffffff, // (right-YELLOW) (left -WHITE)
|
72
|
0xff0000ff, 0xff00ff00, // (top -BLUE ) (bottom-GREEN)
|
73
|
0xffff0000, 0xffb5651d // (front-RED ) (back -BROWN)
|
74
|
};
|
75
|
|
76
|
private static final int NUM_FACES = FACE_COLORS.length;
|
77
|
|
78
|
private static final Static4D RIG_MAP = new Static4D(0.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
|
79
|
private static final Static4D LEF_MAP = new Static4D(1.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
|
80
|
private static final Static4D TOP_MAP = new Static4D(2.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
|
81
|
private static final Static4D BOT_MAP = new Static4D(3.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
|
82
|
private static final Static4D FRO_MAP = new Static4D(4.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
|
83
|
private static final Static4D BAC_MAP = new Static4D(5.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
|
84
|
private static final Static4D INT_MAP = new Static4D(6.0f/(NUM_FACES+1),0.0f,1.0f/(NUM_FACES+1),1.0f);
|
85
|
|
86
|
private static final Static4D[][] TEXTURE_MAP = new Static4D[][]
|
87
|
{
|
88
|
{ INT_MAP, LEF_MAP, INT_MAP, BOT_MAP, INT_MAP, BAC_MAP },
|
89
|
{ INT_MAP, LEF_MAP, INT_MAP, BOT_MAP, FRO_MAP, INT_MAP },
|
90
|
{ INT_MAP, LEF_MAP, TOP_MAP, INT_MAP, INT_MAP, BAC_MAP },
|
91
|
{ INT_MAP, LEF_MAP, TOP_MAP, INT_MAP, FRO_MAP, INT_MAP },
|
92
|
{ RIG_MAP, INT_MAP, INT_MAP, BOT_MAP, INT_MAP, BAC_MAP },
|
93
|
{ RIG_MAP, INT_MAP, INT_MAP, BOT_MAP, FRO_MAP, INT_MAP },
|
94
|
{ RIG_MAP, INT_MAP, TOP_MAP, INT_MAP, INT_MAP, BAC_MAP },
|
95
|
{ RIG_MAP, INT_MAP, TOP_MAP, INT_MAP, FRO_MAP, INT_MAP }
|
96
|
};
|
97
|
|
98
|
private static final Static3D[] CUBIT_MOVES = new Static3D[]
|
99
|
{
|
100
|
new Static3D(-DIST,-DIST,-DIST),
|
101
|
new Static3D(-DIST,-DIST,+DIST),
|
102
|
new Static3D(-DIST,+DIST,-DIST),
|
103
|
new Static3D(-DIST,+DIST,+DIST),
|
104
|
new Static3D(+DIST,-DIST,-DIST),
|
105
|
new Static3D(+DIST,-DIST,+DIST),
|
106
|
new Static3D(+DIST,+DIST,-DIST),
|
107
|
new Static3D(+DIST,+DIST,+DIST),
|
108
|
};
|
109
|
|
110
|
private final Resources mResources;
|
111
|
private final DistortedScreen mScreen;
|
112
|
private final DistortedEffects mEffects;
|
113
|
private final Static3D mScale;
|
114
|
private final VertexEffectRotate mRotate;
|
115
|
private final Dynamic1D mAngleDyn;
|
116
|
private final Static1D mAngle;
|
117
|
private final Static3D mAxis;
|
118
|
|
119
|
private DistortedTexture mTexture;
|
120
|
private MeshBase mMesh;
|
121
|
|
122
|
Static4D mQuat1, mQuat2;
|
123
|
int mScreenMin;
|
124
|
|
125
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126
|
|
127
|
SingleMeshRenderer(GLSurfaceView v)
|
128
|
{
|
129
|
mResources = v.getResources();
|
130
|
|
131
|
mScreen = new DistortedScreen();
|
132
|
mScale= new Static3D(1,1,1);
|
133
|
Static3D center=new Static3D(0,0,0);
|
134
|
|
135
|
mQuat1 = new Static4D(0,0,0,1);
|
136
|
mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
|
137
|
|
138
|
DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
|
139
|
DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
|
140
|
|
141
|
quatInt1.add(mQuat1);
|
142
|
quatInt2.add(mQuat2);
|
143
|
|
144
|
mAngle = new Static1D(0);
|
145
|
mAxis = new Static3D(1,0,0);
|
146
|
|
147
|
mAngleDyn = new Dynamic1D(2000,0.5f);
|
148
|
mAngleDyn.add(new Static1D(0));
|
149
|
mAngleDyn.add( mAngle );
|
150
|
|
151
|
mRotate = new VertexEffectRotate( mAngleDyn, mAxis, new Static3D(0,0,0) );
|
152
|
|
153
|
mEffects = new DistortedEffects();
|
154
|
mEffects.apply( mRotate );
|
155
|
mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
|
156
|
mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
|
157
|
mEffects.apply( new MatrixEffectScale(mScale));
|
158
|
|
159
|
mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
160
|
}
|
161
|
|
162
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
163
|
|
164
|
public void onDrawFrame(GL10 glUnused)
|
165
|
{
|
166
|
mScreen.render( System.currentTimeMillis() );
|
167
|
}
|
168
|
|
169
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
170
|
|
171
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
172
|
{
|
173
|
final float SCALE = 0.3f;
|
174
|
mScreenMin = Math.min(width, height);
|
175
|
float factor = SCALE*mScreenMin;
|
176
|
mScale.set(factor,factor,factor);
|
177
|
mScreen.resize(width, height);
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181
|
|
182
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
183
|
{
|
184
|
if( mTexture==null ) mTexture = new DistortedTexture();
|
185
|
mTexture.setTexture( createTexture() );
|
186
|
|
187
|
if( mMesh==null ) mMesh = createMesh();
|
188
|
|
189
|
mScreen.detachAll();
|
190
|
mScreen.attach(mTexture,mEffects,mMesh);
|
191
|
|
192
|
VertexEffectRotate.enable();
|
193
|
|
194
|
DistortedLibrary.onSurfaceCreated(this);
|
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
|
|
373
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
374
|
|
375
|
public void distortedException(Exception ex)
|
376
|
{
|
377
|
android.util.Log.e("SingleMesh", ex.getMessage() );
|
378
|
}
|
379
|
|
380
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
381
|
|
382
|
public InputStream localFile(int fileID)
|
383
|
{
|
384
|
return mResources.openRawResource(fileID);
|
385
|
}
|
386
|
|
387
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
388
|
|
389
|
public void logMessage(String message)
|
390
|
{
|
391
|
android.util.Log.e("SingleMesh", message );
|
392
|
}
|
393
|
}
|