简体   繁体   中英

What's wrong with my XNA cube?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM