我是c#中的DirectX新手,有一个让我困惑的问题,基本上我想在屏幕上渲染两个立方体,一个靠近相机,另一个远离相机,我期望的是更接近的一个总是在另一个前面,但事实上,它取决于渲染序列,最后渲染的一个总是在另一个前面,我试图清除z缓冲区但是那样做根本不工作,所以我想知道是否有什么我做错了?
以下是我的代码段
private void Form1_Load(object sender, EventArgs e)
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.EnableAutoDepthStencil = true;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.MixedVertexProcessing, presentParams);
device.VertexFormat = CustomVertex.PositionColored.Format;
device.RenderState.CullMode = Cull.CounterClockwise;
device.RenderState.Lighting = false;
Matrix projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 0f, 10000.0f);
device.Transform.Projection = projection;
}
protected override void OnPaint(PaintEventArgs e)
{
Cube a = new Cube(new Vector3(0, 0, 0), 5);
Cube b = new Cube(new Vector3(0, 0, 15), 5);
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkGray, 1, 0);
device.BeginScene();
Matrix viewMatrix = Matrix.LookAtLH(cameraPosition, targetPosition, up);
device.Transform.View = viewMatrix;
device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, 8, 12, a.IndexData, false, a.GetVertices());
device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, 8, 12, b.IndexData, false, b.GetVertices());
device.EndScene();
device.Present();
}
答案 0 :(得分:0)
好吧,我终于通过改变
解决了这个问题Matrix projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 0f, 10000.0f);
到
Matrix projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 10000.0f);
但我不知道原因,为什么会发生这种情况,有人知道吗?