Revision 36d65d88
Added by Leszek Koltunski almost 4 years ago
src/main/java/org/distorted/library/mesh/MeshBase.java | ||
---|---|---|
23 | 23 |
import android.util.Log; |
24 | 24 |
|
25 | 25 |
import org.distorted.library.effect.VertexEffect; |
26 |
import org.distorted.library.effectqueue.EffectQueue; |
|
26 | 27 |
import org.distorted.library.effectqueue.EffectQueueVertex; |
27 | 28 |
import org.distorted.library.main.DistortedLibrary; |
28 | 29 |
import org.distorted.library.main.InternalBuffer; |
... | ... | |
43 | 44 |
*/ |
44 | 45 |
public abstract class MeshBase |
45 | 46 |
{ |
46 |
static final float DEFAULT_ASSOCIATION = 0xffffffff; |
|
47 |
private static final int MAX_COMPONENTS= 100; |
|
48 |
static final int DEFAULT_ASSOCIATION = 0xffffffff; |
|
47 | 49 |
|
48 | 50 |
// sizes of attributes of an individual vertex. |
49 | 51 |
private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z |
50 | 52 |
private static final int NOR_DATA_SIZE= 3; // normal vector: x,y,z |
51 | 53 |
private static final int INF_DATA_SIZE= 3; // 'inflate' vector: x,y,z |
52 | 54 |
private static final int TEX_DATA_SIZE= 2; // texture coordinates: s,t |
53 |
private static final int ASS_DATA_SIZE= 1; // association, a single float
|
|
55 |
private static final int COM_DATA_SIZE= 1; // component number, a single float
|
|
54 | 56 |
|
55 | 57 |
static final int POS_ATTRIB = 0; |
56 | 58 |
static final int NOR_ATTRIB = POS_DATA_SIZE; |
57 | 59 |
static final int INF_ATTRIB = POS_DATA_SIZE + NOR_DATA_SIZE; |
58 | 60 |
static final int TEX_ATTRIB = 0; |
59 |
static final int ASS_ATTRIB = TEX_DATA_SIZE;
|
|
61 |
static final int COM_ATTRIB = TEX_DATA_SIZE;
|
|
60 | 62 |
|
61 | 63 |
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) |
62 |
static final int VERT2_ATTRIBS= TEX_DATA_SIZE + ASS_DATA_SIZE; // number of attributes of a vertex (the 'preapply invariant' part)
|
|
64 |
static final int VERT2_ATTRIBS= TEX_DATA_SIZE + COM_DATA_SIZE; // number of attributes of a vertex (the 'preapply invariant' part)
|
|
63 | 65 |
static final int TRAN_ATTRIBS = POS_DATA_SIZE + NOR_DATA_SIZE + INF_DATA_SIZE; // number of attributes of a transform feedback vertex |
64 | 66 |
|
65 | 67 |
private static final int BYTES_PER_FLOAT = 4; |
... | ... | |
68 | 70 |
private static final int OFFSET_NOR = NOR_ATTRIB*BYTES_PER_FLOAT; |
69 | 71 |
private static final int OFFSET_INF = INF_ATTRIB*BYTES_PER_FLOAT; |
70 | 72 |
private static final int OFFSET_TEX = TEX_ATTRIB*BYTES_PER_FLOAT; |
71 |
private static final int OFFSET_TAG = ASS_ATTRIB*BYTES_PER_FLOAT;
|
|
73 |
private static final int OFFSET_COM = COM_ATTRIB*BYTES_PER_FLOAT;
|
|
72 | 74 |
|
73 | 75 |
private static final int TRAN_SIZE = TRAN_ATTRIBS*BYTES_PER_FLOAT; |
74 | 76 |
private static final int VERT1_SIZE = VERT1_ATTRIBS*BYTES_PER_FLOAT; |
... | ... | |
78 | 80 |
private InternalBuffer mVBO1, mVBO2, mTFO; // main vertex buffer and transform feedback buffer |
79 | 81 |
private int mNumVertices; |
80 | 82 |
private float[] mVertAttribs1; // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ |
81 |
private float[] mVertAttribs2; // packed: TexS,TexT |
|
83 |
private float[] mVertAttribs2; // packed: TexS,TexT, Component
|
|
82 | 84 |
private float mInflate; |
83 | 85 |
private boolean[] mNeedsAdjustment; |
86 |
private int[] mAssociation; |
|
87 |
|
|
88 |
private static int[] mComponentAssociationH = new int[EffectQueue.MAIN_VARIANTS]; |
|
84 | 89 |
|
85 | 90 |
private static class Component |
86 | 91 |
{ |
... | ... | |
121 | 126 |
mShowNormals= false; |
122 | 127 |
mInflate = 0.0f; |
123 | 128 |
mComponent = new ArrayList<>(); |
129 |
mAssociation= new int[MAX_COMPONENTS]; |
|
130 |
|
|
131 |
for(int i=0; i<MAX_COMPONENTS; i++) mAssociation[i] = DEFAULT_ASSOCIATION; |
|
124 | 132 |
|
125 | 133 |
mNeedsAdjustment = new boolean[1]; |
126 |
mNeedsAdjustment[0] = false; |
|
127 | 134 |
|
128 | 135 |
mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER , GLES30.GL_STATIC_READ); |
129 | 136 |
mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER , GLES30.GL_STATIC_READ); |
... | ... | |
148 | 155 |
mComponent.add(comp); |
149 | 156 |
} |
150 | 157 |
|
158 |
mAssociation= new int[MAX_COMPONENTS]; |
|
159 |
System.arraycopy(original.mAssociation, 0, mAssociation, 0, MAX_COMPONENTS); |
|
160 |
|
|
151 | 161 |
if( deep ) |
152 | 162 |
{ |
153 | 163 |
mNeedsAdjustment = new boolean[1]; |
... | ... | |
174 | 184 |
mTFO.invalidate(); |
175 | 185 |
} |
176 | 186 |
|
187 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
188 |
|
|
189 |
public static int getMaxComponents() |
|
190 |
{ |
|
191 |
return MAX_COMPONENTS; |
|
192 |
} |
|
193 |
|
|
194 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
195 |
|
|
196 |
public static void getUniforms(int mProgramH, int variant) |
|
197 |
{ |
|
198 |
mComponentAssociationH[variant] = GLES30.glGetUniformLocation( mProgramH, "vComponAssoc"); |
|
199 |
} |
|
200 |
|
|
177 | 201 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
178 | 202 |
|
179 | 203 |
int numComponents() |
... | ... | |
190 | 214 |
mVertAttribs1= vert1Attribs; |
191 | 215 |
mVertAttribs2= vert2Attribs; |
192 | 216 |
|
193 |
mComponent.add(new Component(mNumVertices)); |
|
217 |
mComponent.add(new Component(mNumVertices-1));
|
|
194 | 218 |
|
195 | 219 |
mVBO1.invalidate(); |
196 | 220 |
mVBO2.invalidate(); |
... | ... | |
203 | 227 |
{ |
204 | 228 |
MeshBase mesh; |
205 | 229 |
Component comp; |
206 |
int numComponents, numVertices, numMeshes = meshes.length; |
|
230 |
int origComponents=0, numComponents, numVertices, numMeshes = meshes.length;
|
|
207 | 231 |
int origVertices = mNumVertices; |
208 | 232 |
|
209 | 233 |
// compute new numVertices; take care of Components |
210 | 234 |
|
211 | 235 |
if( origVertices>0 ) |
212 | 236 |
{ |
213 |
numComponents = mComponent.size();
|
|
237 |
origComponents = mComponent.size();
|
|
214 | 238 |
mNumVertices+= ( mNumVertices%2==1 ? 2:1 ); |
215 |
mComponent.get(numComponents-1).mEndIndex = mNumVertices;
|
|
239 |
mComponent.get(origComponents-1).mEndIndex = mNumVertices-1;
|
|
216 | 240 |
} |
217 | 241 |
|
218 | 242 |
for(int i=0; i<numMeshes; i++) |
... | ... | |
220 | 244 |
mesh = meshes[i]; |
221 | 245 |
numComponents = mesh.mComponent.size(); |
222 | 246 |
|
247 |
numVertices = mesh.mNumVertices; |
|
248 |
|
|
249 |
int extraVerticesBefore = mNumVertices==0 ? 0:1; |
|
250 |
int extraVerticesAfter = (i==numMeshes-1) ? 0 : (numVertices%2==1 ? 2:1); |
|
251 |
|
|
223 | 252 |
for(int j=0; j<numComponents; j++) |
224 | 253 |
{ |
225 | 254 |
comp = new Component(mesh.mComponent.get(j)); |
226 |
comp.mEndIndex += mNumVertices;
|
|
255 |
comp.mEndIndex += (extraVerticesBefore+mNumVertices+extraVerticesAfter);
|
|
227 | 256 |
mComponent.add(comp); |
228 |
} |
|
229 | 257 |
|
230 |
numVertices = mesh.mNumVertices; |
|
231 |
|
|
232 |
if( mNumVertices==0 ) |
|
233 |
{ |
|
234 |
if( numMeshes>1 ) |
|
235 |
mNumVertices += (numVertices%2==1 ? numVertices+2 : numVertices+1); |
|
236 |
else |
|
237 |
mNumVertices += numVertices; |
|
258 |
if( origComponents<MAX_COMPONENTS ) |
|
259 |
{ |
|
260 |
mAssociation[origComponents] = mesh.mAssociation[j]; |
|
261 |
origComponents++; |
|
262 |
} |
|
238 | 263 |
} |
239 |
else if( i==numMeshes-1 ) mNumVertices += (numVertices+1); |
|
240 |
else mNumVertices += (numVertices%2==1 ? numVertices+3 : numVertices+2);
|
|
264 |
|
|
265 |
mNumVertices += (extraVerticesBefore+numVertices+extraVerticesAfter);
|
|
241 | 266 |
} |
242 | 267 |
|
243 | 268 |
// allocate new attrib array |
... | ... | |
296 | 321 |
android.util.Log.e("mesh", "join: origVertices: "+origVertices+" numVertices: "+mNumVertices); |
297 | 322 |
} |
298 | 323 |
|
324 |
int endIndex, index=0, numComp = mComponent.size(); |
|
325 |
|
|
326 |
for(int component=0; component<numComp; component++) |
|
327 |
{ |
|
328 |
endIndex = mComponent.get(component).mEndIndex; |
|
329 |
|
|
330 |
for( ; index<=endIndex; index++) newAttribs2[VERT2_ATTRIBS*index+COM_ATTRIB] = component; |
|
331 |
} |
|
332 |
|
|
299 | 333 |
mVertAttribs1 = newAttribs1; |
300 | 334 |
mVertAttribs2 = newAttribs2; |
301 | 335 |
|
... | ... | |
389 | 423 |
return mNumVertices; |
390 | 424 |
} |
391 | 425 |
|
426 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
427 |
/** |
|
428 |
* Not part of public API, do not document (public only because has to be used from the main package) |
|
429 |
* |
|
430 |
* @y.exclude |
|
431 |
*/ |
|
432 |
public void send(int variant) |
|
433 |
{ |
|
434 |
GLES30.glUniform1iv( mComponentAssociationH[variant], MAX_COMPONENTS, mAssociation, 0); |
|
435 |
} |
|
436 |
|
|
392 | 437 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
393 | 438 |
/** |
394 | 439 |
* Not part of public API, do not document (public only because has to be used from the main package) |
... | ... | |
412 | 457 |
GLES30.glVertexAttribPointer(program.mAttribute[2], INF_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_INF); |
413 | 458 |
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 ); |
414 | 459 |
GLES30.glVertexAttribPointer(program.mAttribute[3], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX); |
415 |
GLES30.glVertexAttribPointer(program.mAttribute[4], ASS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TAG);
|
|
460 |
GLES30.glVertexAttribPointer(program.mAttribute[4], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
|
|
416 | 461 |
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0); |
417 | 462 |
} |
418 | 463 |
|
... | ... | |
599 | 644 |
* |
600 | 645 |
* This creates an association between a Component of this Mesh and a Vertex Effect. |
601 | 646 |
* One can set the association of an Effect and of a Component, and the Effect will only be active on |
602 |
* vertices of Components such that (effect assoc) & (component assoc) != 0. (see VertexEffect.retSection() ) |
|
647 |
* vertices of Components such that |
|
648 |
* |
|
649 |
* (effect assoc) & (component assoc) != 0 || (effect component) == (mesh component) |
|
650 |
* |
|
651 |
* (see main_vertex_shader) |
|
603 | 652 |
* |
604 | 653 |
* The point: this way we can configure the system so that each Vertex Effect acts only on a certain |
605 | 654 |
* subset of a Mesh, thus potentially significantly reducing the number of render calls. |
606 | 655 |
*/ |
607 | 656 |
public void setEffectAssociation(int component, int association) |
608 | 657 |
{ |
609 |
int num = mComponent.size(); |
|
610 |
|
|
611 |
if( component>=0 && component<num ) |
|
658 |
if( component>=0 && component<MAX_COMPONENTS ) |
|
612 | 659 |
{ |
613 |
int sta = component>0 ? mComponent.get(component-1).mEndIndex : 0; |
|
614 |
int end = mComponent.get(component).mEndIndex; |
|
615 |
|
|
616 |
sta = sta*VERT2_ATTRIBS + ASS_ATTRIB; |
|
617 |
end = end*VERT2_ATTRIBS + ASS_ATTRIB; |
|
618 |
|
|
619 |
for(int i=sta; i<end; i+=VERT2_ATTRIBS) |
|
620 |
{ |
|
621 |
mVertAttribs2[i] = association; |
|
622 |
} |
|
623 |
|
|
624 |
mVBO2.invalidate(); |
|
660 |
mAssociation[component] = association; |
|
625 | 661 |
} |
626 | 662 |
} |
627 | 663 |
|
... | ... | |
631 | 667 |
* |
632 | 668 |
* @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices, |
633 | 669 |
* normals and inflates (the rest, in particular the mVertAttribs2 containing texture |
634 |
* coordinates and effect assocciations, is always deep copied)
|
|
670 |
* coordinates and effect associations, is always deep copied) |
|
635 | 671 |
*/ |
636 | 672 |
public abstract MeshBase copy(boolean deep); |
637 | 673 |
} |
Also available in: Unified diff
Progress making it possible to apply Vertex Effects only to some Components of a Mesh.