Project

General

Profile

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

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

1 e979d285 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 cc62b34a Leszek Koltunski
// Copyright 2017 Leszek Koltunski  leszek@koltunski.pl                                          //
3 e979d285 Leszek Koltunski
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6 cc62b34a Leszek Koltunski
// 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 e979d285 Leszek Koltunski
//                                                                                               //
11 cc62b34a Leszek Koltunski
// This library is distributed in the hope that it will be useful,                               //
12 e979d285 Leszek Koltunski
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13 cc62b34a Leszek Koltunski
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15 e979d285 Leszek Koltunski
//                                                                                               //
16 cc62b34a Leszek Koltunski
// 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 e979d285 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
20
21
package org.distorted.library.effect;
22
23
import org.distorted.library.type.Data3D;
24
import org.distorted.library.type.Data4D;
25
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
28
/**
29 943b2e18 Leszek Koltunski
 * Apply a given quaternion rotation to the Mesh.
30 e979d285 Leszek Koltunski
 */
31
public class VertexEffectQuaternion extends VertexEffect
32
  {
33 f046b159 Leszek Koltunski
  private static final EffectName NAME = EffectName.VERTEX_QUATERNION;
34
35 0e1d3d2e Leszek Koltunski
  private final Data4D mQuaternion;
36
  private final Data3D mCenter;
37 e979d285 Leszek Koltunski
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39
/**
40
 * Only for use by the library itself.
41
 *
42
 * @y.exclude
43
 */
44
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
45
    {
46
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
47 f046b159 Leszek Koltunski
    return mQuaternion.get(uniforms,index+VALUES_OFFSET,currentDuration,step);
48
    }
49
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51
52
  static String code()
53
    {
54
    return
55
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
     + "vec3 center = vUniforms[effect+1].yzw;        \n"
61
62
     + "v -= center;                                  \n"
63
64
     + "float tx = qx - v.z*qy + v.y*qz + v.x*qw;     \n"
65
     + "float ty = qy + v.z*qx + v.y*qw - v.x*qz;     \n"
66
     + "float tz = qz + v.z*qw - v.y*qx + v.x*qy;     \n"
67
     + "float tw = qw - v.z*qz - v.y*qy - v.x*qx;     \n"
68
69
     + "v.x = qw*tx + qz*ty - qy*tz - qx*tw;          \n"
70
     + "v.y = qw*ty - qz*tx - qy*tw + qx*tz;          \n"
71
     + "v.z = qw*tz - qz*tw + qy*tx - qx*ty;          \n"
72
73
     + "v += center;                                  \n"
74
75
     + "float nx =  - n.z*qy + n.y*qz + n.x*qw;       \n"
76
     + "float ny =  + n.z*qx + n.y*qw - n.x*qz;       \n"
77
     + "float nz =  + n.z*qw - n.y*qx + n.x*qy;       \n"
78
     + "float nw =  - n.z*qz - n.y*qy - n.x*qx;       \n"
79
80
     + "n.x = qw*nx + qz*ny - qy*nz - qx*nw;          \n"
81
     + "n.y = qw*ny - qz*nx - qy*nw + qx*nz;          \n"
82 457bd08e Leszek Koltunski
     + "n.z = qw*nz - qz*nw + qy*nx - qx*ny;          \n"
83
84
     + "n = normalize(n);";
85 e979d285 Leszek Koltunski
    }
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
// PUBLIC API
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
/**
91
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
92
 */
93
  public static void enable()
94
    {
95 f046b159 Leszek Koltunski
    addEffect( NAME, code() );
96 e979d285 Leszek Koltunski
    }
97
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
/**
100
 * Rotate the Mesh by a (possibly changing in time) quaternion.
101
 *
102
 * @param quaternion Quaternion describing the rotation.
103
 * @param center     Coordinates of the Point we are rotating around.
104
 */
105
  public VertexEffectQuaternion(Data4D quaternion, Data3D center )
106
    {
107 f046b159 Leszek Koltunski
    super(NAME);
108 e979d285 Leszek Koltunski
    mQuaternion = quaternion;
109
    mCenter = center;
110
    }
111
  }