0

I'm following this sample trying to build on it. I incorporated the code into my solution, which is a windows form app (a little tricky w/ XNA).

When I finally got a cube to draw it ended up inside out... or outside in... or? See for yourself.

The app is now several thousand lines so I can't paste it here. I'll need to know where to start looking.

Any idea what's wrong here?

It looks like the sides are getting drawn out of order... but that shouldn't matter. The graphics engine should determine what's visible and what's not visible but it seems to be getting it wrong.

broken box

Thanks in advance.

Community
  • 1
  • 1
John Yost
  • 693
  • 5
  • 20
  • What does it look like when you rotate it? – Simon Whitehead Feb 25 '14 at 03:24
  • It looks like two sides are drawn in the back. For example, in the cube I attached the right side and the bottom are drawn first when they should be drawn last. I don't think they're missing. This is the case from any perspective. – John Yost Feb 25 '14 at 03:34
  • I have seen this before.. I cannot remember what it's called.. but its got to do with how far away your viewport is and its projection. There's like a minimum distance or something.... I can't remember what it is though! (and Google isn't helping..) – Simon Whitehead Feb 25 '14 at 04:41
  • I think you're right. That is part of the code that came from a different soln. I'll try a few different settings and see if something clicks. Thanks for the tip. – John Yost Feb 25 '14 at 04:52

2 Answers2

2

There is an XNA Framework class called GraphicsDevice which contains all of the properties for the basic rendering parameters.

Inside GraphicsDevice there is a member struct DepthStencilState which needs to be configured with the following attributes:

  • DepthBufferEnable=true
  • DepthBufferFunction=LessThanEqual
  • DepthBufferWriteEnable=true

The easiest way is to simply set it to the statically defined Default.

GraphicsDevice.DepthStencilState = DepthStencilState.Default;

If you are still having problems, make sure the RenderTarget to which you are rendering is a texture that supports depth. Example:

finalFrameRT = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8, 0, RenderTargetUsage.PreserveContents);

If you do not wish to see the backs of rear-facing sides, you need to set RasterizerState to CullClockwise or CullCounterClockwise according to the order of your vertex indices.

selkathguy
  • 1,171
  • 7
  • 17
1

This mechanism is called "Back-face culling", which means shapes whose vertices are ordered counter clockwise will not be drawn. You could cancel this by running the following code:

RasterizerState rs = new RasterizerState();  
rs.CullMode = CullMode.None;  
GraphicsDevice.RasterizerState = rs; 

However, this is not the recommended approach, as it will usually cause the graphics device to draw shapes which are not visible to the user. The correct approach is changing the code which generates the vertices to create them in a clockwise order.

Itamar Marom
  • 525
  • 8
  • 21