9

I can't figure out how to use a vertex buffer object for my terrain in opengl es 2.0 for iphone. It's static data so I'm hoping for a speed boost by using VBO. In regular OpenGL, I use display lists along with shaders no problem. However, in opengl es 2.0 I have to send the vertex data to the shader as an attribute and don't know how this works with the VBO. How can the vertex buffer know what attribute it has to bind the vertex data to when called? Is this even possible in opengl es 2.0? If not, are there other ways I can optimize the rendering of my terrain that is static?

1 Answer 1

12

Sure, this is pretty simple actually, your attribute has a location, and vertex data is fed with glVertexAttribPointer() for plain Vertex Arrays, like this:

float *vertices = ...;
int loc = glGetAttribLocation(program, "position");
glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 0, vertices);

For VBOs, it's the same, but you have to bind the buffer to the GL_ARRAY_BUFFER target, and the last parameter of glVertexAttribPointer() is now an offset into the buffer memory storage. The pointer value itself is interpreted as a offset:

glBindBuffer(GL_ARRAY_BUFFER, buffer);
int loc = glGetAttribLocation(program, "position");
glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 0, 0);

In this case the offset is 0, assuming the vertex data is uploaded at the start of the buffer. The offset is measures in bytes.

The drawing is then performed with glDrawArrays()/glDrawElements(). Hope this helps!

Sign up to request clarification or add additional context in comments.

4 Comments

Yes this should do what I need. I'll try it tomorrow. Thanks!
It would maybe help me if I could find what to do with vertices in the second example. An idea?
@Stephane vertices should be uploaded to a VBO using glBufferData.
Can you quote the OpenGL ES 2.0 spec on that, where does it say it should be done like this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.