1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2017 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.effect;
|
21
|
|
22
|
import android.opengl.Matrix;
|
23
|
|
24
|
import org.distorted.library.type.Data3D;
|
25
|
import org.distorted.library.type.Data4D;
|
26
|
|
27
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28
|
/**
|
29
|
* Rotate the Mesh by a quaternion.
|
30
|
*/
|
31
|
public class MatrixEffectQuaternion extends MatrixEffect
|
32
|
{
|
33
|
private final Data4D mQuaternion;
|
34
|
private final Data3D mCenter;
|
35
|
|
36
|
private final float[] mTmpMatrix1 = new float[16];
|
37
|
private final float[] mTmpMatrix2 = new float[16];
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
/**
|
41
|
* Only for use by the library itself.
|
42
|
*
|
43
|
* @y.exclude
|
44
|
*/
|
45
|
public boolean compute(float[] uniforms, int index, long currentDuration, long step )
|
46
|
{
|
47
|
mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
|
48
|
return mQuaternion.get(uniforms,index,currentDuration,step);
|
49
|
}
|
50
|
|
51
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52
|
/**
|
53
|
* Only for use by the library itself.
|
54
|
*
|
55
|
* @y.exclude
|
56
|
*/
|
57
|
public void apply(float[] matrixP, float[] matrixV, float[] uniforms, int index)
|
58
|
{
|
59
|
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
|
|
64
|
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
|
|
68
|
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
|
}
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
|
79
|
private 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
|
|
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
|
}
|