Using VBOs / IBO in OpenGL ES 2.0 on Android

I am trying to create a simple android test program (API 10) using OpenGL ES 2.0 to draw a simple rectangle. I can get this to work with floating point buffers directly referencing vertices, but I'd rather do it with VBOs / IBOs. I've searched for countless hours trying to find a simple explanation (tutorial) but haven't come across one yet. My code compiles and works very well, but nothing appears on the screen other than a clear color.

Here are some code snippets to help explain how I set it up right now.

Part ofSurfaceChanged ():

int[] buffers = new int[2];
GLES20.glGenBuffers(2, buffers, 0);
rectVerts = buffers[0];
rectInds = buffers[1];
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, rectVerts);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, (rectBuffer.limit()*4), rectBuffer, GLES20.GL_STATIC_DRAW);

GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, rectInds);
GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, (rectIndices.limit()*4), rectIndices, GLES20.GL_STATIC_DRAW);

      

The onDrawFrame () part:

GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, rectVerts);
GLES20.glEnableVertexAttribArray(0);
GLES20.glVertexAttribPointer(0, 3, GLES20.GL_FLOAT, false, 0, 0);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, rectInds);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_INT, 0);

      

+3


source to share


1 answer


I don't see anything wrong, but here are some ideas you might want to look at.

1) "Compilation and execution penalty" is a useless metric for the opengl program. Errors are reported by actively calling glGetError and checking the compilation status and shader reference with glGet (Shader | Program) iv. Are you checking for errors somewhere?

2) You shouldn't assume that 0 is the correct index for vertices. It may work now, but will likely break it if you change the shader. Get the correct index using glGetAttribLocation.



3) You are binding the verts onDraw buffer, but I cannot see anything in the index buffer. Is it always connected?

4) You can also try drawing your VBO with glDrawArrays to get the index buffer from the equation, just to help debug to see which part doesn't match.

Otherwise, what you have looks correct as far as I can tell in this little snippet. Perhaps something else outside of this is going wrong.

+3


source







All Articles