最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档。于是我也觉的有这个必要。

写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博客。

第一步:修改Program.cs,主要是判断显卡支不支持

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows.Forms;
  6. namespace _3DPrimitive
  7. {
  8. static class Program
  9. {
  10. /// <summary>
  11. /// The main entry point for the application.
  12. /// </summary>
  13. [STAThread]
  14. static void Main()
  15. {
  16. Application.EnableVisualStyles();
  17. Application.SetCompatibleTextRenderingDefault(false);
  18. Form1 form1 = new Form1();
  19. if (form1.InitializeGraphics() == false)
  20. {
  21. MessageBox.Show("显卡不支持3D或者未安装配套的显卡驱动程序!");
  22. }
  23. Application.Run(form1);
  24. }
  25. }
  26. }

第二步:主程序代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. using Microsoft.DirectX;
  12. using Microsoft.DirectX.Direct3D;
  13.  
  14. namespace _3DPrimitive
  15. {
  16. public partial class Form1 : Form
  17. {
  18. private Device device = null;
  19. private VertexBuffer vertexBuffer = null;
  20. private const int vertexNumber = 18;
  21. private string primitivesType = "点列";
  22. private Microsoft.DirectX.Direct3D.Font d3dfont;
  23. private string helpString;
  24. private bool showHelpString = true;
  25. private float angle = 0.0f;
  26. private bool enableRotator = false;
  27. private int rotatorXYZ = 0;
  28. private bool enableCullMode = false;
  29.  
  30. public Form1()
  31. {
  32. InitializeComponent();
  33. }
  34.  
  35. public bool InitializeGraphics()
  36. {
  37. try
  38. {
  39. PresentParameters presentParams = new PresentParameters();
  40. presentParams.Windowed = true;
  41. presentParams.SwapEffect = SwapEffect.Discard;
  42.  
  43. presentParams.AutoDepthStencilFormat=DepthFormat.D16;
  44. presentParams.EnableAutoDepthStencil=true;
  45.  
  46. device = new Device(0, DeviceType.Hardware, this,
  47. CreateFlags.SoftwareVertexProcessing, presentParams);
  48. return true;
  49. }
  50. catch (DirectXException)
  51. {
  52. return false;
  53. }
  54. }
  55. private void SetupCamera()
  56. {
  57. //设置投影矩阵
  58. float fieldOfView = (float)Math.PI / 4;
  59. float aspectRatio = this.Width / this.Height;
  60. float nearPlane = 1.0f;
  61. float farPlane = 100.0f;
  62. device.Transform.Projection =
  63. Matrix.PerspectiveFovLH(fieldOfView, aspectRatio, nearPlane, farPlane);
  64. //设置视图矩阵
  65. Vector3 cameraPosition = new Vector3(0, 0, -30.0f);
  66. Vector3 cameraTarget = new Vector3(0, 0, 0);
  67. Vector3 upDirection = new Vector3(0, 1, 0);
  68. device.Transform.View = Matrix.LookAtLH(cameraPosition, cameraTarget, upDirection);
  69. //点的大小
  70. device.RenderState.PointSize = 4.0f;
  71. //不要灯光
  72. device.RenderState.Lighting = false;
  73. //背面剔除
  74. if (enableCullMode == true)
  75. {
  76. device.RenderState.CullMode = Cull.CounterClockwise;
  77. }
  78. else
  79. {
  80. device.RenderState.CullMode = Cull.None;
  81. }
  82. }
  83. private void OnVertexBufferCreate(object sender, EventArgs e)
  84. {
  85. //锁定顶点缓冲-->定义顶点-->解除锁定。
  86. VertexBuffer buffer = (VertexBuffer)sender;
  87. CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[])buffer.Lock(0, 0);
  88. Random random = new Random();
  89. for (int i = 0; i < vertexNumber; i++)
  90. {
  91. float x = (float)(vertexNumber * (random.NextDouble() - 0.5f));
  92. float y = (float)(vertexNumber * (random.NextDouble() - 0.5f));
  93. float z = (float)(vertexNumber * (random.NextDouble() - 0.5f));
  94. verts[i].Position = new Vector3(x, y, z);
  95. Color color = Color.FromArgb(random.Next(byte.MaxValue),
  96. random.Next(byte.MaxValue), random.Next(byte.MaxValue));
  97. verts[i].Color = color.ToArgb();
  98. }
  99. buffer.Unlock();
  100. }
  101.  
  102. private void Form1_Load(object sender, EventArgs e)
  103. {
  104. this.Width = 570;
  105. this.Height = 430;
  106. this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
  107. this.KeyPreview = true;
  108. helpString = "<Esc>:退出\n\n\n<F1>:显示/隐藏提示信息\n" +
  109. "<F2>:变换顶点\n" +
  110. "<F3>:旋转/不旋转\n" +
  111. "<F4>:旋转轴(x轴、y轴、z轴)\n" +
  112. "<F5>:背面剔除/不剔除\n\n" +
  113. "<1>:点列\n" +
  114. "<2>:线列\n" +
  115. "<3>:线带\n" +
  116. "<4>:三角形列\n" +
  117. "<5>:三角形带\n" +
  118. "<6>:三角扇形\n";
  119. System.Drawing.Font winFont = new System.Drawing.Font("宋体", 9, FontStyle.Regular);
  120. d3dfont = new Microsoft.DirectX.Direct3D.Font(device, winFont);
  121. d3dfont.PreloadText(helpString);
  122. //创建顶点缓冲
  123. vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored),
  124. vertexNumber,
  125. device, Usage.Dynamic | Usage.WriteOnly,
  126. CustomVertex.PositionColored.Format, Pool.Default);
  127. vertexBuffer.Created += new EventHandler(OnVertexBufferCreate);
  128. OnVertexBufferCreate(vertexBuffer, null);
  129. }
  130.  
  131. private void Form1_Paint(object sender, PaintEventArgs e)
  132. {
  133. SetupCamera();
  134. // device.Clear(ClearFlags.Target, System.Drawing.Color.AliceBlue, 1.0f, 0);
  135. device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.AliceBlue, 1.0f, 0);
  136.  
  137. device.BeginScene();
  138. device.VertexFormat = CustomVertex.PositionColored.Format;
  139. device.SetStreamSource(0, vertexBuffer, 0);
  140. switch (primitivesType)
  141. {
  142. case "点列":
  143. device.DrawPrimitives(PrimitiveType.PointList, 0, vertexNumber);
  144. break;
  145. case "线列":
  146. device.DrawPrimitives(PrimitiveType.LineList, 0, vertexNumber / 2);
  147. break;
  148. case "线带":
  149. device.DrawPrimitives(PrimitiveType.LineStrip, 0, vertexNumber - 2);
  150. break;
  151. case "三角形列":
  152. device.DrawPrimitives(PrimitiveType.TriangleList, 0, vertexNumber / 3);
  153. break;
  154. case "三角形带":
  155. device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, vertexNumber - 2);
  156. break;
  157. case "三角扇形":
  158. device.DrawPrimitives(PrimitiveType.TriangleFan, 0, vertexNumber - 2);
  159. break;
  160. }
  161. if (showHelpString == true)
  162. {
  163. d3dfont.DrawText(null, helpString, 25, 30, Color.Green);
  164. }
  165. if (enableRotator == true)
  166. {
  167. Vector3 world;
  168. if (rotatorXYZ == 0)
  169. {
  170. world = new Vector3(angle, 0, 0);
  171. }
  172. else if (rotatorXYZ == 1)
  173. {
  174. world = new Vector3(0, angle, 0);
  175. }
  176. else
  177. {
  178. world = new Vector3(0, 0, angle);
  179. }
  180. device.Transform.World = Matrix.RotationAxis(world, angle);
  181. angle += 0.05f / (float)Math.PI;
  182. }
  183. device.EndScene();
  184. device.Present();
  185. if (WindowState != FormWindowState.Minimized)
  186. {
  187. this.Invalidate();
  188. }
  189.  
  190. }
  191.  
  192. private void Form1_KeyDown(object sender, KeyEventArgs e)
  193. {
  194. switch (e.KeyCode)
  195. {
  196. case Keys.Escape:
  197. this.Close();
  198. break;
  199. case Keys.F1:
  200. showHelpString = !showHelpString;
  201. break;
  202. case Keys.F2:
  203. OnVertexBufferCreate(vertexBuffer, null);
  204. break;
  205. case Keys.F3:
  206. enableRotator = !enableRotator;
  207. break;
  208. case Keys.F4:
  209. rotatorXYZ = (rotatorXYZ + 1) % 3;
  210. break;
  211. case Keys.F5:
  212. enableCullMode = !enableCullMode;
  213. break;
  214. case Keys.D1:
  215. primitivesType = "点列";
  216. break;
  217. case Keys.D2:
  218. primitivesType = "线列";
  219. break;
  220. case Keys.D3:
  221. primitivesType = "线带";
  222. break;
  223. case Keys.D4:
  224. primitivesType = "三角形列";
  225. break;
  226. case Keys.D5:
  227. primitivesType = "三角形带";
  228. break;
  229. case Keys.D6:
  230. primitivesType = "三角扇形";
  231. break;
  232. }
  233.  
  234. }
  235. }
  236. }

