diff options
author | Sven Gothel <[email protected]> | 2011-04-08 14:57:32 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-04-08 14:57:32 +0200 |
commit | c004a86e24fcc1cd026a7d1d52f61e8eafc8058a (patch) | |
tree | bc2130efac61bc218a931d63a54ccc3ddfe2e9d4 /src/jogl/classes/jogamp | |
parent | e48537024014104e517d988907eb102154679f88 (diff) |
Use GLArrayDataServer to handle shader attributes/data using VBO
Diffstat (limited to 'src/jogl/classes/jogamp')
-rw-r--r-- | src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java | 321 | ||||
-rw-r--r-- | src/jogl/classes/jogamp/graph/curve/opengl/VBORegionSPES2.java | 121 |
2 files changed, 227 insertions, 215 deletions
diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java b/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java index 075a65ec9..f1c82f83c 100644 --- a/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java +++ b/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java @@ -27,9 +27,6 @@ */ package jogamp.graph.curve.opengl; -import java.nio.FloatBuffer; -import java.nio.IntBuffer; -import java.nio.ShortBuffer; import java.util.ArrayList; import javax.media.opengl.GL2ES2; @@ -39,7 +36,7 @@ import javax.media.opengl.GLContext; import javax.media.opengl.GLUniformData; import javax.media.opengl.fixedfunc.GLMatrixFunc; -import com.jogamp.common.nio.Buffers; +import jogamp.opengl.Debug; import com.jogamp.graph.geom.AABBox; import com.jogamp.graph.geom.Triangle; @@ -47,20 +44,23 @@ import com.jogamp.graph.geom.Vertex; import com.jogamp.graph.curve.Region; import com.jogamp.opengl.util.FBObject; +import com.jogamp.opengl.util.GLArrayDataServer; import com.jogamp.opengl.util.PMVMatrix; import com.jogamp.opengl.util.glsl.ShaderState; -public class VBORegion2PES2 implements Region{ - private int numVertices = 0; - private IntBuffer vboIds; - - private IntBuffer t_vboIds; - +public class VBORegion2PES2 implements Region { + private int numVertices = 0; + private ArrayList<Triangle> triangles = new ArrayList<Triangle>(); private ArrayList<Vertex> vertices = new ArrayList<Vertex>(); - private GLContext context; + private GLArrayDataServer verticeTxtAttr = null; + private GLArrayDataServer texCoordTxtAttr = null; + private GLArrayDataServer indicesTxt = null; + private GLArrayDataServer verticeFboAttr = null; + private GLArrayDataServer texCoordFboAttr = null; + private GLArrayDataServer indicesFbo = null; - private int numBuffers = 3; + private GLContext context; private boolean flipped = false; @@ -77,14 +77,50 @@ public class VBORegion2PES2 implements Region{ public VBORegion2PES2(GLContext context, ShaderState st){ this.context =context; this.st = st; + + GL2ES2 gl = context.getGL().getGL2ES2(); + + indicesFbo = GLArrayDataServer.createGLSL(gl, null, 3, GL2ES2.GL_SHORT, false, + 2, GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER); + indicesFbo.puts((short) 0); indicesFbo.puts((short) 1); indicesFbo.puts((short) 3); + indicesFbo.puts((short) 1); indicesFbo.puts((short) 2); indicesFbo.puts((short) 3); + indicesFbo.seal(gl, true); + + texCoordFboAttr = GLArrayDataServer.createGLSL(gl, Region.TEXCOORD_ATTR_NAME, 2, GL2ES2.GL_FLOAT, false, + 4, GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); + texCoordFboAttr.setLocation(Region.TEXCOORD_ATTR_IDX); + texCoordFboAttr.putf(5); texCoordFboAttr.putf(5); + texCoordFboAttr.putf(5); texCoordFboAttr.putf(6); + texCoordFboAttr.putf(6); texCoordFboAttr.putf(6); + texCoordFboAttr.putf(6); texCoordFboAttr.putf(5); + texCoordFboAttr.seal(gl, true); + + verticeFboAttr = GLArrayDataServer.createGLSL(gl, Region.VERTEX_ATTR_NAME, 3, GL2ES2.GL_FLOAT, false, + 4, GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); + verticeFboAttr.setLocation(Region.VERTEX_ATTR_IDX); + verticeFboAttr.putf(0f); indicesFbo.putf(0f); indicesFbo.putf(0f); + verticeFboAttr.putf(0f); indicesFbo.putf(0f); indicesFbo.putf(0f); + verticeFboAttr.putf(0f); indicesFbo.putf(0f); indicesFbo.putf(0f); + verticeFboAttr.putf(0f); indicesFbo.putf(0f); indicesFbo.putf(0f); + verticeFboAttr.seal(gl, true); + + verticeFboAttr.enableBuffer(gl, false); + texCoordFboAttr.enableBuffer(gl, false); + indicesFbo.enableBuffer(gl, false); + if(DEBUG) { + System.err.println("VBORegion2PES2 Create: " + this); + } } public void update(){ - box = new AABBox(); - GL2ES2 gl = context.getGL().getGL2ES2(); - ShortBuffer indicies = Buffers.newDirectShortBuffer(triangles.size() * 3); + destroyTxtAttr(gl); + + box = new AABBox(); + + indicesTxt = GLArrayDataServer.createGLSL(gl, null, 3, GL2ES2.GL_SHORT, false, + triangles.size(), GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER); for(Triangle t:triangles){ if(t.getVertices()[0].getId() == Integer.MAX_VALUE){ t.getVertices()[0].setId(numVertices++); @@ -95,82 +131,71 @@ public class VBORegion2PES2 implements Region{ vertices.add(t.getVertices()[1]); vertices.add(t.getVertices()[2]); - indicies.put((short) t.getVertices()[0].getId()); - indicies.put((short) t.getVertices()[1].getId()); - indicies.put((short) t.getVertices()[2].getId()); + indicesTxt.puts((short) t.getVertices()[0].getId()); + indicesTxt.puts((short) t.getVertices()[1].getId()); + indicesTxt.puts((short) t.getVertices()[2].getId()); } else{ Vertex v1 = t.getVertices()[0]; Vertex v2 = t.getVertices()[1]; Vertex v3 = t.getVertices()[2]; - indicies.put((short) v1.getId()); - indicies.put((short) v2.getId()); - indicies.put((short) v3.getId()); + indicesTxt.puts((short) v1.getId()); + indicesTxt.puts((short) v2.getId()); + indicesTxt.puts((short) v3.getId()); } } - indicies.rewind(); - - FloatBuffer verticesBuffer = Buffers.newDirectFloatBuffer(vertices.size() * 3); + indicesTxt.seal(gl, true); + + verticeTxtAttr = GLArrayDataServer.createGLSL(gl, Region.VERTEX_ATTR_NAME, 3, GL2ES2.GL_FLOAT, false, + vertices.size(), GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); + verticeTxtAttr.setLocation(Region.VERTEX_ATTR_IDX); for(Vertex v:vertices){ - verticesBuffer.put(v.getX()); + verticeTxtAttr.putf(v.getX()); if(flipped){ - verticesBuffer.put(-1*v.getY()); - } - else{ - verticesBuffer.put(v.getY()); + verticeTxtAttr.putf(-1*v.getY()); + } else { + verticeTxtAttr.putf(v.getY()); } - verticesBuffer.put(v.getZ()); + verticeTxtAttr.putf(v.getZ()); if(flipped){ box.resize(v.getX(), -1*v.getY(), v.getZ()); - } - else{ + } else { box.resize(v.getX(), v.getY(), v.getZ()); } } - verticesBuffer.rewind(); + verticeTxtAttr.seal(gl, true); - FloatBuffer texCoordBuffer = Buffers.newDirectFloatBuffer(vertices.size() * 2); + texCoordTxtAttr = GLArrayDataServer.createGLSL(gl, Region.TEXCOORD_ATTR_NAME, 2, GL2ES2.GL_FLOAT, false, + vertices.size(), GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); + texCoordTxtAttr.setLocation(Region.TEXCOORD_ATTR_IDX); for(Vertex v:vertices){ float[] tex = v.getTexCoord(); - texCoordBuffer.put(tex[0]); - texCoordBuffer.put(tex[1]); + texCoordTxtAttr.putf(tex[0]); + texCoordTxtAttr.putf(tex[1]); } - texCoordBuffer.rewind(); + texCoordTxtAttr.seal(gl, true); - vboIds = IntBuffer.allocate(numBuffers); - gl.glGenBuffers(numBuffers, vboIds); - - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, vboIds.get(0)); // vertices - gl.glBufferData(GL2ES2.GL_ARRAY_BUFFER, numVertices * 3 * Buffers.SIZEOF_FLOAT, verticesBuffer, GL2ES2.GL_STATIC_DRAW); - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, 0); - - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, vboIds.get(1)); //texture - gl.glBufferData(GL2ES2.GL_ARRAY_BUFFER, numVertices * 2 * Buffers.SIZEOF_FLOAT, texCoordBuffer, GL2ES2.GL_STATIC_DRAW); - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, 0); - - gl.glBindBuffer(GL2ES2.GL_ELEMENT_ARRAY_BUFFER, vboIds.get(2)); //triangles - gl.glBufferData(GL2ES2.GL_ELEMENT_ARRAY_BUFFER, triangles.size()* 3 * Buffers.SIZEOF_SHORT, indicies, GL2ES2.GL_STATIC_DRAW); - gl.glBindBuffer(GL2ES2.GL_ELEMENT_ARRAY_BUFFER, 0); - + // leave the buffers enabled for subsequent render call + dirty = false; } public void render(PMVMatrix matrix, int vp_width, int vp_height, int width){ + GL2ES2 gl = context.getGL().getGL2ES2(); if(null == matrix || vp_width <=0 || vp_height <= 0 || width <= 0){ - renderRegion(); - } - else { + renderRegion(gl); + } else { if(width != tex_width_c){ - initFBOTexture(matrix, width); + renderRegion2FBO(gl, matrix, width); + setupBBox2FboAttr(gl); } // System.out.println("Scale: " + matrix.glGetMatrixf().get(1+4*3) +" " + matrix.glGetMatrixf().get(2+4*3)); - renderTexture(matrix, vp_width, vp_height); + renderFBO(gl, matrix, vp_width, vp_height); } } - private void renderTexture(PMVMatrix matrix, int width, int hight){ - GL2ES2 gl = context.getGL().getGL2ES2(); + private void renderFBO(GL2ES2 gl, PMVMatrix matrix, int width, int hight) { gl.glViewport(0, 0, width, hight); if(!st.glUniform(gl, new GLUniformData("mgl_PMVMatrix", 4, 4, matrix.glGetPMvMatrixf()))){ System.out.println("Cnt set tex based mat"); @@ -184,96 +209,50 @@ public class VBORegion2PES2 implements Region{ gl.glUniform1i(loc, 0); - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, t_vboIds.get(0)); - gl.glEnableVertexAttribArray(VERTEX_ATTR_IDX); - gl.glVertexAttribPointer(VERTEX_ATTR_IDX, 3, GL2ES2.GL_FLOAT, false, 3 * Buffers.SIZEOF_FLOAT, 0); - - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, t_vboIds.get(1)); - gl.glEnableVertexAttribArray(TEXCOORD_ATTR_IDX); - gl.glVertexAttribPointer(TEXCOORD_ATTR_IDX, 2, GL2ES2.GL_FLOAT, false, 2 * Buffers.SIZEOF_FLOAT, 0); - - gl.glBindBuffer(GL2ES2.GL_ELEMENT_ARRAY_BUFFER, t_vboIds.get(2)); - gl.glDrawElements(GL2ES2.GL_TRIANGLES, 2 * 3, GL2ES2.GL_UNSIGNED_SHORT, 0); + verticeFboAttr.enableBuffer(gl, true); + texCoordFboAttr.enableBuffer(gl, true); + indicesFbo.enableBuffer(gl, true); + + gl.glDrawElements(GL2ES2.GL_TRIANGLES, indicesFbo.getElementNumber() * indicesFbo.getComponentNumber(), GL2ES2.GL_UNSIGNED_SHORT, 0); - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, 0); + verticeFboAttr.enableBuffer(gl, false); + texCoordFboAttr.enableBuffer(gl, false); + indicesFbo.enableBuffer(gl, false); } - private void setupBoundingBuffers(){ - GL2ES2 gl = context.getGL().getGL2ES2(); - - ShortBuffer indicies = Buffers.newDirectShortBuffer(6); - indicies.put((short) 0); indicies.put((short) 1); indicies.put((short) 3); - indicies.put((short) 1); indicies.put((short) 2); indicies.put((short) 3); - indicies.rewind(); - - FloatBuffer verticesBuffer = Buffers.newDirectFloatBuffer(4 * 3); - FloatBuffer texCoordBuffer = Buffers.newDirectFloatBuffer(4 * 2); - - verticesBuffer.put(box.getLow()[0]); - verticesBuffer.put(box.getLow()[1]); - verticesBuffer.put(box.getLow()[2]); - texCoordBuffer.put(5); - texCoordBuffer.put(5); - - verticesBuffer.put(box.getLow()[0]); - verticesBuffer.put(box.getHigh()[1]); - verticesBuffer.put(box.getLow()[2]); - - texCoordBuffer.put(5); - texCoordBuffer.put(6); - - verticesBuffer.put(box.getHigh()[0]); - verticesBuffer.put(box.getHigh()[1]); - verticesBuffer.put(box.getLow()[2]); - - texCoordBuffer.put(6); - texCoordBuffer.put(6); - - verticesBuffer.put(box.getHigh()[0]); - verticesBuffer.put(box.getLow()[1]); - verticesBuffer.put(box.getLow()[2]); - - texCoordBuffer.put(6); - texCoordBuffer.put(5); - - verticesBuffer.rewind(); - texCoordBuffer.rewind(); - - t_vboIds = IntBuffer.allocate(3); - gl.glGenBuffers(numBuffers, t_vboIds); - - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, t_vboIds.get(0)); // vertices - gl.glBufferData(GL2ES2.GL_ARRAY_BUFFER, 4 * 3 * Buffers.SIZEOF_FLOAT, verticesBuffer, GL2ES2.GL_STATIC_DRAW); - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, 0); - - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, t_vboIds.get(1)); //texture - gl.glBufferData(GL2ES2.GL_ARRAY_BUFFER, 4 * 2 * Buffers.SIZEOF_FLOAT, texCoordBuffer, GL2ES2.GL_STATIC_DRAW); - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, 0); + private void setupBBox2FboAttr(GL2ES2 gl){ + verticeFboAttr.seal(gl, false); + verticeFboAttr.rewind(); + + verticeFboAttr.putf(box.getLow()[0]); verticeFboAttr.putf(box.getLow()[1]); verticeFboAttr.putf(box.getLow()[2]); + verticeFboAttr.putf(box.getLow()[0]); verticeFboAttr.putf(box.getHigh()[1]); verticeFboAttr.putf(box.getLow()[2]); + verticeFboAttr.putf(box.getHigh()[0]); verticeFboAttr.putf(box.getHigh()[1]); verticeFboAttr.putf(box.getLow()[2]); + verticeFboAttr.putf(box.getHigh()[0]); verticeFboAttr.putf(box.getLow()[1]); verticeFboAttr.putf(box.getLow()[2]); - gl.glBindBuffer(GL2ES2.GL_ELEMENT_ARRAY_BUFFER, t_vboIds.get(2)); //triangles - gl.glBufferData(GL2ES2.GL_ELEMENT_ARRAY_BUFFER, 4 * 3 * Buffers.SIZEOF_SHORT, indicies, GL2ES2.GL_STATIC_DRAW); - gl.glBindBuffer(GL2ES2.GL_ELEMENT_ARRAY_BUFFER, 0); + verticeFboAttr.seal(gl, true); } - private void initFBOTexture(PMVMatrix m, int tex_width){ + private void renderRegion2FBO(GL2ES2 gl, PMVMatrix m, int tex_width){ tex_width_c = tex_width; tex_height_c = (int)(tex_width_c*box.getHeight()/box.getWidth()); - System.out.println("FBO Size: "+tex_height_c+"x"+tex_width_c); - System.out.println("FBO Scale: " + m.glGetMatrixf().get(0) +" " + m.glGetMatrixf().get(5)); + // System.out.println("FBO Size: "+tex_width+" -> "+tex_height_c+"x"+tex_width_c); + // System.out.println("FBO Scale: " + m.glGetMatrixf().get(0) +" " + m.glGetMatrixf().get(5)); - GL2ES2 gl = context.getGL().getGL2ES2(); - if(null != fbo) { + if(null != fbo && fbo.getWidth() != tex_width_c && fbo.getHeight() != tex_height_c ) { fbo.destroy(gl); fbo = null; } - fbo = new FBObject(tex_width_c, tex_height_c); - // FIXME: shall not use bilinear, due to own AA ? However, w/o bilinear result is not smooth - fbo.init(gl, GL2ES2.GL_LINEAR, GL2ES2.GL_LINEAR, GL2ES2.GL_CLAMP_TO_EDGE, GL2ES2.GL_CLAMP_TO_EDGE); - // fbo.init(gl, GL2ES2.GL_NEAREST, GL2ES2.GL_NEAREST, GL2ES2.GL_CLAMP_TO_EDGE, GL2ES2.GL_CLAMP_TO_EDGE); - fbo.attachDepthBuffer(gl, GL.GL_DEPTH_COMPONENT16); // FIXME: or shall we use 24 or 32 bit depth ? - + if(null == fbo) { + fbo = new FBObject(tex_width_c, tex_height_c); + // FIXME: shall not use bilinear, due to own AA ? However, w/o bilinear result is not smooth + fbo.init(gl, GL2ES2.GL_LINEAR, GL2ES2.GL_LINEAR, GL2ES2.GL_CLAMP_TO_EDGE, GL2ES2.GL_CLAMP_TO_EDGE); + // fbo.init(gl, GL2ES2.GL_NEAREST, GL2ES2.GL_NEAREST, GL2ES2.GL_CLAMP_TO_EDGE, GL2ES2.GL_CLAMP_TO_EDGE); + fbo.attachDepthBuffer(gl, GL.GL_DEPTH_COMPONENT16); // FIXME: or shall we use 24 or 32 bit depth ? + } else { + fbo.bind(gl); + } //render texture PMVMatrix tex_matrix = new PMVMatrix(); @@ -288,28 +267,21 @@ public class VBORegion2PES2 implements Region{ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glClear(GL2ES2.GL_COLOR_BUFFER_BIT | GL2ES2.GL_DEPTH_BUFFER_BIT); - renderRegion(); + renderRegion(gl); fbo.unbind(gl); - - setupBoundingBuffers(); } - private void renderRegion(){ - GL2ES2 gl = context.getGL().getGL2ES2(); - - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, vboIds.get(0)); - gl.glEnableVertexAttribArray(VERTEX_ATTR_IDX); - gl.glVertexAttribPointer(VERTEX_ATTR_IDX, 3, GL2ES2.GL_FLOAT, false, 3 * Buffers.SIZEOF_FLOAT, 0); + private void renderRegion(GL2ES2 gl) { + verticeTxtAttr.enableBuffer(gl, true); + texCoordTxtAttr.enableBuffer(gl, true); + indicesTxt.enableBuffer(gl, true); - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, vboIds.get(1)); - gl.glEnableVertexAttribArray(TEXCOORD_ATTR_IDX); - gl.glVertexAttribPointer(TEXCOORD_ATTR_IDX, 2, GL2ES2.GL_FLOAT, false, 2 * Buffers.SIZEOF_FLOAT, 0); + gl.glDrawElements(GL2ES2.GL_TRIANGLES, indicesTxt.getElementNumber() * indicesTxt.getComponentNumber(), GL2ES2.GL_UNSIGNED_SHORT, 0); - gl.glBindBuffer(GL2ES2.GL_ELEMENT_ARRAY_BUFFER, vboIds.get(2)); - gl.glDrawElements(GL2ES2.GL_TRIANGLES, triangles.size() * 3, GL2ES2.GL_UNSIGNED_SHORT, 0); - - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, 0); + verticeTxtAttr.enableBuffer(gl, false); + texCoordTxtAttr.enableBuffer(gl, false); + indicesTxt.enableBuffer(gl, false); } public void addTriangles(ArrayList<Triangle> tris) { @@ -332,14 +304,49 @@ public class VBORegion2PES2 implements Region{ } public void destroy() { + if(DEBUG) { + System.err.println("VBORegion2PES2 Destroy: " + this); + } GL2ES2 gl = context.getGL().getGL2ES2(); - gl.glDeleteBuffers(numBuffers, vboIds); - if(null != fbo) { - fbo.destroy(gl); - fbo = null; - } + destroyFbo(gl); + destroyTxtAttr(gl); + destroyFboAttr(gl); + } + final void destroyFbo(GL2ES2 gl) { + if(null != fbo) { + fbo.destroy(gl); + fbo = null; + } } - + final void destroyTxtAttr(GL2ES2 gl) { + if(null != verticeTxtAttr) { + verticeTxtAttr.destroy(gl); + verticeTxtAttr = null; + } + if(null != texCoordTxtAttr) { + texCoordTxtAttr.destroy(gl); + texCoordTxtAttr = null; + } + if(null != indicesTxt) { + indicesTxt.destroy(gl); + indicesTxt = null; + } + } + final void destroyFboAttr(GL2ES2 gl) { + if(null != verticeFboAttr) { + verticeFboAttr.destroy(gl); + verticeFboAttr = null; + } + if(null != texCoordFboAttr) { + texCoordFboAttr.destroy(gl); + texCoordFboAttr = null; + } + if(null != indicesFbo) { + indicesFbo.destroy(gl); + indicesFbo = null; + } + } + public boolean isFlipped() { return flipped; } diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/VBORegionSPES2.java b/src/jogl/classes/jogamp/graph/curve/opengl/VBORegionSPES2.java index 701549d46..80bf00428 100644 --- a/src/jogl/classes/jogamp/graph/curve/opengl/VBORegionSPES2.java +++ b/src/jogl/classes/jogamp/graph/curve/opengl/VBORegionSPES2.java @@ -27,31 +27,29 @@ */ package jogamp.graph.curve.opengl; -import java.nio.FloatBuffer; -import java.nio.IntBuffer; -import java.nio.ShortBuffer; import java.util.ArrayList; +import javax.media.opengl.GL; import javax.media.opengl.GL2ES2; import javax.media.opengl.GLContext; -import com.jogamp.common.nio.Buffers; import com.jogamp.graph.curve.Region; import com.jogamp.graph.geom.Vertex; import com.jogamp.graph.geom.Triangle; +import com.jogamp.opengl.util.GLArrayDataServer; import com.jogamp.opengl.util.PMVMatrix; -public class VBORegionSPES2 implements Region{ +public class VBORegionSPES2 implements Region { private int numVertices = 0; - private IntBuffer vboIds; private ArrayList<Triangle> triangles = new ArrayList<Triangle>(); private ArrayList<Vertex> vertices = new ArrayList<Vertex>(); + private GLArrayDataServer verticeAttr = null; + private GLArrayDataServer texCoordAttr = null; + private GLArrayDataServer indices = null; private GLContext context; - private int numBuffers = 3; - private boolean flipped = false; private boolean dirty = false; @@ -61,8 +59,11 @@ public class VBORegionSPES2 implements Region{ public void update(){ GL2ES2 gl = context.getGL().getGL2ES2(); - ShortBuffer indicies = Buffers.newDirectShortBuffer(triangles.size() * 3); + destroy(gl); + + indices = GLArrayDataServer.createGLSL(gl, null, 3, GL2ES2.GL_SHORT, false, + triangles.size(), GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER); for(Triangle t:triangles){ final Vertex[] t_vertices = t.getVertices(); @@ -70,81 +71,70 @@ public class VBORegionSPES2 implements Region{ t_vertices[0].setId(numVertices++); t_vertices[1].setId(numVertices++); t_vertices[2].setId(numVertices++); - + vertices.add(t.getVertices()[0]); vertices.add(t.getVertices()[1]); vertices.add(t.getVertices()[2]); - indicies.put((short) t.getVertices()[0].getId()); - indicies.put((short) t.getVertices()[1].getId()); - indicies.put((short) t.getVertices()[2].getId()); + indices.puts((short) t.getVertices()[0].getId()); + indices.puts((short) t.getVertices()[1].getId()); + indices.puts((short) t.getVertices()[2].getId()); } else{ Vertex v1 = t_vertices[0]; Vertex v2 = t_vertices[1]; Vertex v3 = t_vertices[2]; - indicies.put((short) v1.getId()); - indicies.put((short) v2.getId()); - indicies.put((short) v3.getId()); + indices.puts((short) v1.getId()); + indices.puts((short) v2.getId()); + indices.puts((short) v3.getId()); } } - indicies.rewind(); + indices.seal(gl, true); - FloatBuffer verticesBuffer = Buffers.newDirectFloatBuffer(vertices.size() * 3); + verticeAttr = GLArrayDataServer.createGLSL(gl, Region.VERTEX_ATTR_NAME, 3, GL2ES2.GL_FLOAT, false, + vertices.size(), GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); + verticeAttr.setLocation(Region.VERTEX_ATTR_IDX); for(Vertex v:vertices){ - verticesBuffer.put(v.getX()); + verticeAttr.putf(v.getX()); if(flipped){ - verticesBuffer.put(-1*v.getY()); - } - else{ - verticesBuffer.put(v.getY()); + verticeAttr.putf(-1*v.getY()); + } else { + verticeAttr.putf(v.getY()); } - verticesBuffer.put(v.getZ()); + verticeAttr.putf(v.getZ()); } - verticesBuffer.rewind(); - - FloatBuffer texCoordBuffer = Buffers.newDirectFloatBuffer(vertices.size() * 2); + verticeAttr.seal(gl, true); + + texCoordAttr = GLArrayDataServer.createGLSL(gl, Region.TEXCOORD_ATTR_NAME, 2, GL2ES2.GL_FLOAT, false, + vertices.size(), GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); + texCoordAttr.setLocation(Region.TEXCOORD_ATTR_IDX); for(Vertex v:vertices){ float[] tex = v.getTexCoord(); - texCoordBuffer.put(tex[0]); - texCoordBuffer.put(tex[1]); + texCoordAttr.putf(tex[0]); + texCoordAttr.putf(tex[1]); } - texCoordBuffer.rewind(); - - vboIds = IntBuffer.allocate(numBuffers); - gl.glGenBuffers(numBuffers, vboIds); - - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, vboIds.get(0)); // vertices - gl.glBufferData(GL2ES2.GL_ARRAY_BUFFER, numVertices * 3 * Buffers.SIZEOF_FLOAT, verticesBuffer, GL2ES2.GL_STATIC_DRAW); - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, 0); - - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, vboIds.get(1)); //texture - gl.glBufferData(GL2ES2.GL_ARRAY_BUFFER, numVertices * 2 * Buffers.SIZEOF_FLOAT, texCoordBuffer, GL2ES2.GL_STATIC_DRAW); - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, 0); - - gl.glBindBuffer(GL2ES2.GL_ELEMENT_ARRAY_BUFFER, vboIds.get(2)); //triangles - gl.glBufferData(GL2ES2.GL_ELEMENT_ARRAY_BUFFER, triangles.size()* 3 * Buffers.SIZEOF_SHORT, indicies, GL2ES2.GL_STATIC_DRAW); - gl.glBindBuffer(GL2ES2.GL_ELEMENT_ARRAY_BUFFER, 0); - + texCoordAttr.seal(gl, true); + + verticeAttr.enableBuffer(gl, false); + texCoordAttr.enableBuffer(gl, false); + indices.enableBuffer(gl, false); + dirty = false; } private void render() { GL2ES2 gl = context.getGL().getGL2ES2(); + + verticeAttr.enableBuffer(gl, true); + texCoordAttr.enableBuffer(gl, true); + indices.enableBuffer(gl, true); - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, vboIds.get(0)); - gl.glEnableVertexAttribArray(VERTEX_ATTR_IDX); - gl.glVertexAttribPointer(VERTEX_ATTR_IDX, 3, GL2ES2.GL_FLOAT, false, 3 * Buffers.SIZEOF_FLOAT, 0); - - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, vboIds.get(1)); - gl.glEnableVertexAttribArray(TEXCOORD_ATTR_IDX); - gl.glVertexAttribPointer(TEXCOORD_ATTR_IDX, 2, GL2ES2.GL_FLOAT, false, 2 * Buffers.SIZEOF_FLOAT, 0); - - gl.glBindBuffer(GL2ES2.GL_ELEMENT_ARRAY_BUFFER, vboIds.get(2)); - gl.glDrawElements(GL2ES2.GL_TRIANGLES, triangles.size() * 3, GL2ES2.GL_UNSIGNED_SHORT, 0); + gl.glDrawElements(GL2ES2.GL_TRIANGLES, indices.getElementNumber() * indices.getComponentNumber(), GL2ES2.GL_UNSIGNED_SHORT, 0); - gl.glBindBuffer(GL2ES2.GL_ARRAY_BUFFER, 0); + verticeAttr.enableBuffer(gl, false); + texCoordAttr.enableBuffer(gl, false); + indices.enableBuffer(gl, false); } public void render(PMVMatrix matrix, int vp_width, int vp_height, int width){ @@ -172,7 +162,22 @@ public class VBORegionSPES2 implements Region{ public void destroy() { GL2ES2 gl = context.getGL().getGL2ES2(); - gl.glDeleteBuffers(numBuffers, vboIds); + destroy(gl); + } + + final void destroy(GL2ES2 gl) { + if(null != verticeAttr) { + verticeAttr.destroy(gl); + verticeAttr = null; + } + if(null != texCoordAttr) { + texCoordAttr.destroy(gl); + texCoordAttr = null; + } + if(null != indices) { + indices.destroy(gl); + indices = null; + } } public boolean isFlipped() { |