Skybox depthrange/farclip trick
Posted: Sat May 28, 2011 11:50 pm
This one's easy; if you're drawing a skybox you normally need to worry about the far clipping plane, and that it's sufficiently large to accommodate the skybox dimensions.
But the thing is that you don't. It's actually completely unnecessary.
This one is based on the FitzQuake 0.85 code, but it's easily adaptable to any engine.
First of all, find this code block:
Change it to:
Now find this code block:
And change it to:
OK, what we're doing here is drawing the sky as a 10x10 cube instead of positioning it at the far clipping plane. Madness? Nope, because all we need to do in order to push it to the back of the depth buffer is find this code:
And change it to:
Hey-presto. Sky is now automatically positioned at the back of the scene, and you don't need to worry about your far clipping plane, positioning things correctly, or any of that. It will just automatically be correct with any scene geometry.
But the thing is that you don't. It's actually completely unnecessary.
This one is based on the FitzQuake 0.85 code, but it's easily adaptable to any engine.
First of all, find this code block:
Code: Select all
void Sky_EmitSkyBoxVertex (float s, float t, int axis)
{
vec3_t v, b;
int j, k;
float w, h;
b[0] = s * gl_farclip.value / sqrt(3.0);
b[1] = t * gl_farclip.value / sqrt(3.0);
b[2] = gl_farclip.value / sqrt(3.0);Code: Select all
void Sky_EmitSkyBoxVertex (float s, float t, int axis)
{
vec3_t v, b;
int j, k;
float w, h;
b[0] = s * 10.0f;
b[1] = t * 10.0f;
b[2] = 10.0f;Code: Select all
void Sky_SetBoxVert (float s, float t, int axis, vec3_t v)
{
vec3_t b;
int j, k;
b[0] = s * gl_farclip.value / sqrt(3.0);
b[1] = t * gl_farclip.value / sqrt(3.0);
b[2] = gl_farclip.value / sqrt(3.0);Code: Select all
void Sky_SetBoxVert (float s, float t, int axis, vec3_t v)
{
vec3_t b;
int j, k;
b[0] = s * 10.0f;
b[1] = t * 10.0f;
b[2] = 10.0f;Code: Select all
if (!r_fastsky.value && !(Fog_GetDensity() > 0 && r_skyfog.value >= 1))
{
glDepthFunc(GL_GEQUAL);
glDepthMask(0);
if (skybox_name[0])
Sky_DrawSkyBox ();
else
Sky_DrawSkyLayers();
glDepthMask(1);
glDepthFunc(GL_LEQUAL);
}Code: Select all
if (!r_fastsky.value && !(Fog_GetDensity() > 0 && r_skyfog.value >= 1))
{
glDepthRange (1, 1);
glDepthFunc(GL_GEQUAL);
glDepthMask(0);
if (skybox_name[0])
Sky_DrawSkyBox ();
else
Sky_DrawSkyLayers();
glDepthMask(1);
glDepthFunc(GL_LEQUAL);
glDepthRange (0, 1);
}