Created
January 20, 2016 17:19
-
-
Save capnramses/8dfacfddb315fc1a33ae to your computer and use it in GitHub Desktop.
depth-write pre-pass to fight overdraw. inspired by Doom3
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
static void draw_frame (double elapsed) { | |
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
if (do_pre_pass) { // depth-writing pre-pass | |
glUseProgram (dshader_programme); | |
for (int i = 0; i < NUM_MONKEYS; i++) { | |
float x = 0.0f; | |
float y = 0.0f; | |
float z = sinf ((float)i) * 100.0f - 100.0f; | |
mat4 M = translate_mat4 (vec3_from_3f (x, y, z)); | |
mat4 PVM = mult_mat4_mat4 (PV, M); | |
glUniformMatrix4fv (dsp_PVM_loc, 1, GL_FALSE, PVM.m); | |
glBindVertexArray (mesh.vao); | |
glDrawArrays (GL_TRIANGLES, 0, mesh.pc); | |
} | |
glDepthFunc (GL_LEQUAL); // because self is gonna be equal duh! | |
glDepthMask (GL_FALSE); // disable depth writing - already done | |
} | |
{ // normal render pass with no depth rendering | |
glUseProgram (shader_programme); | |
for (int i = 0; i < NUM_MONKEYS; i++) { | |
float x = 0.0f; | |
float y = 0.0f; | |
float z = sinf ((float)i) * 100.0f - 100.0f; | |
mat4 M = translate_mat4 (vec3_from_3f (x, y, z)); | |
mat4 PVM = mult_mat4_mat4 (PV, M); | |
glUniformMatrix4fv (sp_PVM_loc, 1, GL_FALSE, PVM.m); | |
glBindVertexArray (mesh.vao); | |
glDrawArrays (GL_TRIANGLES, 0, mesh.pc); | |
} | |
glDepthFunc (GL_LESS); | |
glDepthMask (GL_TRUE); // disable depth writing - already done | |
} | |
glfwSwapBuffers (g_gl.win); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment