Project

General

Profile

« Previous | Next » 

Revision e979d285

Added by Leszek Koltunski over 4 years ago

Add the first two VERTEX 'pseudo-matrix' effects: VERTEX_MOVE & VERTEX_QUATERNION i.e. implementations of those two MATRIX effects in VERTEX queue.

The point(s):

1) preparation for the upcoming MeshBase.preApply(VertexEffect effect) API that's going to supersede the current much more limited MeshBase.apply(MatrixEffect effect)
2) those can be put in any position in the VERTEX queue, whereas the Matrix effects always have to come last

View differences:

src/main/java/org/distorted/library/effect/EffectName.java
55 55
  PINCH            ( EffectType.VERTEX  ,   new float[] {1.0f}           , 3, 4,     3    , VertexEffectPinch.class        ),
56 56
  SWIRL            ( EffectType.VERTEX  ,   new float[] {0.0f}           , 1, 4,     3    , VertexEffectSwirl.class        ),
57 57
  WAVE             ( EffectType.VERTEX  ,   new float[] {0.0f}           , 5, 4,     3    , VertexEffectWave.class         ),
58
  VERTEX_MOVE      ( EffectType.VERTEX  ,   new float[] {0.0f,0.0f,0.0f} , 3, 0,     0    , VertexEffectMove.class         ),
59
  VERTEX_QUATERNION( EffectType.VERTEX  ,   new float[] {0.0f,0.0f,0.0f} , 4, 0,     3    , VertexEffectQuaternion.class   ),
58 60

  
59 61
  ALPHA            ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, 3,     3    , FragmentEffectAlpha.class      ),
60 62
  SMOOTH_ALPHA     ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, 3,     3    , FragmentEffectAlpha.class      ),
src/main/java/org/distorted/library/effect/MatrixEffect.java
29 29
 * 7: 4 per-effect interpolated values + 3 dimensional center.
30 30
 */
31 31
  public static final int NUM_UNIFORMS = 7;
32
  static final int CENTER_OFFSET = 4;
32 33

  
33 34
///////////////////////////////////////////////////////////////////////////////////////////////////
34 35
/**
src/main/java/org/distorted/library/effect/MatrixEffectQuaternion.java
44 44
 */
45 45
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
46 46
    {
47
    mCenter.get(uniforms,index+4,currentDuration,step);
47
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
48 48
    return mQuaternion.get(uniforms,index,currentDuration,step);
49 49
    }
50 50

  
......
61 61
    float qZ = uniforms[NUM_UNIFORMS*index+2];
62 62
    float qW = uniforms[NUM_UNIFORMS*index+3];
63 63

  
64
    float x = uniforms[NUM_UNIFORMS*index+4];
65
    float y = uniforms[NUM_UNIFORMS*index+5];
66
    float z = uniforms[NUM_UNIFORMS*index+6];
64
    float x = uniforms[NUM_UNIFORMS*index+CENTER_OFFSET  ];
65
    float y = uniforms[NUM_UNIFORMS*index+CENTER_OFFSET+1];
66
    float z = uniforms[NUM_UNIFORMS*index+CENTER_OFFSET+2];
67 67

  
68 68
    Matrix.translateM(matrix, 0, x, y, z);
69 69
    multiplyByQuat( matrix, qX, qY, qZ, qW);
src/main/java/org/distorted/library/effect/MatrixEffectRotate.java
41 41
 */
42 42
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
43 43
    {
44
    mCenter.get(uniforms,index+4,currentDuration,step);
44
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
45 45
    mAxis.get(uniforms,index+1,currentDuration,step);
46 46
    return mAngle.get(uniforms,index,currentDuration,step);
47 47
    }
......
59 59
    float axisY = uniforms[NUM_UNIFORMS*index+2];
60 60
    float axisZ = uniforms[NUM_UNIFORMS*index+3];
61 61

  
62
    float x = uniforms[NUM_UNIFORMS*index+4];
63
    float y = uniforms[NUM_UNIFORMS*index+5];
64
    float z = uniforms[NUM_UNIFORMS*index+6];
62
    float x = uniforms[NUM_UNIFORMS*index+CENTER_OFFSET  ];
63
    float y = uniforms[NUM_UNIFORMS*index+CENTER_OFFSET+1];
64
    float z = uniforms[NUM_UNIFORMS*index+CENTER_OFFSET+2];
65 65

  
66 66
    Matrix.translateM(matrix, 0, x, y, z);
67 67
    Matrix.rotateM( matrix, 0, alpha, axisX, axisY, axisZ);
src/main/java/org/distorted/library/effect/MatrixEffectShear.java
39 39
 */
40 40
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
41 41
    {
42
    mCenter.get(uniforms,index+4,currentDuration,step);
42
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
43 43
    return mShear.get(uniforms,index,currentDuration,step);
44 44
    }
45 45

  
......
55 55
    float sy = uniforms[NUM_UNIFORMS*index+1];
56 56
    float sz = uniforms[NUM_UNIFORMS*index+2];
