Reputation: 693
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.
Thanks in advance.
Upvotes: 0
Views: 216
Reputation: 525
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.
Upvotes: 1
Reputation: 1171
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:
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.
Upvotes: 2