Project

General

Profile

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

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

1 9f34a0f6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 cc62b34a Leszek Koltunski
// Copyright 2017 Leszek Koltunski  leszek@koltunski.pl                                          //
3 9f34a0f6 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 9f34a0f6 Leszek Koltunski
//                                                                                               //
11 cc62b34a Leszek Koltunski
// This library is distributed in the hope that it will be useful,                               //
12 9f34a0f6 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 9f34a0f6 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 9f34a0f6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
20
21
package org.distorted.library.effect;
22
23
import org.distorted.library.type.Data1D;
24
import org.distorted.library.type.Data3D;
25
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
28
/**
29 943b2e18 Leszek Koltunski
 * Rotate the Mesh along a given axis by a given angle.
30 9f34a0f6 Leszek Koltunski
 */
31
public class VertexEffectRotate extends VertexEffect
32
  {
33 f046b159 Leszek Koltunski
  private static final EffectName NAME = EffectName.VERTEX_ROTATE;
34
35 835b197e Leszek Koltunski
  private final Data1D mAngle;
36
  private final Data3D mAxis, mCenter;
37 9f34a0f6 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
    mAxis.get(uniforms,index+VALUES_OFFSET+1,currentDuration,step);
48 9f34a0f6 Leszek Koltunski
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 f046b159 Leszek Koltunski
      uniforms[index+VALUES_OFFSET+1] /= len;
55
      uniforms[index+VALUES_OFFSET+2] /= len;
56
      uniforms[index+VALUES_OFFSET+3] /= len;
57 9f34a0f6 Leszek Koltunski
      }
58
59 f046b159 Leszek Koltunski
    return mAngle.get(uniforms,index+VALUES_OFFSET,currentDuration,step);
60 9f34a0f6 Leszek Koltunski
    }
61
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63 f046b159 Leszek Koltunski
64
  static String code()
65 9f34a0f6 Leszek Koltunski
    {
66 f046b159 Leszek Koltunski
    return
67
68
      "float angle = vUniforms[effect].x*3.1415/360.0;\n"
69
    + "vec3 center = vUniforms[effect+1].yzw;         \n"
70 c61b08e4 Leszek Koltunski
    + "float sinHalf =-sin(angle);                    \n"
71 f046b159 Leszek Koltunski
    + "float cosHalf = cos(angle);                    \n"
72 9f34a0f6 Leszek Koltunski
73 f046b159 Leszek Koltunski
    + "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 9f34a0f6 Leszek Koltunski
78 f046b159 Leszek Koltunski
    + "v -= center;                                   \n"
79 9f34a0f6 Leszek Koltunski
80 f046b159 Leszek Koltunski
    + "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 9f34a0f6 Leszek Koltunski
85 f046b159 Leszek Koltunski
    + "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 9f34a0f6 Leszek Koltunski
89 f046b159 Leszek Koltunski
    + "v += center;                                   \n"
90 9f34a0f6 Leszek Koltunski
91 f046b159 Leszek Koltunski
    + "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 9f34a0f6 Leszek Koltunski
96 f046b159 Leszek Koltunski
    + "n.x = qw*nx + qz*ny - qy*nz - qx*nw;           \n"
97
    + "n.y = qw*ny - qz*nx - qy*nw + qx*nz;           \n"
98 457bd08e Leszek Koltunski
    + "n.z = qw*nz - qz*nw + qy*nx - qx*ny;           \n"
99
100
    + "n = normalize(n);";
101 f046b159 Leszek Koltunski
    }
102 9f34a0f6 Leszek Koltunski
103 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
104
// PUBLIC API
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
/**
107
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
108
 */
109
  public static void enable()
110
    {
111
    addEffect( NAME, code() );
112 9f34a0f6 Leszek Koltunski
    }
113
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
/**
116
 * Rotate the Mesh by 'angle' degrees around the center, along an axis.
117
 *
118
 * @param angle  Angle that we want to rotate the Object to. Unit: degrees
119
 * @param axis   Axis of rotation
120
 * @param center Coordinates of the Point we are rotating around.
121
 */
122
  public VertexEffectRotate(Data1D angle, Data3D axis, Data3D center)
123
    {
124 f046b159 Leszek Koltunski
    super(NAME);
125 9f34a0f6 Leszek Koltunski
    mAngle = angle;
126
    mAxis = axis;
127
    mCenter = center;
128
    }
129
  }