Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// 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
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
 * Rotate the Mesh along a given axis by a given angle.
30
 */
31
public class VertexEffectRotate extends VertexEffect
32
  {
33
  private static final EffectName NAME = EffectName.VERTEX_ROTATE;
34

    
35
  private final Data1D mAngle;
36
  private final 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
    + "n = normalize(n);";
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
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
    }
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
    super(NAME);
125
    mAngle = angle;
126
    mAxis = axis;
127
    mCenter = center;
128
    }
129
  }
(29-29/34)