-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlViewRenderer.java
385 lines (310 loc) · 14.9 KB
/
GlViewRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package com.melcosoft.money_screenholder.glrenderer;
import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLES20;
import com.melcosoft.money_screenholder.R;
import com.melcosoft.money_screenholder.sensors.GyroscopeSensorManager;
import javax.microedition.khronos.opengles.GL10;
import ru.cnv.opengl.renderer.GlRenderer;
import ru.cnv.opengl.features.GLAnimation;
import ru.cnv.opengl.features.GLElementsDrawer;
import ru.cnv.opengl.features.GlModels;
public class GlViewRenderer extends GlRenderer implements
GLAnimation.GlFrameAnimation.GlAnimationFinishListener {
private final String TAG = this.getClass().getSimpleName();
private static final float LEFT_GROUP_TRANS_X = -3f;
private static final float LEFT_GROUP_RANDOM_TRANS_Z_MIN = -8f;
private static final float LEFT_GROUP_RANDOM_TRANS_Z_MAX = 8f;
private static final float RIGHT_GROUP_TRANS_X = 3f;
private static final float RIGHT_GROUP_RANDOM_TRANS_Z_MIN = -8f;
private static final float RIGHT_GROUP_RANDOM_TRANS_Z_MAX = 8f;
private static final int MODELS_ARRAY_CAPACITY = 7;
private static final int ANIMATIONS_ARRAY_CAPACITY = 7;
private static final int TEXTURES_MAX_WIDTH = 1280;
private static final int TEXTURES_MAX_HEIGHT = 1800;
private static final float PERSPECTIVE_ANGLE = 60f;
private static final float PERSPECTIVE_NEAR_PLANE = 0.1f;
private static final float PERSPECTIVE_FAR_PLANE = 60f;
private static final float PERSPECTIVE_ASPECT_RATIO = 0.56f;
private static final float CAMERA_INITIAL_Y = 22f;
private static final float CAMERA_INITIAL_DIR_Y = 0;
private static final float[] CAMERA_INITIAL_ORIENT_VECTOR = {0, 1, 0};
public static final float GYROSCOPE_VALUE_DIVIDER = 50f;
private int mMoneySpeed;
private int mMoneyCount;
private int mMoneyTextureUnitId, mBgTextureUnitId;
private int mProgramId;
private GlModels.GlBufferedModel[] mGlBufferedModels;
private GLAnimation.GlFrameAnimation[] mFrameAnimations;
private volatile float[] mRotationSensorValues;
public GlViewRenderer(Context context) {
super(context);
mGlBufferedModels = new GlModels.GlBufferedModel[MODELS_ARRAY_CAPACITY];
mFrameAnimations = new GLAnimation.GlFrameAnimation[ANIMATIONS_ARRAY_CAPACITY];
mGlRandomGenerator = new GLAnimation.GlRandomGenerator();
mRotationSensorValues = new float[]{0, 0, 0};
}
public void setRotationSensorValues(float[] vector) {
mRotationSensorValues = vector;
}
public void onPreferenceChanged(int texturePref, int countPref) {
PreferenceSelector preferenceSelector = new PreferenceSelector();
if (preferenceSelector.getMoneyTexture(texturePref) != mMoneyTextureUnitId) {
mMoneyTextureUnitId = preferenceSelector.getMoneyTexture(texturePref);
mBgTextureUnitId = preferenceSelector.getBgTexture(texturePref);
onCreateModels();
}
if (preferenceSelector.getMoneyCount(countPref) != mMoneyCount) {
mMoneyCount = preferenceSelector.getMoneyCount(countPref);
mGlBufferedModels = new GlModels.GlBufferedModel[mMoneyCount + 1];
mFrameAnimations = new GLAnimation.GlFrameAnimation[mMoneyCount + 1];
onCreateModels();
onCreateAnimations();
}
}
@Override
protected void onConfigureEnvironment() {
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
GLES20.glClearColor(0f, 1f, 1f, 0f);
}
@Override
public void onCreateProgram() {
String vertexShaderSource = mGlResource.getGlResourceString(R.raw.scene_vertex_shader);
String fragmentShaderSource = mGlResource.getGlResourceString(R.raw.scene_fragment_shader);
int fragmentShaderId = mGlShaders.createFragmentShader(fragmentShaderSource);
int vertexShaderId = mGlShaders.createVertexShader(vertexShaderSource);
mProgramId = mGlProgram.createProgram(vertexShaderId, fragmentShaderId);
// Important! need to be called before setting any uniforms
mGlProgram.useProgram(mProgramId);
}
@Override
public void onCreateTextures() {
Bitmap bgDollarTextureSource = mGlResource.getGlResourceBitmap(context, R.drawable.gl_texture_bg_dollar,
TEXTURES_MAX_WIDTH, TEXTURES_MAX_HEIGHT);
mGlTextures.create2DTexture(bgDollarTextureSource, GLES20.GL_TEXTURE0);
Bitmap dollarTextureSource = mGlResource.getGlResourceBitmap(context, R.drawable.gl_texture_dollars,
TEXTURES_MAX_WIDTH, TEXTURES_MAX_HEIGHT);
mGlTextures.create2DTexture(dollarTextureSource, GLES20.GL_TEXTURE1);
Bitmap bgEuroTextureSource = mGlResource.getGlResourceBitmap(context, R.drawable.gl_texture_bg_euro,
TEXTURES_MAX_WIDTH, TEXTURES_MAX_HEIGHT);
mGlTextures.create2DTexture(bgEuroTextureSource, GLES20.GL_TEXTURE2);
Bitmap euroTextureSource = mGlResource.getGlResourceBitmap(context, R.drawable.gl_texture_euros,
TEXTURES_MAX_WIDTH, TEXTURES_MAX_HEIGHT);
mGlTextures.create2DTexture(euroTextureSource, GLES20.GL_TEXTURE3);
}
@Override
public void onCreateDrawer() {
mGlElementsDrawer = new GLElementsDrawer.GlTexturedElementsDrawer(
mProgramId, PERSPECTIVE_ANGLE, PERSPECTIVE_NEAR_PLANE,
PERSPECTIVE_FAR_PLANE, PERSPECTIVE_ASPECT_RATIO);
}
@Override
public void onCreateModels() {
String objModelSource = mGlResource.getGlResourceString(R.raw.money_two_sided_texture);
GlModels.GlObjModel objModel = mGlModels.createGlObjModel(objModelSource);
byte[] indicesByte = new byte[objModel.getIndices().length];
for (int i = 0; i < objModel.getIndices().length; i++) {
indicesByte[i] = (byte) objModel.getIndices()[i];
}
String objCrumpledModelSource = mGlResource.getGlResourceString(R.raw.money_two_sided_texture_crampled);
GlModels.GlObjModel objCrumpledModel = mGlModels.createGlObjModel(objCrumpledModelSource);
byte[] crumpledIndicesByte = new byte[objCrumpledModel.getIndices().length];
for (int i = 0; i < objCrumpledModel.getIndices().length; i++) {
crumpledIndicesByte[i] = (byte) objCrumpledModel.getIndices()[i];
}
mGlBufferedModels[0] = mGlModels.createGlBufferedModel(mGlTools, mGlResource,
R.raw.bg_vertices, R.raw.bg_textures, R.raw.bg_indices, mBgTextureUnitId);
for (int i = 1; i < mGlBufferedModels.length; i++) {
if (i % 2 == 0) {
mGlBufferedModels[i] = mGlModels.createGlBufferedModel(objModel.getVertices(),
objCrumpledModel.getTextures(), crumpledIndicesByte, mGlTools, mMoneyTextureUnitId);
} else {
mGlBufferedModels[i] = mGlModels.createGlBufferedModel(objModel.getVertices(),
objModel.getTextures(), indicesByte, mGlTools, mMoneyTextureUnitId);
}
if (i < mGlBufferedModels.length / 2) {
// Generate and stock model initial
// transition on Z for left group
mGlRandomGenerator.stockRandomFloat(i, 0,
LEFT_GROUP_RANDOM_TRANS_Z_MIN,
LEFT_GROUP_RANDOM_TRANS_Z_MAX);
} else {
// Generate and stock model initial
// transition on Z for right group
mGlRandomGenerator.stockRandomFloat(i, 0,
RIGHT_GROUP_RANDOM_TRANS_Z_MIN,
RIGHT_GROUP_RANDOM_TRANS_Z_MAX);
}
}
}
@Override
public void onCreateAnimations() {
for (int i = 0; i < mFrameAnimations.length; i++) {
float[][] floatMatrix;
if (i % 3 == 0) {
floatMatrix = mGlResource.getGlResourceFloatMatrix(R.raw.animation_rotation_two_axis);
} else if (i % 2 == 0) {
floatMatrix = mGlResource.getGlResourceFloatMatrix(R.raw.animation_rotation_clockwise);
} else {
floatMatrix = mGlResource.getGlResourceFloatMatrix(R.raw.animation_rotation_counterclockwise);
}
mFrameAnimations[i] = new GLAnimation.GlFrameAnimation(floatMatrix, i);
int animationDelay = Math.round(mGlRandomGenerator.getRandomFloat(0, 800));
mFrameAnimations[i].setDelay(animationDelay);
mFrameAnimations[i].setFinishListener(this);
}
}
@Override
public void onGlAnimationFinished(int id) {
if (id < mGlBufferedModels.length / 2) {
// Generate and stock model initial
// transition Z for left group
mGlRandomGenerator.stockRandomFloat(id, 0,
LEFT_GROUP_RANDOM_TRANS_Z_MIN,
LEFT_GROUP_RANDOM_TRANS_Z_MAX);
} else {
// Generate and stock model initial
// transition Z for right group
mGlRandomGenerator.stockRandomFloat(id, 0,
RIGHT_GROUP_RANDOM_TRANS_Z_MIN,
RIGHT_GROUP_RANDOM_TRANS_Z_MAX);
}
}
@Override
public void onSurfaceChanged(GL10 gl10, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
@Override
public void onDrawFrame(GL10 gl10) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
mGlElementsDrawer.applyCameraMatrix(
mRotationSensorValues[GyroscopeSensorManager.GYROSCOPE_Y] / GYROSCOPE_VALUE_DIVIDER,
CAMERA_INITIAL_Y,
mRotationSensorValues[GyroscopeSensorManager.GYROSCOPE_X] / GYROSCOPE_VALUE_DIVIDER,
mRotationSensorValues[GyroscopeSensorManager.GYROSCOPE_Y] / GYROSCOPE_VALUE_DIVIDER,
CAMERA_INITIAL_DIR_Y,
mRotationSensorValues[GyroscopeSensorManager.GYROSCOPE_X] / GYROSCOPE_VALUE_DIVIDER - 1f,
CAMERA_INITIAL_ORIENT_VECTOR);
mGlElementsDrawer.drawBufferedModel(mGlBufferedModels[0]);
for (int i = 1; i < mGlBufferedModels.length; i++) {
mGlBufferedModels[i].applyTransparency(
mFrameAnimations[i].getFrame()[6]);
// First, apply model initial transition on X and Z
mGlBufferedModels[i].applyTransition(
i < mGlBufferedModels.length / 2 ? LEFT_GROUP_TRANS_X : RIGHT_GROUP_TRANS_X,
0,
mGlRandomGenerator.getStockedFloat(i, 0),
true
);
// Second, apply animation transition
mGlBufferedModels[i].applyTransition(
mFrameAnimations[i].getFrame()[0],
mFrameAnimations[i].getFrame()[2],
mFrameAnimations[i].getFrame()[1],
false);
// Apply animation rotation
mGlBufferedModels[i].applyRotation(
mFrameAnimations[i].getFrame()[3],
mFrameAnimations[i].getFrame()[5],
mFrameAnimations[i].getFrame()[4],
true);
mGlElementsDrawer.drawBufferedModel(mGlBufferedModels[i]);
mFrameAnimations[i].nextFrame();
}
}
// This runnable is responsible for setting rotation vector
// came from another thread. So to set variables in this thread (GlThread)
// we need to use inter process communication such us queueEvent() method of GlSurfaceView
public static class RotationVectorSetter implements Runnable {
private float[] mVector;
private GlViewRenderer mRenderer;
public RotationVectorSetter(float[] vector, GlViewRenderer renderer) {
mVector = vector;
mRenderer = renderer;
}
@Override
public void run() {
mRenderer.setRotationSensorValues(mVector);
}
}
// This runnable is responsible for setting preferences
// for this renderer object in GlThread
public static class PreferencesSetter implements Runnable {
private int mTexture;
private int mCount;
private GlViewRenderer mRenderer;
public PreferencesSetter(int count, int texture, GlViewRenderer renderer) {
mCount = count;
mTexture = texture;
mRenderer = renderer;
}
@Override
public void run() {
mRenderer.onPreferenceChanged(mTexture, mCount);
}
}
// This class is responsible for choosing correspond preference
// from incoming constants
public static class PreferenceSelector {
private static final int MONEY_COUNT_LITTLE = 4;
private static final int MONEY_COUNT_MIDDLE = 6;
private static final int MONEY_COUNT_MANY = 10;
private static final int MONEY_SPEED_SLOW = 4;
private static final int MONEY_SPEED_MIDDLE = 6;
private static final int MONEY_SPEED_FAST = 10;
private static final int MONEY_TEXTURE_DOLLAR_BG = 0;
private static final int MONEY_TEXTURE_DOLLAR = 1;
private static final int MONEY_TEXTURE_EURO_BG = 2;
private static final int MONEY_TEXTURE_EURO = 3;
public int getMoneyTexture(int texture) {
switch (texture) {
case 0:
return MONEY_TEXTURE_DOLLAR;
case 1:
return MONEY_TEXTURE_EURO;
default:
throw new IllegalArgumentException(
"Illegal preference type for money count");
}
}
public int getBgTexture(int texture) {
switch (texture) {
case 0:
return MONEY_TEXTURE_DOLLAR_BG;
case 1:
return MONEY_TEXTURE_EURO_BG;
default:
throw new IllegalArgumentException(
"Illegal preference type for money count");
}
}
public int getMoneyCount(int countPreference) {
switch (countPreference) {
case 0:
return MONEY_COUNT_LITTLE;
case 1:
return MONEY_COUNT_MIDDLE;
case 2:
return MONEY_COUNT_MANY;
default:
throw new IllegalArgumentException(
"Illegal preference type for money count");
}
}
public int getMoneySpeed(int speedPreference) {
switch (speedPreference) {
case 0:
return MONEY_SPEED_SLOW;
case 1:
return MONEY_SPEED_MIDDLE;
case 2:
return MONEY_SPEED_FAST;
default:
throw new IllegalArgumentException(
"Illegal preference type for money speed");
}
}
}
}