Revision 22e60fba
Added by Leszek Koltunski over 4 years ago
src/main/java/org/distorted/library/main/InternalBuffer.java | ||
---|---|---|
21 | 21 |
|
22 | 22 |
import android.opengl.GLES31; |
23 | 23 |
import java.nio.Buffer; |
24 |
import java.nio.ByteBuffer; |
|
25 |
import java.nio.ByteOrder; |
|
26 |
import java.nio.FloatBuffer; |
|
24 | 27 |
|
25 | 28 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
26 | 29 |
/** |
... | ... | |
33 | 36 |
*/ |
34 | 37 |
public class InternalBuffer extends InternalObject |
35 | 38 |
{ |
36 |
public int[] mIndex; |
|
39 |
public final int[] mIndex;
|
|
37 | 40 |
|
38 | 41 |
private int mTarget, mSize, mUsage; |
39 | 42 |
private Buffer mBuffer; |
... | ... | |
54 | 57 |
} |
55 | 58 |
|
56 | 59 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
57 |
// mark for creation only now, when we do have all data ready, in order to avoid the situation |
|
58 |
// when create() would be called before this. |
|
60 |
// must be called from a thread holding OpenGL Context. |
|
59 | 61 |
|
60 |
public void setData(int size, Buffer buffer)
|
|
62 |
public void createImmediately(int size, float[] buffer)
|
|
61 | 63 |
{ |
62 |
mSize = size; |
|
63 |
mBuffer = buffer; |
|
64 |
if( mIndex[0]<0 ) |
|
65 |
{ |
|
66 |
mSize= size; |
|
67 |
|
|
68 |
if( buffer!=null ) |
|
69 |
{ |
|
70 |
FloatBuffer buf = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()).asFloatBuffer(); |
|
71 |
buf.put(buffer).position(0); |
|
72 |
mBuffer = buf; |
|
73 |
} |
|
74 |
else |
|
75 |
{ |
|
76 |
mBuffer = null; |
|
77 |
} |
|
78 |
|
|
79 |
GLES31.glGenBuffers( 1, mIndex, 0); |
|
80 |
GLES31.glBindBuffer( mTarget, mIndex[0]); |
|
81 |
GLES31.glBufferData( mTarget, mSize, mBuffer, mUsage); |
|
82 |
GLES31.glBindBuffer( mTarget, 0); |
|
64 | 83 |
|
65 |
markForCreation(); |
|
84 |
markWasCreatedImmediately(); |
|
85 |
} |
|
86 |
} |
|
87 |
|
|
88 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
89 |
|
|
90 |
public void invalidate() |
|
91 |
{ |
|
92 |
recreate(); |
|
66 | 93 |
} |
67 | 94 |
|
68 | 95 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
src/main/java/org/distorted/library/main/InternalObject.java | ||
---|---|---|
212 | 212 |
mType= type; |
213 | 213 |
} |
214 | 214 |
|
215 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
216 |
|
|
217 |
void markWasCreatedImmediately() |
|
218 |
{ |
|
219 |
mDoneList.add(this); |
|
220 |
} |
|
221 |
|
|
215 | 222 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
216 | 223 |
|
217 | 224 |
void markForCreation() |
src/main/java/org/distorted/library/mesh/MeshBase.java | ||
---|---|---|
27 | 27 |
import org.distorted.library.main.InternalBuffer; |
28 | 28 |
import org.distorted.library.program.DistortedProgram; |
29 | 29 |
|
30 |
import java.nio.ByteBuffer; |
|
31 |
import java.nio.ByteOrder; |
|
32 |
import java.nio.FloatBuffer; |
|
33 | 30 |
import java.util.ArrayList; |
34 | 31 |
|
35 | 32 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
187 | 184 |
|
188 | 185 |
mComponent.add(new Component(mNumVertices)); |
189 | 186 |
|
190 |
FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer(); |
|
191 |
attribs.put(vertexAttribs).position(0); |
|
192 |
|
|
193 |
mVBO.setData(mNumVertices*VERT_SIZE, attribs); |
|
194 |
mTFO.setData(mNumVertices*TRAN_SIZE, null ); |
|
187 |
mVBO.invalidate(); |
|
188 |
mTFO.invalidate(); |
|
195 | 189 |
} |
196 | 190 |
|
197 | 191 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
226 | 220 |
} |
227 | 221 |
|
228 | 222 |
num = mesh.mNumVertices; |
229 |
mNumVertices+= (i<len-1 ? ( num%2==1 ? num+2 : num+1 ) : num); |
|
223 |
|
|
224 |
if( mNumVertices==0 ) mNumVertices += (num%2==1 ? num+2 : num+1); |
|
225 |
else if( i==len-1 ) mNumVertices += (num+1); |
|
226 |
else mNumVertices += (num%2==1 ? num+3 : num+2); |
|
230 | 227 |
} |
231 | 228 |
|
232 | 229 |
// allocate new attrib array |
... | ... | |
251 | 248 |
mesh = meshes[i]; |
252 | 249 |
num = mesh.mNumVertices; |
253 | 250 |
|
254 |
System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS ); |
|
255 |
origVertices++; |
|
251 |
if( origVertices>0 ) |
|
252 |
{ |
|
253 |
System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS ); |
|
254 |
origVertices++; |
|
255 |
} |
|
256 | 256 |
System.arraycopy(mesh.mVertAttribs, 0, newAttribs, VERT_ATTRIBS*origVertices, VERT_ATTRIBS*num); |
257 | 257 |
origVertices+=num; |
258 | 258 |
|
... | ... | |
276 | 276 |
|
277 | 277 |
mVertAttribs = newAttribs; |
278 | 278 |
|
279 |
FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer(); |
|
280 |
attribs.put(mVertAttribs).position(0); |
|
281 |
|
|
282 |
mVBO.setData(mNumVertices*VERT_SIZE, attribs); |
|
283 |
mTFO.setData(mNumVertices*TRAN_SIZE, null ); |
|
279 |
mVBO.invalidate(); |
|
284 | 280 |
} |
285 | 281 |
|
286 | 282 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
313 | 309 |
*/ |
314 | 310 |
public void bindVertexAttribs(DistortedProgram program) |
315 | 311 |
{ |
312 |
mVBO.createImmediately(mNumVertices*VERT_SIZE, mVertAttribs); |
|
313 |
|
|
316 | 314 |
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mVBO.mIndex[0] ); |
317 | 315 |
GLES31.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_POS); |
318 | 316 |
GLES31.glVertexAttribPointer(program.mAttribute[1], NOR_DATA_SIZE, GLES31.GL_FLOAT, false, VERT_SIZE, OFFSET_NOR); |
... | ... | |
329 | 327 |
*/ |
330 | 328 |
public void bindTransformAttribs(DistortedProgram program) |
331 | 329 |
{ |
330 |
mTFO.createImmediately(mNumVertices*TRAN_SIZE, null); |
|
331 |
|
|
332 | 332 |
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mTFO.mIndex[0] ); |
333 | 333 |
GLES31.glVertexAttribPointer(program.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0); |
334 | 334 |
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0); |
... | ... | |
456 | 456 |
} |
457 | 457 |
} |
458 | 458 |
|
459 |
FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer(); |
|
460 |
attribs.put(mVertAttribs).position(0); |
|
461 |
|
|
462 |
mVBO.setData(mNumVertices*VERT_SIZE, attribs); |
|
463 |
mTFO.setData(mNumVertices*TRAN_SIZE, null ); |
|
459 |
mTFO.invalidate(); |
|
464 | 460 |
} |
465 | 461 |
|
466 | 462 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
493 | 489 |
} |
494 | 490 |
} |
495 | 491 |
|
496 |
FloatBuffer attribs = ByteBuffer.allocateDirect(mNumVertices*VERT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer(); |
|
497 |
attribs.put(mVertAttribs).position(0); |
|
498 |
|
|
499 |
mVBO.setData(mNumVertices*VERT_SIZE, attribs); |
|
500 |
mTFO.setData(mNumVertices*TRAN_SIZE, null ); |
|
492 |
mTFO.invalidate(); |
|
501 | 493 |
} |
502 | 494 |
|
503 | 495 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
Fix MeshBase.join()
Only upload Mesh Buffers to GPU when we actually use them.