1.应用directx图形库进行开发;

2.代码:

public class TClass : System.Windows.Forms.Form
{
/// <summary>
/// 设备对象,场景中所有图形对象的父对象
/// </summary>
private Device device = null; /// <summary>
/// 坐标系四棱锥顶点缓冲
/// </summary>
VertexBuffer vertexBuffer = null; /// <summary>
/// 此参数设置为必须,它定义了要创建的Direct3D设备的表示参数,如后台缓冲区的高度、宽度和像素格式、如何从后台缓冲区复制到前台缓存、以及屏幕显示的方式等等
/// </summary>
PresentParameters presentParameters; /// <summary>
/// 暂停标志
/// </summary>
bool pause = false; /// <summary>
/// 随机数,用来生成随机颜色用的
/// </summary>
Random rn = new Random(); /// <summary>
/// 构造函数,设置窗体大小
/// </summary>
public TClass()
{
this.ClientSize = new System.Drawing.Size(300, 300);
} /// <summary>
/// 初始化绘图环境
/// </summary>
/// <returns></returns>
public bool InitializeGraphics()
{
try
{
presentParameters = new PresentParameters();
//设置屏幕显示模式为窗口模式
presentParameters.Windowed = true;
//设置如何从后台缓冲区复制到前台缓冲区(SwapEffect.Discard表示缓冲区在显示后立即被舍弃,这样可以节省开销)
presentParameters.SwapEffect = SwapEffect.Discard;
//创建一个设备
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParameters);
//为设备释放订阅事件处理
device.DeviceReset += new System.EventHandler(this.OnResetDevice); this.OnCreateDevice(device, null);
this.OnResetDevice(device, null);
pause = false;
return true;
} catch (DirectXException)
{
return false;
}
} /// <summary>
/// 设备创建时建立顶点缓冲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void OnCreateDevice(object sender, EventArgs e)
{
Device dev = (Device)sender; //创建顶点缓冲,有个顶点
vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 18, dev, 0, CustomVertex.PositionColored.Format, Pool.Default);
//为创建顶点缓存订阅事件处理
vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer); this.OnCreateVertexBuffer(vertexBuffer, null);
} /// <summary>
/// 设备撤销的事件处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void OnResetDevice(object sender, EventArgs e)
{
Device dev = (Device)sender;
//关闭剔除模式,使我们能看见此四棱锥的前面和后面
dev.RenderState.CullMode = Cull.None;
// 关闭场景里的灯光,显示顶点自己的颜色
dev.RenderState.Lighting = false;
} /// <summary>
/// 创建顶点缓存的事件处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void OnCreateVertexBuffer(object sender, EventArgs e)
{ VertexBuffer vb = (VertexBuffer)sender;
CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[])vb.Lock(0, 0); //四棱锥原始的个点
Vector3 vertex1 = new Vector3(25, 0, 0);
Vector3 vertex2 = new Vector3(0, 0, -25);
Vector3 vertex3 = new Vector3(-25, 0, 0);
Vector3 vertex4 = new Vector3(0, 0, 25);
Vector3 vertex5 = new Vector3(0, 25, 0); //四棱锥中包含个三角形,所以要构造个点来绘制
verts[0].Position = vertex1;
verts[1].Position = vertex2;
verts[2].Position = vertex5;
verts[3].Position = vertex2;
verts[4].Position = vertex3;
verts[5].Position = vertex5;
verts[6].Position = vertex3;
verts[7].Position = vertex4;
verts[8].Position = vertex5;
verts[9].Position = vertex4;
verts[10].Position = vertex1;
verts[11].Position = vertex5;
verts[12].Position = vertex2;
verts[13].Position = vertex1;
verts[14].Position = vertex3;
verts[15].Position = vertex3;
verts[16].Position = vertex1;
verts[17].Position = vertex4; //给每个点赋予随机颜色
for (int i = 0; i < 18; i++)
{
verts[i].Color = Color.FromArgb(SetColor(), SetColor(), SetColor()).ToArgb(); }
vb.Unlock(); } /// <summary>
/// 返回到之间的一个随机数,用来生成随机颜色
/// </summary>
/// <returns></returns>
public int SetColor()
{
int number = rn.Next(256);
return number;
} /// <summary>
/// 设置摄像机的位置
/// </summary>
private void SetupCamera()
{
//设置世界矩阵,根据系统运行时间而变化
device.Transform.World = Matrix.RotationAxis(new Vector3((float)Math.Cos(Environment.TickCount / 250.0f), 1, (float)Math.Sin(Environment.TickCount / 250.0f)), Environment.TickCount / 3000.0f);
//设置摄像机的位置,它在z轴上-50处,看着原点,y轴为正方向
device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 0.0f, -50f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f)); //设置摄像机的视界,角度为度,看的最近为,看的最远处为.不再这个视界中的影像都不会被显示
device.Transform.Projection = Matrix.PerspectiveFovLH(((float)(float)Math.PI / 2), 1.0f, 10.0f, 200.0f);
} /// <summary>
/// 绘制图形
/// </summary>
public void Render()
{
if (device == null)
return; if (pause)
return; //背景设为绿色
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
//开始场景
device.BeginScene();
// 设置世界,视野和投影矩阵
SetupCamera(); // 给设备指定顶点缓存
device.SetStreamSource(0, vertexBuffer, 0); //设置设备的顶点格式
device.VertexFormat = CustomVertex.PositionColored.Format; //绘制图形,使用的方法为三角形列表,个数为个
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 6); //结束场景
device.EndScene(); //更新场景
device.Present();
} //重载OnPaint函数
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
//绘制图形
this.Render();
}
}

  调用代码:

