• Create Account

## Depth Test Fail

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1Orangeatang  Members

Posted 08 June 2013 - 10:43 AM

Hi there,

At the moment the 2D editor I'm working on is using draw order to define the visual depth of objects, but I'm trying to switch this over to a depth test so I can apply some post processing effects (like depth of field and ambient occlusion).

Trouble is that if the depth that I render the objects is isn't set to -1.0f, nothing is drawn. I must be missing something obvious here, so if I could get a second set of eyes that would be most helpful! Unfortunately I can't use pix as the xna window is in a WinFormsHost control in a WPF application... it can't pick up on any draw calls.

OK my depth/stencil state is set up like this (I'm using the stencil buffer to work out which objects I should apply light-maps to):

myColourTarget = new RenderTarget2D( aGraphicsDevice, width, height, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8 );

myLightingEnabledState                      = new DepthStencilState();
myLightingEnabledState.StencilEnable        = true;
myLightingEnabledState.StencilFunction      = CompareFunction.Always;
myLightingEnabledState.StencilPass          = StencilOperation.Replace;
myLightingEnabledState.ReferenceStencil     = 1;
myLightingEnabledState.DepthBufferEnable    = true;


I'm setting up the world/view matrices as follows, with a near plane of 0.0f and a far plane of -10.0f. The view matrix here is just an identity matrix (and I'll be caching the matrix calculation at a later stage):

 // Set up the main object rendering effect
Effect objectEffect = effectManager.GetEffect( myEngineObjectEffectID );
objectEffect.Parameters["View"].SetValue( currentCamera.View);
objectEffect.Parameters["Projection"].SetValue(Matrix.CreateOrthographicOffCenter(0.0f, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height, 0.0f, 0.0f, -10.0f)); 

graphicsDevice.SetRenderTarget( myColourTarget );
graphicsDevice.Clear( ClearOptions.DepthBuffer | ClearOptions.Stencil | ClearOptions.Target, ClearColor, 0.0f, 0 );


The world matrix is set based on the type of object being rendered (to account for parallax scrolling in the foreground/background):

anEffect.Parameters["World"].SetValue( aCamera.GetParallaxWorld(currentScene.CentralParallax) );

...

myWorld = Matrix.CreateTranslation(new Vector3(-myPosition, 1.0f)) *   // Position
Matrix.CreateRotationZ(myRotation) *                         // Rotation
Matrix.CreateScale(new Vector3(myZoom, myZoom, 1.0f));       // Scale


And when I draw my objects with a depth of 0.5f, everything gets culled:

Vector2 objectCenter = new Vector2(aComponentObject.Size.X / 2.0f, aComponentObject.Size.Y / 2.0f);

aSpriteBatch.Draw( aTextureManager.GetTexture(graphicComponent.ColourMap),
aComponentObject.Position,
null,
drawColour,
aComponentObject.Rotation,
objectCenter,
aComponentObject.Scale,
SpriteEffects.None,
-0.5f );


So as far as I can tell I should have an orthographic camera, positioned at (x,y,1.0f) with a near plane of 0.0f and a far plane of -10.0f... but the depth tests are failing with depth set to -0.5f.

Any ideas what I'm doing wrong here? The odd thing is if I have the depth set to -10.0 (right on the far view plane) in the sprite batch call, everything renders without any problems. The vertex shader I'm using dead simple :

VertexShaderOutput VS( VertexShaderInput input )
{

float4 worldPosition 	= mul(input.myPosition, World);
float4 viewPosition 	= mul(worldPosition, View);

output.myPosition 		= mul(viewPosition, Projection);
output.myTexCoord 		= input.myTexCoord;
output.myColour			= input.myColour;

return output;
}


Thanks for the help!

Edited by Orangeatang, 08 June 2013 - 11:08 AM.

### #2unbird  Members

Posted 08 June 2013 - 01:51 PM

