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.meshfile;
|
21
|
|
22
|
import android.content.Context;
|
23
|
import android.content.res.Resources;
|
24
|
import android.graphics.Bitmap;
|
25
|
import android.graphics.Canvas;
|
26
|
import android.graphics.Paint;
|
27
|
import android.opengl.GLSurfaceView;
|
28
|
|
29
|
import org.distorted.examples.R;
|
30
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
31
|
import org.distorted.library.effect.MatrixEffectScale;
|
32
|
import org.distorted.library.main.DistortedEffects;
|
33
|
import org.distorted.library.main.DistortedLibrary;
|
34
|
import org.distorted.library.main.DistortedScreen;
|
35
|
import org.distorted.library.main.DistortedTexture;
|
36
|
import org.distorted.library.mesh.MeshBase;
|
37
|
import org.distorted.library.mesh.MeshFile;
|
38
|
import org.distorted.library.type.DynamicQuat;
|
39
|
import org.distorted.library.type.Static3D;
|
40
|
import org.distorted.library.type.Static4D;
|
41
|
|
42
|
import java.io.DataInputStream;
|
43
|
import java.io.IOException;
|
44
|
import java.io.InputStream;
|
45
|
|
46
|
import javax.microedition.khronos.egl.EGLConfig;
|
47
|
import javax.microedition.khronos.opengles.GL10;
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
class MeshFileRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
|
52
|
{
|
53
|
private final float DEFAULT_SCALE = 0.3f;
|
54
|
|
55
|
private GLSurfaceView mView;
|
56
|
private DistortedTexture mTexture;
|
57
|
private DistortedScreen mScreen;
|
58
|
private DistortedEffects mEffects;
|
59
|
private Static3D mScale;
|
60
|
private long mTime;
|
61
|
private int mBytes;
|
62
|
private int mVertices;
|
63
|
private float mCurrentScale;
|
64
|
|
65
|
Static4D mQuat1, mQuat2;
|
66
|
int mScreenMin;
|
67
|
|
68
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
69
|
|
70
|
MeshFileRenderer(GLSurfaceView v)
|
71
|
{
|
72
|
mView = v;
|
73
|
mScreen = new DistortedScreen();
|
74
|
mScale= new Static3D(1,1,1);
|
75
|
Static3D center=new Static3D(0,0,0);
|
76
|
|
77
|
mCurrentScale = DEFAULT_SCALE;
|
78
|
|
79
|
mQuat1 = new Static4D(0,0,0,1);
|
80
|
mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
|
81
|
|
82
|
DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
|
83
|
DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
|
84
|
|
85
|
quatInt1.add(mQuat1);
|
86
|
quatInt2.add(mQuat2);
|
87
|
|
88
|
mEffects = new DistortedEffects();
|
89
|
mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
|
90
|
mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
|
91
|
mEffects.apply( new MatrixEffectScale(mScale));
|
92
|
|
93
|
mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
94
|
mScreen.showFPS();
|
95
|
}
|
96
|
|
97
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98
|
|
99
|
public void onDrawFrame(GL10 glUnused)
|
100
|
{
|
101
|
mScreen.render( System.currentTimeMillis() );
|
102
|
}
|
103
|
|
104
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
105
|
|
106
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
107
|
{
|
108
|
mScreenMin = Math.min(width, height);
|
109
|
float factor = mCurrentScale*mScreenMin;
|
110
|
mScale.set(factor,factor,factor);
|
111
|
mScreen.resize(width, height);
|
112
|
}
|
113
|
|
114
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
115
|
|
116
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
117
|
{
|
118
|
if( mTexture==null ) mTexture = new DistortedTexture();
|
119
|
|
120
|
DistortedLibrary.onCreate(mView.getContext(), this);
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
public void distortedException(Exception ex)
|
126
|
{
|
127
|
android.util.Log.e("MeshFile", ex.getMessage() );
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
// 0 ---> 0
|
132
|
// 50 ---> DEFAULT_SCALE
|
133
|
// 100 ---> 4*DEFAULT_SCALE
|
134
|
|
135
|
void setScale(int scale)
|
136
|
{
|
137
|
if( scale<= 50 )
|
138
|
{
|
139
|
mCurrentScale = DEFAULT_SCALE * scale / 50.0f;
|
140
|
}
|
141
|
else
|
142
|
{
|
143
|
mCurrentScale = DEFAULT_SCALE * ( 3*(scale/50.0f) - 2.0f);
|
144
|
}
|
145
|
|
146
|
float factor = mCurrentScale*mScreenMin;
|
147
|
mScale.set(factor,factor,factor);
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
void open(int resourceID)
|
153
|
{
|
154
|
if( mTexture==null ) mTexture = new DistortedTexture();
|
155
|
mTexture.setTexture( createTexture(resourceID) );
|
156
|
|
157
|
long t1 = System.currentTimeMillis();
|
158
|
MeshBase mesh = createMesh(resourceID);
|
159
|
long t2 = System.currentTimeMillis();
|
160
|
|
161
|
mTime = t2-t1;
|
162
|
|
163
|
mScreen.detachAll();
|
164
|
mScreen.attach(mTexture,mEffects,mesh);
|
165
|
}
|
166
|
|
167
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
168
|
|
169
|
private Bitmap createTexture(int resourceID)
|
170
|
{
|
171
|
switch(resourceID)
|
172
|
{
|
173
|
case R.raw.deferredjob:
|
174
|
case R.raw.meshjoin : final int[] TET_COLORS4 = new int[] { 0xffffff00,
|
175
|
0xff00ff00,
|
176
|
0xff0000ff,
|
177
|
0xffff0000 };
|
178
|
return createTetrahedrenTexture(TET_COLORS4);
|
179
|
case R.raw.predeform : return createGridTexture(3);
|
180
|
case R.raw.cube2 :
|
181
|
case R.raw.cube3 :
|
182
|
case R.raw.cube4 :
|
183
|
case R.raw.cube5 : final int[] CUBE_COLORS = new int[] { 0xffffff00,
|
184
|
0xffffffff,
|
185
|
0xff0000ff,
|
186
|
0xff00ff00,
|
187
|
0xffff0000,
|
188
|
0xffb5651d };
|
189
|
return createCubeTexture(CUBE_COLORS);
|
190
|
case R.raw.pyra3 :
|
191
|
case R.raw.pyra4 :
|
192
|
case R.raw.pyra5 : final int[] TET_COLORS5 = new int[] { 0xffffff00,
|
193
|
0xff00ff00,
|
194
|
0xff0000ff,
|
195
|
0xffff0000,
|
196
|
0xff000000 };
|
197
|
return createTetrahedrenTexture(TET_COLORS5);
|
198
|
}
|
199
|
|
200
|
return null;
|
201
|
}
|
202
|
|
203
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
204
|
|
205
|
private Bitmap createCubeTexture(int[] faceColors)
|
206
|
{
|
207
|
final int FACES=faceColors.length;
|
208
|
int SIZE = 200;
|
209
|
final float R = SIZE*0.10f;
|
210
|
final float M = SIZE*0.05f;
|
211
|
|
212
|
Bitmap bitmap;
|
213
|
Paint paint = new Paint();
|
214
|
bitmap = Bitmap.createBitmap( (FACES+1)*SIZE, SIZE, Bitmap.Config.ARGB_8888);
|
215
|
Canvas canvas = new Canvas(bitmap);
|
216
|
|
217
|
paint.setStyle(Paint.Style.FILL);
|
218
|
paint.setColor(0xff000000);
|
219
|
canvas.drawRect(0, 0, (FACES+1)*SIZE, SIZE, paint);
|
220
|
|
221
|
for(int face=0; face<FACES; face++)
|
222
|
{
|
223
|
paint.setColor(faceColors[face]);
|
224
|
canvas.drawRoundRect( face*SIZE+M, M, (face+1)*SIZE-M, SIZE-M, R, R, paint);
|
225
|
}
|
226
|
|
227
|
return bitmap;
|
228
|
}
|
229
|
|
230
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
231
|
|
232
|
private Bitmap createGridTexture(int lines)
|
233
|
{
|
234
|
int SIZE = 200;
|
235
|
Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
|
236
|
Canvas canvas = new Canvas(bitmap);
|
237
|
|
238
|
Paint paint = new Paint();
|
239
|
paint.setColor(0xff008800);
|
240
|
paint.setStyle(Paint.Style.FILL);
|
241
|
canvas.drawRect(0, 0, SIZE, SIZE, paint);
|
242
|
paint.setColor(0xffffffff);
|
243
|
|
244
|
for(int i=0; i<=lines ; i++ )
|
245
|
{
|
246
|
int x = (SIZE*i)/lines;
|
247
|
canvas.drawRect(x-1, 0, x+1, SIZE, paint);
|
248
|
canvas.drawRect( 0, x-1, SIZE, x+1, paint);
|
249
|
}
|
250
|
|
251
|
return bitmap;
|
252
|
}
|
253
|
|
254
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
255
|
|
256
|
private Bitmap createTetrahedrenTexture(int[] faceColors)
|
257
|
{
|
258
|
final int FACES=faceColors.length;
|
259
|
int SIZE = 200;
|
260
|
float STROKE = 0.05f*SIZE;
|
261
|
float OFF = STROKE/2 -1;
|
262
|
float OFF2 = 0.5f*SIZE + OFF;
|
263
|
float HEIGHT = SIZE - OFF;
|
264
|
float RADIUS = SIZE/12;
|
265
|
float ARC1_H = 0.2f*SIZE;
|
266
|
float ARC1_W = SIZE*0.5f;
|
267
|
float ARC2_W = 0.153f*SIZE;
|
268
|
float ARC2_H = 0.905f*SIZE;
|
269
|
float ARC3_W = SIZE-ARC2_W;
|
270
|
|
271
|
Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
|
272
|
Canvas canvas = new Canvas(result);
|
273
|
Paint paint = new Paint();
|
274
|
paint.setAntiAlias(true);
|
275
|
paint.setStrokeWidth(STROKE);
|
276
|
|
277
|
for(int i=0; i<FACES; i++)
|
278
|
{
|
279
|
paint.setColor(faceColors[i]);
|
280
|
paint.setStyle(Paint.Style.FILL);
|
281
|
|
282
|
canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
|
283
|
|
284
|
paint.setColor(0xff000000);
|
285
|
paint.setStyle(Paint.Style.STROKE);
|
286
|
|
287
|
canvas.drawLine( i*SIZE, HEIGHT, SIZE +i*SIZE, HEIGHT, paint);
|
288
|
canvas.drawLine( OFF +i*SIZE, SIZE, OFF2 +i*SIZE, 0, paint);
|
289
|
canvas.drawLine((SIZE-OFF)+i*SIZE, SIZE, (SIZE-OFF2) +i*SIZE, 0, paint);
|
290
|
|
291
|
canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
|
292
|
canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
|
293
|
canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
|
294
|
}
|
295
|
|
296
|
return result;
|
297
|
}
|
298
|
|
299
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
300
|
|
301
|
private MeshBase createMesh(int resourceID)
|
302
|
{
|
303
|
Context con = mView.getContext();
|
304
|
Resources res = con.getResources();
|
305
|
InputStream is = res.openRawResource(resourceID);
|
306
|
DataInputStream dos = new DataInputStream(is);
|
307
|
MeshFile mesh = new MeshFile(dos);
|
308
|
|
309
|
mBytes = mesh.getNumBytes();
|
310
|
mVertices = mesh.getNumVertices();
|
311
|
|
312
|
try
|
313
|
{
|
314
|
is.close();
|
315
|
}
|
316
|
catch(IOException e)
|
317
|
{
|
318
|
android.util.Log.e("MeshFile", "Error closing InputStream: "+e.toString());
|
319
|
}
|
320
|
|
321
|
return mesh;
|
322
|
}
|
323
|
|
324
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
325
|
|
326
|
long getTime()
|
327
|
{
|
328
|
return mTime;
|
329
|
}
|
330
|
|
331
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
332
|
|
333
|
int getBytes()
|
334
|
{
|
335
|
return mBytes;
|
336
|
}
|
337
|
|
338
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
339
|
|
340
|
int getVertices()
|
341
|
{
|
342
|
return mVertices;
|
343
|
}
|
344
|
}
|