Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectRotate.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.Data1D;
23
import org.distorted.library.type.Data3D;
24
import org.distorted.library.type.Data4D;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

    
28
/**
29
 * Deform the Mesh by applying a 3D vector of force.
30
 */
31
public class VertexEffectRotate extends VertexEffect
32
  {
33
  private static final EffectName NAME = EffectName.VERTEX_ROTATE;
34

    
35
  private Data1D mAngle;
36
  private Data3D mAxis, mCenter;
37

    
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
    mAxis.get(uniforms,index+VALUES_OFFSET+1,currentDuration,step);
48

    
49
    float len = uniforms[index+1]*uniforms[index+1] + uniforms[index+2]*uniforms[index+2] + uniforms[index+3]*uniforms[index+3];
50
    len = (float)Math.sqrt(len);
51

    
52
    if( len!=0 )
53
      {
54
      uniforms[index+VALUES_OFFSET+1] /= len;
55
      uniforms[index+VALUES_OFFSET+2] /= len;
56
      uniforms[index+VALUES_OFFSET+3] /= len;
57
      }
58

    
59
    return mAngle.get(uniforms,index+VALUES_OFFSET,currentDuration,step);
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  static String code()
65
    {
66
    return
67

    
68
      "float angle = vUniforms[effect].x*3.1415/360.0;\n"
69
    + "vec3 center = vUniforms[effect+1].yzw;         \n"
70
    + "float sinHalf = sin(angle);                    \n"
71
    + "float cosHalf = cos(angle);                    \n"
72

    
73
    + "float qx = vUniforms[effect].y * sinHalf;      \n"
74
    + "float qy = vUniforms[effect].z * sinHalf;      \n"
75
    + "float qz = vUniforms[effect].w * sinHalf;      \n"
76
    + "float qw = cosHalf;                            \n"
77

    
78
    + "v -= center;                                   \n"
79

    
80
    + "float tx = qx - v.z*qy + v.y*qz + v.x*qw;      \n"
81
    + "float ty = qy + v.z*qx + v.y*qw - v.x*qz;      \n"
82
    + "float tz = qz + v.z*qw - v.y*qx + v.x*qy;      \n"
83
    + "float tw = qw - v.z*qz - v.y*qy - v.x*qx;      \n"
84

    
85
    + "v.x = qw*tx + qz*ty - qy*tz - qx*tw;           \n"
86
    + "v.y = qw*ty - qz*tx - qy*tw + qx*tz;           \n"
87
    + "v.z = qw*tz - qz*tw + qy*tx - qx*ty;           \n"
88

    
89
    + "v += center;                                   \n"
90

    
91
    + "float nx =  - n.z*qy + n.y*qz + n.x*qw;        \n"
92
    + "float ny =  + n.z*qx + n.y*qw - n.x*qz;        \n"
93
    + "float nz =  + n.z*qw - n.y*qx + n.x*qy;        \n"
94
    + "float nw =  - n.z*qz - n.y*qy - n.x*qx;        \n"
95

    
96
    + "n.x = qw*nx + qz*ny - qy*nz - qx*nw;           \n"
97
    + "n.y = qw*ny - qz*nx - qy*nw + qx*nz;           \n"
98
    + "n.z = qw*nz - qz*nw + qy*nx - qx*ny;           \n"
99

    
100
    + "#ifdef PREAPPLY                                \n"
101

    
102
    + "float ix =  - inf.z*qy + inf.y*qz + inf.x*qw;  \n"
103
    + "float iy =  + inf.z*qx + inf.y*qw - inf.x*qz;  \n"
104
    + "float iz =  + inf.z*qw - inf.y*qx + inf.x*qy;  \n"
105
    + "float iw =  - inf.z*qz - inf.y*qy - inf.x*qx;  \n"
106

    
107
    + "inf.x = qw*ix + qz*iy - qy*iz - qx*iw;         \n"
108
    + "inf.y = qw*iy - qz*ix - qy*iw + qx*iz;         \n"
109
    + "inf.z = qw*iz - qz*iw + qy*ix - qx*iy;         \n"
110

    
111
    + "#endif                                         \n";
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
// PUBLIC API
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
/**
118
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
119
 */
120
  public static void enable()
121
    {
122
    addEffect( NAME, code() );
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
/**
127
 * Rotate the Mesh by 'angle' degrees around the center, along an axis.
128
 *
129
 * @param angle  Angle that we want to rotate the Object to. Unit: degrees
130
 * @param axis   Axis of rotation
131
 * @param center Coordinates of the Point we are rotating around.
132
 */
133
  public VertexEffectRotate(Data1D angle, Data3D axis, Data3D center)
134
    {
135
    super(NAME);
136
    mAngle = angle;
137
    mAxis = axis;
138
    mCenter = center;
139
    }
140
  }
(26-26/31)