Wednesday, March 28, 2012

Part 5: VBOs and VAOs and more


In this post, we'll be creating the 2 VBOs that will allow us to pass our vertex data as well as the indices describing the shape directly to the GPU's memory. VBOs will make drawing much faster since you won't need to pass that data every time you want to draw. What you still need to do is bind those buffers and "describe" the data so that OpenGL can understand how to use it as an attribute in your shader program.

We'll also be creating a VAO, which will store the set of bindings. So, instead of binding the first VBO and describing it, as well as binding the second one and describing it, we'll just bind the VAO, and everything else will be done for us inside OpenGL.
First, add these 3 variables in your private interface:
GLuint _verticesVBO;
GLuint _indicesVBO;
GLuint _VAO;
Here's the code to create the VBOs and VAOs:


Now, there's one last thing you can do in the ViewDidLoad method, which is set up the Projection transformation matrix, and the ModelView transformation matrix's initial state (if you want anything to move, you'll have to modify for sure). This will be your first time using GLKit (Yay!). To do that, first add these two variables in your private interface:



Errata:
As noted by Youpitou, GLKMatrix4MakePerspective takes the fovy in radians, and I used 45 degrees instead. If you want to get pi/4, then you should use GLKMathDegreesToRadians(45.0f) instead.

With the ModelView matrix, I position the object relative to the viewer. We call it the eye coordinate space. The Projection matrix will distort the objects to make them appear smaller as they are further away from the viewer (to imitate reality).

4 comments:

Youpitu said...

note:
the private variable must be aded with this name:
GLuint _verticesVBO;
GLuint _indicesVBO;
GLuint _VAO;

Youpitu said...

I think you have an error in your code.
When you call GLKMatrix4MakePerspective...
In the docs this method wait for:
GLKMatrix4MakePerspective(float fovyRadians, float aspect, float nearZ, float farZ)
And you call it like this:
GLKMatrix4MakePerspective(45.0f, (float)width/(float)height, 0.01f, 100.0f)
but it should be GLKMatrix4MakePerspective(GLKMathDegreesToRadians(45.0f), (float)width/(float)height, 0.01f, 100.0f)
I think you pass degree (45°) in place of a radians...
Hopefully it works but i don't think you got a real FOV of 45° in your App.

Gabriel Gohier-Roy said...

Thanks for the comments. I wonder what happens in my case. I do get a projection, although like you said, it's probably not pi/4 like I wanted.

Youpitu said...

Maybe you got something like 45%pi or 45%2pi and it happens to be something equal to (+/-)pi/4...
I think the result of the modulo is something like pi/3...