Skybox rendering problem with far plane (Direct3D9)

Started by
2 comments, last by maxest 12 years, 2 months ago
I have a skybox in my scene. I render it as a very last entity. It has some fixed small size. I position it in the position of the camera. To make sure it is rendered behind all other surfaces I use Device->SetViewport with minZ and maxZ of viewport set both to 1.0.

My second OpenGL renderer does exactly the same, but here I set the viewport's minZ and maxZ using glDepthRange. I guess it's the counterpart of DX's SetViewport z values.

Now, I also render some geometry plane, which is always parallel to the view plane and it is very close to the far plane. Problem is that when I increase the zFar range (so my camera gets farther from the geometry plane, while the geometry plane is still the same distance from the far plane) the skybox at some point jumps in front of the geometry plane, covering it entirely. That should not happen as the skybox is forced to render on Z = 1.0 (set by Device->SetViewport), while the gometry plane should be smaller than 1.0 by some epsilon. I thought that the problem could be that the geometry plane gets Z values to 1.0, and as skybox is rendered last, it overwrites the geometry plane, when LessEqual depth test function was turned on. Then I switched it to Less for the skybox rendering, meaning that it should not be possible for the skybox to pop up in front of any object that is also on Z = 1.0. Unfortunately, this didn't help.

Weird thing here is that it works ok on OpenGL, and breaks on my D3D9 renderer. I'm completely run out of ideas what could the problem be. Any idea?
Advertisement
Try rendering the skybox as the first object maybe? Turn off depth testing, draw skybox, turn depth testing on and then draw everything else.

o3o

That would probably work but I don't want to waste fillrate for covering the entire screen with skybox pixels that will be overwritten anyway. So that idea is not an option
I think I've just solved my problem. Instead of using SetViewport minZ and maxZ values, I modified my sky box's vertex shader from this:

VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT output;

output.position = mul(input.position, worldViewProjTransform);
output.texCoord0 = input.texCoord0;

return output;
}

to this:

VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT output;

output.position = mul(input.position, worldViewProjTransform);
output.position.z = output.position.w;
output.texCoord0 = input.texCoord0;

return output;
}

This guarantess that post-projection Z values are all 1.0.

But still, I think that my previous approach should work as well...

This topic is closed to new replies.

Advertisement