D3D里面最基本的就是顶点了,虽说一直在用,可是却也是自己比较模糊的一个点,知道其中的意思,却不是很清楚,今天就总结一下,扫一下这个盲区:

D3D中的顶点缓冲区的声明:

LPDIRECT3DVERTEXBUFFER9 g_pVB        = NULL;    //顶点缓冲区对象

通常都是用LPDIRECT3DVERTEXBUFFER9 来声明顶点缓冲区,它其实就是IDirect3DVertexBuffer9的指针类型,这两个起到的效果是一样的。用LPDIRECT3DVERTEXBUFFER9 声明之后,只是镇定了一个缓冲区的指针,下面还需要开辟一个缓冲区给这个指针。

在开辟真正的内存之前,我们先看一下顶点格式的定义,D3D里面是采用的灵活顶点格式,这点大家应该都是知道的,下面就来总结一下这些灵活顶点格式都具体有哪些,有什么用处。

一般定义顶点结构的时候都是用一个结构体,当然用类去定义也可以,但是一般没有那个必要。

struct CUSTOMVERTEX

{

FLOAT x, y, z, rhw;

DWORD color;

};

在还需要定义一个宏,来向D3D说明一下,自己定义的顶点的格式到底有哪些。

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)  //顶点格式

上面这一句话的意思就是,定义的顶点结构包含:位置变换信息(D3DFVF_XYZRHW)和漫反射颜色信息(D3DFVF_DIFFUSE);

那么,一共都有哪些类型可以定义呢,都有什么样的用呢。

---------------------------------------------------------------------------------------------------------------------------------------

Vertex Data Flags

#define Description Data order and type
D3DFVF_DIFFUSE Vertex format includes a diffuse color component. DWORD in ARGB order. See D3DCOLOR_ARGB.
D3DFVF_NORMAL Vertex format includes a vertex normal vector. This flag cannot be used with the D3DFVF_XYZRHW flag. float, float, float
D3DFVF_PSIZE Vertex format specified in point size. This size is expressed in camera space units for vertices that are not transformed and lit, and in device-space units for transformed and lit vertices. float
D3DFVF_SPECULAR Vertex format includes a specular color component. DWORD in ARGB order. See D3DCOLOR_ARGB.
D3DFVF_XYZ Vertex format includes the position of an untransformed vertex. This flag cannot be used with the D3DFVF_XYZRHW flag. float, float, float.
D3DFVF_XYZRHW Vertex format includes the position of a transformed vertex. This flag cannot be used with the D3DFVF_XYZ or D3DFVF_NORMAL flags. float, float, float, float.
D3DFVF_XYZB1 through D3DFVF_XYZB5 Vertex format contains position data, and a corresponding number of weighting (beta) values to use for multimatrix vertex blending operations. Currently, Direct3D can blend with up to three weighting values and four blending matrices. For more information about using blending matrices, see Indexed Vertex Blending (Direct3D 9). 1, 2, or 3 floats. When D3DFVF_LASTBETA_UBYTE4 is used, the last blending weight is treated as a DWORD.
D3DFVF_XYZW Vertex format contains transformed and clipped (x, y, z, w) data. ProcessVertices does not invoke the clipper, instead outputting data in clip coordinates. This constant is designed for, and can only be used with, the programmable vertex pipeline. float, float, float, float

Texture Flags

#define Description
D3DFVF_TEX0 - D3DFVF_TEX8 Number of texture coordinate sets for this vertex. The actual values for these flags are not sequential.
D3DFVF_TEXCOORDSIZEN(coordIndex) Define a texture coordinate data set. n indicates the dimension of the texture coordinates. coordIndex indicates texture coordinate index number. See D3DFVF_TEXCOORDSIZEN and Texture coordinates and Texture Stages.

Mask Flags

#define Description
D3DFVF_POSITION_MASK Mask for position bits.
D3DFVF_RESERVED0, D3DFVF_RESERVED2 Mask values for reserved bits in the FVF. Do not use.
D3DFVF_TEXCOUNT_MASK Mask value for texture flag bits.

Miscellaneous Flags

#define Description
D3DFVF_LASTBETA_D3DCOLOR The last beta field in the vertex position data will be of type D3DCOLOR. The data in the beta fields are used with matrix palette skinning to specify matrix indices.
D3DFVF_LASTBETA_UBYTE4 The last beta field in the vertex position data will be of type UBYTE4. The data in the beta fields are used with matrix palette skinning to specify matrix indices.

// Given the following vertex data definition:
struct VERTEXPOSITION
{
float pos[3];
union
{
float beta[5];
struct
{
float weights[4];
DWORD MatrixIndices;  // Used as UBYTEs
}
}
};

Given the FVF is declared as: D3DFVF_XYZB5 | D3DFVF_LASTBETA_UBYTE4. Weight and MatrixIndices are included in beta[5], where D3DFVF_LASTBETA_UBYTE4 says to interpret the last DWORD in beta[5] as type UBYTE4.

D3DFVF_TEXCOUNT_SHIFT The number of bits by which to shift an integer value that identifies the number of texture coordinates for a vertex. This value might be used as shown below.

DWORD dwNumTextures = 1;  // Vertex has only one set of coordinates.
// Shift the value for use when creating a
//   flexible vertex format (FVF) combination.
dwFVF = dwNumTextures << D3DFVF_TEXCOUNT_SHIFT;
// Now, create an FVF combination using the shifted value.

Examples

The following examples show other common flag combinations.

// Untransformed vertex for lit, untextured, Gouraud-shaded content.
dwFVF = ( D3DFVF_XYZ | D3DFVF_DIFFUSE );
// Untransformed vertex for unlit, untextured, Gouraud-shaded
//   content with diffuse material color specified per vertex.
dwFVF = ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE );
// Untransformed vertex for light-map-based lighting.
dwFVF = ( D3DFVF_XYZ | D3DFVF_TEX2 );
// Transformed vertex for light-map-based lighting with shared rhw.
dwFVF = ( D3DFVF_XYZRHW | D3DFVF_TEX2 );
// Heavyweight vertex for unlit, colored content with two
//   sets of texture coordinates.
dwFVF = ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE |
D3DFVF_SPECULAR | D3DFVF_TEX2 );

------------------------------------------------------------------------------------------------------------------

在顶点结构体中没有RHW时,Direct3D将执行视、投影、世界等变换以及进行光线计算,之后你才能在窗口中得到你所绘制的物体。当顶点结构体中有RHW时,就像上面那段英文所述,告知Direct3D使用的顶点已经在屏幕坐标系中了,不再执行视图、投影、世界等变换和光线计算,因为D3DFVF_XYZRHW标志告诉它顶点已经经过了这些处理,并直接将顶点进行光栅操作,任何用SetTransform进行的转换都对其无效。不过这时的原点就在客户区的左上角了,其中x向右为正,y向下为正,而z的意义已经变为z-buffer的象素深度。

值得注意的是,D3DFVF_XYZRHW和D3DFVF_XYZ、D3DFVF_NORMAL不能共存,因为后两个标志与前一个矛盾。在使用这种顶点时,系统需要顶点的位置已经经过变换了,也就是说x、y必须在屏幕坐标系中,z必须是z-buffer中的象素深度,取值范围:0.0-1.0,离观察者最近的地方为0.0,观察范围内最远可见的地方为1.0。(不过我测试的时候似乎z值不起作用。)引自:http://www.cppblog.com/lovedday/archive/2009/03/22/48507.html

在定义完顶点格式以后,就要开辟一块顶点缓冲区:

g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),

0, D3DFVF_CUSTOMVERTEX,

D3DPOOL_DEFAULT, &g_pVB, NULL )

开辟缓冲区后,就需要对这个缓冲区进行填写,那么填写的数据呢,也需要先指定出来:

CUSTOMVERTEX vertices[] =

{

{ 100.0f, 400.0f, 0.5f, 1.0f, 0xffff0000, },

{ 300.0f,  50.0f, 0.5f, 1.0f, 0xff00ff00, },

{ 500.0f, 400.0f, 0.5f, 1.0f, 0xff0000ff, },

};

然后将数据写入缓冲区:

VOID* pVertices;

if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )

return E_FAIL;

memcpy( pVertices, vertices, sizeof(vertices) );

g_pVB->Unlock();

这里写入的过程用的是Lock函数得到的缓冲区的地址,然后用memcpy函数将自己写好的数据写进去。到这里,顶点就算是创建好了。

Direct3D顶点结构使用总结相关推荐

  1. 【转】Direct3D顶点结构使用总结

    [转]Direct3D顶点结构使用总结 D3D里面最基本的就是顶点了,虽说一直在用,可是却也是自己比较模糊的一个点,知道其中的意思,却不是很清楚,今天就总结一下,扫一下这个盲区: D3D中的顶点缓冲区 ...

  2. 第六章 使用Direct3D绘制

    第五章主要关注渲染管道的概念和数学方面.本章重点介绍配置渲染管道所需的Direct3D API接口和方法,定义顶点和像素着色器,并将几何图形提交给绘制管道进行绘制.学完本章,您将能够绘制各种几何形状的 ...

  3. Direct3D 10系统(一)

    Direct3D 10系统(一) 作者:David Blythe 本文版权归原作者所有,仅供个人学习使用,请勿转载,勿用于任何商业用途. 由于本人水平有限,难免出错,不清楚的地方请大家以原著为准.欢迎 ...

  4. DirectX12学习笔记(四)Direct3D Initialization

    目录 4.1 PRELIMINARIES 4.1.1 Direct3D 4.1.2 COM(Component Object Model) 4.1.3 Textures Formats 4.1.4 T ...

  5. Introduction to 3D Game Programming with DirectX 11学习笔记 6 Direct3D中的绘制(一)

    顶点和顶点布局 在Direct3D中,顶点由空间位置和各种附加属性组成,Direct3D可以让我们灵活地建立属于我们自己的顶点格式:换句话说,它允许我们定义顶点的分量.要创建一个自定义的顶点格式,我们 ...

  6. Direct3D 12入门教程之 ---- 渲染流水线介绍

    本文主要参考 <DirectX 12 3D游戏开发实战>一书, 以及微软官方的Direct3D 12示例:DirectX-Graphics-Samples 渲染流水线:又称渲染管线,指的是 ...

  7. D3D12渲染技术之顶点着色器

    相信大家以前用过D3D9的或是编写过Shader的对顶点着色器都比较了解,现在我们回顾一下: cbuffer cbPerObject : register(b0) {float4x4 gWorldVi ...

  8. Vertex and FragmentShader顶点与片段着色器

    一.顶点与片段着色器简介 Vertex and FragmentShader:最强大的Shader类型,也是本系列的重点,下文中简称V&FShader,属于可编程渲染管线.使用的是CG/HLS ...

  9. 【Visual C++】游戏开发笔记三十六 浅墨DirectX提高班之四 顶点缓存的逆袭

    本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接: http://blog.csdn.net/zhmxy555/article/details/8276363 作者:毛星云(浅墨 ...

最新文章

  1. ORB-SLAM(1) --- 让程序飞起来
  2. 第五章 逻辑回归模型在评分卡开发中的应用
  3. Linux 脚本文件中开头的#!/bin/bash和#!/bin/sh是什么意思
  4. reactnative 获取定位_【React Native】定位获取经纬度,当前城市等地址信息
  5. Ext.tree.Panel示例
  6. sys模块,subprocess模块
  7. linux的前端环境搭建-安装配置git客户端与github连接
  8. 软件测试总结--02缺陷报告
  9. IDEA统计代码行数
  10. QQ浏览器计算机未安装flash,Win8提示计算机没有安装Flash播放器怎么办
  11. 基于TensorFlow让机器生成赵雷曲风的歌词
  12. 安卓机自动肝手游脚本
  13. 使用ensembl的API下载数据
  14. ElasticSearch之 ik分词器详解
  15. 17、文件IO详解及实例
  16. pspice仿真库DC电源设置请教
  17. matlab实现细胞分割,MATLAB图像处理实验——细胞图像的分割和计数
  18. 你开车都有什么小技巧?
  19. 【EA-MT4】外汇程序化交易之枢轴点
  20. 谷歌AI助NASA发现第二个太阳系;传快手完成新一轮融资;摩拜起诉小广告公司丨价值早报

热门文章

  1. vue中watch监听路由传来的参数变化
  2. 块级元素(HTML、CSS)
  3. mysql之配置使其可用python远程控制
  4. 中职计算机专业建设构想,《关于技能大赛引导中职计算机专业建设的思考原稿》...
  5. 计算机网络在金融领域的应用,计算机网络毕业论文计算机网络技术在金融领域的应用.pdf...
  6. 微软以75亿美元收购GitHub
  7. hibernate里的实体类中不能重写toString
  8. ‘nvidia-smi‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
  9. Java大数据-Week2-Day4-IDEA安装
  10. 佛祖保佑代码无bug图片_程序员都有哪些奇趣的代码注释,细思极恐