Directx 3D编程实例:随机绘制的立体图案旋转的更多相关文章

  1. Directx 3D编程实例:绘制3DMesh

    最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要.写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博客 ...

  2. Directx 3D编程实例:绘制可变速旋转的三角形

    最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要. 写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博 ...

  3. Directx 3D编程实例:多个3D球的综合Directx实例

    最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要.写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博客 ...

  4. 开始3D编程前需注意的十件事

    http://www.csdn.net/article/2013-06-21/2815949-3d-programming 原文作者Vasily Tserekh是名3D编程爱好者,他发表了一篇博文&l ...

  5. PHP多进程编程实例

    这篇文章主要介绍了PHP多进程编程实例,本文讲解的是在Linux下实现PHP多进程编程,需要的朋友可以参考下 羡慕火影忍者里鸣人的影分身么?没错,PHP程序是可以开动影分身的!想完成任务,又觉得一个进 ...

  6. DirectX API 编程起步 #01 项目设置

    =========================================================== 目录: DirectX API 编程起步 #02 窗口的诞生 DirectX A ...

  7. QT Graphics-View 3D编程例子- 3D Model Viewer

    学习在Graphics-View框架中使用opengl进行3D编程,在网上找了一个不错的例子“3D Model Viewer”,很值得学习. 可以在http://www.oyonale.com/acc ...

  8. 如何在ChemDraw中绘制分子立体结构

    ChemDraw是当前最常用的的化学结构绘图软件,软件功能包括化学作图.分子模型生成.化学数据库信息管理等,可以说是化学家和生物学家所需要最终极的化学结构绘图工具.本教程主要介绍ChemDraw绘制分 ...

  9. 近中期3D编程研究目标

    近几年一直在用业余时间研究3D编程,研究的中期目标是建立一个实用的开源3D编程框架.3D编程技术最直接的应用是开发游戏,所以3D编程框架也就是3D游戏开发框架.在我看来,游戏是否好玩的关键是能否为玩家 ...

