1 |
2014665f
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
cc62b34a
|
Leszek Koltunski
|
// Copyright 2017 Leszek Koltunski leszek@koltunski.pl //
|
3 |
2014665f
|
leszek
|
// //
|
4 |
46b572b5
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
2014665f
|
leszek
|
// //
|
6 |
cc62b34a
|
Leszek Koltunski
|
// This library is free software; you can redistribute it and/or //
|
7 |
|
|
// modify it under the terms of the GNU Lesser General Public //
|
8 |
|
|
// License as published by the Free Software Foundation; either //
|
9 |
|
|
// version 2.1 of the License, or (at your option) any later version. //
|
10 |
2014665f
|
leszek
|
// //
|
11 |
cc62b34a
|
Leszek Koltunski
|
// This library is distributed in the hope that it will be useful, //
|
12 |
2014665f
|
leszek
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13 |
cc62b34a
|
Leszek Koltunski
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
|
14 |
|
|
// Lesser General Public License for more details. //
|
15 |
2014665f
|
leszek
|
// //
|
16 |
cc62b34a
|
Leszek Koltunski
|
// You should have received a copy of the GNU Lesser General Public //
|
17 |
|
|
// License along with this library; if not, write to the Free Software //
|
18 |
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA //
|
19 |
2014665f
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20 |
|
|
|
21 |
|
|
package org.distorted.library.effect;
|
22 |
|
|
|
23 |
227b03bd
|
Leszek Koltunski
|
import android.opengl.Matrix;
|
24 |
|
|
|
25 |
6d62a900
|
Leszek Koltunski
|
import org.distorted.library.type.Data3D;
|
26 |
|
|
import org.distorted.library.type.Data4D;
|
27 |
|
|
|
28 |
2014665f
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
29 |
faa3ff56
|
Leszek Koltunski
|
/**
|
30 |
|
|
* Rotate the Mesh by a quaternion.
|
31 |
|
|
*/
|
32 |
2014665f
|
leszek
|
public class MatrixEffectQuaternion extends MatrixEffect
|
33 |
|
|
{
|
34 |
bff329fb
|
Leszek Koltunski
|
private final Data4D mQuaternion;
|
35 |
|
|
private final Data3D mCenter;
|
36 |
0dd98279
|
Leszek Koltunski
|
|
37 |
728562ae
|
Leszek Koltunski
|
private final float[] mTmpMatrix1 = new float[16];
|
38 |
|
|
private final float[] mTmpMatrix2 = new float[16];
|
39 |
227b03bd
|
Leszek Koltunski
|
|
40 |
2014665f
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41 |
6bb59aad
|
Leszek Koltunski
|
/**
|
42 |
faa3ff56
|
Leszek Koltunski
|
* Only for use by the library itself.
|
43 |
6bb59aad
|
Leszek Koltunski
|
*
|
44 |
faa3ff56
|
Leszek Koltunski
|
* @y.exclude
|
45 |
6bb59aad
|
Leszek Koltunski
|
*/
|
46 |
15aa7d94
|
Leszek Koltunski
|
public boolean compute(float[] uniforms, int index, long currentDuration, long step )
|
47 |
|
|
{
|
48 |
e979d285
|
Leszek Koltunski
|
mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
|
49 |
0dd98279
|
Leszek Koltunski
|
return mQuaternion.get(uniforms,index,currentDuration,step);
|
50 |
2014665f
|
leszek
|
}
|
51 |
227b03bd
|
Leszek Koltunski
|
|
52 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53 |
faa3ff56
|
Leszek Koltunski
|
/**
|
54 |
|
|
* Only for use by the library itself.
|
55 |
|
|
*
|
56 |
|
|
* @y.exclude
|
57 |
|
|
*/
|
58 |
62c869ad
|
Leszek Koltunski
|
public void apply(float[] matrixP, float[] matrixV, float[] uniforms, int index)
|
59 |
227b03bd
|
Leszek Koltunski
|
{
|
60 |
96e3b88a
|
Leszek Koltunski
|
float qX = uniforms[NUM_FLOAT_UNIFORMS*index ];
|
61 |
|
|
float qY = uniforms[NUM_FLOAT_UNIFORMS*index+1];
|
62 |
|
|
float qZ = uniforms[NUM_FLOAT_UNIFORMS*index+2];
|
63 |
|
|
float qW = uniforms[NUM_FLOAT_UNIFORMS*index+3];
|
64 |
227b03bd
|
Leszek Koltunski
|
|
65 |
96e3b88a
|
Leszek Koltunski
|
float x = uniforms[NUM_FLOAT_UNIFORMS*index+CENTER_OFFSET ];
|
66 |
|
|
float y = uniforms[NUM_FLOAT_UNIFORMS*index+CENTER_OFFSET+1];
|
67 |
|
|
float z = uniforms[NUM_FLOAT_UNIFORMS*index+CENTER_OFFSET+2];
|
68 |
227b03bd
|
Leszek Koltunski
|
|
69 |
62c869ad
|
Leszek Koltunski
|
Matrix.translateM(matrixP, 0, x, y, z);
|
70 |
|
|
multiplyByQuat (matrixP, qX, qY, qZ, qW);
|
71 |
|
|
Matrix.translateM(matrixP, 0,-x,-y,-z);
|
72 |
|
|
|
73 |
|
|
Matrix.translateM(matrixV, 0, x, y, z);
|
74 |
|
|
multiplyByQuat (matrixV, qX, qY, qZ, qW);
|
75 |
|
|
Matrix.translateM(matrixV, 0,-x,-y,-z);
|
76 |
227b03bd
|
Leszek Koltunski
|
}
|
77 |
|
|
|
78 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
79 |
|
|
|
80 |
728562ae
|
Leszek Koltunski
|
private void multiplyByQuat(float[] matrix, float X, float Y, float Z, float W)
|
81 |
227b03bd
|
Leszek Koltunski
|
{
|
82 |
|
|
float xx= X * X;
|
83 |
|
|
float xy= X * Y;
|
84 |
|
|
float xz= X * Z;
|
85 |
|
|
float xw= X * W;
|
86 |
|
|
float yy= Y * Y;
|
87 |
|
|
float yz= Y * Z;
|
88 |
|
|
float yw= Y * W;
|
89 |
|
|
float zz= Z * Z;
|
90 |
|
|
float zw= Z * W;
|
91 |
|
|
|
92 |
|
|
mTmpMatrix1[0] = 1 - 2 * ( yy + zz );
|
93 |
|
|
mTmpMatrix1[1] = 2 * ( xy - zw );
|
94 |
|
|
mTmpMatrix1[2] = 2 * ( xz + yw );
|
95 |
|
|
mTmpMatrix1[4] = 2 * ( xy + zw );
|
96 |
|
|
mTmpMatrix1[5] = 1 - 2 * ( xx + zz );
|
97 |
|
|
mTmpMatrix1[6] = 2 * ( yz - xw );
|
98 |
|
|
mTmpMatrix1[8] = 2 * ( xz - yw );
|
99 |
|
|
mTmpMatrix1[9] = 2 * ( yz + xw );
|
100 |
|
|
mTmpMatrix1[10] = 1 - 2 * ( xx + yy );
|
101 |
|
|
mTmpMatrix1[3] = mTmpMatrix1[7] = mTmpMatrix1[11] = mTmpMatrix1[12] = mTmpMatrix1[13] = mTmpMatrix1[14] = 0;
|
102 |
|
|
mTmpMatrix1[15] = 1;
|
103 |
|
|
|
104 |
|
|
Matrix.multiplyMM(mTmpMatrix2, 0, matrix, 0, mTmpMatrix1, 0);
|
105 |
|
|
|
106 |
|
|
matrix[ 0] = mTmpMatrix2[ 0];
|
107 |
|
|
matrix[ 1] = mTmpMatrix2[ 1];
|
108 |
|
|
matrix[ 2] = mTmpMatrix2[ 2];
|
109 |
|
|
matrix[ 3] = mTmpMatrix2[ 3];
|
110 |
|
|
matrix[ 4] = mTmpMatrix2[ 4];
|
111 |
|
|
matrix[ 5] = mTmpMatrix2[ 5];
|
112 |
|
|
matrix[ 6] = mTmpMatrix2[ 6];
|
113 |
|
|
matrix[ 7] = mTmpMatrix2[ 7];
|
114 |
|
|
matrix[ 8] = mTmpMatrix2[ 8];
|
115 |
|
|
matrix[ 9] = mTmpMatrix2[ 9];
|
116 |
|
|
matrix[10] = mTmpMatrix2[10];
|
117 |
|
|
matrix[11] = mTmpMatrix2[11];
|
118 |
|
|
matrix[12] = mTmpMatrix2[12];
|
119 |
|
|
matrix[13] = mTmpMatrix2[13];
|
120 |
|
|
matrix[14] = mTmpMatrix2[14];
|
121 |
|
|
matrix[15] = mTmpMatrix2[15];
|
122 |
|
|
}
|
123 |
faa3ff56
|
Leszek Koltunski
|
|
124 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125 |
|
|
// PUBLIC API
|
126 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
127 |
|
|
/**
|
128 |
|
|
* Rotate the Mesh by a quaternion.
|
129 |
|
|
*
|
130 |
|
|
* @param quaternion Quaternion describing the rotation.
|
131 |
|
|
* @param center Coordinates of the Point we are rotating around.
|
132 |
|
|
*/
|
133 |
|
|
public MatrixEffectQuaternion(Data4D quaternion, Data3D center )
|
134 |
|
|
{
|
135 |
|
|
super(EffectName.QUATERNION);
|
136 |
|
|
mQuaternion = quaternion;
|
137 |
|
|
mCenter = center;
|
138 |
|
|
}
|
139 |
2014665f
|
leszek
|
}
|