Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectRotate.java @ 457bd08e

1 9f34a0f6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26
27
/**
28 943b2e18 Leszek Koltunski
 * Rotate the Mesh along a given axis by a given angle.
29 9f34a0f6 Leszek Koltunski
 */
30
public class VertexEffectRotate extends VertexEffect
31
  {
32 f046b159 Leszek Koltunski
  private static final EffectName NAME = EffectName.VERTEX_ROTATE;
33
34 9f34a0f6 Leszek Koltunski
  private Data1D mAngle;
35
  private Data3D mAxis, 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 f046b159 Leszek Koltunski
    mAxis.get(uniforms,index+VALUES_OFFSET+1,currentDuration,step);
47 9f34a0f6 Leszek Koltunski
48
    float len = uniforms[index+1]*uniforms[index+1] + uniforms[index+2]*uniforms[index+2] + uniforms[index+3]*uniforms[index+3];
49
    len = (float)Math.sqrt(len);
50
51
    if( len!=0 )
52
      {
53 f046b159 Leszek Koltunski
      uniforms[index+VALUES_OFFSET+1] /= len;
54
      uniforms[index+VALUES_OFFSET+2] /= len;
55
      uniforms[index+VALUES_OFFSET+3] /= len;
56 9f34a0f6 Leszek Koltunski
      }
57
58 f046b159 Leszek Koltunski
    return mAngle.get(uniforms,index+VALUES_OFFSET,currentDuration,step);
59 9f34a0f6 Leszek Koltunski
    }
60
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62 f046b159 Leszek Koltunski
63
  static String code()
64 9f34a0f6 Leszek Koltunski
    {
65 f046b159 Leszek Koltunski
    return
66
67
      "float angle = vUniforms[effect].x*3.1415/360.0;\n"
68
    + "vec3 center = vUniforms[effect+1].yzw;         \n"
69 c61b08e4 Leszek Koltunski
    + "float sinHalf =-sin(angle);                    \n"
70 f046b159 Leszek Koltunski
    + "float cosHalf = cos(angle);                    \n"
71 9f34a0f6 Leszek Koltunski
72 f046b159 Leszek Koltunski
    + "float qx = vUniforms[effect].y * sinHalf;      \n"
73
    + "float qy = vUniforms[effect].z * sinHalf;      \n"
74
    + "float qz = vUniforms[effect].w * sinHalf;      \n"
75
    + "float qw = cosHalf;                            \n"
76 9f34a0f6 Leszek Koltunski
77 f046b159 Leszek Koltunski
    + "v -= center;                                   \n"
78 9f34a0f6 Leszek Koltunski
79 f046b159 Leszek Koltunski
    + "float tx = qx - v.z*qy + v.y*qz + v.x*qw;      \n"
80
    + "float ty = qy + v.z*qx + v.y*qw - v.x*qz;      \n"
81
    + "float tz = qz + v.z*qw - v.y*qx + v.x*qy;      \n"
82
    + "float tw = qw - v.z*qz - v.y*qy - v.x*qx;      \n"
83 9f34a0f6 Leszek Koltunski
84 f046b159 Leszek Koltunski
    + "v.x = qw*tx + qz*ty - qy*tz - qx*tw;           \n"
85
    + "v.y = qw*ty - qz*tx - qy*tw + qx*tz;           \n"
86
    + "v.z = qw*tz - qz*tw + qy*tx - qx*ty;           \n"
87 9f34a0f6 Leszek Koltunski
88 f046b159 Leszek Koltunski
    + "v += center;                                   \n"
89 9f34a0f6 Leszek Koltunski
90 f046b159 Leszek Koltunski
    + "float nx =  - n.z*qy + n.y*qz + n.x*qw;        \n"
91
    + "float ny =  + n.z*qx + n.y*qw - n.x*qz;        \n"
92
    + "float nz =  + n.z*qw - n.y*qx + n.x*qy;        \n"
93
    + "float nw =  - n.z*qz - n.y*qy - n.x*qx;        \n"
94 9f34a0f6 Leszek Koltunski
95 f046b159 Leszek Koltunski
    + "n.x = qw*nx + qz*ny - qy*nz - qx*nw;           \n"
96
    + "n.y = qw*ny - qz*nx - qy*nw + qx*nz;           \n"
97 457bd08e Leszek Koltunski
    + "n.z = qw*nz - qz*nw + qy*nx - qx*ny;           \n"
98
99
    + "n = normalize(n);";
100 f046b159 Leszek Koltunski
    }
101 9f34a0f6 Leszek Koltunski
102 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
103
// PUBLIC API
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
/**
106
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
107
 */
108
  public static void enable()
109
    {
110
    addEffect( NAME, code() );
111 9f34a0f6 Leszek Koltunski
    }
112
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
/**
115
 * Rotate the Mesh by 'angle' degrees around the center, along an axis.
116
 *
117
 * @param angle  Angle that we want to rotate the Object to. Unit: degrees
118
 * @param axis   Axis of rotation
119
 * @param center Coordinates of the Point we are rotating around.
120
 */
121
  public VertexEffectRotate(Data1D angle, Data3D axis, Data3D center)
122
    {
123 f046b159 Leszek Koltunski
    super(NAME);
124 9f34a0f6 Leszek Koltunski
    mAngle = angle;
125
    mAxis = axis;
126
    mCenter = center;
127
    }
128
  }