Project

General

Profile

« Previous | Next » 

Revision fa8bc998

Added by Leszek Koltunski about 4 years ago

New API MeshBase.apply(MatrixEffect[])

View differences:

src/main/java/org/distorted/library/effect/MatrixEffect.java
19 19

  
20 20
package org.distorted.library.effect;
21 21

  
22
import android.opengl.Matrix;
23

  
22 24
///////////////////////////////////////////////////////////////////////////////////////////////////
23 25
/**
24 26
 * Abstract class that represents an Effect that works by modifying the ModelView matrix.
......
30 32
 */
31 33
  public static final int NUM_UNIFORMS = 7;
32 34

  
35
  private float[] mMatrix,mTmpArray;
36

  
33 37
///////////////////////////////////////////////////////////////////////////////////////////////////
34 38
/**
35 39
 * Only for use by the library itself.
......
38 42
 */
39 43
  public abstract void apply(float[] matrix, float[] uniforms, int index);
40 44

  
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
/**
47
 * Only for use by the library itself.
48
 * <p>
49
 * The three values buffer[index], buffer[index+1], buffer[index+2] are assumed to contain the
50
 * x,y,z coordinates of a point. We apply this Matrix effect to this point and overwrite those
51
 * three values.
52
 *
53
 * This is on purpose a static evaluation - if this Effect contains any Dynamics, they will be
54
 * evaluated at 0.
55
 *
56
 * @y.exclude
57
 */
58
 public void applyToPoint(float[] buffer, int index)
59
   {
60
   Matrix.setIdentityM(mMatrix,0);
61
   compute(mTmpArray,0,0,0);
62
   apply(mMatrix, mTmpArray, 0);
63

  
64
   float x = buffer[index  ];
65
   float y = buffer[index+1];
66
   float z = buffer[index+2];
67

  
68
   buffer[index  ] = mMatrix[0]*x + mMatrix[4]*y + mMatrix[ 8]*z + mMatrix[12];
69
   buffer[index+1] = mMatrix[1]*x + mMatrix[5]*y + mMatrix[ 9]*z + mMatrix[13];
70
   buffer[index+2] = mMatrix[2]*x + mMatrix[6]*y + mMatrix[10]*z + mMatrix[14];
71
   }
72

  
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
/**
75
 * Only for use by the library itself.
76
 * <p>
77
 * The three values buffer[index], buffer[index+1], buffer[index+2] are assumed to contain the
78
 * x,y,z coordinates of a vector. We apply this Matrix effect to this vector and overwrite those
79
 * three values.
80
 *
81
 * This is on purpose a static evaluation - if this Effect contains any Dynamics, they will be
82
 * evaluated at 0.
83
 *
84
 * @y.exclude
85
 */
86
 public void applyToVector(float[] buffer, int index)
87
   {
88
   Matrix.setIdentityM(mMatrix,0);
89
   compute(mTmpArray,0,0,0);
90
   apply(mMatrix, mTmpArray, 0);
91

  
92
   float x = buffer[index  ];
93
   float y = buffer[index+1];
94
   float z = buffer[index+2];
95

  
96
   buffer[index  ] = mMatrix[0]*x + mMatrix[4]*y + mMatrix[ 8]*z;
97
   buffer[index+1] = mMatrix[1]*x + mMatrix[5]*y + mMatrix[ 9]*z;
98
   buffer[index+2] = mMatrix[2]*x + mMatrix[6]*y + mMatrix[10]*z;
99
   }
100

  
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
/**
103
 * Only for use by the library itself.
104
 * <p>
105
 * The 9 floats starting at buffer[index] are assumed to contain info about a vertex arranged in
106
 * MeshBase way, i.e.  coord point (x,y,z) ; normal vector (x,y,z) ; inflate vector (x,y,z)
107
 *
108
 * @y.exclude
109
 */
110
 public void applyToVertex(float[] buffer, int index)
111
   {
112
   float x,y,z;
113

  
114
   Matrix.setIdentityM(mMatrix,0);
115
   compute(mTmpArray,0,0,0);
116
   apply(mMatrix, mTmpArray, 0);
117

  
118
   x = buffer[index  ];
119
   y = buffer[index+1];
120
   z = buffer[index+2];
121

  
122
   buffer[index  ] = mMatrix[0]*x + mMatrix[4]*y + mMatrix[ 8]*z + mMatrix[12];
123
   buffer[index+1] = mMatrix[1]*x + mMatrix[5]*y + mMatrix[ 9]*z + mMatrix[13];
124
   buffer[index+2] = mMatrix[2]*x + mMatrix[6]*y + mMatrix[10]*z + mMatrix[14];
125

  
126
   x = buffer[index+3];
127
   y = buffer[index+4];
128
   z = buffer[index+5];
129

  
130
   buffer[index+3] = mMatrix[0]*x + mMatrix[4]*y + mMatrix[ 8]*z;
131
   buffer[index+4] = mMatrix[1]*x + mMatrix[5]*y + mMatrix[ 9]*z;
132
   buffer[index+5] = mMatrix[2]*x + mMatrix[6]*y + mMatrix[10]*z;
133

  
134
   x = buffer[index+6];
135
   y = buffer[index+7];
136
   z = buffer[index+8];
137

  
138
   buffer[index+6] = mMatrix[0]*x + mMatrix[4]*y + mMatrix[ 8]*z;
139
   buffer[index+7] = mMatrix[1]*x + mMatrix[5]*y + mMatrix[ 9]*z;
140
   buffer[index+8] = mMatrix[2]*x + mMatrix[6]*y + mMatrix[10]*z;
141
   }
