I'm trying to make a LensFlare effect when the player watch the sun in my XNA 4.0 Game.
To do this, I use OcclusionQuery, here's my code:
I also have some models, a terrain and a skybox.
Here's my main Draw code:
terrain.Draw();
model1.Draw();
model2.Draw();
skybox.Draw();
lensFlare.UpdateOcclusion();
lensFlare.Draw();
The problem is that the occlusion considers the sun to be behind the skybox, and the lensFlare wasn't showing up.
So I moved lensFlare.UpdateOcclusion()
before the drawing of the Skybox, and now the lensFlare appears, but my skybox is blinking (it's like it disappear and reappear at each frames...)
How do I ignore the skybox in the occlusion?
Answer
The easiest way is to change the depth range for your skybox. I'm not familiar with XNA but in D3D terms you would create a new viewport with the same size as the one used for your main scene but with MinZ 1.0f, MaxZ 1.0f, then draw the skybox, then restore the original viewport. You could alternatively achieve the same result by writing a depth of 1.0f from your pixel shader (although that wouldn't play nice with early-Z rejection on your GPU).
This ensures that your skybox will always be behind any other scene objects, and has other advantages too; for example, you no longer need to ensure that your skybox geometry is large enough to enclose your entire scene (you could draw it as a 10x10x10 cube if you wished) and you no longer need to have a large enough zFar in your projection matrix to contain it.
No comments:
Post a Comment