What's wrong with my XNA cube?

风格不统一 提交于 2019-12-13 20:16:01

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/22003937/whats-wrong-with-my-xna-cube

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!