Skip to content

Instantly share code, notes, and snippets.

@terryjsmith
Created October 7, 2014 00:28
Show Gist options
  • Save terryjsmith/b463612c262debab4015 to your computer and use it in GitHub Desktop.
Save terryjsmith/b463612c262debab4015 to your computer and use it in GitHub Desktop.
Evolution render loop
void RenderSystem::Update(float elapsed) {
// Clear the screen
glClearColor(0.0, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Update our camera info
glm::mat4 view = glm::lookAt(m_camera->translation, m_camera->translation + m_camera->look, m_camera->up);
glm::mat4 projection = glm::perspective(glm::radians(m_camera->fov), m_camera->aspect, m_camera->fnear, m_camera->ffar);
// Loop through all of our renderables and render them
for(int i = 0; i < m_bucketSize; i++) {
if(m_components[i]) {
// Create an MVP matrix for us
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 mvp = projection * view * model;
// Cast to a Renderable we can use
Renderable* component = (Renderable*)m_components[i];
// Attach the shader program
ShaderProgram* program = component->program;
glUseProgram(program->program);
// TODO: Move this outside of the loop
glUniformMatrix4fv(program->uniform("mvp"), 1, GL_FALSE, &mvp[0][0]);
// Bind the vertex attribute object, which will bind the buffers and attributes for us
glBindVertexArray(component->vertexAttribObject);
// Either draw from vertex array or indices if defined
if(component->indexBuffer == 0)
glDrawArrays(GL_TRIANGLES, 0, component->numTriangles * 3);
else
glDrawElements(GL_TRIANGLES, component->numTriangles * 3, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glUseProgram(0);
}
}
// Swap OpenGL buffers
glfwSwapBuffers(m_window);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment