DirectX 编程 第四步: 顶点(Vertex)绘图

来源:互联网 发布:知乎r级别电影 编辑:程序博客网 时间:2024/05/18 03:20

终于要画图了,先说些题外话:微软看来很不重视受管的DirectX, 我已经下载了最新的DirectX版本(June2006)版,然而不论是类库参考,还是样例程序,无一例外都是错的,放着老掉牙的东西让人猜测新函数的 使用方法,实在是很痛苦,尤其对我这种自学入门的新手。

好了,先说说几个必要的方法及属性的使用:

1. SetStreamSource :设定设备所要画图时需要的顶点数据源。

2. VertexFormat :数据源存储的顶点(Vertex)格式。

3. DrawPrimitives : 根据设备的顶点信息绘制出图形。

关键代码如下:

VertexBuffer vb = null; //首先声明类变量用于开辟顶点缓冲区

...

vb = new VertexBuffer(theDevice, 80, 0, TransformedColored.Format, Pool.Default, OnCreateVertexBuffer); //在系统初始化阶段同时初始化该缓冲区类

//theDevice:在第三步中已经初始化的设备实例

//80:开辟的缓冲区大小(sizeInBytes)

//0:usage

//TransformedColored.Format: 顶点格式

//Pool.Default: 池类型

//OnCreateVertexBuffer : 初始化事件的代理函数(可用于初始化顶点值)

...

 //Begin the scene
 theDevice.BeginScene();

 theDevice.SetStreamSource(0, vb, 0,TransformedColored.StrideSize);//绑定数据源到设备

 theDevice.VertexFormat = TransformedColored.Format; //确定顶点格式

 theDevice.DrawPrimitives(PrimitiveType.TriangleFan, 0, 1); // 绘制图形
           

//End the scene
 theDevice.EndScene();

  转自: http://ddgame.blog.sohu.com/5183122.html
原创粉丝点击