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.library;
|
21
|
|
22
|
import android.opengl.GLES20;
|
23
|
import android.opengl.Matrix;
|
24
|
|
25
|
import org.distorted.library.message.EffectMessage;
|
26
|
import org.distorted.library.type.Data1D;
|
27
|
import org.distorted.library.type.Data3D;
|
28
|
import org.distorted.library.type.Data4D;
|
29
|
import org.distorted.library.type.Dynamic1D;
|
30
|
import org.distorted.library.type.Dynamic3D;
|
31
|
import org.distorted.library.type.Dynamic4D;
|
32
|
import org.distorted.library.type.DynamicQuat;
|
33
|
import org.distorted.library.type.Static1D;
|
34
|
import org.distorted.library.type.Static3D;
|
35
|
import org.distorted.library.type.Static4D;
|
36
|
|
37
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38
|
|
39
|
class EffectQueueMatrix extends EffectQueue
|
40
|
{
|
41
|
private static final int NUM_UNIFORMS = 7;
|
42
|
private static final int INDEX = EffectTypes.MATRIX.ordinal();
|
43
|
private static float[] mMVPMatrix= new float[16];
|
44
|
private static float[] mTmpMatrix= new float[16];
|
45
|
|
46
|
private static int mObjDH; // This is a handle to half a Object dimensions
|
47
|
private static int mDepthH; // Handle to the max Depth, i.e (farplane-nearplane)/2
|
48
|
private static int mMVPMatrixH; // pass in the transformation matrix
|
49
|
private static int mMVMatrixH; // pass in the modelview matrix.
|
50
|
|
51
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52
|
|
53
|
EffectQueueMatrix(DistortedObject obj)
|
54
|
{
|
55
|
super(obj,NUM_UNIFORMS, INDEX );
|
56
|
}
|
57
|
|
58
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
59
|
|
60
|
private static void multiplyByQuat(float[] matrix, float X, float Y, float Z, float W)
|
61
|
{
|
62
|
float xx= X * X;
|
63
|
float xy= X * Y;
|
64
|
float xz= X * Z;
|
65
|
float xw= X * W;
|
66
|
float yy= Y * Y;
|
67
|
float yz= Y * Z;
|
68
|
float yw= Y * W;
|
69
|
float zz= Z * Z;
|
70
|
float zw= Z * W;
|
71
|
|
72
|
mTmpMatrix[0] = 1 - 2 * ( yy + zz );
|
73
|
mTmpMatrix[1] = 2 * ( xy - zw );
|
74
|
mTmpMatrix[2] = 2 * ( xz + yw );
|
75
|
mTmpMatrix[4] = 2 * ( xy + zw );
|
76
|
mTmpMatrix[5] = 1 - 2 * ( xx + zz );
|
77
|
mTmpMatrix[6] = 2 * ( yz - xw );
|
78
|
mTmpMatrix[8] = 2 * ( xz - yw );
|
79
|
mTmpMatrix[9] = 2 * ( yz + xw );
|
80
|
mTmpMatrix[10] = 1 - 2 * ( xx + yy );
|
81
|
mTmpMatrix[3] = mTmpMatrix[7] = mTmpMatrix[11] = mTmpMatrix[12] = mTmpMatrix[13] = mTmpMatrix[14] = 0;
|
82
|
mTmpMatrix[15] = 1;
|
83
|
|
84
|
Matrix.multiplyMM(mMVPMatrix, 0, matrix, 0, mTmpMatrix, 0);
|
85
|
for(int j=0; j<16; j++) matrix[j] = mMVPMatrix[j];
|
86
|
}
|
87
|
|
88
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89
|
|
90
|
static void getUniforms(int mProgramH)
|
91
|
{
|
92
|
mObjDH = GLES20.glGetUniformLocation(mProgramH, "u_objD");
|
93
|
mDepthH = GLES20.glGetUniformLocation(mProgramH, "u_Depth");
|
94
|
mMVPMatrixH= GLES20.glGetUniformLocation(mProgramH, "u_MVPMatrix");
|
95
|
mMVMatrixH = GLES20.glGetUniformLocation(mProgramH, "u_MVMatrix");
|
96
|
}
|
97
|
|
98
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99
|
|
100
|
synchronized void compute(long currTime)
|
101
|
{
|
102
|
if( currTime==mTime ) return;
|
103
|
if( mTime==0 ) mTime = currTime;
|
104
|
long step = (currTime-mTime);
|
105
|
|
106
|
for(int i=0; i<mNumEffects; i++)
|
107
|
{
|
108
|
if( mInter[0][i]!=null && mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
|
109
|
{
|
110
|
for(int j=0; j<mNumListeners; j++)
|
111
|
EffectMessageSender.newMessage( mListeners.elementAt(j),
|
112
|
EffectMessage.EFFECT_FINISHED,
|
113
|
(mID[i]<<EffectTypes.LENGTH)+EffectTypes.MATRIX.type,
|
114
|
mName[i],
|
115
|
mBitmapID,
|
116
|
null);
|
117
|
|
118
|
if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
|
119
|
{
|
120
|
remove(i);
|
121
|
i--;
|
122
|
continue;
|
123
|
}
|
124
|
else mInter[0][i] = null;
|
125
|
}
|
126
|
|
127
|
if( mInter[1][i]!=null )
|
128
|
{
|
129
|
mInter[1][i].interpolateMain(mUniforms, NUM_UNIFORMS*i+4, mCurrentDuration[i], step);
|
130
|
}
|
131
|
|
132
|
mCurrentDuration[i] += step;
|
133
|
}
|
134
|
|
135
|
mTime = currTime;
|
136
|
}
|
137
|
|
138
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
139
|
|
140
|
protected void moveEffect(int index)
|
141
|
{
|
142
|
mUniforms[NUM_UNIFORMS*index ] = mUniforms[NUM_UNIFORMS*(index+1) ];
|
143
|
mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
|
144
|
mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
|
145
|
mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
|
146
|
mUniforms[NUM_UNIFORMS*index+4] = mUniforms[NUM_UNIFORMS*(index+1)+4];
|
147
|
mUniforms[NUM_UNIFORMS*index+5] = mUniforms[NUM_UNIFORMS*(index+1)+5];
|
148
|
mUniforms[NUM_UNIFORMS*index+6] = mUniforms[NUM_UNIFORMS*(index+1)+6];
|
149
|
}
|
150
|
|
151
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152
|
// here construct the ModelView Matrix
|
153
|
|
154
|
synchronized void send(float[] viewMatrix, DistortedProjection dp)
|
155
|
{
|
156
|
Matrix.setIdentityM(viewMatrix, 0);
|
157
|
Matrix.translateM(viewMatrix, 0, -dp.width/2, dp.height/2, -dp.distance);
|
158
|
|
159
|
float x,y,z, sx,sy,sz;
|
160
|
|
161
|
for(int i=0; i<mNumEffects; i++)
|
162
|
{
|
163
|
if (mName[i] == EffectNames.ROTATE.ordinal() )
|
164
|
{
|
165
|
x = mUniforms[NUM_UNIFORMS*i+4];
|
166
|
y = mUniforms[NUM_UNIFORMS*i+5];
|
167
|
z = mUniforms[NUM_UNIFORMS*i+6];
|
168
|
|
169
|
Matrix.translateM(viewMatrix, 0, x,-y, z);
|
170
|
Matrix.rotateM( viewMatrix, 0, mUniforms[NUM_UNIFORMS*i], mUniforms[NUM_UNIFORMS*i+1], mUniforms[NUM_UNIFORMS*i+2], mUniforms[NUM_UNIFORMS*i+3]);
|
171
|
Matrix.translateM(viewMatrix, 0,-x, y,-z);
|
172
|
}
|
173
|
else if(mName[i] == EffectNames.QUATERNION.ordinal() )
|
174
|
{
|
175
|
x = mUniforms[NUM_UNIFORMS*i+4];
|
176
|
y = mUniforms[NUM_UNIFORMS*i+5];
|
177
|
z = mUniforms[NUM_UNIFORMS*i+6];
|
178
|
|
179
|
Matrix.translateM(viewMatrix, 0, x,-y, z);
|
180
|
multiplyByQuat(viewMatrix, mUniforms[NUM_UNIFORMS*i], mUniforms[NUM_UNIFORMS*i+1], mUniforms[NUM_UNIFORMS*i+2], mUniforms[NUM_UNIFORMS*i+3]);
|
181
|
Matrix.translateM(viewMatrix, 0,-x, y,-z);
|
182
|
}
|
183
|
else if(mName[i] == EffectNames.MOVE.ordinal() )
|
184
|
{
|
185
|
sx = mUniforms[NUM_UNIFORMS*i ];
|
186
|
sy = mUniforms[NUM_UNIFORMS*i+1];
|
187
|
sz = mUniforms[NUM_UNIFORMS*i+2];
|
188
|
|
189
|
Matrix.translateM(viewMatrix, 0, sx,-sy, sz);
|
190
|
}
|
191
|
else if(mName[i] == EffectNames.SCALE.ordinal() )
|
192
|
{
|
193
|
sx = mUniforms[NUM_UNIFORMS*i ];
|
194
|
sy = mUniforms[NUM_UNIFORMS*i+1];
|
195
|
sz = mUniforms[NUM_UNIFORMS*i+2];
|
196
|
|
197
|
Matrix.scaleM(viewMatrix, 0, sx, sy, sz);
|
198
|
}
|
199
|
else if(mName[i] == EffectNames.SHEAR.ordinal() )
|
200
|
{
|
201
|
sx = mUniforms[NUM_UNIFORMS*i ];
|
202
|
sy = mUniforms[NUM_UNIFORMS*i+1];
|
203
|
sz = mUniforms[NUM_UNIFORMS*i+2];
|
204
|
|
205
|
x = mUniforms[NUM_UNIFORMS*i+4];
|
206
|
y = mUniforms[NUM_UNIFORMS*i+5];
|
207
|
z = mUniforms[NUM_UNIFORMS*i+6];
|
208
|
|
209
|
Matrix.translateM(viewMatrix, 0, x,-y, z);
|
210
|
|
211
|
viewMatrix[4] += sx*viewMatrix[0]; // Multiply viewMatrix by 1 x 0 0 , i.e. X-shear.
|
212
|
viewMatrix[5] += sx*viewMatrix[1]; // 0 1 0 0
|
213
|
viewMatrix[6] += sx*viewMatrix[2]; // 0 0 1 0
|
214
|
viewMatrix[7] += sx*viewMatrix[3]; // 0 0 0 1
|
215
|
|
216
|
viewMatrix[0] += sy*viewMatrix[4]; // Multiply viewMatrix by 1 0 0 0 , i.e. Y-shear.
|
217
|
viewMatrix[1] += sy*viewMatrix[5]; // y 1 0 0
|
218
|
viewMatrix[2] += sy*viewMatrix[6]; // 0 0 1 0
|
219
|
viewMatrix[3] += sy*viewMatrix[7]; // 0 0 0 1
|
220
|
|
221
|
viewMatrix[4] += sz*viewMatrix[8]; // Multiply viewMatrix by 1 0 0 0 , i.e. Z-shear.
|
222
|
viewMatrix[5] += sz*viewMatrix[9]; // 0 1 0 0
|
223
|
viewMatrix[6] += sz*viewMatrix[10];// 0 z 1 0
|
224
|
viewMatrix[7] += sz*viewMatrix[11];// 0 0 0 1
|
225
|
|
226
|
Matrix.translateM(viewMatrix, 0,-x, y, -z);
|
227
|
}
|
228
|
}
|
229
|
|
230
|
Matrix.translateM(viewMatrix, 0, mObjHalfX,-mObjHalfY, 0);
|
231
|
Matrix.multiplyMM(mMVPMatrix, 0, dp.projectionMatrix, 0, viewMatrix, 0);
|
232
|
|
233
|
GLES20.glUniform3f( mObjDH , mObjHalfX, mObjHalfY, mObjHalfZ);
|
234
|
GLES20.glUniform1f( mDepthH, dp.depth);
|
235
|
GLES20.glUniformMatrix4fv(mMVMatrixH , 1, false, viewMatrix, 0);
|
236
|
GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix, 0);
|
237
|
}
|
238
|
|
239
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
240
|
// here construct the ModelView Matrix, but without any effects
|
241
|
|
242
|
synchronized void sendNoEffects(DistortedProjection dp)
|
243
|
{
|
244
|
Matrix.setIdentityM(mTmpMatrix, 0);
|
245
|
Matrix.translateM(mTmpMatrix, 0, mObjHalfX-dp.width/2, dp.height/2-mObjHalfY, -dp.distance);
|
246
|
Matrix.multiplyMM(mMVPMatrix, 0, dp.projectionMatrix, 0, mTmpMatrix, 0);
|
247
|
|
248
|
GLES20.glUniform3f( mObjDH , mObjHalfX, mObjHalfY, mObjHalfZ);
|
249
|
GLES20.glUniform1f( mDepthH, dp.depth);
|
250
|
GLES20.glUniformMatrix4fv(mMVMatrixH , 1, false, mTmpMatrix, 0);
|
251
|
GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix, 0);
|
252
|
}
|
253
|
|
254
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
255
|
// move, scale
|
256
|
|
257
|
synchronized long add(EffectNames eln, Data3D vector)
|
258
|
{
|
259
|
if( mMax[INDEX]>mNumEffects )
|
260
|
{
|
261
|
mInter[1][mNumEffects] = null;
|
262
|
|
263
|
if( vector instanceof Dynamic3D)
|
264
|
{
|
265
|
mInter[0][mNumEffects] = (Dynamic3D)vector;
|
266
|
}
|
267
|
else if( vector instanceof Static3D )
|
268
|
{
|
269
|
mInter[0][mNumEffects] = null;
|
270
|
mUniforms[NUM_UNIFORMS*mNumEffects ] = ((Static3D)vector).getX();
|
271
|
mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static3D)vector).getY();
|
272
|
mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static3D)vector).getZ();
|
273
|
}
|
274
|
else return -1;
|
275
|
|
276
|
return addBase(eln);
|
277
|
}
|
278
|
|
279
|
return -1;
|
280
|
}
|
281
|
|
282
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
283
|
// rotate - static axis
|
284
|
|
285
|
synchronized long add(EffectNames eln, Data1D angle, Static3D axis, Data3D center)
|
286
|
{
|
287
|
if( mMax[INDEX]>mNumEffects )
|
288
|
{
|
289
|
if( angle instanceof Dynamic1D)
|
290
|
{
|
291
|
mInter[0][mNumEffects] = (Dynamic1D)angle;
|
292
|
}
|
293
|
else if( angle instanceof Static1D)
|
294
|
{
|
295
|
mInter[0][mNumEffects] = null;
|
296
|
mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)angle).getX();
|
297
|
}
|
298
|
else return -1;
|
299
|
|
300
|
mUniforms[NUM_UNIFORMS*mNumEffects+1] = axis.getX();
|
301
|
mUniforms[NUM_UNIFORMS*mNumEffects+2] = axis.getY();
|
302
|
mUniforms[NUM_UNIFORMS*mNumEffects+3] = axis.getZ();
|
303
|
|
304
|
if( center instanceof Dynamic3D)
|
305
|
{
|
306
|
mInter[1][mNumEffects] = (Dynamic3D)center;
|
307
|
}
|
308
|
else if( center instanceof Static3D )
|
309
|
{
|
310
|
mInter[1][mNumEffects] = null;
|
311
|
mUniforms[NUM_UNIFORMS*mNumEffects+4] = ((Static3D)center).getX();
|
312
|
mUniforms[NUM_UNIFORMS*mNumEffects+5] = ((Static3D)center).getY();
|
313
|
mUniforms[NUM_UNIFORMS*mNumEffects+6] = ((Static3D)center).getZ();
|
314
|
}
|
315
|
else return -1;
|
316
|
|
317
|
return addBase(eln);
|
318
|
}
|
319
|
|
320
|
return -1;
|
321
|
}
|
322
|
|
323
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
324
|
// quaternion or rotate - dynamic axis
|
325
|
|
326
|
synchronized long add(EffectNames eln, Data4D data, Data3D center)
|
327
|
{
|
328
|
if( mMax[INDEX]>mNumEffects )
|
329
|
{
|
330
|
if( data instanceof Dynamic4D )
|
331
|
{
|
332
|
mInter[0][mNumEffects] = (Dynamic4D)data;
|
333
|
}
|
334
|
else if( data instanceof DynamicQuat)
|
335
|
{
|
336
|
mInter[0][mNumEffects] = (DynamicQuat)data;
|
337
|
}
|
338
|
else if( data instanceof Static4D )
|
339
|
{
|
340
|
mInter[0][mNumEffects] = null;
|
341
|
mUniforms[NUM_UNIFORMS*mNumEffects ] = ((Static4D)data).getX();
|
342
|
mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static4D)data).getY();
|
343
|
mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static4D)data).getZ();
|
344
|
mUniforms[NUM_UNIFORMS*mNumEffects+3] = ((Static4D)data).getW();
|
345
|
}
|
346
|
else return -1;
|
347
|
|
348
|
if( center instanceof Dynamic3D)
|
349
|
{
|
350
|
mInter[1][mNumEffects] = (Dynamic3D)center;
|
351
|
}
|
352
|
else if( center instanceof Static3D )
|
353
|
{
|
354
|
mInter[1][mNumEffects] = null;
|
355
|
mUniforms[NUM_UNIFORMS*mNumEffects+4] = ((Static3D)center).getX();
|
356
|
mUniforms[NUM_UNIFORMS*mNumEffects+5] = ((Static3D)center).getY();
|
357
|
mUniforms[NUM_UNIFORMS*mNumEffects+6] = ((Static3D)center).getZ();
|
358
|
}
|
359
|
else return -1;
|
360
|
|
361
|
return addBase(eln);
|
362
|
}
|
363
|
|
364
|
return -1;
|
365
|
}
|
366
|
|
367
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
368
|
// shear
|
369
|
|
370
|
synchronized long add(EffectNames eln, Data3D shear, Data3D center)
|
371
|
{
|
372
|
if( mMax[INDEX]>mNumEffects )
|
373
|
{
|
374
|
if( shear instanceof Dynamic3D)
|
375
|
{
|
376
|
mInter[0][mNumEffects] = (Dynamic3D)shear;
|
377
|
}
|
378
|
else if( shear instanceof Static3D )
|
379
|
{
|
380
|
mInter[0][mNumEffects] = null;
|
381
|
mUniforms[NUM_UNIFORMS*mNumEffects ] = ((Static3D)shear).getX();
|
382
|
mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static3D)shear).getY();
|
383
|
mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static3D)shear).getZ();
|
384
|
}
|
385
|
else return -1;
|
386
|
|
387
|
if( center instanceof Dynamic3D)
|
388
|
{
|
389
|
mInter[1][mNumEffects] = (Dynamic3D)center;
|
390
|
}
|
391
|
else if( center instanceof Static3D )
|
392
|
{
|
393
|
mInter[1][mNumEffects] = null;
|
394
|
mUniforms[NUM_UNIFORMS*mNumEffects+4] = ((Static3D)center).getX();
|
395
|
mUniforms[NUM_UNIFORMS*mNumEffects+5] = ((Static3D)center).getY();
|
396
|
mUniforms[NUM_UNIFORMS*mNumEffects+6] = ((Static3D)center).getZ();
|
397
|
}
|
398
|
else return -1;
|
399
|
|
400
|
return addBase(eln);
|
401
|
}
|
402
|
|
403
|
return -1;
|
404
|
}
|
405
|
}
|