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 )
{
VertexShaderOutput output;
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!