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

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

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

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

第二步:主程序代码

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 _3DPrimitive
{
public partial class Form1 : Form
{
private Device device = null;
private VertexBuffer vertexBuffer = null;
private const int vertexNumber = 18;
private string primitivesType = "点列";
private Microsoft.DirectX.Direct3D.Font d3dfont;
private string helpString;
private bool showHelpString = true;
private float angle = 0.0f;
private bool enableRotator = false;
private int rotatorXYZ = 0;
private bool enableCullMode = false; public Form1()
{
InitializeComponent();
} public bool InitializeGraphics()
{
try
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard; presentParams.AutoDepthStencilFormat=DepthFormat.D16;
presentParams.EnableAutoDepthStencil=true; 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, -30.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.PointSize = 4.0f;
//不要灯光
device.RenderState.Lighting = false;
//背面剔除
if (enableCullMode == true)
{
device.RenderState.CullMode = Cull.CounterClockwise;
}
else
{
device.RenderState.CullMode = Cull.None;
}
}
private void OnVertexBufferCreate(object sender, EventArgs e)
{
//锁定顶点缓冲-->定义顶点-->解除锁定。
VertexBuffer buffer = (VertexBuffer)sender;
CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[])buffer.Lock(0, 0);
Random random = new Random();
for (int i = 0; i < vertexNumber; i++)
{
float x = (float)(vertexNumber * (random.NextDouble() - 0.5f));
float y = (float)(vertexNumber * (random.NextDouble() - 0.5f));
float z = (float)(vertexNumber * (random.NextDouble() - 0.5f));
verts[i].Position = new Vector3(x, y, z);
Color color = Color.FromArgb(random.Next(byte.MaxValue),
random.Next(byte.MaxValue), random.Next(byte.MaxValue));
verts[i].Color = color.ToArgb();
}
buffer.Unlock();
} private void Form1_Load(object sender, EventArgs e)
{
this.Width = 570;
this.Height = 430;
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" +
"<1>:点列\n" +
"<2>:线列\n" +
"<3>:线带\n" +
"<4>:三角形列\n" +
"<5>:三角形带\n" +
"<6>:三角扇形\n";
System.Drawing.Font winFont = new System.Drawing.Font("宋体", 9, FontStyle.Regular);
d3dfont = new Microsoft.DirectX.Direct3D.Font(device, winFont);
d3dfont.PreloadText(helpString);
//创建顶点缓冲
vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored),
vertexNumber,
device, Usage.Dynamic | Usage.WriteOnly,
CustomVertex.PositionColored.Format, Pool.Default);
vertexBuffer.Created += new EventHandler(OnVertexBufferCreate);
OnVertexBufferCreate(vertexBuffer, null);
} private void Form1_Paint(object sender, PaintEventArgs e)
{
SetupCamera();
// device.Clear(ClearFlags.Target, System.Drawing.Color.AliceBlue, 1.0f, 0);
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.AliceBlue, 1.0f, 0); device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.SetStreamSource(0, vertexBuffer, 0);
switch (primitivesType)
{
case "点列":
device.DrawPrimitives(PrimitiveType.PointList, 0, vertexNumber);
break;
case "线列":
device.DrawPrimitives(PrimitiveType.LineList, 0, vertexNumber / 2);
break;
case "线带":
device.DrawPrimitives(PrimitiveType.LineStrip, 0, vertexNumber - 2);
break;
case "三角形列":
device.DrawPrimitives(PrimitiveType.TriangleList, 0, vertexNumber / 3);
break;
case "三角形带":
device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, vertexNumber - 2);
break;
case "三角扇形":
device.DrawPrimitives(PrimitiveType.TriangleFan, 0, vertexNumber - 2);
break;
}
if (showHelpString == true)
{
d3dfont.DrawText(null, helpString, 25, 30, Color.Green);
}
if (enableRotator == true)
{
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);
angle += 0.05f / (float)Math.PI;
}
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:
OnVertexBufferCreate(vertexBuffer, null);
break;
case Keys.F3:
enableRotator = !enableRotator;
break;
case Keys.F4:
rotatorXYZ = (rotatorXYZ + 1) % 3;
break;
case Keys.F5:
enableCullMode = !enableCullMode;
break;
case Keys.D1:
primitivesType = "点列";
break;
case Keys.D2:
primitivesType = "线列";
break;
case Keys.D3:
primitivesType = "线带";
break;
case Keys.D4:
primitivesType = "三角形列";
break;
case Keys.D5:
primitivesType = "三角形带";
break;
case Keys.D6:
primitivesType = "三角扇形";
break;
} }
}
}

