DrawBasicGeometry.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:17k
- ///////////////////////////////////////////////////////////////////////
- // ■■■■ ■■■■■ ■■■■ ■ ■ //
- // ■ ■ ■ ■ ■ //
- // ■ ■ ■ ■■■ ■ ■ //
- // ■ ■ ■ ■ ■ ■ //
- // ■■■■ ■ ■■■■ ■■■■ //
- // Copyright (c) 三峡大学水利与环境学院 肖泽云. All rights reserved. //
- ////////////////////////////////////////////////////////////////////////
- using System;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Windows.Forms;
- using Microsoft.DirectX;
- using Microsoft.DirectX.Direct3D;
- namespace 绘制基本几何体
- {
- public partial class DrawBasicGeometry : Form
- {
- Device device = null;//定义绘图设备
- private CustomVertex.PositionColored[] vertices;//定义顶点变量
- private VertexBuffer vertexBuffer;//定义顶点缓冲变量
- private IndexBuffer indexBuffer;//定义索引缓冲变量
- private int[] indexData;//定义顶点索引
- private float angleY=0.01f;//定义绕Y轴旋转变量
- private Vector3 CamPostion = new Vector3(10, 10, -50);//定义摄像机位置
- private Vector3 CamTarget = new Vector3(10, 10, 0);//定义摄像机目标位置
- private int mouseLastX,mouseLastY;//记录鼠标按下时的坐标位置
- private bool isRotateByMouse=false;//记录是否由鼠标控制旋转
- private bool isMoveByMouse = false;//记录是否由鼠标控制移动
- public DrawBasicGeometry()
- {
- this.ClientSize = new Size(800, 600);//指定窗体尺寸
- this.Text = "绘制基本几何体";//指定窗体标题
- }
- public bool InitializeDirect3D()
- {
- try
- {
- PresentParameters presentParams = new PresentParameters();
- presentParams.Windowed = true; //指定以Windows窗体形式显示
- presentParams.SwapEffect = SwapEffect.Discard; //当前屏幕绘制后它将自动从内存中删除
- device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); //实例化device对象
- VertexDeclaration();//定义顶点
- IndicesDeclaration();//定义索引
- return true;
- }
- catch (DirectXException e)
- {
- MessageBox.Show(e.ToString(), "Error"); //处理异常
- return false;
- }
- }
- private void VertexDeclaration()//定义顶点
- {
- vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 9, device,
- Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
- vertices = new CustomVertex.PositionColored[9];
- vertices[0].Position = new Vector3(0f, 20f, 0f);
- vertices[0].Color = Color.Yellow.ToArgb();
- vertices[1].Position = new Vector3(10f, 20f, 0f);
- vertices[1].Color = Color.White.ToArgb();
- vertices[2].Position = new Vector3(0f, 10f, 0f);
- vertices[2].Color = Color.Green.ToArgb();
- vertices[3].Position = new Vector3(20f, 20f, 0f);
- vertices[3].Color = Color.IndianRed.ToArgb();
- vertices[4].Position = new Vector3(10f, 10f, 0f);
- vertices[4].Color = Color.LightPink.ToArgb();
- vertices[5].Position = new Vector3(0f, 0f, 0f);
- vertices[5].Color = Color.Orange.ToArgb();
- vertices[6].Position = new Vector3(20f, 10f, 0f);
- vertices[6].Color = Color.Green.ToArgb();
- vertices[7].Position = new Vector3(10f, 0f, 0f);
- vertices[7].Color = Color.DeepPink.ToArgb();
- vertices[8].Position = new Vector3(20f, 0f, 0f);
- vertices[8].Color = Color.IndianRed.ToArgb();
- vertexBuffer.SetData(vertices, 0, LockFlags.None);//设置顶点数据至顶点缓冲中
- }
-
- private void IndicesDeclaration()//定义索引
- {
- indexBuffer = new IndexBuffer(typeof(int), 24, device, Usage.WriteOnly, Pool.Default);
- indexData = new int[24];
- indexData[0] = 0;indexData[1] = 1;indexData[2] = 2;
- indexData[3] = 2;indexData[4] = 1;indexData[5] = 4;
- indexData[6] = 1;indexData[7] = 3;indexData[8] = 4;
- indexData[9] = 3;indexData[10] = 6;indexData[11] = 4;
- indexData[12] = 5;indexData[13] = 2;indexData[14] = 4;
- indexData[15] = 5;indexData[16] = 4;indexData[17] = 7;
- indexData[18] = 6;indexData[19] = 7;indexData[20] = 4;
- indexData[21] = 7;indexData[22] = 6;indexData[23] = 8;
- indexBuffer.SetData(indexData, 0, LockFlags.None);
- }
-
- /*
- private void LoadTexturesAndMaterials()//导入贴图和材质
- {
- material = new Material();
- material.Diffuse = Color.White;
- material.Specular = Color.LightGray;
- material.SpecularSharpness = 15.0F;
- device.Material = material;
- texture = TextureLoader.FromFile(device, "CTGU.jpg");
- }
- */
- public void Render()
- {
- System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
- stopWatch.Start();
- if (device == null) //如果device为空则不渲染
- {
- return;
- }
- SetUpCamera();
- device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0); //清除windows界面为深蓝色
- device.BeginScene();
- //在此添加渲染图形代码
- //device.RenderState.CullMode=Cull.None;
- device.RenderState.Lighting = false;
- device.VertexFormat = CustomVertex.PositionColored.Format;
- //device.DrawUserPrimitives(PrimitiveType.TriangleList,8, vertices);
- device.RenderState.FillMode = FillMode.WireFrame;
- //device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, vertices.Length, indexData.Length/3, indexData, false, vertices);
- device.SetStreamSource(0, vertexBuffer,0);//设置顶点缓冲为绘图设备的数据流
- device.Indices = indexBuffer;//设置索引缓冲为绘图设备的索引数据
- //device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 9, 0, 8);
- //device.DrawPrimitives(PrimitiveType.TriangleList, 0, 8);
- //device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
- //device.DrawPrimitives(PrimitiveType.TriangleList, 18, 1);
- Vector3[] points = new Vector3[8];
- points[0] = new Vector3(30f, 20f, 0f);
- points[1] = new Vector3(30f, 20f, 10f);
- points[2] = new Vector3(40f, 20f, 10f);
- points[3] = new Vector3(40f, 20f, 0f);
- points[4] = new Vector3(30f, 10f, 0f);
- points[5] = new Vector3(30f, 10f, 10f);
- points[6] = new Vector3(40f, 10f, 10f);
- points[7] = new Vector3(40f, 10f, 0f);
- DrawHexClass drawTri = new DrawHexClass(device, points);
- //drawTri.DrawHex();
- /*
- Vector3 sphereCenter = new Vector3(10,10,0);
- DrawSphereClass drawSphere = new DrawSphereClass(device, sphereCenter, 50f, 50, 50);
- drawSphere.DrawSphere();
- */
- /*
- Vector3 circleCenter = new Vector3(10, 10, 10);
- Vector3 circleNormal = new Vector3(1, 1, 1);
- DrawCircleClass drawCircle = new DrawCircleClass(device, circleCenter, 10f, circleNormal, 16, 5);
- drawCircle.DrawCircle();
- */
- /*
- Vector3 taperCenter = new Vector3(10, 10, 10);
- Vector3 taperTop = new Vector3(15, 15, 15);
- DrawTaperClass drawTaper = new DrawTaperClass(device, taperCenter, 10f, taperTop, 16, 5);
- drawTaper.DrawTaper();
- */
- Vector3 topCenter = new Vector3(25, 25, 25);
- Vector3 underCenter = new Vector3(10, 10, 10);
- DrawCylinderClass drawCylinder = new DrawCylinderClass(device, topCenter, underCenter,10f, 16, 5,10);
- drawCylinder.DrawCylinder();
-
-
-
- device.EndScene();
- device.Present();
- stopWatch.Stop();
- Console.WriteLine(stopWatch.Elapsed.ToString());
- }
- private void SetUpCamera()//摄像机
- {
- Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
- device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 0.3f, 500f);
- device.Transform.View = viewMatrix;
- }
- protected override void OnKeyDown(KeyEventArgs e)
- {
- Vector4 tempV4;
- Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
- switch (e.KeyCode)
- {
- case Keys.Left:
- CamPostion.Subtract(CamTarget);
- tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
- Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), -angleY)));
- CamPostion.X = tempV4.X + CamTarget.X;
- CamPostion.Y = tempV4.Y + CamTarget.Y;
- CamPostion.Z = tempV4.Z + CamTarget.Z;
- break;
- case Keys.Right:
- CamPostion.Subtract(CamTarget);
- tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
- Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), angleY)));
- CamPostion.X = tempV4.X + CamTarget.X;
- CamPostion.Y = tempV4.Y + CamTarget.Y;
- CamPostion.Z = tempV4.Z + CamTarget.Z;
- break;
- case Keys.Up:
- CamPostion.Subtract(CamTarget);
- tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
- Quaternion.RotationAxis(new Vector3(device.Transform.View.M11
- , device.Transform.View.M21, device.Transform.View.M31), -angleY)));
- CamPostion.X = tempV4.X + CamTarget.X;
- CamPostion.Y = tempV4.Y + CamTarget.Y;
- CamPostion.Z = tempV4.Z + CamTarget.Z;
- break;
- case Keys.Down:
- CamPostion.Subtract(CamTarget);
- tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
- Quaternion.RotationAxis(new Vector3(device.Transform.View.M11
- , device.Transform.View.M21, device.Transform.View.M31), angleY)));
- CamPostion.X = tempV4.X + CamTarget.X;
- CamPostion.Y = tempV4.Y + CamTarget.Y;
- CamPostion.Z = tempV4.Z + CamTarget.Z;
- break;
- case Keys.Add:
- CamPostion.Subtract(CamTarget);
- CamPostion.Scale(0.95f);
- CamPostion.Add(CamTarget);
- break;
- case Keys.Subtract:
- CamPostion.Subtract(CamTarget);
- CamPostion.Scale(1.05f);
- CamPostion.Add(CamTarget);
- break;
- case Keys.Enter:
- Vector3[] points = new Vector3[8];
- points[0] = new Vector3(30f, 20f, 0f);
- points[1] = new Vector3(30f, 20f, 10f);
- points[2] = new Vector3(40f, 20f, 0f);
- points[3] = new Vector3(40f, 20f, 10f);
- points[4] = new Vector3(30f, 10f, 0f);
- points[5] = new Vector3(30f, 10f, 10f);
- points[6] = new Vector3(40f, 10f, 0f);
- points[7] = new Vector3(40f, 10f, 10f);
- DrawHexClass drawTri = new DrawHexClass(device, points);
- drawTri.DrawHex();
- break;
- }
- Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
- device.Transform.View = viewMatrix;
- }
- protected override void OnMouseDown(MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- mouseLastX = e.X;
- mouseLastY = e.Y;
- isRotateByMouse = true;
- }
- else if (e.Button == MouseButtons.Middle)
- {
- mouseLastX = e.X;
- mouseLastY = e.Y;
- isMoveByMouse=true;
- }
- }
-
- protected override void OnMouseUp(MouseEventArgs e)
- {
- isRotateByMouse = false;
- isMoveByMouse = false;
- }
- protected override void OnMouseMove(MouseEventArgs e)
- {
- if (isRotateByMouse)
- {
- Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
- float tempAngleY = 2 * (float)(e.X - mouseLastX) / this.Width;
- CamPostion.Subtract(CamTarget);
- Vector4 tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
- Quaternion.RotationAxis(new Vector3(currentView.M12, currentView.M22, currentView.M32), tempAngleY)));
- CamPostion.X = tempV4.X;
- CamPostion.Y = tempV4.Y;
- CamPostion.Z = tempV4.Z;
- float tempAngleX = 4 * (float)(e.Y - mouseLastY) / this.Height;
- tempV4 = Vector3.Transform(CamPostion, Matrix.RotationQuaternion(
- Quaternion.RotationAxis(new Vector3(currentView.M11, currentView.M21, currentView.M31), tempAngleX)));
- CamPostion.X = tempV4.X + CamTarget.X;
- CamPostion.Y = tempV4.Y + CamTarget.Y;
- CamPostion.Z = tempV4.Z + CamTarget.Z;
- Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
- device.Transform.View = viewMatrix;
- mouseLastX = e.X;
- mouseLastY = e.Y;
- }
- else if (isMoveByMouse)
- {
- Matrix currentView = device.Transform.View;//当前摄像机的视图矩阵
- float moveFactor=0.01f;
- CamTarget.X += -moveFactor * ((e.X - mouseLastX) * currentView.M11 - (e.Y - mouseLastY) * currentView.M12);
- CamTarget.Y += -moveFactor * ((e.X - mouseLastX) * currentView.M21 - (e.Y - mouseLastY) * currentView.M22);
- CamTarget.Z += -moveFactor * ((e.X - mouseLastX) * currentView.M31 - (e.Y - mouseLastY) * currentView.M32);
- CamPostion.X +=- moveFactor * ((e.X - mouseLastX) * currentView.M11 - (e.Y - mouseLastY) * currentView.M12);
- CamPostion.Y += -moveFactor * ((e.X - mouseLastX) * currentView.M21 - (e.Y - mouseLastY) * currentView.M22);
- CamPostion.Z += -moveFactor * ((e.X - mouseLastX) * currentView.M31 - (e.Y - mouseLastY) * currentView.M32);
-
- Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
- device.Transform.View = viewMatrix;
- mouseLastX = e.X;
- mouseLastY = e.Y;
- }
- }
- protected override void OnMouseWheel(MouseEventArgs e)
- {
- float scaleFactor = -(float)e.Delta / 2000 + 1f;
- CamPostion.Subtract(CamTarget);
- CamPostion.Scale(scaleFactor);
- CamPostion.Add(CamTarget);
- Matrix viewMatrix = Matrix.LookAtLH(CamPostion, CamTarget, new Vector3(0, 1, 0));
- device.Transform.View = viewMatrix;
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- Render(); //保持device渲染,直到程序结束
- }
- static void Main()
- {
- DrawBasicGeometry drawBasicGeometry = new DrawBasicGeometry(); //创建窗体对象
- if (drawBasicGeometry.InitializeDirect3D() == false) //检查Direct3D是否启动
- {
- MessageBox.Show("无法启动Direct3D!", "错误!");
- return;
- }
- drawBasicGeometry.Show(); //如果一切都初始化成功,则显示窗体
- while (drawBasicGeometry.Created)
- {
- drawBasicGeometry.Render(); //保持device渲染,直到程序结束
- Application.DoEvents();
- }
- }
- }
- }