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

    在软件系统开发中经常回会遇到这样的情况,你实现了一些接口(模块),而这些接口(模块)都分布在几个类中(比如 A和 B.C.D) :A中实现了一些接口,B 中实现一些接口(或者 A代表一个独立模块,B. ...

  2. opencv 常用函数介绍

    ××××××××××××××××××××××××××××××××××××××× CvScalar imgmean,imgstd; double imgmax,imgmin; cvAvgSdv(img, ...

  3. SGU 193.Chinese Girls' Amusement

    /* 实际上就是求一个k,满足k<=n/2,且gcd(n,k)=1 如果n为奇数,k为[n/2] 如果n为偶数,k=n/2-1-(n/2)%2 */ #include <iostream& ...

  4. 下拉框——把一个select框中选中内容移到另一个select框中遇到的问题

    在使用jQuery实现把一个select框中选中内容移到另一个select框中功能时遇到了一个问题,就是点击按钮时内容可以到另一个select框中,但是到了另一个select框中的内容却很快闪退回原来 ...

  5. [转]iframe自适应宽度高度

    <iframe id="iframe" onLoad="AutoFit();" frameborder="0" scrolling=& ...

  6. 前端,移动开发者,UI须懂: 不同设备的之间的尺寸

    在开发前端,移动APP,以及设计UI的时候,我们经常会去搜索不同设备之间的尺寸,来开始自己的工作,以保证显示效果达到更好,这里收集了现在常用的设备. 设备更新速度快,有些没罗列的,大家可以谷歌或者百度 ...

  7. PHP 字符串常用方法

    implode(“”,“”)-->字符串分割方法,第一个参数以什么样的形式分割,第二个参数需要分割的字符串 数组操作 is_array(),判断这个数是否是一个数组

  8. Day14 HTML补充

    一.认识前端 前端开发的核心语言: html - 超文本标记语言 结构 css - 层叠样式表 样式 javascript - 脚本语言 行为 <html></html> 双标 ...

  9. BAE 环境下配置 struts2 + spring + hibernate(SSH)(一)准备

    1.首先选择版本控制 SVN 或者 Git ,但是由于Git在windows下需要环境,所以优先选择SVN. 2.安装一个SVN客户端 windows下使用TortoiseSVN:立即下载 注意:BA ...

  10. 开源中国iOS客户端学习

    开源中国iOS客户端学习 续写前言 <开源中国iOS客户端学习>续写前系列博客    http://blog.csdn.net/column/details/xfzl-kykhd.html ...