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

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

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms; namespace SimpleDirect3DExample
{
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 SimpleDirect3DExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private Device device = null; //Device指显卡适配器,一个显卡至少有一个适配器
private VertexBuffer vertexBuffer = null; private Microsoft.DirectX.Direct3D.Font d3dfont; private string adapterInformationString;
private bool showAdapterString = true;
private float angle = 0.0f;
private float incrementAngle = 0.1f;
private bool enableRotator = true; 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 Form1_Load(object sender, EventArgs e)
{
//设置窗体显示方式
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
this.Width = 600;
this.Height = 500;
//为了能响应键盘事件,该属性必须设为true
this.KeyPreview = true;
adapterInformationString = "F1:显示/隐藏提示信息\r\n" +
"<F2>:旋转/不旋转\r\n" +
"上箭头:提高转速\n" +
"下箭头:降低转速\n" +
"<Esc>:退出\n\n\n";
AdapterDetails adapterDetails = Manager.Adapters.Default.Information;
adapterInformationString += string.Format(
"显卡驱动程序:{0}\n", adapterDetails.DriverName);
adapterInformationString += string.Format(
"显卡驱动程序版本:{0}\n", adapterDetails.DriverVersion);
DisplayMode displayMode = Manager.Adapters.Default.CurrentDisplayMode;
adapterInformationString += string.Format(
"显示器当前分辨率:{0} X {1}\n", displayMode.Width, displayMode.Height);
adapterInformationString += string.Format(
"显示器当前颜色质量:{0}\n", displayMode.Format);
adapterInformationString += string.Format(
"显示器当前刷新频率(Hz):{0}\n", displayMode.RefreshRate); //创建3D字体对象,显示字符用,这两句不能放在场景中,否则会很慢
System.Drawing.Font winFont = new System.Drawing.Font("宋体", 12, FontStyle.Regular);
d3dfont = new Microsoft.DirectX.Direct3D.Font(device, winFont);
d3dfont.PreloadText(adapterInformationString); //创建顶点缓冲
vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored),
3, device, Usage.Dynamic | Usage.WriteOnly,
CustomVertex.PositionColored.Format, Pool.Default);
vertexBuffer.Created += new EventHandler(OnVertexBufferCreate);
OnVertexBufferCreate(vertexBuffer, null); } private void OnVertexBufferCreate(object sender, EventArgs e)
{
//锁定顶点缓冲-->定义顶点-->解除锁定。
VertexBuffer buffer = (VertexBuffer)sender;
CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[])buffer.Lock(0, 0);
verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
verts[0].Color = Color.BlueViolet.ToArgb();
verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
verts[1].Color = Color.GreenYellow.ToArgb();
verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
verts[2].Color = Color.Red.ToArgb();
buffer.Unlock();
} private void SetupCamera()
{
//--------设置世界矩阵
Vector3 world = new Vector3(angle, angle / 2.0f, angle / 4.0f); device.Transform.World = Matrix.RotationAxis(world, angle);
if (enableRotator)
{
angle += incrementAngle / (float)(Math.PI);
}
//--------设置投影矩阵
//纵横比
float aspectRatio = 1;
//只能显示nearPlane到farPlane之间的场景
float nearPlane = 1;
float farPlane = 100;
//视界
float fieldOfView = (float)Math.PI / 4.0f;
device.Transform.Projection = Matrix.PerspectiveFovLH(fieldOfView, aspectRatio, nearPlane, farPlane);
//--------设置视图矩阵
Vector3 cameraPosition = new Vector3(0, 0, -5);
Vector3 cameraTarget = new Vector3(0, 0, 0);
Vector3 upDirection = new Vector3(0, 1, 0);
device.Transform.View = Matrix.LookAtLH(cameraPosition, cameraTarget, upDirection);
//--------不进行背面剔除
device.RenderState.CullMode = Cull.None;
//--------不要灯光
device.RenderState.Lighting = false;
} private void Form1_Paint(object sender, PaintEventArgs e)
{
device.Clear(ClearFlags.Target, System.Drawing.Color.AliceBlue, 1.0f, 0);
SetupCamera();
//--------------场景处理
device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.SetStreamSource(0, vertexBuffer, 0);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
if (showAdapterString == true)
{
d3dfont.DrawText(null, adapterInformationString, 25, 30, Color.Green);
}
device.EndScene();
//---------------发送场景
device.Present();
//---------------强制重新调用Form1_Paint事件
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:
showAdapterString = !showAdapterString;
break;
case Keys.F2:
enableRotator = !enableRotator;
break;
case Keys.Up:
if (enableRotator)
{
incrementAngle += 0.01f;
}
break;
case Keys.Down:
if (enableRotator && incrementAngle > 0.02f)
{
incrementAngle -= 0.01f;
}
break;
}
}
}
}

Directx 3D编程实例:绘制可变速旋转的三角形的更多相关文章

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

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

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

    最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的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 3D之入门

    环境配置以及背景知识 环境 Windows 8.1 64bit VS2013 Microsoft DirectX SDK (June 2010) NVDIA Geforce GT755 环境的配置參考 ...

  7. UWP简单示例(二):快速开始你的3D编程

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

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

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

  9. UWP简单示例(二):快速开始你的3D编程

    准备 IDE:Visual Studio 开源库:GitHub.SharpDx 入门示例:SharpDX_D3D12HelloWorld 为什么选择 SharpDx? SharpDx 库与 UWP 兼 ...

随机推荐

  1. PHP框架_ThinkPHP数据库

    目录 1.ThinkPHP数据库配置 2.ThinkPHP数据库实例化模型 3.ThinkPHP数据库CURD操作 4.ThinkPHP数据库连贯操作 1.ThinkPHP数据库配置 App/Conf ...

  2. js 强制转换

    强制转换为布尔类型: <script> var text =Boolean(0) //=>以下转换的类型都为false text = Boolean(0.0) text = Bool ...

  3. Android 应用层知识纲要

    Java基础 * 面向对象 * Java集合框架 * 异常处理 * Java反射, Spring框架,通过反射实现 * 泛型, 静态变成语言 * 文件操作 Android基础 * Activity * ...

  4. TatukGIS - GisDefs - CreateMSJET 函数

    函数名称  CreateMSJET 所在单元  GisDefs 函数原型           function CreateMSJET(const _path: String): String;   ...

  5. Python自动化运维之31、Tornado框架

    Tornado 官网:http://www.tornadoweb.org/en/stable/ Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本. ...

  6. 代码之美——Doom3源代码赏析2

    http://www.csdn.net/article/2013-01-17/2813778-the-beauty-of-doom3-source-code/2 摘要:Dyad作者.资深C++工程师S ...

  7. Gridview BottomPagerRow添加自定义控件

    pager.ascx public partial class pager : System.Web.UI.UserControl{ } .aspx protected pager ctl; prot ...

  8. Xamarin Mono错误: unable to find explicit activity class

    unable to find explicit activity class在android开发很常见,网上一般是java的解决办法,对我们这些xamariner就无语了. xamarin中用attr ...

  9. N.O.W,O.R,N.E.V.E.R--12days to LNOI2015

    双向链表 单调队列,双端队列 单调栈 堆 带权并查集 hash 表 双hash 树状数组 线段树合并 平衡树 Treap 随机平衡二叉树 Scapegoat Tree 替罪羊树 朝鲜树 块状数组,块状 ...

  10. [C#] - 注入DLL

    原文:http://xyzlht.blog.163.com/blog/static/69301417200882834211787/ ) { MessageBox.Show("创建远程线程失 ...