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

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms; namespace _3DMesh
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 frm = new Form1();
if (frm.InitializeGraphics() == false)
{
MessageBox.Show("显卡不支持3D或者未安装配套的显卡驱动程序!");
return;
}
Application.Run(frm);
}
}
}

第二步:程序源码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D; namespace _3DMesh
{
public partial class Form1 : Form
{
private Device device = null;
private Mesh boxMesh = null;
private Mesh cylinderMesh = null;
private Mesh polygonMesh = null;
private Mesh sphereMesh = null;
private Mesh teapotMesh = null;
private Mesh torusMesh = null;
private Mesh textMesh = null;
private Microsoft.DirectX.Direct3D.Font d3dfont;
private string helpString;
private bool showHelpString = true;
private float angle = 0.0f;
private int rotatorXYZ = 0;
private bool enableRotator = true;
private bool enableCullMode = false;
private bool enableSolidMode = false; public Form1()
{
InitializeComponent();
}
public bool InitializeGraphics()
{
try
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this,
CreateFlags.SoftwareVertexProcessing, presentParams);
return true;
}
catch (DirectXException)
{
return false;
}
} private void SetupCamera()
{
float fieldOfView = (float)Math.PI / 4;
float aspectRatio = this.Width / this.Height;
float nearPlane = 1.0f;
float farPlane = 100.0f;
device.Transform.Projection =
Matrix.PerspectiveFovLH(fieldOfView, aspectRatio, nearPlane, farPlane);
Vector3 cameraPosition = new Vector3(0, 0, -18.0f);
Vector3 cameraTarget = new Vector3(0, 0, 0);
Vector3 upDirection = new Vector3(0, 1, 0);
device.Transform.View = Matrix.LookAtLH(cameraPosition, cameraTarget, upDirection);
device.RenderState.Lighting = false;
device.RenderState.CullMode = (enableCullMode ? Cull.CounterClockwise : Cull.None);
device.RenderState.FillMode = (enableSolidMode ? FillMode.Solid : FillMode.WireFrame);
} private void SetWorldTransform(float x, float y, float z)
{
Vector3 world;
if (rotatorXYZ == 0)
{
world = new Vector3(angle, 0, 0);
}
else if (rotatorXYZ == 1)
{
world = new Vector3(0, angle, 0);
}
else
{
world = new Vector3(0, 0, angle);
}
device.Transform.World = Matrix.RotationAxis(world, angle) * Matrix.Translation(x, y, z);
if (enableRotator == true)
{
angle += 0.03f / (float)Math.PI;
}
} private void Form1_Load(object sender, EventArgs e)
{
this.Width = 575;
this.Height = 475;
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
this.KeyPreview = true;
helpString = "<Esc>:退出\n\n\n" +
"<F1>:显示/隐藏提示信息\n" +
"<F2>:实心/线框\n" +
"<F3>:旋转/不旋转\n" +
"<F4>:旋转轴(x轴、y轴、z轴)\n" +
"<F5>:背景剔除/不剔除\n\n";
System.Drawing.Font winFont = new System.Drawing.Font("宋体", 9, FontStyle.Regular);
d3dfont = new Microsoft.DirectX.Direct3D.Font(device, winFont);
d3dfont.PreloadText(helpString);
textMesh = Mesh.TextFromFont(
device, new System.Drawing.Font("宋体", 12), "Mesh对象展示", 1.0f, 0.3f);
boxMesh = Mesh.Box(device, 2.0f, 2.0f, 2.0f);
cylinderMesh = Mesh.Cylinder(device, 1.0f, 0.5f, 2.0f, 10, 10);
polygonMesh = Mesh.Polygon(device, 0.4f, 20);
sphereMesh = Mesh.Sphere(device, 1.0f, 20, 10);
teapotMesh = Mesh.Teapot(device);
torusMesh = Mesh.Torus(device, 0.3f, 1.0f, 20, 20);
} private void Form1_Paint(object sender, PaintEventArgs e)
{
SetupCamera();
device.Clear(ClearFlags.Target, Color.DarkSeaGreen, 1.0f, 0);
device.BeginScene();
// 绘制文本
SetWorldTransform(0, 5, 0);
textMesh.DrawSubset(0);
// 绘制立方体
SetWorldTransform(-5, 0, 0);
boxMesh.DrawSubset(0);
// 绘制圆筒
SetWorldTransform(0, 0, 0);
cylinderMesh.DrawSubset(0);
// 绘制多边形
SetWorldTransform(5, 0, 0);
polygonMesh.DrawSubset(0);
// 绘制球形
SetWorldTransform(-5, -5, 0);
sphereMesh.DrawSubset(0);
// 绘制茶壶
SetWorldTransform(0, -5, 0);
teapotMesh.DrawSubset(0);
// 绘制圆环
SetWorldTransform(5, -5, 0);
torusMesh.DrawSubset(0);
if (showHelpString == true)
{
d3dfont.DrawText(null, helpString, 25, 30, Color.Tomato);
}
device.EndScene();
device.Present();
if (WindowState != FormWindowState.Minimized)
{
this.Invalidate();
}
} private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Escape:
this.Close();
break;
case Keys.F1:
showHelpString = !showHelpString;
break;
case Keys.F2:
enableSolidMode = !enableSolidMode;
break;
case Keys.F3:
enableRotator = !enableRotator;
break;
case Keys.F4:
rotatorXYZ = (rotatorXYZ + 1) % 3;
break;
case Keys.F5:
enableCullMode = !enableCullMode;
break;
} }
}
}

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. IOS 中得runloop 详细解释

    1.Runloop基础知识- 1.1 字面意思 a 运行循环 b 跑圈 - 1.2 基本作用(作用重大) a 保持程序的持续运行(ios程序为什么能一直活着不会死) b 处理app中的各种事件(比如触 ...

  2. 在xcode6.1和ios10.10.1环境下实现app发布

    之前写过在xcode6.1和ios10.10.1环境下实现真机测试,以及最近提交的app一直在审核当中,所以木有发布如何实现app发布来分享给大家.刚好昨天app审核通过了,所以就分享一篇如何实现ap ...

  3. 【HOJ2430】【贪心+树状数组】 Counting the algorithms

    As most of the ACMers, wy's next target is algorithms, too. wy is clever, so he can learn most of th ...

  4. javascript——迭代方法

    <script type="text/javascript"> //五个迭代方法 都接受两个参数:要在每一项上运行的函数 和 运行该函数的作用域(可选) //every ...

  5. NPOI读写Excel0307

    #region NPOI 操作 Excel 2007 /// <summary> /// 将Excel文件中的数据读出到DataTable中(xlsx) /// </summary& ...

  6. sphinx ---rotate 运行机制

    如果sphinx在运行中,要indexer时,需要加上--rotate参数,这样索引完就直接生效了. 原因是sphinx的searchd在启动时会创建一个 .spl 锁文件,并在关闭时会删除它.在in ...

  7. php获取https下的内容

    直接用file_get_contents,会报错: $url = ('https://xxx.com"); file_get_contents($url); 错误: Warning: fil ...

  8. Ubuntu14.04 安装QQ(国际版)

    1.在/etc/apt/source.list文件中添加: deb http://packages.linuxdeepin.com/deepin trusty main non-free univer ...

  9. ios 排序汇总

    ios 排序汇总  IOS几种简单有效的数组排序方法 //第一种,利用数组的sortedArrayUsingComparator调用 NSComparator ,obj1和obj2指的数组中的对象 N ...

  10. Hybrid UI framework shootout: Ionic vs. Famo.us vs. F7 vs. OnsenUI

    1 Introduction In the past 2 years I’ve been working intensively on mobile applications, mostly hybr ...