Directx 3D编程实例:随机绘制的立体图案旋转的更多相关文章

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

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

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

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

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

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

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

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

  5. PHP多进程编程实例

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

  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. 如何在ChemDraw中绘制分子立体结构

    ChemDraw是当前最常用的的化学结构绘图软件,软件功能包括化学作图.分子模型生成.化学数据库信息管理等,可以说是化学家和生物学家所需要最终极的化学结构绘图工具.本教程主要介绍ChemDraw绘制分 ...

  9. 近中期3D编程研究目标

    近几年一直在用业余时间研究3D编程,研究的中期目标是建立一个实用的开源3D编程框架.3D编程技术最直接的应用是开发游戏,所以3D编程框架也就是3D游戏开发框架.在我看来,游戏是否好玩的关键是能否为玩家 ...

随机推荐

  1. HTTP请求返回的NSData无法转换为NSString

    最近在做的一个项目中有一个功能是有一个网页,模拟http请求获取到这个网页返回的相应的数据. 在请求完成后获取到的数据为NSData类型,按照我们通常的转换为NSString的方法: NSString ...

  2. IOS 生成设备唯一标识

    前言 iOS设备5.0以上放弃使用[[UIDevice currentDevice] uniqueIdentifier]来获得设备唯一ID iOS设备私有方法禁止用户获取和使用IMEI 需求 需要一个 ...

  3. C# XML文件操作类XmlHelper

    类的完整代码: using System;using System.Collections;using System.Xml; namespace Keleyi.Com.XmlDAL{public c ...

  4. CListCtrl 的应用

    CMFCListCtrl实现的功能:比较特别的就是通过CImage来扩展每行的高度 CMFCListCtrl m_lisTestResult; 1.插入列(创建表格) m_lisTestResult. ...

  5. [C++]C++类基本语法

    本测试代码包括以下内容: (1)如何使用构造函数:(2)默认构造函数:(3)对象间赋值:(4)const使用语法:(5)定义类常量: 一种方法是用enum,另一种方法是使用static. #inclu ...

  6. .getBoundingClientRect()

    .getBoundingClientRect() 该方法获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置,他返回的是一个对象,即Object,该对象有4个属性:top,left,right, ...

  7. javascript DOM艺术

    一.DOM基础1.节点(node)层次Document--最顶层的节点,所有的其他节点都是附属于它的.DocumentType--DTD引用(使用<!DOCTYPE>语法)的对象表现形式, ...

  8. python读写Excel文件的函数--使用xlrd/xlwt

    python中读取Excel的模块或者说工具有很多,如以下几种: Packages 文档下载 说明 openpyxl Download | Documentation | Bitbucket  The ...

  9. SVN 使用的简单整理

    1. 在SVN服务器上创建存储Dir,并和个人主机建立联系.      现在SVN服务器上创建一个存储文件夹svn_storeDir.然后在个人电脑上建立一个本地文件夹local_Dir.    进入 ...

  10. 屏蔽手势UIGestureRecognizer 先后响应

    在iOS5一下对于手势的识别能力并不强,比如iOS6上面按钮的一个tap事件,最先接收的是uiview,并相应,而不是最上面的button,这时候就需要判断手势所在的位置和手势所在的控制器了 如下例子 ...