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?
OpenGL ES 2.0 and vertex buffer objects (VBO)
Asked Answered
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!
Yes this should do what I need. I'll try it tomorrow. Thanks! –
Mesquite
It would maybe help me if I could find what to do with
vertices
in the second example. An idea? –
Gestalt @Stephane vertices should be uploaded to a VBO using glBufferData. –
Foghorn
Can you quote the OpenGL ES 2.0 spec on that, where does it say it should be done like this? –
Pneumatograph
© 2022 - 2024 — McMap. All rights reserved.