Revision a2878a67
Added by Leszek Koltunski over 3 years ago
src/main/java/org/distorted/library/mesh/MeshBase.java | ||
---|---|---|
46 | 46 |
*/ |
47 | 47 |
public abstract class MeshBase |
48 | 48 |
{ |
49 |
private static final int UBO_BINDING = 3; |
|
49 |
private static final int ASSOC_UBO_BINDING = 3; |
|
50 |
private static final int CENTER_UBO_BINDING = 4; |
|
50 | 51 |
static final int MAX_EFFECT_COMPONENTS= 100; |
51 | 52 |
|
52 | 53 |
// sizes of attributes of an individual vertex. |
53 | 54 |
private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z |
54 | 55 |
private static final int NOR_DATA_SIZE= 3; // normal vector: x,y,z |
55 |
private static final int INF_DATA_SIZE= 3; // 'inflate' vector: x,y,z |
|
56 | 56 |
private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t |
57 | 57 |
private static final int COM_DATA_SIZE= 1; // component number, a single float |
58 | 58 |
|
59 | 59 |
static final int POS_ATTRIB = 0; |
60 | 60 |
static final int NOR_ATTRIB = POS_DATA_SIZE; |
61 |
static final int INF_ATTRIB = POS_DATA_SIZE + NOR_DATA_SIZE; |
|
62 | 61 |
static final int TEX_ATTRIB = 0; |
63 | 62 |
static final int COM_ATTRIB = TEX_DATA_SIZE; |
64 | 63 |
|
65 |
static final int VERT1_ATTRIBS= POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE; // number of attributes of a vertex (the part changed by preapply)
|
|
66 |
static final int VERT2_ATTRIBS= TEX_DATA_SIZE + COM_DATA_SIZE; // number of attributes of a vertex (the 'preapply invariant' part)
|
|
67 |
static final int TRAN_ATTRIBS = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE; // number of attributes of a transform feedback vertex
|
|
64 |
static final int VERT1_ATTRIBS= POS_DATA_SIZE + NOR_DATA_SIZE; // number of attributes of a vertex (the part changed by preapply) |
|
65 |
static final int VERT2_ATTRIBS= TEX_DATA_SIZE + COM_DATA_SIZE; // number of attributes of a vertex (the 'preapply invariant' part) |
|
66 |
static final int TRAN_ATTRIBS = POS_DATA_SIZE + NOR_DATA_SIZE; // number of attributes of a transform feedback vertex |
|
68 | 67 |
|
69 | 68 |
private static final int BYTES_PER_FLOAT = 4; |
70 | 69 |
|
71 | 70 |
private static final int OFFSET_POS = POS_ATTRIB*BYTES_PER_FLOAT; |
72 | 71 |
private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT; |
73 |
private static final int OFFSET_INF = INF_ATTRIB*BYTES_PER_FLOAT; |
|
74 | 72 |
private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT; |
75 | 73 |
private static final int OFFSET_COM = COM_ATTRIB*BYTES_PER_FLOAT; |
76 | 74 |
|
... | ... | |
81 | 79 |
private boolean mShowNormals; // when rendering this mesh, draw normal vectors? |
82 | 80 |
private InternalBuffer mVBO1, mVBO2, mTFO; // main vertex buffer and transform feedback buffer |
83 | 81 |
private int mNumVertices; |
84 |
private float[] mVertAttribs1; // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ
|
|
82 |
private float[] mVertAttribs1; // packed: PosX,PosY,PosZ, NorX,NorY,NorZ |
|
85 | 83 |
private float[] mVertAttribs2; // packed: TexS,TexT, Component |
86 | 84 |
private float mInflate; |
87 |
private AssociationUniformBlock mAUB; |
|
85 |
private UniformBlockAssociation mUBA; |
|
86 |
private UniformBlockCenter mUBC; |
|
88 | 87 |
|
89 | 88 |
DeferredJobs.JobNode[] mJobNode; |
90 | 89 |
|
91 |
private static final int TEX_COMP_SIZE = 5; // 5 four-bytes entities inside the component
|
|
90 |
private static final int TEX_COMP_SIZE = 5; // 5 four-byte entities inside the component |
|
92 | 91 |
|
93 | 92 |
private static class TexComponent |
94 | 93 |
{ |
... | ... | |
131 | 130 |
|
132 | 131 |
mJobNode = new DeferredJobs.JobNode[1]; |
133 | 132 |
|
134 |
mAUB = new AssociationUniformBlock(); |
|
133 |
mUBA = new UniformBlockAssociation(); |
|
134 |
mUBC = new UniformBlockCenter(); |
|
135 | 135 |
|
136 | 136 |
mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER , GLES30.GL_STATIC_READ); |
137 | 137 |
mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER , GLES30.GL_STATIC_READ); |
... | ... | |
147 | 147 |
mInflate = original.mInflate; |
148 | 148 |
mNumVertices= original.mNumVertices; |
149 | 149 |
|
150 |
mAUB = new AssociationUniformBlock(original.mAUB); |
|
150 |
mUBA = new UniformBlockAssociation(original.mUBA); |
|
151 |
mUBC = new UniformBlockCenter(original.mUBC); |
|
151 | 152 |
|
152 | 153 |
if( deep ) |
153 | 154 |
{ |
... | ... | |
254 | 255 |
start = end+1; |
255 | 256 |
end = mEffComponent.get(comp); |
256 | 257 |
|
257 |
if( mAUB.matchesAssociation(comp, andAssoc, equAssoc) )
|
|
258 |
if( mUBA.matchesAssociation(comp, andAssoc, equAssoc) )
|
|
258 | 259 |
{ |
259 | 260 |
applyMatrixToComponent(matrixP, matrixV, start, end); |
260 | 261 |
} |
... | ... | |
299 | 300 |
mVertAttribs1[index+NOR_ATTRIB+1] /= len1; |
300 | 301 |
mVertAttribs1[index+NOR_ATTRIB+2] /= len1; |
301 | 302 |
} |
302 |
|
|
303 |
x = mVertAttribs1[index+INF_ATTRIB ]; |
|
304 |
y = mVertAttribs1[index+INF_ATTRIB+1]; |
|
305 |
z = mVertAttribs1[index+INF_ATTRIB+2]; |
|
306 |
|
|
307 |
mVertAttribs1[index+INF_ATTRIB ] = matrixV[0]*x + matrixV[4]*y + matrixV[ 8]*z; |
|
308 |
mVertAttribs1[index+INF_ATTRIB+1] = matrixV[1]*x + matrixV[5]*y + matrixV[ 9]*z; |
|
309 |
mVertAttribs1[index+INF_ATTRIB+2] = matrixV[2]*x + matrixV[6]*y + matrixV[10]*z; |
|
310 |
|
|
311 |
x = mVertAttribs1[index+INF_ATTRIB ]; |
|
312 |
y = mVertAttribs1[index+INF_ATTRIB+1]; |
|
313 |
z = mVertAttribs1[index+INF_ATTRIB+2]; |
|
314 |
|
|
315 |
float len2 = (float)Math.sqrt(x*x + y*y + z*z); |
|
316 |
|
|
317 |
if( len2>0.0f ) |
|
318 |
{ |
|
319 |
mVertAttribs1[index+INF_ATTRIB ] /= len2; |
|
320 |
mVertAttribs1[index+INF_ATTRIB+1] /= len2; |
|
321 |
mVertAttribs1[index+INF_ATTRIB+2] /= len2; |
|
322 |
} |
|
323 | 303 |
} |
324 | 304 |
} |
325 | 305 |
|
... | ... | |
327 | 307 |
|
328 | 308 |
void setEffectAssociationNow(int component, int andAssociation, int equAssociation) |
329 | 309 |
{ |
330 |
mAUB.setEffectAssociationNow(component, andAssociation, equAssociation); |
|
310 |
mUBA.setEffectAssociationNow(component, andAssociation, equAssociation); |
|
311 |
} |
|
312 |
|
|
313 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
314 |
|
|
315 |
void setEffectCenterNow(int component, float x, float y, float z) |
|
316 |
{ |
|
317 |
mUBC.setEffectCenterNow(component, x, y, z); |
|
331 | 318 |
} |
332 | 319 |
|
333 | 320 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
393 | 380 |
|
394 | 381 |
if( origEffComponents<MAX_EFFECT_COMPONENTS ) |
395 | 382 |
{ |
396 |
mAUB.copy(origEffComponents, mesh.mAUB, j); |
|
383 |
mUBA.copy(origEffComponents, mesh.mUBA, j); |
|
384 |
mUBC.copy(origEffComponents, mesh.mUBC, j); |
|
397 | 385 |
origEffComponents++; |
398 | 386 |
} |
399 | 387 |
} |
... | ... | |
690 | 678 |
*/ |
691 | 679 |
public void send(int programH) |
692 | 680 |
{ |
693 |
int index = mAUB.getIndex(); |
|
694 |
GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, UBO_BINDING, index); |
|
695 |
GLES30.glUniformBlockBinding(programH, UBO_BINDING, index); |
|
681 |
int indexA = mUBA.getIndex(); |
|
682 |
GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, ASSOC_UBO_BINDING, indexA); |
|
683 |
GLES30.glUniformBlockBinding(programH, ASSOC_UBO_BINDING, indexA); |
|
684 |
|
|
685 |
int indexC = mUBC.getIndex(); |
|
686 |
GLES30.glBindBufferBase(GLES30.GL_UNIFORM_BUFFER, CENTER_UBO_BINDING, indexC); |
|
687 |
GLES30.glUniformBlockBinding(programH, CENTER_UBO_BINDING, indexC); |
|
696 | 688 |
} |
697 | 689 |
|
698 | 690 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
727 | 719 |
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 ); |
728 | 720 |
GLES30.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS); |
729 | 721 |
GLES30.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR); |
730 |
GLES30.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_INF); |
|
731 | 722 |
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 ); |
732 |
GLES30.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
|
|
733 |
GLES30.glVertexAttribPointer(program.mAttribute[4], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
|
|
723 |
GLES30.glVertexAttribPointer(program.mAttribute[2], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
|
|
724 |
GLES30.glVertexAttribPointer(program.mAttribute[3], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
|
|
734 | 725 |
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0); |
735 | 726 |
} |
736 | 727 |
|
... | ... | |
786 | 777 |
|
787 | 778 |
version = byteBuf.getInt(0); |
788 | 779 |
|
789 |
if( version==1 ) |
|
780 |
if( version==1 || version==2 )
|
|
790 | 781 |
{ |
791 | 782 |
mNumVertices= byteBuf.getInt(4); |
792 | 783 |
numTex = byteBuf.getInt(8); |
... | ... | |
811 | 802 |
mVertAttribs1 = new float[VERT1_ATTRIBS*mNumVertices]; |
812 | 803 |
mVertAttribs2 = new float[VERT2_ATTRIBS*mNumVertices]; |
813 | 804 |
|
814 |
buffer = new byte[BYTES_PER_FLOAT*(size + mNumVertices*(VERT1_ATTRIBS+VERT2_ATTRIBS))]; |
|
805 |
// version 1 had extra 3 floats (the 'inflate' vector) in its vert1 array |
|
806 |
int vert1InFile = version==1 ? VERT1_ATTRIBS+3 : VERT1_ATTRIBS; |
|
807 |
|
|
808 |
buffer = new byte[BYTES_PER_FLOAT*(size + mNumVertices*(vert1InFile+VERT2_ATTRIBS))]; |
|
815 | 809 |
|
816 | 810 |
try |
817 | 811 |
{ |
... | ... | |
827 | 821 |
FloatBuffer floatBuf = byteBuf.asFloatBuffer(); |
828 | 822 |
|
829 | 823 |
floatBuf.get(tmp,0,size); |
830 |
floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices); |
|
824 |
|
|
825 |
switch(version) |
|
826 |
{ |
|
827 |
case 1 : int index = floatBuf.position(); |
|
828 |
|
|
829 |
for(int vert=0; vert<mNumVertices; vert++) |
|
830 |
{ |
|
831 |
mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB ] = floatBuf.get(index+POS_ATTRIB ); |
|
832 |
mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+1] = floatBuf.get(index+POS_ATTRIB+1); |
|
833 |
mVertAttribs1[VERT1_ATTRIBS*vert+POS_ATTRIB+2] = floatBuf.get(index+POS_ATTRIB+2); |
|
834 |
mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB ] = floatBuf.get(index+NOR_ATTRIB ); |
|
835 |
mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+1] = floatBuf.get(index+NOR_ATTRIB+1); |
|
836 |
mVertAttribs1[VERT1_ATTRIBS*vert+NOR_ATTRIB+2] = floatBuf.get(index+NOR_ATTRIB+2); |
|
837 |
index+=vert1InFile; |
|
838 |
} |
|
839 |
floatBuf.position(index); |
|
840 |
break; |
|
841 |
case 2 : floatBuf.get(mVertAttribs1, 0, VERT1_ATTRIBS*mNumVertices); |
|
842 |
break; |
|
843 |
default: android.util.Log.e("mesh", "Error: unknown mesh file version "+String.format("0x%08X", version)); |
|
844 |
return 0; |
|
845 |
} |
|
846 |
|
|
831 | 847 |
floatBuf.get(mVertAttribs2, 0, VERT2_ATTRIBS*mNumVertices); |
832 | 848 |
|
833 | 849 |
TexComponent tex; |
... | ... | |
902 | 918 |
|
903 | 919 |
try |
904 | 920 |
{ |
905 |
stream.writeInt(1); // version
|
|
921 |
stream.writeInt(2); // version
|
|
906 | 922 |
stream.writeInt(mNumVertices); |
907 | 923 |
stream.writeInt(numTex); |
908 | 924 |
stream.writeInt(numEff); |
... | ... | |
1152 | 1168 |
* Copy the Mesh. |
1153 | 1169 |
* |
1154 | 1170 |
* @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices, |
1155 |
* normals and inflates (the rest, in particular the mVertAttribs2 containing texture
|
|
1171 |
* and normals (the rest, in particular the mVertAttribs2 containing texture
|
|
1156 | 1172 |
* coordinates and effect associations, is always deep copied) |
1157 | 1173 |
*/ |
1158 | 1174 |
public abstract MeshBase copy(boolean deep); |
Also available in: Unified diff
Remove the 'inflate' vertex attributs from the Mesh and file format, and replace that with a per-component 'inflate centers' (which are as of yet untested)