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

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();

posted on 2012-10-29 17:37 Lilac_F 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/Lilac-F/archive/2012/10/29/2745128.html

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

  1. Direct3D顶点结构使用总结

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

  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. Oracle之物化视图
  2. 看一家公司发展得如何就看这家公司的财务部门
  3. python callback函数_回调函数callbacks
  4. 建立索引lucene_用Lucene建立搜索索引
  5. ————————————————————————动态规划——————————————————————1003——————————...
  6. ZetCode PHP Symfony 教程
  7. 为节省内存,动态添加view布局和控件
  8. pr生成html文件格式,pr支持哪些格式
  9. 使用linux批量引物设计,使用NCBI-ePCR和Primer3进行引物批量化设计
  10. 存储服务器内的温度检测信号线 用线,常用的3线和4线电阻温度检测器介绍
  11. Python|十五个超级炫酷的代码
  12. Matlab求四面体体积
  13. 外汇短线交易者的规则
  14. #51CTO学院四周年# 感谢51CTO学院让我走出迷茫
  15. 2021-09-29破解小米“铁蛋”,只需9999元,你也可以做一个四足机器人!
  16. 数字图像处理——隐形眼镜缺陷检测算法
  17. 电子印章怎么验证真假?
  18. 信息安全实验:标准IP的ACLs的配置(cisco模拟器)
  19. 记一次IOS打包报错
  20. 支持在线预览,方便快捷

热门文章

  1. 电脑常见故障排除_中央空调常见故障解析
  2. 数组及字符串相关知识
  3. PortraitFCN算法详解
  4. linux应用之----进程控制理论
  5. shell date常用运算命令
  6. CSDN-Markdown编辑器如何修改图像大小
  7. 统计 python_Python统计简介
  8. 第三届类型文学研讨会小记(转)
  9. CSS3选择非第一个子元素
  10. 64位Ubuntu kylin 16.04 安装laptop mode解决关闭盖子无法唤醒,并解决安装此模式后鼠标间歇断电