Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL ES 2.0 and vertex buffer objects (VBO)

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?

like image 949
Nitrex88 Avatar asked Jul 28 '11 05:07

Nitrex88


People also ask

What is a VBO in OpenGL?

A vertex buffer object (VBO) is an OpenGL feature that provides methods for uploading vertex data (position, normal vector, color, etc.) to the video device for non-immediate-mode rendering.

What are buffer objects in OpenGL?

Buffer Objects are OpenGL Objects that store an array of unformatted memory allocated by the OpenGL context (AKA the GPU). These can be used to store vertex data, pixel data retrieved from images or the framebuffer, and a variety of other things.

What is the difference between Vao and VBO?

A VBO is a buffer of memory which the gpu can access. That's all it is. A VAO is an object that stores vertex bindings. This means that when you call glVertexAttribPointer and friends to describe your vertex format that format information gets stored into the currently bound VAO.

What function creates a vertex buffer object?

Creating a VBO requires 3 steps; Generate a new buffer object with glGenBuffers(). Bind the buffer object with glBindBuffer(). Copy vertex data to the buffer object with glBufferData().


1 Answers

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!

like image 129
Dr. Snoopy Avatar answered Oct 07 '22 14:10

Dr. Snoopy