Wednesday, March 28, 2012

Part 6 : Drawing!


This post is about one little thing left do to after all this code. Draw the damn cube! Everything we've done with VAOs and VBOs will make our task considerably easier.

And the code:

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
// Clear the screen
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
// Bind the VAO and the program
glBindVertexArrayOES( _VAO );
glUseProgram( _program );
for (int i = 0; i < _uniformArraySize; i++) {
if (!strcmp(_uniformArray[i].Name, "ModelViewProjectionMatrix")) {
// Multiply the transformation matrices together
GLKMatrix4 modelViewProjectionMatrix = GLKMatrix4Multiply(_projectionMatrix, _modelViewMatrix);
glUniformMatrix4fv(_uniformArray[i].Location, 1, GL_FALSE, modelViewProjectionMatrix.m);
}
}
// Draw!
glDrawElements( GL_TRIANGLES, sizeof(CubeIndicesData)/sizeof(GLuint), GL_UNSIGNED_INT, NULL );
}
view raw drawInRect.m hosted with ❤ by GitHub


You might think that looping through our uniformArray is not very efficient, but when you have 10 uniforms to manage, this will make everything a lot easier.


One last thing! Cleanup:

- (void)viewDidUnload
{
[super viewDidUnload];
[EAGLContext setCurrentContext:self.context];
glDeleteBuffers(1, &_verticesVBO);
glDeleteBuffers(1, &_indicesVBO);
glDeleteVertexArraysOES(1, &_VAO);
if (_program) {
glDeleteProgram(_program);
_program = 0;
}
for (int i = 0; i < _uniformArraySize; i++) {
free(_uniformArray[i].Name);
}
free(_uniformArray);
}
view raw cleanup.m hosted with ❤ by GitHub


No comments: