Jump to content
  • Advertisement
Sign in to follow this  
Capoeirista

XNA 4.0 Depth Test

This topic is 2577 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey folks,

I'm trying to render a scene with the depth buffer enabled, first using a BasicEffect instance to draw a model and then a call to DrawIndexedPrimitives to display a ground plane. I set up the depth buffer like this:




// Initialise the depth & stencil buffers
DepthStencilState depthStencilState = new DepthStencilState();
depthStencilState.DepthBufferEnable = true;
depthStencilState.DepthBufferFunction = CompareFunction.LessEqual;
depthStencilState.DepthBufferWriteEnable = true;
device.DepthStencilState = DepthStencilState.Default;


But the resulting image looks like this :

tYeWg.png


Must be doing something wrong here, but I'm not sure what... Do I have to set up a 2D render target and then use a sprite batch to draw the resulting texture for the depth buffer to kick in?

Cheers!

Share this post


Link to post
Share on other sites
Advertisement

Ah never mind - didn't realize you have to set the state on a per-frame basis


You shouldnt have to set in on a per frame basis, you have to set it again each time you change that particular state. It's a state machine so it remebers the state you last set in it. Did you set up a different state for another render call as that would invalidate the state.

Share this post


Link to post
Share on other sites

You shouldnt have to set in on a per frame basis, you have to set it again each time you change that particular state. It's a state machine so it remebers the state you last set in it. Did you set up a different state for another render call as that would invalidate the state.

I don't change the DepthStencilState directly, but each time I get to the rendering stage the GraphicDevice version is set to DepthStencilState.None

I'm not sure what would be clearing the depth stencil state, the only other thing I modify on the device are the Vertex & Index buffers when I'm rendering the ground plane or object bounding boxes

Share this post


Link to post
Share on other sites

[quote name='NightCreature83' timestamp='1321624843' post='4885308']
You shouldnt have to set in on a per frame basis, you have to set it again each time you change that particular state. It's a state machine so it remebers the state you last set in it. Did you set up a different state for another render call as that would invalidate the state.

I don't change the DepthStencilState directly, but each time I get to the rendering stage the GraphicDevice version is set to DepthStencilState.None

I'm not sure what would be clearing the depth stencil state, the only other thing I modify on the device are the Vertex & Index buffers when I'm rendering the ground plane or object bounding boxes


[/quote]
Do you set any state information in the effect file as that will affect the render state as well. These are the things you generally set before the lines that compile the vertex and pixel shaders.

Share this post


Link to post
Share on other sites

[quote name='NightCreature83' timestamp='1321624843' post='4885308']
You shouldnt have to set in on a per frame basis, you have to set it again each time you change that particular state. It's a state machine so it remebers the state you last set in it. Did you set up a different state for another render call as that would invalidate the state.

I don't change the DepthStencilState directly, but each time I get to the rendering stage the GraphicDevice version is set to DepthStencilState.None

I'm not sure what would be clearing the depth stencil state, the only other thing I modify on the device are the Vertex & Index buffers when I'm rendering the ground plane or object bounding boxes

[/quote]

A good rule of thumb is to only ever set the renderstates that you need when setting up a draw call. Between frames states don't change unless if you explicitly change them (as NightCreature explained), so you have to check your drawing code. If you're using BasicEffect, that won't be setting any renderstates on its own, but if you build your own effects you can set renderstates in the pass declarations.

Do you use SpriteBatch in any way or form? By default that uses DepthStencilState.None, so even if you aren't explicitly changing the state, spritebatch would. This sometimes is the culprit in these situations, and why it's a good idea to set the states you depend on before drawing different geometry since things like SpriteBatch or third party game components may change states without your knowledge or direct control.

Edit:

Additionally, a DepthStencilState doesn't define how to setup the depth buffer - it specifies how to use the depth buffer during drawing, e.g. not write to it, write to if depth is less than or greater than, etc. When you create the back buffer (in XNA, its closely tied to the graphics device) you can specify if you want a depth buffer, and if so its format. Same thing when creating render targets.

Share this post


Link to post
Share on other sites


Do you use SpriteBatch in any way or form? By default that uses DepthStencilState.None, so even if you aren't explicitly changing the state, spritebatch would. This sometimes is the culprit in these situations, and why it's a good idea to set the states you depend on before drawing different geometry since things like SpriteBatch or third party game components may change states without your knowledge or direct control.

Edit:

Additionally, a DepthStencilState doesn't define how to setup the depth buffer - it specifies how to use the depth buffer during drawing, e.g. not write to it, write to if depth is less than or greater than, etc. When you create the back buffer (in XNA, its closely tied to the graphics device) you can specify if you want a depth buffer, and if so its format. Same thing when creating render targets.

Ah the sprite batch will be what's doing it I'm sure - I use it to render line lists for both the ground plane and Model bounding boxes.

Cheers!


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!