1 |
2014665f
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2017 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
46b572b5
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
2014665f
|
leszek
|
// //
|
6 |
46b572b5
|
Leszek Koltunski
|
// Distorted is free software: you can redistribute it and/or modify //
|
7 |
2014665f
|
leszek
|
// 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 |
46b572b5
|
Leszek Koltunski
|
// Distorted is distributed in the hope that it will be useful, //
|
12 |
2014665f
|
leszek
|
// 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 |
46b572b5
|
Leszek Koltunski
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
2014665f
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
|
|
package org.distorted.library.effect;
|
21 |
|
|
|
22 |
227b03bd
|
Leszek Koltunski
|
import android.opengl.Matrix;
|
23 |
|
|
|
24 |
6d62a900
|
Leszek Koltunski
|
import org.distorted.library.type.Data3D;
|
25 |
|
|
import org.distorted.library.type.Data4D;
|
26 |
|
|
|
27 |
2014665f
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28 |
faa3ff56
|
Leszek Koltunski
|
/**
|
29 |
|
|
* Rotate the Mesh by a quaternion.
|
30 |
|
|
*/
|
31 |
2014665f
|
leszek
|
public class MatrixEffectQuaternion extends MatrixEffect
|
32 |
|
|
{
|
33 |
0dd98279
|
Leszek Koltunski
|
private Data4D mQuaternion;
|
34 |
|
|
private Data3D mCenter;
|
35 |
|
|
|
36 |
227b03bd
|
Leszek Koltunski
|
private static float[] mTmpMatrix1 = new float[16];
|
37 |
|
|
private static float[] mTmpMatrix2 = new float[16];
|
38 |
|
|
|
39 |
2014665f
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40 |
6bb59aad
|
Leszek Koltunski
|
/**
|
41 |
faa3ff56
|
Leszek Koltunski
|
* Only for use by the library itself.
|
42 |
6bb59aad
|
Leszek Koltunski
|
*
|
43 |
faa3ff56
|
Leszek Koltunski
|
* @y.exclude
|
44 |
6bb59aad
|
Leszek Koltunski
|
*/
|
45 |
15aa7d94
|
Leszek Koltunski
|
public boolean compute(float[] uniforms, int index, long currentDuration, long step )
|
46 |
|
|
{
|
47 |
e979d285
|
Leszek Koltunski
|
mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
|
48 |
0dd98279
|
Leszek Koltunski
|
return mQuaternion.get(uniforms,index,currentDuration,step);
|
49 |
2014665f
|
leszek
|
}
|
50 |
227b03bd
|
Leszek Koltunski
|
|
51 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52 |
faa3ff56
|
Leszek Koltunski
|
/**
|
53 |
|
|
* Only for use by the library itself.
|
54 |
|
|
*
|
55 |
|
|
* @y.exclude
|
56 |
|
|
*/
|
57 |
62c869ad
|
Leszek Koltunski
|
public void apply(float[] matrixP, float[] matrixV, float[] uniforms, int index)
|
58 |
227b03bd
|
Leszek Koltunski
|
{
|
59 |
96e3b88a
|
Leszek Koltunski
|
float qX = uniforms[NUM_FLOAT_UNIFORMS*index ];
|
60 |
|
|
float qY = uniforms[NUM_FLOAT_UNIFORMS*index+1];
|
61 |
|
|
float qZ = uniforms[NUM_FLOAT_UNIFORMS*index+2];
|
62 |
|
|
float qW = uniforms[NUM_FLOAT_UNIFORMS*index+3];
|
63 |
227b03bd
|
Leszek Koltunski
|
|
64 |
96e3b88a
|
Leszek Koltunski
|
float x = uniforms[NUM_FLOAT_UNIFORMS*index+CENTER_OFFSET ];
|
65 |
|
|
float y = uniforms[NUM_FLOAT_UNIFORMS*index+CENTER_OFFSET+1];
|
66 |
|
|
float z = uniforms[NUM_FLOAT_UNIFORMS*index+CENTER_OFFSET+2];
|
67 |
227b03bd
|
Leszek Koltunski
|
|
68 |
62c869ad
|
Leszek Koltunski
|
Matrix.translateM(matrixP, 0, x, y, z);
|
69 |
|
|
multiplyByQuat (matrixP, qX, qY, qZ, qW);
|
70 |
|
|
Matrix.translateM(matrixP, 0,-x,-y,-z);
|
71 |
|
|
|
72 |
|
|
Matrix.translateM(matrixV, 0, x, y, z);
|
73 |
|
|
multiplyByQuat (matrixV, qX, qY, qZ, qW);
|
74 |
|
|
Matrix.translateM(matrixV, 0,-x,-y,-z);
|
75 |
227b03bd
|
Leszek Koltunski
|
}
|
76 |
|
|
|
77 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78 |
|
|
|
79 |
|
|
private static void multiplyByQuat(float[] matrix, float X, float Y, float Z, float W)
|
80 |
|
|
{
|
81 |
|
|
float xx= X * X;
|
82 |
|
|
float xy= X * Y;
|
83 |
|
|
float xz= X * Z;
|
84 |
|
|
float xw= X * W;
|
85 |
|
|
float yy= Y * Y;
|
86 |
|
|
float yz= Y * Z;
|
87 |
|
|
float yw= Y * W;
|
88 |
|
|
float zz= Z * Z;
|
89 |
|
|
float zw= Z * W;
|
90 |
|
|
|
91 |
|
|
mTmpMatrix1[0] = 1 - 2 * ( yy + zz );
|
92 |
|
|
mTmpMatrix1[1] = 2 * ( xy - zw );
|
93 |
|
|
mTmpMatrix1[2] = 2 * ( xz + yw );
|
94 |
|
|
mTmpMatrix1[4] = 2 * ( xy + zw );
|
95 |
|
|
mTmpMatrix1[5] = 1 - 2 * ( xx + zz );
|
96 |
|
|
mTmpMatrix1[6] = 2 * ( yz - xw );
|
97 |
|
|
mTmpMatrix1[8] = 2 * ( xz - yw );
|
98 |
|
|
mTmpMatrix1[9] = 2 * ( yz + xw );
|
99 |
|
|
mTmpMatrix1[10] = 1 - 2 * ( xx + yy );
|
100 |
|
|
mTmpMatrix1[3] = mTmpMatrix1[7] = mTmpMatrix1[11] = mTmpMatrix1[12] = mTmpMatrix1[13] = mTmpMatrix1[14] = 0;
|
101 |
|
|
mTmpMatrix1[15] = 1;
|
102 |
|
|
|
103 |
|
|
Matrix.multiplyMM(mTmpMatrix2, 0, matrix, 0, mTmpMatrix1, 0);
|
104 |
|
|
|
105 |
|
|
matrix[ 0] = mTmpMatrix2[ 0];
|
106 |
|
|
matrix[ 1] = mTmpMatrix2[ 1];
|
107 |
|
|
matrix[ 2] = mTmpMatrix2[ 2];
|
108 |
|
|
matrix[ 3] = mTmpMatrix2[ 3];
|
109 |
|
|
matrix[ 4] = mTmpMatrix2[ 4];
|
110 |
|
|
matrix[ 5] = mTmpMatrix2[ 5];
|
111 |
|
|
matrix[ 6] = mTmpMatrix2[ 6];
|
112 |
|
|
matrix[ 7] = mTmpMatrix2[ 7];
|
113 |
|
|
matrix[ 8] = mTmpMatrix2[ 8];
|
114 |
|
|
matrix[ 9] = mTmpMatrix2[ 9];
|
115 |
|
|
matrix[10] = mTmpMatrix2[10];
|
116 |
|
|
matrix[11] = mTmpMatrix2[11];
|
117 |
|
|
matrix[12] = mTmpMatrix2[12];
|
118 |
|
|
matrix[13] = mTmpMatrix2[13];
|
119 |
|
|
matrix[14] = mTmpMatrix2[14];
|
120 |
|
|
matrix[15] = mTmpMatrix2[15];
|
121 |
|
|
}
|
122 |
faa3ff56
|
Leszek Koltunski
|
|
123 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124 |
|
|
// PUBLIC API
|
125 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126 |
|
|
/**
|
127 |
|
|
* Rotate the Mesh by a quaternion.
|
128 |
|
|
*
|
129 |
|
|
* @param quaternion Quaternion describing the rotation.
|
130 |
|
|
* @param center Coordinates of the Point we are rotating around.
|
131 |
|
|
*/
|
132 |
|
|
public MatrixEffectQuaternion(Data4D quaternion, Data3D center )
|
133 |
|
|
{
|
134 |
|
|
super(EffectName.QUATERNION);
|
135 |
|
|
mQuaternion = quaternion;
|
136 |
|
|
mCenter = center;
|
137 |
|
|
}
|
138 |
2014665f
|
leszek
|
}
|