• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Capoeirista

Depth Test Fail

10 posts in this topic

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!

Edited by Orangeatang
0

Share this post


Link to post
Share on other sites
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 ?
1

Share this post


Link to post
Share on other sites

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.

 

Thanks for your help!

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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)

0

Share this post


Link to post
Share on other sites

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

 

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
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0