最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的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. ExtJs中动态加载机制研究(转)

    觉得写的太好了,怕弄丢了,转一下:http://extjs.org.cn/node/659 昨天我们team对于extjs的动态加载机制做了些深入研究,这里先share下controller加载的结果 ...

  2. JQUERY 键盘事件

    一 一.首先需要知道的是: 1.keydown()keydown 事件会在键盘按下时触发. 2.keypress()keypress 事件会在敲击按键时触发,我们可以理解为按下并抬起同一个按键. 3. ...

  3. window 配置 sendmail

    从http://glob.com.au/sendmail/下载sendmail.zip 解压sendmail.zip到目录下(最好使用短路径,长路径会导致问题的出现),我安装的路径是: E:\wamp ...

  4. Aspnet MVC 异步调用

    一个简图来描述下Aspnet MVC下的异步调用 { request } / \/ -------ISS------- > work thread | \ | \ route - aysn co ...

  5. CSS lib

    Yahoo的轻型CSS框架Pure来加速web开发 http://purecss.io/buttons/#

  6. Git error- fatal- Needed a single revision

    最近在开发中由于项目结构的重构,有一部分代码被抽出来作为了公共库(git submodule),这样公共库可以独立维护,同时其他库调用它也是非常方便的,避免了到处复制代码的痛苦. 但我在项目重构后第一 ...

  7. shell笔记(基本知识)

    一.编写第一个shell程序 实例1: [程序] #!/bin/sh echo "hello word !" [运行]

  8. C# asp:Repeater DataSource List<T>

    1. asp:Repeater 数据源为List<T> 2.页面显示 3.行绑定取值

  9. MySQL ubuntu启动

    service mysql start 启动 service mysql restart 重启 service mysql stop 停止 mysql -uroot -ppassword 登入mysq ...

  10. JAVA存取对象属性时,如果开程多线程,记得对相关存取方法作原子化操作定义

    最显著的应用当然是银行存款和取款,不要存在存取数字和实际发生不一样的情况. synchronized关键字. class BankAccount { private int balance = 100 ...