using (TClass frm = new TClass())
{
if (!frm.InitializeGraphics()) // 初始化 Direct3D
{
MessageBox.Show("不能初始化 Direct3D.程序将退出.");
return;
}
frm.Show(); // While the form is still valid, render and process messages
while (frm.Created)
{
frm.Render();
Application.DoEvents(); //处理当前在消息队列中的所有 Windows 消息
}
}

  3.需要引用directx的程序集,下载连接(含项目):

  链接:https://pan.baidu.com/s/1D4wrHC7c2Pg1wpWXlrLrSA
  提取码:7f6w

  4.注意调用Microsoft.DirectX.dll时候,需要在程序配置文件中设置useLegacyV2RuntimeActivationPolicy为true。

最新文章

  1. [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
  2. Sql Server系列:字符串函数
  3. Java开发面试总结
  4. web页面如何播放amr的音频文件
  5. No.014:Longest Common Prefix
  6. JS中常遇到的浏览器兼容问题和解决方法【转】
  7. enmo_day_03
  8. weblogic管理1——创建 和 删除一个domain
  9. ASCII 码表对照
  10. linux下修改MAC地址方法
  11. 转:【Java集合源码剖析】LinkedHashmap源码剖析
  12. 未来五年什么样的IT技术最具颠覆性?这里有你想知道的答案
  13. 实验与作业(Python)-02 Python函数入门与温度转换程序
  14. node.js(node.js+mongoose小案例)_实现简单的注册登录退出
  15. You can&#39;t specify target table &#39;ship_product_cat&#39; for update in FROM clause
  16. React-组件 &amp; Props
  17. Centos7.x 执行top命令教程
  18. CentOS 6.4 安装 rabbitmq(3.6.15)
  19. HTTPS 接入优化建议
  20. ActiveMQ实现消息的发送与接受

热门文章

  1. MySQL UNION 操作符
  2. winfrom判断程序是否运行,并给提示
  3. 一问带你区分清楚Authentication,Authorization以及Cookie、Session、Token
  4. 基于注解的SpringAOP源码解析(二)
  5. redis的两种持久化方案
  6. html5样式初始化,你值得拥有!!
  7. css flex兼容性
  8. Fiddler模拟响应
  9. bugku——宽带信息泄露(RouterPassView 查看路由器配置信息)
  10. Caml 多表关联查询