v0.4.2: updated quad indicies to be stored in a VBO
This commit is contained in:
@@ -21,6 +21,8 @@
|
||||
|
||||
package com.watabou.glwrap;
|
||||
|
||||
import android.opengl.GLES20;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.FloatBuffer;
|
||||
@@ -37,6 +39,7 @@ public class Quad {
|
||||
|
||||
private static ShortBuffer indices;
|
||||
private static int indexSize = 0;
|
||||
private static int bufferIndex = -1;
|
||||
|
||||
public static FloatBuffer create() {
|
||||
return ByteBuffer.
|
||||
@@ -51,13 +54,32 @@ public class Quad {
|
||||
order( ByteOrder.nativeOrder() ).
|
||||
asFloatBuffer();
|
||||
}
|
||||
|
||||
//sets up for drawing up to 32k quads in one command, shouldn't ever need to exceed this
|
||||
public static void setupIndices(){
|
||||
ShortBuffer indices = getIndices( Short.MAX_VALUE );
|
||||
if (bufferIndex == -1){
|
||||
int[] buf = new int[1];
|
||||
GLES20.glGenBuffers(1, buf, 0);
|
||||
bufferIndex = buf[0];
|
||||
}
|
||||
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, bufferIndex);
|
||||
GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, (indices.capacity()*2), indices, GLES20.GL_STATIC_DRAW);
|
||||
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
public static void bindIndices(){
|
||||
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, bufferIndex);
|
||||
}
|
||||
|
||||
public static void releaseIndices(){
|
||||
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
public static ShortBuffer getIndices( int size ) {
|
||||
|
||||
if (size > indexSize) {
|
||||
|
||||
// TODO: Optimize it!
|
||||
|
||||
indexSize = size;
|
||||
indices = ByteBuffer.
|
||||
allocateDirect( size * SIZE * Short.SIZE / 8 ).
|
||||
|
||||
@@ -55,6 +55,9 @@ public class NoosaScript extends Script {
|
||||
uColorA = uniform( "uColorA" );
|
||||
aXY = attribute( "aXYZW" );
|
||||
aUV = attribute( "aUV" );
|
||||
|
||||
Quad.setupIndices();
|
||||
Quad.bindIndices();
|
||||
|
||||
}
|
||||
|
||||
@@ -75,11 +78,13 @@ public class NoosaScript extends Script {
|
||||
|
||||
vertices.position( 2 );
|
||||
aUV.vertexPointer( 2, 4, vertices );
|
||||
|
||||
|
||||
Quad.releaseIndices();
|
||||
GLES20.glDrawElements( GLES20.GL_TRIANGLES, size, GLES20.GL_UNSIGNED_SHORT, indices );
|
||||
|
||||
Quad.bindIndices();
|
||||
}
|
||||
|
||||
|
||||
//FIXME need to do some voodoo to get this working properly on android 2.2
|
||||
public void drawQuad( FloatBuffer vertices ) {
|
||||
|
||||
vertices.position( 0 );
|
||||
@@ -88,7 +93,7 @@ public class NoosaScript extends Script {
|
||||
vertices.position( 2 );
|
||||
aUV.vertexPointer( 2, 4, vertices );
|
||||
|
||||
GLES20.glDrawElements( GLES20.GL_TRIANGLES, Quad.SIZE, GLES20.GL_UNSIGNED_SHORT, Quad.getIndices( 1 ) );
|
||||
GLES20.glDrawElements( GLES20.GL_TRIANGLES, Quad.SIZE, GLES20.GL_UNSIGNED_SHORT, 0 );
|
||||
|
||||
}
|
||||
|
||||
@@ -104,11 +109,7 @@ public class NoosaScript extends Script {
|
||||
vertices.position( 2 );
|
||||
aUV.vertexPointer( 2, 4, vertices );
|
||||
|
||||
GLES20.glDrawElements(
|
||||
GLES20.GL_TRIANGLES,
|
||||
Quad.SIZE * size,
|
||||
GLES20.GL_UNSIGNED_SHORT,
|
||||
Quad.getIndices( size ) );
|
||||
GLES20.glDrawElements( GLES20.GL_TRIANGLES, Quad.SIZE * size, GLES20.GL_UNSIGNED_SHORT, 0 );
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user