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?
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!