Not sure what the exact problem is, so I just throw some ideas:
• Clear your depth to 1.0f. The default DepthStencilState uses LessEqual for the compare function (0 is near, 1 is far)
• Though a negative far plane should work, too, I consider it quite unusual. Have you tried a positive one (and adjusting your layer values accordingly).
• According to the documentation the layer parameter of SpriteBatch.Draw expects values between 0 and 1. I wonder if it actually is used as a vertex position z value later, or if it's just used for one of the sorting modes. Speaking of which: What mode do you use when calling SpriteBatch.Begin() ?
• Your world transformation is peculiar. Usually you rotate and scale first, then translate. Matrix multiplication order matters. Can you explain, please ?

### #3Orangeatang  Members

Posted 08 June 2013 - 02:09 PM

Not sure what the exact problem is, so I just throw some ideas:

• Clear your depth to 1.0f. The default DepthStencilState uses LessEqual for the compare function (0 is near, 1 is far)
• Though a negative far plane should work, too, I consider it quite unusual. Have you tried a positive one (and adjusting your layer values accordingly).
• According to the documentation the layer parameter of SpriteBatch.Draw expects values between 0 and 1. I wonder if it actually is used as a vertex position z value later, or if it's just used for one of the sorting modes. Speaking of which: What mode do you use when calling SpriteBatch.Begin() ?
• Your world transformation is peculiar. Usually you rotate and scale first, then translate. Matrix multiplication order matters. Can you explain, please ?

Hey unbird thanks for the reply... in response :

Using positive values for the near/far planes fixed everything perfectly - thanks! I'm not sure I why I was using negative values... I had it in my head that my camera was pointing down the -z axis.

I'm using an odd looking world transform because I'm transforming the world based on the position of the camera. My implementation is loosely based on the camera outlined in this article http://www.david-amador.com/2009/10/xna-camera-2d-with-zoom-and-rotation/ but without the origin offset.

### #4unbird  Members

Posted 08 June 2013 - 02:13 PM

Ah, ok, a was expecting something like this (2D camera). Glad to hear it's working.

### #5Orangeatang  Members

Posted 08 June 2013 - 02:21 PM

Unfortunately objects with a depth value of 1.0f are getting rendered behind objects with a depth value of 100.0f.

I'm using :

aSpriteBatch.Begin( SpriteSortMode.Deferred, BlendState.AlphaBlend, null, depthStencilState, null, anEffect );


I guess I'll read up on what the depth value that you pass to SpriteBatch.Draw is used for. It may be that I'll have to pass the depth directly to the effect I'm using.

Edited by Orangeatang, 08 June 2013 - 02:21 PM.

### #6Orangeatang  Members

Posted 08 June 2013 - 02:34 PM

Ah yeah - the SpriteBatch.Draw 'depth' parameter is a layer depth (between 0 & 1 as you said)... I'll have to think of another way to get the depth in on a per-object basis (I don't think you can modify shader parameters after calling SpriteBatch.Begin)

### #7unbird  Members

Posted 08 June 2013 - 02:43 PM

Hmmm, that would a bit a pity performance-wise, since you probably need to draw sprite by sprite then  .

There's one Begin()-overload which takes a transformation as parameter, which you could play with. So you probably won't need to change your shader too much. Still you need to draw every sprite with Begin()/Draw()/End(). Well, you could group the sprites of the same layer at least.

I gonna have a look at what happens in PIX, maybe I see something better. If not that would mean rolling your own SpriteBatch (or maybe you find one, give google a shot!).

Edited by unbird, 08 June 2013 - 02:44 PM.

### #8unbird  Members

Posted 08 June 2013 - 03:11 PM

You can save you the trouble. Spritebatch has proper 3D coords. The layer value goes into pre-VS z.

Here you got something to play with: Hit space to reverse the layer value.

### #9Orangeatang  Members

Posted 08 June 2013 - 04:37 PM

Fantastic! Thanks

### #10mhagain  Members

Posted 09 June 2013 - 08:05 AM

The problem is your near plane of 0.  For an ortho matrix you can set near to a sign-flipped version of far (so for a far of -10 you'd use a near of 10) and everything should come out as expected.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #11Orangeatang  Members

Posted 09 June 2013 - 01:23 PM

The problem is your near plane of 0.  For an ortho matrix you can set near to a sign-flipped version of far (so for a far of -10 you'd use a near of 10) and everything should come out as expected.

Excellent point, thanks mahagian! This cleared up the last issues I was having with the rendering.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.