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;
|
package com.watabou.glwrap;
|
||||||
|
|
||||||
|
import android.opengl.GLES20;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.nio.FloatBuffer;
|
import java.nio.FloatBuffer;
|
||||||
@@ -37,6 +39,7 @@ public class Quad {
|
|||||||
|
|
||||||
private static ShortBuffer indices;
|
private static ShortBuffer indices;
|
||||||
private static int indexSize = 0;
|
private static int indexSize = 0;
|
||||||
|
private static int bufferIndex = -1;
|
||||||
|
|
||||||
public static FloatBuffer create() {
|
public static FloatBuffer create() {
|
||||||
return ByteBuffer.
|
return ByteBuffer.
|
||||||
@@ -52,12 +55,31 @@ public class Quad {
|
|||||||
asFloatBuffer();
|
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 ) {
|
public static ShortBuffer getIndices( int size ) {
|
||||||
|
|
||||||
if (size > indexSize) {
|
if (size > indexSize) {
|
||||||
|
|
||||||
// TODO: Optimize it!
|
|
||||||
|
|
||||||
indexSize = size;
|
indexSize = size;
|
||||||
indices = ByteBuffer.
|
indices = ByteBuffer.
|
||||||
allocateDirect( size * SIZE * Short.SIZE / 8 ).
|
allocateDirect( size * SIZE * Short.SIZE / 8 ).
|
||||||
|
|||||||
@@ -56,6 +56,9 @@ public class NoosaScript extends Script {
|
|||||||
aXY = attribute( "aXYZW" );
|
aXY = attribute( "aXYZW" );
|
||||||
aUV = attribute( "aUV" );
|
aUV = attribute( "aUV" );
|
||||||
|
|
||||||
|
Quad.setupIndices();
|
||||||
|
Quad.bindIndices();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -76,10 +79,12 @@ public class NoosaScript extends Script {
|
|||||||
vertices.position( 2 );
|
vertices.position( 2 );
|
||||||
aUV.vertexPointer( 2, 4, vertices );
|
aUV.vertexPointer( 2, 4, vertices );
|
||||||
|
|
||||||
|
Quad.releaseIndices();
|
||||||
GLES20.glDrawElements( GLES20.GL_TRIANGLES, size, GLES20.GL_UNSIGNED_SHORT, indices );
|
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 ) {
|
public void drawQuad( FloatBuffer vertices ) {
|
||||||
|
|
||||||
vertices.position( 0 );
|
vertices.position( 0 );
|
||||||
@@ -88,7 +93,7 @@ public class NoosaScript extends Script {
|
|||||||
vertices.position( 2 );
|
vertices.position( 2 );
|
||||||
aUV.vertexPointer( 2, 4, vertices );
|
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 );
|
vertices.position( 2 );
|
||||||
aUV.vertexPointer( 2, 4, vertices );
|
aUV.vertexPointer( 2, 4, vertices );
|
||||||
|
|
||||||
GLES20.glDrawElements(
|
GLES20.glDrawElements( GLES20.GL_TRIANGLES, Quad.SIZE * size, GLES20.GL_UNSIGNED_SHORT, 0 );
|
||||||
GLES20.GL_TRIANGLES,
|
|
||||||
Quad.SIZE * size,
|
|
||||||
GLES20.GL_UNSIGNED_SHORT,
|
|
||||||
Quad.getIndices( size ) );
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user