Camera.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:4k
源码类别:

DirextX编程

开发平台:

C#

  1. ////////////////////////////////////////////////////////////////////////
  2. //      ■■■■     ■■■■■       ■■■■       ■       ■      //
  3. //    ■                 ■         ■               ■       ■      //
  4. //    ■                 ■         ■    ■■■     ■       ■      //
  5. //    ■                 ■         ■       ■      ■       ■      //
  6. //      ■■■■         ■           ■■■■         ■■■■       //
  7. // Copyright (c) 三峡大学水利与环境学院 肖泽云. All rights reserved.  //
  8. ////////////////////////////////////////////////////////////////////////
  9. using System;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.Windows.Forms;
  14. using Microsoft.DirectX;
  15. using Microsoft.DirectX.Direct3D;
  16. namespace 摄像机
  17. {
  18.     public partial class Camera : Form
  19.     {
  20.         Device device = null;//定义绘图设备
  21.         public Camera()
  22.         {
  23.             this.ClientSize = new Size(800, 600);//指定窗体尺寸
  24.             this.Text = "摄像机";//指定窗体标题
  25.         }
  26.         public bool InitializeDirect3D()
  27.         {
  28.             try
  29.             {
  30.                 PresentParameters presentParams = new PresentParameters();
  31.                 presentParams.Windowed = true; //指定以Windows窗体形式显示
  32.                 presentParams.SwapEffect = SwapEffect.Discard; //当前屏幕绘制后它将自动从内存中删除
  33.                 device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); //实例化device对象
  34.                 return true;
  35.             }
  36.             catch (DirectXException e)
  37.             {
  38.                 MessageBox.Show(e.ToString(), "Error"); //处理异常
  39.                 return false;
  40.             }
  41.         }
  42.         public void Render()
  43.         {
  44.             if (device == null)   //如果device为空则不渲染
  45.             {
  46.                 return;
  47.             }
  48.             Vector3 eye = new Vector3(30, 0, -30);
  49.             Vector3 at = new Vector3(0, 0, 0);
  50.             Vector3 up = new Vector3(0, 1, 0);
  51.             Matrix viewMatrix = Matrix.LookAtLH(eye, at, up);
  52.             Matrix projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width/this.Height, 1.0f, 50.0f);
  53.             device.Transform.Projection = projection;
  54.             device.Transform.View = viewMatrix;
  55.             device.RenderState.FillMode = FillMode.WireFrame;
  56.             device.RenderState.Lighting = false;
  57.             device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0);  //清除windows界面为深蓝色
  58.             device.BeginScene();
  59.             //在此添加渲染图形代码
  60.             CustomVertex.PositionColored[] vertices = new CustomVertex.PositionColored[3];//定义顶点
  61.             vertices[0].Position = new Vector3(0f, 0f, 0f);
  62.             vertices[0].Color = Color.Red.ToArgb();
  63.             vertices[1].Position = new Vector3(5f, 10f, 0f);
  64.             vertices[1].Color = Color.Green.ToArgb();
  65.             vertices[2].Position = new Vector3(10f, 0f, 0f);
  66.             vertices[2].Color = Color.Yellow.ToArgb();
  67.             device.VertexFormat = CustomVertex.PositionColored.Format;
  68.             device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
  69.             
  70.             device.EndScene();
  71.             device.Present();
  72.         }
  73.         static void Main()
  74.         {
  75.             Camera Camera = new Camera(); //创建窗体对象
  76.             if (Camera.InitializeDirect3D() == false) //检查Direct3D是否启动
  77.             {
  78.                 MessageBox.Show("无法启动Direct3D!", "错误!");
  79.                 return;
  80.             }
  81.             Camera.Show(); //如果一切都初始化成功,则显示窗体
  82.             while (Camera.Created) //设置一个循环用于实时更新渲染状态
  83.             {
  84.                 Camera.Render(); //保持device渲染,直到程序结束
  85.                 Application.DoEvents(); //处理键盘鼠标等输入事件
  86.             }
  87.         }
  88.     }
  89. }