Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / MatrixEffectQuaternion.java @ 227b03bd

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
public class MatrixEffectQuaternion extends MatrixEffect
30
  {
31
  private Data4D mQuaternion;
32
  private Data3D mCenter;
33

    
34
  private static float[] mTmpMatrix1 = new float[16];
35
  private static float[] mTmpMatrix2 = new float[16];
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
/**
39
 * Rotates the Object by quaternion.
40
 *
41
 * @param quaternion Quaternion describing the rotation.
42
 * @param center     Coordinates of the Point we are rotating around.
43
 */
44
  public MatrixEffectQuaternion(Data4D quaternion, Data3D center )
45
    {
46
    super(EffectName.QUATERNION);
47
    mQuaternion = quaternion;
48
    mCenter = center;
49
    }
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
54
    {
55
    mCenter.get(uniforms,index+4,currentDuration,step);
56
    return mQuaternion.get(uniforms,index,currentDuration,step);
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  public void apply(float[] matrix, float[] uniforms, int index)
62
    {
63
    float qX = uniforms[NUM_UNIFORMS*index  ];
64
    float qY = uniforms[NUM_UNIFORMS*index+1];
65
    float qZ = uniforms[NUM_UNIFORMS*index+2];
66
    float qW = uniforms[NUM_UNIFORMS*index+3];
67

    
68
    float x = uniforms[NUM_UNIFORMS*index+4];
69
    float y = uniforms[NUM_UNIFORMS*index+5];
70
    float z = uniforms[NUM_UNIFORMS*index+6];
71

    
72
    Matrix.translateM(matrix, 0, x,-y, z);
73
    multiplyByQuat( matrix, qX, qY, qZ, qW);
74
    Matrix.translateM(matrix, 0,-x, y,-z);
75
    }
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
  }
(12-12/25)