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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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 ); | |
} |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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); | |
} |
No comments:
Post a Comment