Project

General

Profile

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

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

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

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
// PUBLIC API
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
/**
88
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
89
 */
90
  public static void enable()
91
    {
92
    addEffect( NAME, code() );
93
    }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
/**
97
 * Rotate the Mesh by a (possibly changing in time) quaternion.
98
 *
99
 * @param quaternion Quaternion describing the rotation.
100
 * @param center     Coordinates of the Point we are rotating around.
101
 */
102
  public VertexEffectQuaternion(Data4D quaternion, Data3D center )
103
    {
104
    super(NAME);
105
    mQuaternion = quaternion;
106
    mCenter = center;
107
    }
108
  }
(26-26/32)