Revision c90aca24
Added by Leszek Koltunski about 4 years ago
src/main/java/org/distorted/library/mesh/MeshBase.java | ||
---|---|---|
20 | 20 |
package org.distorted.library.mesh; |
21 | 21 |
|
22 | 22 |
import android.opengl.GLES31; |
23 |
import android.opengl.Matrix; |
|
23 | 24 |
|
24 | 25 |
import org.distorted.library.effect.MatrixEffect; |
25 | 26 |
import org.distorted.library.main.DistortedLibrary; |
... | ... | |
29 | 30 |
import java.nio.ByteBuffer; |
30 | 31 |
import java.nio.ByteOrder; |
31 | 32 |
import java.nio.FloatBuffer; |
33 |
import java.util.ArrayList; |
|
32 | 34 |
|
33 | 35 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
34 | 36 |
/** |
... | ... | |
63 | 65 |
|
64 | 66 |
private boolean mShowNormals; // when rendering this mesh, draw normal vectors? |
65 | 67 |
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(). |
|
68 | 68 |
private int mNumVertices; |
69 | 69 |
private float[] mVertAttribs; // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ, TexS,TexT |
70 | 70 |
private float mInflate; |
71 | 71 |
|
72 |
private class Component |
|
73 |
{ |
|
74 |
private int mEndIndex; |
|
75 |
private float[] mTextureMap; |
|
76 |
|
|
77 |
Component() |
|
78 |
{ |
|
79 |
mTextureMap = new float[8]; |
|
80 |
|
|
81 |
mTextureMap[ 0] = 0.0f; // LLX |
|
82 |
mTextureMap[ 1] = 0.0f; // LLY |
|
83 |
mTextureMap[ 2] = 0.0f; // ULX |
|
84 |
mTextureMap[ 3] = 1.0f; // ULY |
|
85 |
mTextureMap[ 4] = 1.0f; // URX |
|
86 |
mTextureMap[ 5] = 1.0f; // URY |
|
87 |
mTextureMap[ 6] = 1.0f; // LRX |
|
88 |
mTextureMap[ 7] = 0.0f; // LRY |
|
89 |
} |
|
90 |
Component(Component original) |
|
91 |
{ |
|
92 |
mEndIndex = original.mEndIndex; |
|
93 |
mTextureMap = new float[8]; |
|
94 |
System.arraycopy(original.mTextureMap,0,mTextureMap,0,8); |
|
95 |
} |
|
96 |
|
|
97 |
} |
|
98 |
|
|
99 |
private ArrayList<Component> mComponent; |
|
100 |
|
|
72 | 101 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
73 | 102 |
|
74 |
MeshBase(float factor)
|
|
103 |
MeshBase() |
|
75 | 104 |
{ |
76 |
zFactor = factor; |
|
77 | 105 |
mShowNormals = false; |
106 |
mInflate = 0.0f; |
|
107 |
mComponent = new ArrayList<>(); |
|
108 |
mComponent.add(new Component()); |
|
109 |
|
|
110 |
mVBO = new InternalBuffer(GLES31.GL_ARRAY_BUFFER , GLES31.GL_STATIC_READ); |
|
111 |
mTFO = new InternalBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ); |
|
112 |
} |
|
113 |
|
|
114 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
115 |
// copy constructor |
|
116 |
|
|
117 |
MeshBase(MeshBase original) |
|
118 |
{ |
|
119 |
mShowNormals = original.mShowNormals; |
|
120 |
mInflate = original.mInflate; |
|
121 |
|
|
122 |
int size = original.mComponent.size(); |
|
123 |
mComponent = new ArrayList<>(); |
|
124 |
for(int i=0; i<size; i++) |
|
125 |
{ |
|
126 |
Component comp = new Component(original.mComponent.get(i)); |
|
127 |
mComponent.add(comp); |
|
128 |
} |
|
78 | 129 |
|
79 | 130 |
mVBO = new InternalBuffer(GLES31.GL_ARRAY_BUFFER , GLES31.GL_STATIC_READ); |
80 | 131 |
mTFO = new InternalBuffer(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, GLES31.GL_STATIC_READ); |
132 |
|
|
133 |
System.arraycopy(original.mVertAttribs,0,mVertAttribs,0,original.mNumVertices*VERT_ATTRIBS); |
|
134 |
setAttribs(mVertAttribs); |
|
81 | 135 |
} |
82 | 136 |
|
83 | 137 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
88 | 142 |
mNumVertices = vertexAttribs.length/VERT_ATTRIBS; |
89 | 143 |
mVertAttribs = vertexAttribs; |
90 | 144 |
|
145 |
mComponent.get(0).mEndIndex = mNumVertices; |
|
146 |
|
|
91 | 147 |
FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer(); |
92 | 148 |
attribs.put(vertexAttribs).position(0); |
93 | 149 |
|
... | ... | |
117 | 173 |
return mNumVertices; |
118 | 174 |
} |
119 | 175 |
|
120 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
121 |
/** |
|
122 |
* Not part of public API, do not document (public only because has to be used from the main package) |
|
123 |
* |
|
124 |
* @y.exclude |
|
125 |
*/ |
|
126 |
public float getZFactor() |
|
127 |
{ |
|
128 |
return zFactor; |
|
129 |
} |
|
130 |
|
|
131 | 176 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
132 | 177 |
/** |
133 | 178 |
* Not part of public API, do not document (public only because has to be used from the main package) |
... | ... | |
219 | 264 |
|
220 | 265 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
221 | 266 |
/** |
222 |
* Multiply all coordinates, normal and inflate vectors by the matrix.
|
|
267 |
* Apply all Effects to the vertex mesh. Overwrite the mesh in place.
|
|
223 | 268 |
* <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.
|
|
269 |
* This is a static, permanent modification of the vertices contained in this Mesh. If the effects
|
|
270 |
* contain any Dynamics, they will be evaluated at 0. |
|
226 | 271 |
* |
227 | 272 |
* Please note that calling this once with the complete list of Effects will be much faster than |
228 | 273 |
* calling it repeatedly with one Effect at a time, as we have to reallocate the array of vertices |
... | ... | |
230 | 275 |
*/ |
231 | 276 |
public void apply(MatrixEffect[] effects) |
232 | 277 |
{ |
233 |
for(int v=0; v<mNumVertices; v++) |
|
278 |
float[][] matrix = new float[effects.length][16]; |
|
279 |
float[] tmp; |
|
280 |
float[] array = new float[4]; |
|
281 |
float x,y,z; |
|
282 |
int numEffects = 0; |
|
283 |
|
|
284 |
for(MatrixEffect eff: effects) |
|
234 | 285 |
{ |
235 |
for(MatrixEffect eff: effects)
|
|
286 |
if( eff!=null )
|
|
236 | 287 |
{ |
237 |
if( eff!=null ) eff.applyToVertex( mVertAttribs, v*VERT_ATTRIBS ); |
|
288 |
Matrix.setIdentityM(matrix[numEffects],0); |
|
289 |
eff.compute(array,0,0,0); |
|
290 |
eff.apply(matrix[numEffects], array, 0); |
|
291 |
numEffects++; |
|
238 | 292 |
} |
239 | 293 |
} |
240 | 294 |
|
241 |
setAttribs(mVertAttribs); |
|
295 |
for(int index=0; index<mNumVertices; index+=VERT_ATTRIBS ) |
|
296 |
{ |
|
297 |
for(int mat=0; mat<numEffects; mat++) |
|
298 |
{ |
|
299 |
tmp = matrix[mat]; |
|
300 |
|
|
301 |
x = mVertAttribs[index+POS_ATTRIB ]; |
|
302 |
y = mVertAttribs[index+POS_ATTRIB+1]; |
|
303 |
z = mVertAttribs[index+POS_ATTRIB+2]; |
|
304 |
|
|
305 |
mVertAttribs[index+POS_ATTRIB ] = tmp[0]*x + tmp[4]*y + tmp[ 8]*z + tmp[12]; |
|
306 |
mVertAttribs[index+POS_ATTRIB+1] = tmp[1]*x + tmp[5]*y + tmp[ 9]*z + tmp[13]; |
|
307 |
mVertAttribs[index+POS_ATTRIB+2] = tmp[2]*x + tmp[6]*y + tmp[10]*z + tmp[14]; |
|
308 |
|
|
309 |
x = mVertAttribs[index+NOR_ATTRIB ]; |
|
310 |
y = mVertAttribs[index+NOR_ATTRIB+1]; |
|
311 |
z = mVertAttribs[index+NOR_ATTRIB+2]; |
|
312 |
|
|
313 |
mVertAttribs[index+NOR_ATTRIB ] = tmp[0]*x + tmp[4]*y + tmp[ 8]*z; |
|
314 |
mVertAttribs[index+NOR_ATTRIB+1] = tmp[1]*x + tmp[5]*y + tmp[ 9]*z; |
|
315 |
mVertAttribs[index+NOR_ATTRIB+2] = tmp[2]*x + tmp[6]*y + tmp[10]*z; |
|
316 |
|
|
317 |
x = mVertAttribs[index+INF_ATTRIB ]; |
|
318 |
y = mVertAttribs[index+INF_ATTRIB+1]; |
|
319 |
z = mVertAttribs[index+INF_ATTRIB+2]; |
|
320 |
|
|
321 |
mVertAttribs[index+INF_ATTRIB ] = tmp[0]*x + tmp[4]*y + tmp[ 8]*z; |
|
322 |
mVertAttribs[index+INF_ATTRIB+1] = tmp[1]*x + tmp[5]*y + tmp[ 9]*z; |
|
323 |
mVertAttribs[index+INF_ATTRIB+2] = tmp[2]*x + tmp[6]*y + tmp[10]*z; |
|
324 |
} |
|
325 |
} |
|
326 |
|
|
327 |
FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer(); |
|
328 |
attribs.put(mVertAttribs).position(0); |
|
329 |
|
|
330 |
mVBO.setData(mNumVertices*VERT_SIZE, attribs); |
|
331 |
mTFO.setData(mNumVertices*TRAN_SIZE, null ); |
|
332 |
} |
|
333 |
|
|
334 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
335 |
/** |
|
336 |
* Join a list of Meshes into this one. |
|
337 |
* <p> |
|
338 |
* Please note that calling this once with the complete list of Meshes will be much faster than |
|
339 |
* calling it repeatedly with one Mesh at a time, as we have to reallocate the array of vertices |
|
340 |
* each time. |
|
341 |
*/ |
|
342 |
public void join(MeshBase[] meshes) |
|
343 |
{ |
|
344 |
|
|
242 | 345 |
} |
243 | 346 |
} |
244 | 347 |
|
Also available in: Unified diff
Move the 'pre-multiply mesh before applying any effects' thing from [(Xsize of texture, Ysize of texture) x Mesh's zFactor] to Effects.setStretch(sx,sy,sz)