57 57

  
58
    float x  = uniforms[NUM_UNIFORMS*index+4];
59
    float y  = uniforms[NUM_UNIFORMS*index+5];
60
    float z  = uniforms[NUM_UNIFORMS*index+6];
58
    float x  = uniforms[NUM_UNIFORMS*index+CENTER_OFFSET  ];
59
    float y  = uniforms[NUM_UNIFORMS*index+CENTER_OFFSET+1];
60
    float z  = uniforms[NUM_UNIFORMS*index+CENTER_OFFSET+2];
61 61

  
62 62
    Matrix.translateM(matrix, 0, x, y, z);
63 63

  
src/main/java/org/distorted/library/effect/VertexEffectMove.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.Data3D;
23

  
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

  
26
/**
27
 * Deform the Mesh by applying a 3D vector of force.
28
 */
29
public class VertexEffectMove extends VertexEffect
30
  {
31
  private Data3D mVector;
32

  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
/**
35
 * Only for use by the library itself.
36
 *
37
 * @y.exclude
38
 */
39
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
40
    {
41
    return mVector.get(uniforms,index,currentDuration,step);
42
    }
43

  
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
// PUBLIC API
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
/**
48
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
49
 */
50
  public static void enable()
51
    {
52
    addEffect( EffectName.VERTEX_MOVE, "v += vUniforms[effect].xyz;" );
53
    }
54

  
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
/**
57
 * Move the whole Mesh with a (possibly changing in time) vector.
58
 *
59
 * @param vector Vector by which to move the Mesh.
60
 */
61
  public VertexEffectMove(Data3D vector)
62
    {
63
    super(EffectName.VERTEX_MOVE);
64
    mVector = vector;
65
    }
66
  }
src/main/java/org/distorted/library/effect/VertexEffectQuaternion.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.Data3D;
23
import org.distorted.library.type.Data4D;
24

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

  
27
/**
28
 * Deform the Mesh by applying a 3D vector of force.
29
 */
30
public class VertexEffectQuaternion extends VertexEffect
31
  {
32
  private Data4D mQuaternion;
33
  private Data3D mCenter;
34

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

  
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
// PUBLIC API
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
/**
51
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
52
 */
53
  public static void enable()
54
    {
55
    addEffect( EffectName.VERTEX_QUATERNION,
56
               "float qx = vUniforms[effect].x;               \n"
57
             + "float qy = vUniforms[effect].y;               \n"
58
             + "float qz = vUniforms[effect].z;               \n"
59
             + "float qw = vUniforms[effect].w;               \n"
60

  
61
             + "float tx = qx - v.z*qy + v.y*qz + v.x*qw;     \n"
62
             + "float ty = qy + v.z*qx + v.y*qw - v.x*qz;     \n"
63
             + "float tz = qz + v.z*qw - v.y*qx + v.x*qy;     \n"
64
             + "float tw = qw - v.z*qz - v.y*qy - v.x*qx;     \n"
65

  
66
             + "v.x = qw*tx + qz*ty - qy*tz - qx*tw;          \n"
67
             + "v.y = qw*ty - qz*tx - qy*tw + qx*tz;          \n"
68
             + "v.z = qw*tz - qz*tw + qy*tx - qx*ty;          \n"
69

  
70
             + "float nx =  - n.z*qy + n.y*qz + n.x*qw;       \n"
71
             + "float ny =  + n.z*qx + n.y*qw - n.x*qz;       \n"
72
             + "float nz =  + n.z*qw - n.y*qx + n.x*qy;       \n"
73
             + "float nw =  - n.z*qz - n.y*qy - n.x*qx;       \n"
74

  
75
             + "n.x = qw*nx + qz*ny - qy*nz - qx*nw;          \n"
76
             + "n.y = qw*ny - qz*nx - qy*nw + qx*nz;          \n"
77
             + "n.z = qw*nz - qz*nw + qy*nx - qx*ny;          \n"
78
             );
79
    }
80

  
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
/**
83
 * Rotate the Mesh by a (possibly changing in time) quaternion.
84
 *
85
 * @param quaternion Quaternion describing the rotation.
86
 * @param center     Coordinates of the Point we are rotating around.
87
 */
88
  public VertexEffectQuaternion(Data4D quaternion, Data3D center )
89
    {
90
    super(EffectName.VERTEX_QUATERNION);
91
    mQuaternion = quaternion;
92
    mCenter = center;
93
    }
94
  }
src/main/res/raw/main_vertex_shader.glsl
181 181
#endif
182 182
   
183 183
  v_Position      = v;
184
  v_endPosition   = v + 0.2*u_Stretch*n;
184
  v_endPosition   = v + (0.2*u_Stretch.x)*n;
185 185
  v_TexCoordinate = a_TexCoordinate;
186 186
  v_Normal        = normalize(vec3(u_MVMatrix*vec4(n,0.0)));
187 187
  gl_Position     = u_MVPMatrix*vec4(v,1.0);

Also available in: Unified diff