我最近刚开始学D3D,但是在用网上的例子自学的时候出现了个问题,自己弄不明白,所以发上来请教一下大家:
我想实现的是一个三维显示的三角型,但是只出现填充的背景色,而不显示那个三维的图像,我想可能是摄像头的设置不正确,可又不会改,希望大家指教啊,下面是OnPaint和SetupCamera两个函数
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Blue, 1.0f, 0);
//画顶点
//二维的显示正常
//CustomVertex.TransformedColored[] verts = new CustomVertex.TransformedColored[3];
//verts[0].Position = new Vector4(this.Width / 2.0f, 50.0f, 0.5f, 1.0f);
//verts[0].Color = System.Drawing.Color.Aqua.ToArgb();
//verts[1].Position = new Vector4(this.Width - (this.Width / 5.0f), this.Height - (this.Height / 5.0f), 0.5f, 1.0f);
//verts[1].Color = System.Drawing.Color.Black.ToArgb();
//verts[2].Position = new Vector4(this.Width / 5.0f, this.Height - (this.Height / 5.0f), 0.5f, 1.0f);
//verts[2].Color = System.Drawing.Color.Purple.ToArgb();
//VertexBuffer vb = (VertexBuffer)sender;
//三维就不显示了
CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3];
verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
verts[0].Color = System.Drawing.Color.Aqua.ToArgb();
verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
verts[1].Color = System.Drawing.Color.Black.ToArgb();
verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
verts[2].Color = System.Drawing.Color.Purple.ToArgb();
device.VertexFormat = CustomVertex.PositionColored.Format; SetupCamera();
device.BeginScene();
device.VertexFormat = CustomVertex.TransformedColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts);
device.EndScene();
device.Present();
this.Invalidate();
}
private void SetupCamera()
{
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 100.0f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 3.0f, -5.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));
device.Transform.World = Matrix.RotationZ((System.Environment.TickCount / 450.0f) / (float)Math.PI);
device.RenderState.Lighting = false;
}
我想实现的是一个三维显示的三角型,但是只出现填充的背景色,而不显示那个三维的图像,我想可能是摄像头的设置不正确,可又不会改,希望大家指教啊,下面是OnPaint和SetupCamera两个函数
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Blue, 1.0f, 0);
//画顶点
//二维的显示正常
//CustomVertex.TransformedColored[] verts = new CustomVertex.TransformedColored[3];
//verts[0].Position = new Vector4(this.Width / 2.0f, 50.0f, 0.5f, 1.0f);
//verts[0].Color = System.Drawing.Color.Aqua.ToArgb();
//verts[1].Position = new Vector4(this.Width - (this.Width / 5.0f), this.Height - (this.Height / 5.0f), 0.5f, 1.0f);
//verts[1].Color = System.Drawing.Color.Black.ToArgb();
//verts[2].Position = new Vector4(this.Width / 5.0f, this.Height - (this.Height / 5.0f), 0.5f, 1.0f);
//verts[2].Color = System.Drawing.Color.Purple.ToArgb();
//VertexBuffer vb = (VertexBuffer)sender;
//三维就不显示了
CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3];
verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
verts[0].Color = System.Drawing.Color.Aqua.ToArgb();
verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
verts[1].Color = System.Drawing.Color.Black.ToArgb();
verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
verts[2].Color = System.Drawing.Color.Purple.ToArgb();
device.VertexFormat = CustomVertex.PositionColored.Format; SetupCamera();
device.BeginScene();
device.VertexFormat = CustomVertex.TransformedColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts);
device.EndScene();
device.Present();
this.Invalidate();
}
private void SetupCamera()
{
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 100.0f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 3.0f, -5.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));
device.Transform.World = Matrix.RotationZ((System.Environment.TickCount / 450.0f) / (float)Math.PI);
device.RenderState.Lighting = false;
}
angle 初始化为0.0f;
private void SetupCamera()
{
device.Transform.World = Matrix.RotationAxis(new Vector3(angle / ((float)Math.PI * 2.0f), angle / ((float)Math.PI * 4.0f),
angle / ((float)Math.PI * 6.0f)), angle / (float)Math.PI);
angle += 0.1f;
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 5.0f), new Vector3(), new Vector3(0, 1, 0));
}