Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// 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
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.effect;
22

    
23
import android.opengl.Matrix;
24

    
25
import org.distorted.library.type.Data3D;
26
import org.distorted.library.type.Data4D;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
/**
30
 * Rotate the Mesh by a quaternion.
31
 */
32
public class MatrixEffectQuaternion extends MatrixEffect
33
  {
34
  private final Data4D mQuaternion;
35
  private final Data3D mCenter;
36

    
37
  private final float[] mTmpMatrix1 = new float[16];
38
  private final float[] mTmpMatrix2 = new float[16];
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
/**
42
 * Only for use by the library itself.
43
 *
44
 * @y.exclude
45
 */
46
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
47
    {
48
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
49
    return mQuaternion.get(uniforms,index,currentDuration,step);
50
    }
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
/**
54
 * Only for use by the library itself.
55
 *
56
 * @y.exclude
57
 */
58
  public void apply(float[] matrixP, float[] matrixV, float[] uniforms, int index)
59
    {
60
    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

    
65
    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

    
69
    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
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  private void multiplyByQuat(float[] matrix, float X, float Y, float Z, float W)
81
    {
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

    
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
  }
(13-13/34)