Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use stencil buffer for drawing shadows.

  • Loading branch information...
commit 0c030b16cafeb11c0af808f5a91de77c24b955a3 1 parent 2ee2ac6
@AndreyNazarov AndreyNazarov authored
View
1  src/gl_local.h
@@ -49,6 +49,7 @@ typedef struct {
float add, modulate, scale;
} world;
GLuint prognum_warp;
+ GLbitfield stencil_buffer_bit;
float entity_modulate;
float inverse_intensity;
float sintab[256];
View
2  src/gl_main.c
@@ -504,7 +504,7 @@ static void GL_DrawTearing(void)
points[3][0] = r_config.width;
points[3][1] = 0;
- qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ qglClear(GL_COLOR_BUFFER_BIT);
qglDisable(GL_TEXTURE_2D);
qglDisableClientState(GL_TEXTURE_COORD_ARRAY);
View
15 src/gl_mesh.c
@@ -530,6 +530,13 @@ static void draw_shadow(maliasmesh_t *mesh)
// load shadow projection matrix
qglLoadMatrixf(shadowmatrix);
+ // eliminate z-fighting by utilizing stencil buffer, if available
+ if (gl_config.stencilbits) {
+ qglEnable(GL_STENCIL_TEST);
+ qglStencilFunc(GL_EQUAL, 0, 0xff);
+ qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
+ }
+
GL_Bits(GLS_BLEND_BLEND);
GL_TexEnv(GL_MODULATE);
GL_BindTexture(TEXNUM_WHITE);
@@ -550,6 +557,14 @@ static void draw_shadow(maliasmesh_t *mesh)
qglEnableClientState(GL_COLOR_ARRAY);
qglEnableClientState(GL_TEXTURE_COORD_ARRAY);
+ // once we have drawn something to stencil buffer, continue to clear it for
+ // the lifetime of OpenGL context. leaving stencil buffer "dirty" and
+ // clearing just depth is slower (verified for Nvidia and ATI drivers).
+ if (gl_config.stencilbits) {
+ qglDisable(GL_STENCIL_TEST);
+ gl_static.stencil_buffer_bit |= GL_STENCIL_BUFFER_BIT;
+ }
+
// fall back to entity matrix
qglLoadMatrixf(glr.entmatrix);
}
View
7 src/gl_state.c
@@ -227,14 +227,15 @@ void GL_Setup3D(void)
GL_Bits(GLS_DEFAULT);
GL_CullFace(GLS_CULL_BACK);
- qglClear(GL_DEPTH_BUFFER_BIT);
+ qglClear(GL_DEPTH_BUFFER_BIT | gl_static.stencil_buffer_bit);
}
void GL_SetDefaultState(void)
{
qglClearColor(0, 0, 0, 1);
qglClearDepth(1);
- qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ qglClearStencil(0);
+
qglEnable(GL_DEPTH_TEST);
qglDepthFunc(GL_LEQUAL);
qglDepthRange(0, 1);
@@ -245,6 +246,8 @@ void GL_SetDefaultState(void)
qglFrontFace(GL_CW);
qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+ qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | gl_static.stencil_buffer_bit);
+
qglEnableClientState(GL_VERTEX_ARRAY);
qglEnableClientState(GL_TEXTURE_COORD_ARRAY);
Please sign in to comment.
Something went wrong with that request. Please try again.