随机推荐

  1. Plsql工具单步调试 存储过程或是 函数(oracle数据库)-留着自己用的

    <案例1> 原地址: http://jingyan.baidu.com/article/3a2f7c2e144d2826aed61167.html 调试过程对找到一个存过的bug或错误是非 ...

  2. IOS常用开源库

    转自:http://www.csdn.net/article/2013-06-18/2815806-GitHub-iOS-open-source-projects-two/1 1. AFNetwork ...

  3. HTML页面中启用360浏览器极速模式

    今天做页面突然遇到浏览器一直在兼容模式下运行,体验不好,通过查询文档,在<head>中加入<meta name="renderer" content=" ...

  4. (JAVA)从零开始之--对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)

    对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流:  Ob ...

  5. JavaMail API 1.4.7邮件发送

    下载oracle javaMail API: http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive- ...

  6. CSS的inherit与auto使用分析

    一个很显浅的寓言,千年老树,电打雷劈,屹立不倒,却毁于蝼蚁的侵袭之下.自以为精通CSS的人,常常被一些小问题搞到头晕脑胀. 通常是一个很小的数值,经过层层放大歪曲后,整个布局就走形了.CSS是一门很简 ...

  7. MVC埰坑日记 文件权限

    public static void DownLoadFile(string FileFullPath) { if (!string.IsNullOrEmpty(FileFullPath) & ...

  8. gzip命令

    http://www.cnblogs.com/peida/archive/2012/12/06/2804323.html 减 少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时, ...

  9. js 表达式与运算符 详解(下)

    比较运算符: > .>= .<. <=.  ==. !=. ===. !==. 比较运算符的结果都为布尔值 ==只比较值是否相等    而    ===比较的是值和数据类型都要 ...

  10. php微信支付接口开发程序

    php微信支付接口开发程序讲解 微信支付接口现在也慢慢的像支付宝一个可以利用api接口来实现第三方网站或应用进行支付了, 下文整理了一个php微信支付接口开发程序并且己测试,有兴趣的朋友可进入参考. ...