Project

General

Profile

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

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

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 static final EffectName NAME = EffectName.VERTEX_QUATERNION;
33

    
34
  private Data4D mQuaternion;
35
  private Data3D mCenter;
36

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

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
  static String code()
52
    {
53
    return
54

    
55
       "float qx = vUniforms[effect].x;               \n"
56
     + "float qy = vUniforms[effect].y;               \n"
57
     + "float qz = vUniforms[effect].z;               \n"
58
     + "float qw = vUniforms[effect].w;               \n"
59
     + "vec3 center = vUniforms[effect+1].yzw;        \n"
60

    
61
     + "v -= center;                                  \n"
62

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

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

    
72
     + "v += center;                                  \n"
73

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

    
79
     + "n.x = qw*nx + qz*ny - qy*nz - qx*nw;          \n"
80
     + "n.y = qw*ny - qz*nx - qy*nw + qx*nz;          \n"
81
     + "n.z = qw*nz - qz*nw + qy*nx - qx*ny;          \n"
82

    
83
     + "#ifdef PREAPPLY                                \n"
84

    
85
     + "float ix =  - inf.z*qy + inf.y*qz + inf.x*qw;  \n"
86
     + "float iy =  + inf.z*qx + inf.y*qw - inf.x*qz;  \n"
87
     + "float iz =  + inf.z*qw - inf.y*qx + inf.x*qy;  \n"
88
     + "float iw =  - inf.z*qz - inf.y*qy - inf.x*qx;  \n"
89

    
90
     + "inf.x = qw*ix + qz*iy - qy*iz - qx*iw;         \n"
91
     + "inf.y = qw*iy - qz*ix - qy*iw + qx*iz;         \n"
92
     + "inf.z = qw*iz - qz*iw + qy*ix - qx*iy;         \n"
93

    
94
     + "#endif                                         \n";
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// PUBLIC API
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
/**
101
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
102
 */
103
  public static void enable()
104
    {
105
    addEffect( NAME, code() );
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
/**
110
 * Rotate the Mesh by a (possibly changing in time) quaternion.
111
 *
112
 * @param quaternion Quaternion describing the rotation.
113
 * @param center     Coordinates of the Point we are rotating around.
114
 */
115
  public VertexEffectQuaternion(Data4D quaternion, Data3D center )
116
    {
117
    super(NAME);
118
    mQuaternion = quaternion;
119
    mCenter = center;
120
    }
121
  }
(25-25/31)