Project

General

Profile

Download (5.43 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / effect / MatrixEffectQuaternion.java @ c1fa9e3c

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 Data4D mQuaternion;
34
  private Data3D mCenter;
35

    
36
  private static float[] mTmpMatrix1 = new float[16];
37
  private static 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[] matrix, float[] uniforms, int index)
58
    {
59
    float qX = uniforms[NUM_UNIFORMS*index  ];
60
    float qY = uniforms[NUM_UNIFORMS*index+1];
61
    float qZ = uniforms[NUM_UNIFORMS*index+2];
62
    float qW = uniforms[NUM_UNIFORMS*index+3];
63

    
64
    float x = uniforms[NUM_UNIFORMS*index+CENTER_OFFSET  ];
65
    float y = uniforms[NUM_UNIFORMS*index+CENTER_OFFSET+1];
66
    float z = uniforms[NUM_UNIFORMS*index+CENTER_OFFSET+2];
67

    
68
    Matrix.translateM(matrix, 0, x, y, z);
69
    multiplyByQuat( matrix, qX, qY, qZ, qW);
70
    Matrix.translateM(matrix, 0,-x,-y,-z);
71
    }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
  private static void multiplyByQuat(float[] matrix, float X, float Y, float Z, float W)
76
    {
77
    float xx= X * X;
78
    float xy= X * Y;
79
    float xz= X * Z;
80
    float xw= X * W;
81
    float yy= Y * Y;
82
    float yz= Y * Z;
83
    float yw= Y * W;
84
    float zz= Z * Z;
85
    float zw= Z * W;
86

    
87
    mTmpMatrix1[0]  = 1 - 2 * ( yy + zz );
88
    mTmpMatrix1[1]  =     2 * ( xy - zw );
89
    mTmpMatrix1[2]  =     2 * ( xz + yw );
90
    mTmpMatrix1[4]  =     2 * ( xy + zw );
91
    mTmpMatrix1[5]  = 1 - 2 * ( xx + zz );
92
    mTmpMatrix1[6]  =     2 * ( yz - xw );
93
    mTmpMatrix1[8]  =     2 * ( xz - yw );
94
    mTmpMatrix1[9]  =     2 * ( yz + xw );
95
    mTmpMatrix1[10] = 1 - 2 * ( xx + yy );
96
    mTmpMatrix1[3]  = mTmpMatrix1[7] = mTmpMatrix1[11] = mTmpMatrix1[12] = mTmpMatrix1[13] = mTmpMatrix1[14] = 0;
97
    mTmpMatrix1[15] = 1;
98

    
99
    Matrix.multiplyMM(mTmpMatrix2, 0, matrix, 0, mTmpMatrix1, 0);
100

    
101
    matrix[ 0] = mTmpMatrix2[ 0];
102
    matrix[ 1] = mTmpMatrix2[ 1];
103
    matrix[ 2] = mTmpMatrix2[ 2];
104
    matrix[ 3] = mTmpMatrix2[ 3];
105
    matrix[ 4] = mTmpMatrix2[ 4];
106
    matrix[ 5] = mTmpMatrix2[ 5];
107
    matrix[ 6] = mTmpMatrix2[ 6];
108
    matrix[ 7] = mTmpMatrix2[ 7];
109
    matrix[ 8] = mTmpMatrix2[ 8];
110
    matrix[ 9] = mTmpMatrix2[ 9];
111
    matrix[10] = mTmpMatrix2[10];
112
    matrix[11] = mTmpMatrix2[11];
113
    matrix[12] = mTmpMatrix2[12];
114
    matrix[13] = mTmpMatrix2[13];
115
    matrix[14] = mTmpMatrix2[14];
116
    matrix[15] = mTmpMatrix2[15];
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
// PUBLIC API
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
/**
123
 * Rotate the Mesh by a quaternion.
124
 *
125
 * @param quaternion Quaternion describing the rotation.
126
 * @param center     Coordinates of the Point we are rotating around.
127
 */
128
  public MatrixEffectQuaternion(Data4D quaternion, Data3D center )
129
    {
130
    super(EffectName.QUATERNION);
131
    mQuaternion = quaternion;
132
    mCenter = center;
133
    }
134
  }
(13-13/31)