v0.4.2: added support for Vertex Buffer Objects
This commit is contained in:
committed by
Evan Debenham
parent
02166c319b
commit
9663a47958
@@ -27,6 +27,7 @@ import com.watabou.gltextures.SmartTexture;
|
||||
import com.watabou.gltextures.TextureCache;
|
||||
import com.watabou.glwrap.Matrix;
|
||||
import com.watabou.glwrap.Quad;
|
||||
import com.watabou.glwrap.Vertexbuffer;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.RectF;
|
||||
@@ -38,6 +39,7 @@ public class BitmapText extends Visual {
|
||||
|
||||
protected float[] vertices = new float[16];
|
||||
protected FloatBuffer quads;
|
||||
protected Vertexbuffer buffer;
|
||||
|
||||
public int realLength;
|
||||
|
||||
@@ -58,15 +60,6 @@ public class BitmapText extends Visual {
|
||||
this.font = font;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
text = null;
|
||||
font = null;
|
||||
vertices = null;
|
||||
quads = null;
|
||||
super.destroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateMatrix() {
|
||||
// "origin" field is ignored
|
||||
@@ -80,25 +73,37 @@ public class BitmapText extends Visual {
|
||||
public void draw() {
|
||||
|
||||
super.draw();
|
||||
|
||||
if (dirty) {
|
||||
updateVertices();
|
||||
quads.limit(quads.position());
|
||||
if (buffer == null)
|
||||
buffer = new Vertexbuffer(quads);
|
||||
else
|
||||
buffer.updateVertices(quads);
|
||||
}
|
||||
|
||||
NoosaScript script = NoosaScript.get();
|
||||
|
||||
font.texture.bind();
|
||||
|
||||
if (dirty) {
|
||||
updateVertices();
|
||||
}
|
||||
|
||||
script.camera( camera() );
|
||||
|
||||
script.uModel.valueM4( matrix );
|
||||
script.lighting(
|
||||
rm, gm, bm, am,
|
||||
ra, ga, ba, aa );
|
||||
script.drawQuadSet( quads, realLength );
|
||||
script.drawQuadSet( buffer, realLength, 0 );
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
super.destroy();
|
||||
if (buffer != null)
|
||||
buffer.delete();
|
||||
}
|
||||
|
||||
protected void updateVertices() {
|
||||
|
||||
width = 0;
|
||||
|
||||
@@ -28,6 +28,7 @@ import javax.microedition.khronos.opengles.GL10;
|
||||
|
||||
import com.watabou.glscripts.Script;
|
||||
import com.watabou.gltextures.TextureCache;
|
||||
import com.watabou.glwrap.Vertexbuffer;
|
||||
import com.watabou.input.Keys;
|
||||
import com.watabou.input.Touchscreen;
|
||||
import com.watabou.noosa.audio.Music;
|
||||
@@ -242,9 +243,11 @@ public class Game extends Activity implements GLSurfaceView.Renderer, View.OnTou
|
||||
GLES20.glBlendFunc( GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA );
|
||||
|
||||
GLES20.glEnable( GL10.GL_SCISSOR_TEST );
|
||||
|
||||
|
||||
//refreshes texture and vertex data stored on the gpu
|
||||
TextureCache.reload();
|
||||
RenderedText.reloadCache();
|
||||
Vertexbuffer.refreshAllBuffers();
|
||||
}
|
||||
|
||||
protected void destroyGame() {
|
||||
|
||||
@@ -28,6 +28,7 @@ import android.graphics.RectF;
|
||||
import com.watabou.gltextures.TextureCache;
|
||||
import com.watabou.gltextures.SmartTexture;
|
||||
import com.watabou.glwrap.Quad;
|
||||
import com.watabou.glwrap.Vertexbuffer;
|
||||
|
||||
public class Image extends Visual {
|
||||
|
||||
@@ -39,6 +40,7 @@ public class Image extends Visual {
|
||||
|
||||
protected float[] vertices;
|
||||
protected FloatBuffer verticesBuffer;
|
||||
protected Vertexbuffer buffer;
|
||||
|
||||
protected boolean dirty;
|
||||
|
||||
@@ -149,6 +151,16 @@ public class Image extends Visual {
|
||||
|
||||
super.draw();
|
||||
|
||||
if (dirty) {
|
||||
verticesBuffer.position( 0 );
|
||||
verticesBuffer.put( vertices );
|
||||
if (buffer == null)
|
||||
buffer = new Vertexbuffer( verticesBuffer );
|
||||
else
|
||||
buffer.updateVertices( verticesBuffer );
|
||||
dirty = false;
|
||||
}
|
||||
|
||||
NoosaScript script = NoosaScript.get();
|
||||
|
||||
texture.bind();
|
||||
@@ -159,13 +171,15 @@ public class Image extends Visual {
|
||||
script.lighting(
|
||||
rm, gm, bm, am,
|
||||
ra, ga, ba, aa );
|
||||
|
||||
if (dirty) {
|
||||
verticesBuffer.position( 0 );
|
||||
verticesBuffer.put( vertices );
|
||||
dirty = false;
|
||||
}
|
||||
script.drawQuad( verticesBuffer );
|
||||
|
||||
script.drawQuad( buffer );
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
super.destroy();
|
||||
if (buffer != null)
|
||||
buffer.delete();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.nio.FloatBuffer;
|
||||
import com.watabou.gltextures.SmartTexture;
|
||||
import com.watabou.gltextures.TextureCache;
|
||||
import com.watabou.glwrap.Quad;
|
||||
import com.watabou.glwrap.Vertexbuffer;
|
||||
|
||||
import android.graphics.RectF;
|
||||
|
||||
@@ -34,7 +35,8 @@ public class NinePatch extends Visual {
|
||||
public SmartTexture texture;
|
||||
|
||||
protected float[] vertices;
|
||||
protected FloatBuffer verticesBuffer;
|
||||
protected FloatBuffer quads;
|
||||
protected Vertexbuffer buffer;
|
||||
|
||||
protected RectF outterF;
|
||||
protected RectF innerF;
|
||||
@@ -49,6 +51,8 @@ public class NinePatch extends Visual {
|
||||
|
||||
protected boolean flipHorizontal;
|
||||
protected boolean flipVertical;
|
||||
|
||||
protected boolean dirty;
|
||||
|
||||
public NinePatch( Object tx, int margin ) {
|
||||
this( tx, margin, margin, margin, margin );
|
||||
@@ -73,7 +77,7 @@ public class NinePatch extends Visual {
|
||||
nHeight = height = h;
|
||||
|
||||
vertices = new float[16];
|
||||
verticesBuffer = Quad.createSet( 9 );
|
||||
quads = Quad.createSet( 9 );
|
||||
|
||||
marginLeft = left;
|
||||
marginRight = right;
|
||||
@@ -88,7 +92,7 @@ public class NinePatch extends Visual {
|
||||
|
||||
protected void updateVertices() {
|
||||
|
||||
verticesBuffer.position( 0 );
|
||||
quads.position( 0 );
|
||||
|
||||
float right = width - marginRight;
|
||||
float bottom = height - marginBottom;
|
||||
@@ -105,33 +109,35 @@ public class NinePatch extends Visual {
|
||||
|
||||
Quad.fill( vertices,
|
||||
0, marginLeft, 0, marginTop, outleft, inleft, outtop, intop );
|
||||
verticesBuffer.put( vertices );
|
||||
quads.put( vertices );
|
||||
Quad.fill( vertices,
|
||||
marginLeft, right, 0, marginTop, inleft, inright, outtop, intop );
|
||||
verticesBuffer.put( vertices );
|
||||
quads.put( vertices );
|
||||
Quad.fill( vertices,
|
||||
right, width, 0, marginTop, inright, outright, outtop, intop );
|
||||
verticesBuffer.put( vertices );
|
||||
quads.put( vertices );
|
||||
|
||||
Quad.fill( vertices,
|
||||
0, marginLeft, marginTop, bottom, outleft, inleft, intop, inbottom );
|
||||
verticesBuffer.put( vertices );
|
||||
quads.put( vertices );
|
||||
Quad.fill( vertices,
|
||||
marginLeft, right, marginTop, bottom, inleft, inright, intop, inbottom );
|
||||
verticesBuffer.put( vertices );
|
||||
quads.put( vertices );
|
||||
Quad.fill( vertices,
|
||||
right, width, marginTop, bottom, inright, outright, intop, inbottom );
|
||||
verticesBuffer.put( vertices );
|
||||
quads.put( vertices );
|
||||
|
||||
Quad.fill( vertices,
|
||||
0, marginLeft, bottom, height, outleft, inleft, inbottom, outbottom );
|
||||
verticesBuffer.put( vertices );
|
||||
quads.put( vertices );
|
||||
Quad.fill( vertices,
|
||||
marginLeft, right, bottom, height, inleft, inright, inbottom, outbottom );
|
||||
verticesBuffer.put( vertices );
|
||||
quads.put( vertices );
|
||||
Quad.fill( vertices,
|
||||
right, width, bottom, height, inright, outright, inbottom, outbottom );
|
||||
verticesBuffer.put( vertices );
|
||||
quads.put( vertices );
|
||||
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
public int marginLeft() {
|
||||
@@ -195,6 +201,14 @@ public class NinePatch extends Visual {
|
||||
|
||||
super.draw();
|
||||
|
||||
if (dirty){
|
||||
if (buffer == null)
|
||||
buffer = new Vertexbuffer(quads);
|
||||
else
|
||||
buffer.updateVertices(quads);
|
||||
dirty = false;
|
||||
}
|
||||
|
||||
NoosaScript script = NoosaScript.get();
|
||||
|
||||
texture.bind();
|
||||
@@ -206,7 +220,14 @@ public class NinePatch extends Visual {
|
||||
rm, gm, bm, am,
|
||||
ra, ga, ba, aa );
|
||||
|
||||
script.drawQuadSet( verticesBuffer, 9 );
|
||||
script.drawQuadSet( buffer, 9, 0 );
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
super.destroy();
|
||||
if (buffer != null)
|
||||
buffer.delete();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import com.watabou.glscripts.Script;
|
||||
import com.watabou.glwrap.Attribute;
|
||||
import com.watabou.glwrap.Quad;
|
||||
import com.watabou.glwrap.Uniform;
|
||||
import com.watabou.glwrap.Vertexbuffer;
|
||||
|
||||
public class NoosaScript extends Script {
|
||||
|
||||
@@ -96,6 +97,20 @@ public class NoosaScript extends Script {
|
||||
GLES20.glDrawElements( GLES20.GL_TRIANGLES, Quad.SIZE, GLES20.GL_UNSIGNED_SHORT, 0 );
|
||||
|
||||
}
|
||||
|
||||
public void drawQuad( Vertexbuffer buffer ) {
|
||||
|
||||
buffer.updateGLData();
|
||||
|
||||
buffer.bind();
|
||||
|
||||
aXY.vertexBuffer( 2, 4, 0 );
|
||||
aUV.vertexBuffer( 2, 4, 2 );
|
||||
|
||||
buffer.release();
|
||||
|
||||
GLES20.glDrawElements( GLES20.GL_TRIANGLES, Quad.SIZE, GLES20.GL_UNSIGNED_SHORT, 0 );
|
||||
}
|
||||
|
||||
public void drawQuadSet( FloatBuffer vertices, int size ) {
|
||||
|
||||
@@ -112,6 +127,24 @@ public class NoosaScript extends Script {
|
||||
GLES20.glDrawElements( GLES20.GL_TRIANGLES, Quad.SIZE * size, GLES20.GL_UNSIGNED_SHORT, 0 );
|
||||
|
||||
}
|
||||
|
||||
public void drawQuadSet( Vertexbuffer buffer, int length, int offset ){
|
||||
|
||||
if (length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
buffer.updateGLData();
|
||||
|
||||
buffer.bind();
|
||||
|
||||
aXY.vertexBuffer( 2, 4, 0 );
|
||||
aUV.vertexBuffer( 2, 4, 2 );
|
||||
|
||||
buffer.release();
|
||||
|
||||
GLES20.glDrawElements( GLES20.GL_TRIANGLES, Quad.SIZE * length, GLES20.GL_UNSIGNED_SHORT, Quad.SIZE * offset );
|
||||
}
|
||||
|
||||
public void lighting( float rm, float gm, float bm, float am, float ra, float ga, float ba, float aa ) {
|
||||
uColorM.value4f( rm, gm, bm, am );
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.nio.FloatBuffer;
|
||||
import com.watabou.gltextures.SmartTexture;
|
||||
import com.watabou.gltextures.TextureCache;
|
||||
import com.watabou.glwrap.Quad;
|
||||
import com.watabou.glwrap.Vertexbuffer;
|
||||
import com.watabou.utils.Rect;
|
||||
|
||||
import android.graphics.RectF;
|
||||
@@ -47,6 +48,7 @@ public class Tilemap extends Visual {
|
||||
protected short[] bufferPositions;
|
||||
protected short bufferLength;
|
||||
protected FloatBuffer quads;
|
||||
protected Vertexbuffer buffer;
|
||||
|
||||
public Rect updated;
|
||||
|
||||
@@ -96,7 +98,6 @@ public class Tilemap extends Visual {
|
||||
float x2 = x1 + cellW;
|
||||
|
||||
int pos = i * mapWidth + updated.left;
|
||||
quads.position( 16 * pos );
|
||||
|
||||
for (int j=updated.left; j < updated.right; j++) {
|
||||
|
||||
@@ -155,6 +156,15 @@ public class Tilemap extends Visual {
|
||||
|
||||
super.draw();
|
||||
|
||||
if (!updated.isEmpty()) {
|
||||
updateVertices();
|
||||
quads.limit(bufferLength*16);
|
||||
if (buffer == null)
|
||||
buffer = new Vertexbuffer(quads);
|
||||
else
|
||||
buffer.updateVertices(quads);
|
||||
}
|
||||
|
||||
NoosaScript script = NoosaScript.get();
|
||||
|
||||
texture.bind();
|
||||
@@ -164,17 +174,18 @@ public class Tilemap extends Visual {
|
||||
rm, gm, bm, am,
|
||||
ra, ga, ba, aa );
|
||||
|
||||
if (!updated.isEmpty()) {
|
||||
quads.limit(quads.capacity());
|
||||
updateVertices();
|
||||
quads.limit(bufferLength);
|
||||
}
|
||||
|
||||
script.camera( camera );
|
||||
script.drawQuadSet( quads, bufferLength );
|
||||
script.drawQuadSet( buffer, bufferLength, 0 );
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
super.destroy();
|
||||
if (buffer != null)
|
||||
buffer.delete();
|
||||
}
|
||||
|
||||
protected boolean needsRender(int pos){
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user