142

  
41 143
///////////////////////////////////////////////////////////////////////////////////////////////////
42 144
// empty function for completeness
43 145

  
......
51 153
  MatrixEffect(EffectName name)
52 154
    {
53 155
    super(name);
156

  
157
    mMatrix  = new float[16];
158
    mTmpArray= new float[4];
54 159
    }
55 160
  }
src/main/java/org/distorted/library/mesh/MeshBase.java
21 21

  
22 22
import android.opengl.GLES31;
23 23

  
24
import org.distorted.library.effect.MatrixEffect;
24 25
import org.distorted.library.main.DistortedLibrary;
25 26
import org.distorted.library.main.InternalBuffer;
26 27
import org.distorted.library.program.DistortedProgram;
......
60 61
   private static final int TRAN_SIZE  = TRAN_ATTRIBS*BYTES_PER_FLOAT;
61 62
   private static final int VERT_SIZE  = VERT_ATTRIBS*BYTES_PER_FLOAT;
62 63

  
63
   private boolean mShowNormals;       // when rendering this mesh, draw normal vectors?
64
   private InternalBuffer mVBO, mTFO;  // main vertex buffer and transform feedback buffer
65
   private final float zFactor;        // strange workaround for the fact that we need to somehow store the 'depth'
66
                                       // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
64
   private boolean mShowNormals;      // when rendering this mesh, draw normal vectors?
65
   private InternalBuffer mVBO, mTFO; // main vertex buffer and transform feedback buffer
66
   private final float zFactor;       // strange workaround for the fact that we need to somehow store the 'depth'
67
                                      // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
67 68
   private int mNumVertices;
68
   private FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ, TexS,TexT
69
   private float[] mVertAttribs;      // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ, TexS,TexT
69 70
   private float mInflate;
70 71

  
71 72
///////////////////////////////////////////////////////////////////////////////////////////////////
......
85 86
   void setAttribs(float[] vertexAttribs)
86 87
     {
87 88
     mNumVertices = vertexAttribs.length/VERT_ATTRIBS;
89
     mVertAttribs = vertexAttribs;
88 90

  
89
     mVertAttribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
90
     mVertAttribs.put(vertexAttribs).position(0);
91
     FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
92
     attribs.put(vertexAttribs).position(0);
91 93

  
92
     mVBO.setData(mNumVertices*VERT_SIZE, mVertAttribs);
93
     mTFO.setData(mNumVertices*TRAN_SIZE, null        );
94
     mVBO.setData(mNumVertices*VERT_SIZE, attribs);
95
     mTFO.setData(mNumVertices*TRAN_SIZE, null   );
94 96
     }
95 97

  
96 98
///////////////////////////////////////////////////////////////////////////////////////////////////
......
209 211
 */
210 212
   public void markForDeletion()
211 213
     {
212
     mVertAttribs.clear();
214
     mVertAttribs = null;
213 215

  
214 216
     mVBO.markForDeletion();
215 217
     mTFO.markForDeletion();
216 218
     }
219

  
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221
/**
222
 * Multiply all coordinates, normal and inflate vectors by the matrix.
223
 * <p>
224
 * This is a static, permanent modification of the vertices contained in this Mesh. If the Effect
225
 * contains any Dynamics, they will be evaluated at 0.
226
 *
227
 * Please note that calling this once with the complete list of Effects will be much faster than
228
 * calling it repeatedly with one Effect at a time, as we have to reallocate the array of vertices
229
 * each time.
230
 */
231
   public void apply(MatrixEffect[] effects)
232
     {
233
     for(int v=0; v<mNumVertices; v++)
234
       {
235
       for(MatrixEffect eff: effects)
236
         {
237
         if( eff!=null ) eff.applyToVertex( mVertAttribs, v*VERT_ATTRIBS );
238
         }
239
       }
240

  
241
     setAttribs(mVertAttribs);
242
     }
217 243
   }
218 244

  
219 245

  

Also available in: Unified diff