Project

General

Profile

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

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

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

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

    
27
/**
28
 * Rotate the Mesh along a given axis by a given angle.
29
 */
30
public class VertexEffectRotate extends VertexEffect
31
  {
32
  private static final EffectName NAME = EffectName.VERTEX_ROTATE;
33

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

    
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
      uniforms[index+VALUES_OFFSET+1] /= len;
54
      uniforms[index+VALUES_OFFSET+2] /= len;
55
      uniforms[index+VALUES_OFFSET+3] /= len;
56
      }
57

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

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  static String code()
64
    {
65
    return
66

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

    
72
    + "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

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

    
79
    + "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

    
84
    + "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

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

    
90
    + "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

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

    
99
    + "n = normalize(n);";
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
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
    }
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
    super(NAME);
124
    mAngle = angle;
125
    mAxis = axis;
126
    mCenter = center;
127
    }
128
  }
(27-27/32)