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

第一步:判断显卡是否支持

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

第二步:程序源码

  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 _3DMesh
  15. {
  16. public partial class Form1 : Form
  17. {
  18. private Device device = null;
  19. private Mesh boxMesh = null;
  20. private Mesh cylinderMesh = null;
  21. private Mesh polygonMesh = null;
  22. private Mesh sphereMesh = null;
  23. private Mesh teapotMesh = null;
  24. private Mesh torusMesh = null;
  25. private Mesh textMesh = null;
  26. private Microsoft.DirectX.Direct3D.Font d3dfont;
  27. private string helpString;
  28. private bool showHelpString = true;
  29. private float angle = 0.0f;
  30. private int rotatorXYZ = 0;
  31. private bool enableRotator = true;
  32. private bool enableCullMode = false;
  33. private bool enableSolidMode = false;
  34.  
  35. public Form1()
  36. {
  37. InitializeComponent();
  38. }
  39. public bool InitializeGraphics()
  40. {
  41. try
  42. {
  43. PresentParameters presentParams = new PresentParameters();
  44. presentParams.Windowed = true;
  45. presentParams.SwapEffect = SwapEffect.Discard;
  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.  
  56. private void SetupCamera()
  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. Vector3 cameraPosition = new Vector3(0, 0, -18.0f);
  65. Vector3 cameraTarget = new Vector3(0, 0, 0);
  66. Vector3 upDirection = new Vector3(0, 1, 0);
  67. device.Transform.View = Matrix.LookAtLH(cameraPosition, cameraTarget, upDirection);
  68. device.RenderState.Lighting = false;
  69. device.RenderState.CullMode = (enableCullMode ? Cull.CounterClockwise : Cull.None);
  70. device.RenderState.FillMode = (enableSolidMode ? FillMode.Solid : FillMode.WireFrame);
  71. }
  72.  
  73. private void SetWorldTransform(float x, float y, float z)
  74. {
  75. Vector3 world;
  76. if (rotatorXYZ == 0)
  77. {
  78. world = new Vector3(angle, 0, 0);
  79. }
  80. else if (rotatorXYZ == 1)
  81. {
  82. world = new Vector3(0, angle, 0);
  83. }
  84. else
  85. {
  86. world = new Vector3(0, 0, angle);
  87. }
  88. device.Transform.World = Matrix.RotationAxis(world, angle) * Matrix.Translation(x, y, z);
  89. if (enableRotator == true)
  90. {
  91. angle += 0.03f / (float)Math.PI;
  92. }
  93. }
  94.  
  95. private void Form1_Load(object sender, EventArgs e)
  96. {
  97. this.Width = 575;
  98. this.Height = 475;
  99. this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
  100. this.KeyPreview = true;
  101. helpString = "<Esc>:退出\n\n\n" +
  102. "<F1>:显示/隐藏提示信息\n" +
  103. "<F2>:实心/线框\n" +
  104. "<F3>:旋转/不旋转\n" +
  105. "<F4>:旋转轴(x轴、y轴、z轴)\n" +
  106. "<F5>:背景剔除/不剔除\n\n";
  107. System.Drawing.Font winFont = new System.Drawing.Font("宋体", 9, FontStyle.Regular);
  108. d3dfont = new Microsoft.DirectX.Direct3D.Font(device, winFont);
  109. d3dfont.PreloadText(helpString);
  110. textMesh = Mesh.TextFromFont(
  111. device, new System.Drawing.Font("宋体", 12), "Mesh对象展示", 1.0f, 0.3f);
  112. boxMesh = Mesh.Box(device, 2.0f, 2.0f, 2.0f);
  113. cylinderMesh = Mesh.Cylinder(device, 1.0f, 0.5f, 2.0f, 10, 10);
  114. polygonMesh = Mesh.Polygon(device, 0.4f, 20);
  115. sphereMesh = Mesh.Sphere(device, 1.0f, 20, 10);
  116. teapotMesh = Mesh.Teapot(device);
  117. torusMesh = Mesh.Torus(device, 0.3f, 1.0f, 20, 20);
  118. }
  119.  
  120. private void Form1_Paint(object sender, PaintEventArgs e)
  121. {
  122. SetupCamera();
  123. device.Clear(ClearFlags.Target, Color.DarkSeaGreen, 1.0f, 0);
  124. device.BeginScene();
  125. // 绘制文本
  126. SetWorldTransform(0, 5, 0);
  127. textMesh.DrawSubset(0);
  128. // 绘制立方体
  129. SetWorldTransform(-5, 0, 0);
  130. boxMesh.DrawSubset(0);
  131. // 绘制圆筒
  132. SetWorldTransform(0, 0, 0);
  133. cylinderMesh.DrawSubset(0);
  134. // 绘制多边形
  135. SetWorldTransform(5, 0, 0);
  136. polygonMesh.DrawSubset(0);
  137. // 绘制球形
  138. SetWorldTransform(-5, -5, 0);
  139. sphereMesh.DrawSubset(0);
  140. // 绘制茶壶
  141. SetWorldTransform(0, -5, 0);
  142. teapotMesh.DrawSubset(0);
  143. // 绘制圆环
  144. SetWorldTransform(5, -5, 0);
  145. torusMesh.DrawSubset(0);
  146. if (showHelpString == true)
  147. {
  148. d3dfont.DrawText(null, helpString, 25, 30, Color.Tomato);
  149. }
  150. device.EndScene();
  151. device.Present();
  152. if (WindowState != FormWindowState.Minimized)
  153. {
  154. this.Invalidate();
  155. }
  156. }
  157.  
  158. private void Form1_KeyDown(object sender, KeyEventArgs e)
  159. {
  160. switch (e.KeyCode)
  161. {
  162. case Keys.Escape:
  163. this.Close();
  164. break;
  165. case Keys.F1:
  166. showHelpString = !showHelpString;
  167. break;
  168. case Keys.F2:
  169. enableSolidMode = !enableSolidMode;
  170. break;
  171. case Keys.F3:
  172. enableRotator = !enableRotator;
  173. break;
  174. case Keys.F4:
  175. rotatorXYZ = (rotatorXYZ + 1) % 3;
  176. break;
  177. case Keys.F5:
  178. enableCullMode = !enableCullMode;
  179. break;
  180. }
  181.  
  182. }
  183. }
  184. }

Directx 3D编程实例:绘制3DMesh的更多相关文章

  1. Directx 3D编程实例:随机绘制的立体图案旋转

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

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

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

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

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

  4. OpenGL学习进程(9)在3D空间的绘制实例

        本节将演示在3D空间中绘制图形的几个简单实例:     (1)在3D空间内绘制圆锥体: #include <GL/glut.h> #include <math.h> # ...

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

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

  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. UWP简单示例(二):快速开始你的3D编程

    准备 IDE:Visual Studio 2015 了解并学习:SharpDx官方GitHub 推荐Demo:SharpDX_D3D12HelloWorld 第一节 世界 世界坐标系是一个特殊的坐标系 ...

  9. PHP多进程编程实例

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

随机推荐

  1. css media

    /* media */ /* 横屏 */ @media screen and (orientation:landscape){ } /* 竖屏 */ @media screen and (orient ...

  2. 226. Invert Binary Tree(C++)

    226. Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 ...

  3. 【转】 C语言自增自减运算符深入剖析

    转自:http://bbs.csdn.net/topics/330189207 C语言的自增++,自减--运算符对于初学者来说一直都是个难题,甚至很多老手也会产生困惑,最近我在网上看到一个问题:#in ...

  4. Winform控件Enable=false显示优化

    在B/S开发中(ASP.NET),往往可以css样式表来让页面控件更加美观,但是在C/S中(Winform)里面,我们则需要通过其他取巧的 方式来实现.例如:当你因为某个需求需要将控件设置为Reado ...

  5. ASP.Net数据导出Excel的几种方法

    方法一 通过GridView(简评:方法比较简单,但是只适合生成格式简单的Excel,且无法保留VBA代码),页面无刷新 aspx.cs部分 代码如下: using System; using Sys ...

  6. srand((double)microtime()*1000000)

    分为4个步骤1:执行microtime(),获取当前的微秒数 2:把获取的微秒数转换为double类型 3:再用转换后的数字去乘以1000000 4:给随机数发生器播种,播种数为第三步得出的结果 ra ...

  7. webapp开发——‘手机屏幕分辨率’与‘浏览器分辨率’不要混淆

    关于webApp响应式设计遇到的问题,分享给大家,最近在做一个手机webApp,因为我手机是”米3“,屏幕截图大小是1080宽,所以css样式用@media screen and(min-width: ...

  8. IIS10 设置支持wcf服务(.svc)

    感谢: http://www.cnblogs.com/dudu/p/3328066.html 如果提示web.config配置重复的话,很有可能是.net framework版本的问题,把IIS中的版 ...

  9. 用C++进行函数式编程

    http://www.programmer.com.cn/12717/   文 / John Carmack 译 / 王江平 <Quake>作者Carmack认为追求函数式的程序设计有着实 ...

  10. Solr4.8.0源码分析(23)之SolrCloud的Recovery策略(四)

    Solr4.8.0源码分析(23)之SolrCloud的Recovery策略(四) 题记:本来计划的SolrCloud的Recovery策略的文章是3篇的,但是没想到Recovery的内容蛮多的,前面 ...