Skip to content

Instantly share code, notes, and snippets.

@DCubix
Created December 8, 2016 16:25
Show Gist options
  • Save DCubix/c5b96f8e2ca27f59aa2143b44d6dcea6 to your computer and use it in GitHub Desktop.
Save DCubix/c5b96f8e2ca27f59aa2143b44d6dcea6 to your computer and use it in GitHub Desktop.
void SpriteBatch::Render() {
glBindVertexArray(m_vao);
/// 1. Draw normal maps to the buffer
glEnable(GL_DEPTH_TEST);
m_gbuffer->Bind();
glDepthFunc(GL_LESS);
glDepthMask(true);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
m_geomShader->Bind();
m_geomShader->SetMatrix4("projection", m_projection);
m_geomShader->SetMatrix4("view", m_view);
for (auto it = m_batches.begin(); it != m_batches.end(); ++it) {
RenderBatch b = *it;
if (b.normal != nullptr) {
b.normal->Bind(0);
m_geomShader->SetSampler("normal", 0);
m_geomShader->SetInt("use_normal", 1);
} else {
m_geomShader->SetInt("use_normal", 0);
}
if (b.diffuse != nullptr) {
b.diffuse->Bind(1);
m_geomShader->SetSampler("diffuse", 1);
m_geomShader->SetInt("use_diffuse", 1);
} else {
m_geomShader->SetInt("use_diffuse", 0);
}
glDrawArrays(GL_TRIANGLE_STRIP, b.offset, b.vertex_count);
glBindTexture(GL_TEXTURE_2D, 0);
}
m_geomShader->Unbind();
m_gbuffer->Unbind();
glDepthMask(false);
glDisable(GL_DEPTH_TEST);
glBindVertexArray(0);
/// 2. Lighting pass
m_sbuffer->Bind();
glClear(GL_COLOR_BUFFER_BIT);
m_lightShader->Bind();
m_lightShader->SetVector2("resolution", Vector2(w, h));
m_lightShader->SetMatrix4("projection", m_projection);
m_lightShader->SetMatrix4("scale", m_scale);
m_normal->Bind(0);
m_depth->Bind(1);
m_diffuse->Bind(2);
m_alpha->Bind(3);
m_lightShader->SetSampler("t_normal", 0);
m_lightShader->SetSampler("t_depth", 1);
m_lightShader->SetSampler("t_diffuse", 2);
m_lightShader->SetSampler("t_alpha", 3);
m_lightShader->SetColor("ambient", m_ambientColor);
m_lightShader->SetInt("ambient_only", 1);
m_quad->Draw();
m_lightShader->SetInt("ambient_only", 0);
glBlendFunc(GL_ONE, GL_ONE);
for (auto it = m_lights.begin(); it != m_lights.end(); ++it) {
Light* l = *it;
if (l->intensity > 0.0f) {
m_lightShader->SetVector3("lightPosition", l->position);
m_lightShader->SetVector2("lightDirection", l->direction);
m_lightShader->SetColor("lightColor", l->color);
m_lightShader->SetFloat("lightIntensity", l->intensity);
m_lightShader->SetFloat("lightRadius", l->radius);
m_lightShader->SetFloat("lightCutOff", l->cutOff);
m_lightShader->SetInt("lightType", (int) l->type);
m_quad->Draw();
}
}
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
m_lightShader->Unbind();
m_sbuffer->Unbind();
m_flatShader->Bind();
m_screen->Bind(0);
m_flatShader->SetMatrix4("projection", m_projection);
m_flatShader->SetSampler("tex", 0);
m_quad->Draw();
m_flatShader->Unbind();
m_screen->Unbind();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment