Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectQuaternion.java @ e979d285

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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 org.distorted.library.type.Data3D;
23
import org.distorted.library.type.Data4D;
24

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

    
27
/**
28
 * Deform the Mesh by applying a 3D vector of force.
29
 */
30
public class VertexEffectQuaternion extends VertexEffect
31
  {
32
  private Data4D mQuaternion;
33
  private Data3D mCenter;
34

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

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
// PUBLIC API
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
/**
51
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
52
 */
53
  public static void enable()
54
    {
55
    addEffect( EffectName.VERTEX_QUATERNION,
56
               "float qx = vUniforms[effect].x;               \n"
57
             + "float qy = vUniforms[effect].y;               \n"
58
             + "float qz = vUniforms[effect].z;               \n"
59
             + "float qw = vUniforms[effect].w;               \n"
60

    
61
             + "float tx = qx - v.z*qy + v.y*qz + v.x*qw;     \n"
62
             + "float ty = qy + v.z*qx + v.y*qw - v.x*qz;     \n"
63
             + "float tz = qz + v.z*qw - v.y*qx + v.x*qy;     \n"
64
             + "float tw = qw - v.z*qz - v.y*qy - v.x*qx;     \n"
65

    
66
             + "v.x = qw*tx + qz*ty - qy*tz - qx*tw;          \n"
67
             + "v.y = qw*ty - qz*tx - qy*tw + qx*tz;          \n"
68
             + "v.z = qw*tz - qz*tw + qy*tx - qx*ty;          \n"
69

    
70
             + "float nx =  - n.z*qy + n.y*qz + n.x*qw;       \n"
71
             + "float ny =  + n.z*qx + n.y*qw - n.x*qz;       \n"
72
             + "float nz =  + n.z*qw - n.y*qx + n.x*qy;       \n"
73
             + "float nw =  - n.z*qz - n.y*qy - n.x*qx;       \n"
74

    
75
             + "n.x = qw*nx + qz*ny - qy*nz - qx*nw;          \n"
76
             + "n.y = qw*ny - qz*nx - qy*nw + qx*nz;          \n"
77
             + "n.z = qw*nz - qz*nw + qy*nx - qx*ny;          \n"
78
             );
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
/**
83
 * Rotate the Mesh by a (possibly changing in time) quaternion.
84
 *
85
 * @param quaternion Quaternion describing the rotation.
86
 * @param center     Coordinates of the Point we are rotating around.
87
 */
88
  public VertexEffectQuaternion(Data4D quaternion, Data3D center )
89
    {
90
    super(EffectName.VERTEX_QUATERNION);
91
    mQuaternion = quaternion;
92
    mCenter = center;
93
    }
94
  }
(25-25/28)