Project

General

Profile

« Previous | Next » 

Revision 9f34a0f6

Added by Leszek Koltunski about 4 years ago

new VertexEffectRotate

View differences:

src/main/java/org/distorted/library/effect/EffectName.java
57 57
  WAVE             ( EffectType.VERTEX  ,   new float[] {0.0f}           , 5, 4,     3    , VertexEffectWave.class         ),
58 58
  VERTEX_MOVE      ( EffectType.VERTEX  ,   new float[] {0.0f,0.0f,0.0f} , 3, 0,     0    , VertexEffectMove.class         ),
59 59
  VERTEX_QUATERNION( EffectType.VERTEX  ,   new float[] {0.0f,0.0f,0.0f} , 4, 0,     3    , VertexEffectQuaternion.class   ),
60
  VERTEX_ROTATE    ( EffectType.VERTEX  ,   new float[] {0.0f}           , 4, 0,     3    , VertexEffectRotate.class       ),
60 61

  
61 62
  ALPHA            ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, 3,     3    , FragmentEffectAlpha.class      ),
62 63
  SMOOTH_ALPHA     ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, 3,     3    , FragmentEffectAlpha.class      ),
src/main/java/org/distorted/library/effect/MatrixEffectRotate.java
54 54
 */
55 55
  public void apply(float[] matrix, float[] uniforms, int index)
56 56
    {
57
    float alpha = uniforms[NUM_UNIFORMS*index  ];
57
    float angle = uniforms[NUM_UNIFORMS*index  ];
58 58
    float axisX = uniforms[NUM_UNIFORMS*index+1];
59 59
    float axisY = uniforms[NUM_UNIFORMS*index+2];
60 60
    float axisZ = uniforms[NUM_UNIFORMS*index+3];
......
64 64
    float z = uniforms[NUM_UNIFORMS*index+CENTER_OFFSET+2];
65 65

  
66 66
    Matrix.translateM(matrix, 0, x, y, z);
67
    Matrix.rotateM( matrix, 0, alpha, axisX, axisY, axisZ);
67
    Matrix.rotateM( matrix, 0, angle, axisX, axisY, axisZ);
68 68
    Matrix.translateM(matrix, 0,-x,-y,-z);
69 69
    }
70 70

  
src/main/java/org/distorted/library/effect/VertexEffectRotate.java
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 Data1D mAngle;
34
  private Data3D mAxis, mCenter;
35

  
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
/**
38
 * Only for use by the library itself.
39
 *
40
 * @y.exclude
41
 */
42
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
43
    {
44
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
45
    mAxis.get(uniforms,index+1,currentDuration,step);
46

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

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

  
57
    return mAngle.get(uniforms,index,currentDuration,step);
58
    }
59

  
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
// PUBLIC API
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
/**
64
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
65
 */
66
  public static void enable()
67
    {
68
    addEffect( EffectName.VERTEX_ROTATE,
69

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

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

  
80
             + "v += center;                                   \n"
81

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

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

  
91
             + "v -= center;                                   \n"
92

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

  
98
             + "n.x = qw*nx + qz*ny - qy*nz - qx*nw;           \n"
99
             + "n.y = qw*ny - qz*nx - qy*nw + qx*nz;           \n"
100
             + "n.z = qw*nz - qz*nw + qy*nx - qx*ny;           \n"
101
             );
102
    }
103

  
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
/**
106
 * Rotate the Mesh by 'angle' degrees around the center, along an axis.
107
 *
108
 * @param angle  Angle that we want to rotate the Object to. Unit: degrees
109
 * @param axis   Axis of rotation
110
 * @param center Coordinates of the Point we are rotating around.
111
 */
112
  public VertexEffectRotate(Data1D angle, Data3D axis, Data3D center)
113
    {
114
    super(EffectName.VERTEX_ROTATE);
115
    mAngle = angle;
116
    mAxis = axis;
117
    mCenter = center;
118
    }
119
  }

Also available in: Unified diff