坐标系与基本图元(2)

坐标系与基本图元(2)

创建顶点缓冲区

在创建顶点缓冲区之前,需要先定义一个表示顶点的结构类型,描述顶点保存格式的FVF和一个保存顶点的结构数组。

struct sCustomVertex{	float x, y, z, rhw;	DWORD color;};
#define D3DFVF_CUSTOM_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE) 
sCustomVertex vertices[] ={    { 100.0f, 400.0f, 1.0f, 1.0f, 0xffffff00, },    { 300.0f,  50.0f, 1.0f, 1.0f, 0xff00ff00, },     { 500.0f, 400.0f, 1.0f, 1.0f, 0xffff00ff, },};

创建顶点缓冲区的函数IDirect3DDevice9::CreateVertexBuffer()声明如下:

Creates a vertex buffer.

HRESULT CreateVertexBuffer(  UINT Length,  DWORD Usage,  DWORD FVF,  D3DPOOL Pool,  IDirect3DVertexBuffer9** ppVertexBuffer,  HANDLE* pSharedHandle);

Parameters

Length
[in] Size of the vertex buffer, in bytes. For FVF vertex buffers, Length must be large enough to contain at least one vertex, but it need not be a multiple of the vertex size. Length is not validated for non-FVF buffers. See Remarks.
Usage
[in] Usage can be 0, which indicates no usage value. However, if usage is desired, use a combination of one or more D3DUSAGE constants. It is good practice to match the usage parameter in CreateVertexBuffer with the behavior flags in IDirect3D9::CreateDevice. For more information, see Remarks.
FVF
[in] Combination of D3DFVF, a usage specifier that describes the vertex format of the vertices in this buffer. If this parameter is set to a valid FVF code, the created vertex buffer is an FVF vertex buffer (see Remarks). Otherwise, if this parameter is set to zero, the vertex buffer is a non-FVF vertex buffer.
Pool
[in] Member of the D3DPOOL enumerated type, describing a valid memory class into which to place the resource. Do not set to D3DPOOL_SCRATCH.
ppVertexBuffer
[out, retval] Address of a pointer to an IDirect3DVertexBuffer9 interface, representing the created vertex buffer resource.
pSharedHandle
[in] Reserved. Set this parameter to NULL.

Return Values

If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be one of the following: D3DERR_INVALIDCALL, D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY.

Defines the memory class that holds the buffers for a resource.

typedef enum D3DPOOL{    D3DPOOL_DEFAULT = 0,    D3DPOOL_MANAGED = 1,    D3DPOOL_SYSTEMMEM = 2,    D3DPOOL_SCRATCH = 3,    D3DPOOL_FORCE_DWORD = 0x7fffffff,} D3DPOOL, *LPD3DPOOL;

Constants

D3DPOOL_DEFAULT
Resources are placed in the memory pool most appropriate for the set of usages requested for the given resource. This is usually video memory, including both local video memory and AGP memory. The D3DPOOL_DEFAULT pool is separate from D3DPOOL_MANAGED and D3DPOOL_SYSTEMMEM, and it specifies that the resource is placed in the preferred memory for device access. Note that D3DPOOL_DEFAULT never indicates that either D3DPOOL_MANAGED or D3DPOOL_SYSTEMMEM should be chosen as the memory pool type for this resource. Textures placed in the D3DPOOL_DEFAULT pool cannot be locked unless they are dynamic textures or they are private, FOURCC, driver formats. To access unlockable textures, you must use functions such as IDirect3DDevice9::UpdateSurface, IDirect3DDevice9::UpdateTexture, IDirect3DDevice9::GetFrontBufferData, and IDirect3DDevice9::GetRenderTargetData. D3DPOOL_MANAGED is probably a better choice than D3DPOOL_DEFAULT for most applications. Note that some textures created in driver-proprietary pixel formats, unknown to the Direct3D runtime, can be locked. Also note that - unlike textures - swap chain back buffers, render targets, vertex buffers, and index buffers can be locked. When a device is lost, resources created using D3DPOOL_DEFAULT must be released before calling IDirect3DDevice9::Reset. For more information, see Lost Devices (Direct3D 9).

When creating resources with D3DPOOL_DEFAULT, if video card memory is already committed, managed resources will be evicted to free enough memory to satisfy the request.

D3DPOOL_MANAGED
Resources are copied automatically to device-accessible memory as needed. Managed resources are backed by system memory and do not need to be recreated when a device is lost. See Managing Resources (Direct3D 9) for more information. Managed resources can be locked. Only the system-memory copy is directly modified. Direct3D copies your changes to driver-accessible memory as needed.
D3DPOOL_SYSTEMMEM
Resources are placed in memory that is not typically accessible by the Direct3D device. This memory allocation consumes system RAM but does not reduce pageable RAM. These resources do not need to be recreated when a device is lost. Resources in this pool can be locked and can be used as the source for a IDirect3DDevice9::UpdateSurface or IDirect3DDevice9::UpdateTexture operation to a memory resource created with D3DPOOL_DEFAULT.
D3DPOOL_SCRATCH
Resources are placed in system RAM and do not need to be recreated when a device is lost. These resources are not bound by device size or format restrictions. Because of this, these resources cannot be accessed by the Direct3D device nor set as textures or render targets. However, these resources can always be created, locked, and copied.
D3DPOOL_FORCE_DWORD
Forces this enumeration to compile to 32 bits in size. Without this value, some compilers would allow this enumeration to compile to a size other than 32 bits. This value is not used.
 

创建顶点缓冲区的代码如下:

g_device->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_DEFAULT, &g_vertex_buffer, NULL);

现在,已经创建了顶点缓冲区g_vertex_buffer,接下来把顶点数据vertices[]中保存的顶点数据复制到顶点缓冲区中:

void* ptr;

g_vertex_buffer->Lock(0, sizeof(vertices), (void**)&ptr, 0);
memcpy(ptr, vertices, sizeof(vertices));
g_vertex_buffer->Unlock();

IDirect3DVertexBuffer9::Lock()通知Direct3D将要对顶点缓冲区进行内存操作,并获得顶点缓冲区的内存指针,顶点数据复制完成后,IDirect3DVertexBuffer9::Unlock()通知Direct3D操作结束。

Locks a range of vertex data and obtains a pointer to the vertex buffer memory.

HRESULT Lock(  UINT OffsetToLock,  UINT SizeToLock,  VOID ** ppbData,  DWORD Flags);

Parameters

OffsetToLock
[in] Offset into the vertex data to lock, in bytes. To lock the entire vertex buffer, specify 0 for both parameters, SizeToLock and OffsetToLock.
SizeToLock
[in] Size of the vertex data to lock, in bytes. To lock the entire vertex buffer, specify 0 for both parameters, SizeToLock and OffsetToLock.
ppbData
[out] VOID* pointer to a memory buffer containing the returned vertex data.
Flags
[in] Combination of zero or more locking flags that describe the type of lock to perform. For this method, the valid flags are:

  • D3DLOCK_DISCARD
  • D3DLOCK_NO_DIRTY_UPDATE
  • D3DLOCK_NOSYSLOCK
  • D3DLOCK_READONLY
  • D3DLOCK_NOOVERWRITE

For a description of the flags, see D3DLOCK.

Return Values

If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

Remarks

When working with vertex buffers, you are allowed to make multiple lock calls; however, you must ensure that the number of lock calls match the number of unlock calls. DrawPrimitive calls will not succeed with any outstanding lock count on any currently set vertex buffer.

The D3DLOCK_DISCARD and D3DLOCK_NOOVERWRITE flags are valid only on buffers created with D3DUSAGE_DYNAMIC.

For information about using D3DLOCK_DISCARD or D3DLOCK_NOOVERWRITE with IDirect3DVertexBuffer9::Lock, see Using Dynamic Vertex and Index Buffers.

参数Flags表示顶点缓冲区的加锁属性,它可以取0(默认值)或者下表中任意值的逻辑或。


标识符


说明

D3DLOCK_DISCARD 更新整个缓冲区
D3DLOCK_NO_DIRTY_UPDATE 在默认状态下,对缓冲区加锁会对该区域设置一个Dirty标记。该属性将不对该区域设置Dirty标记,当对缓冲区有特殊需要时使用。
D3DLOCK_NOOVERWRITE 保证不覆盖缓冲区数据,设置该属性可以立即返回内存指针,提高系统性能。
D3DLOCK_NOSYSLOCK 在加锁过程中系统可以进行其他操作
D3DLOCK_READONLY 设置缓冲区为只读属性

执行Lock()函数需要一定的时间,默认状态下,Direct3D会暂停其他的显示操作,直至Lock()函数执行结束。设置D3DLOCK_NOSYSLOCK属性,可以使Direct3D在执行对缓冲区加锁的同时执行其他的显示操作,比如移动鼠标。

时间: 2024-10-12 15:54:02

坐标系与基本图元(2)的相关文章

坐标系与基本图元~转载天行健 君子当自强而不息

坐标系与基本图元 坐标系与基本图元(8)      摘要: 游戏程序通常都是运行在全屏幕模式下,进行全屏显示的关键是使用全屏显示的渲染设备.创建全屏显示模式渲染设备同窗口模式渲染设备基本相同,区别是将 d3dpp.Windowed设置为FALSE,告诉Direct3D系统,将要创建的是全屏模式渲染设备.此外,还需要明确指定后台缓冲区的大小和格式,这和创建窗口模式渲染设备是不相同的,在创建窗口模式渲染设备时可将后台缓冲区格式设置为D3DFMT_UNKNOWN,后台缓冲区大小也可取默认值,而在创建全

坐标系与基本图元(3)

坐标系与基本图元(3) 渲染顶点缓冲区图形 void render(){ g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_X#050505, 1.0f, 0); g_device->BeginScene(); g_device->SetStreamSource(0, g_vertex_buffer, 0, sizeof(sCustomVertex)); g_device->SetFVF(D3DFVF_CUSTOM_VERTEX)

坐标系与基本图元(7)

坐标系与基本图元(7) 场景提交概述 场景提交即将在后台缓冲区绘制好的场景提交到前台缓冲区,从而在屏幕上显示出来.提交接口函数是一组控制特定的渲染设备状态的方法,这些设备影响显示器的显示. (1)前台缓冲区:这是一块由显卡转换来的矩形存储区,这块矩形存储区的内容显示在显示器或其他输出设备上. (2)后台缓冲区:后台缓冲区是一个表面,其内容可以提交到前台缓冲区. (3)交换链:一组后台缓冲区集合,它们被顺序地提交到前台缓冲区.一般情况下,一个全屏交换链通过翻转设备驱动接口(DDI)来提交随后的显示

坐标系与基本图元(8)

坐标系与基本图元(8) 全屏幕显示 游戏程序通常都是运行在全屏幕模式下,进行全屏显示的关键是使用全屏显示的渲染设备.创建全屏显示模式渲染设备同窗口模式渲染设备基本相同,区别是将d3dpp.Windowed设置为FALSE,告诉Direct3D系统,将要创建的是全屏模式渲染设备.此外,还需要明确指定后台缓冲区的大小和格式,这和创建窗口模式渲染设备是不相同的,在创建窗口模式渲染设备时可将后台缓冲区格式设置为D3DFMT_UNKNOWN,后台缓冲区大小也可取默认值,而在创建全屏模式渲染设备时这些都需要

坐标系与基本图元(1) ~转载天行健 君子当自强而不息

坐标系与基本图元(1) Direct3D基本图元 图元(primitives)是Direct3D中定义的基本图形表示,它是组成一个单一实体的一组顶点.最简单的图元是三维坐标系中多个点的集合,称为点列表(point list).通常,图元是多边形(polygon),一个多边形是由至少三条边组成的封闭图形.最简单的多边形是三角形,Direct3D使用三角形来构成大多数其他多边形,这是因为三角形的三个顶点肯定是共面的,而渲染不共面的顶点效率比较低.通过组合三角形可以形成更大.更复杂的多边形和网格(me

坐标系与基本图元(5)

坐标系与基本图元(5) 使用索引缓冲区绘制图形 当绘制一个比较复杂的图形时,需要使用许多相互邻接的三角形.如果为每个三角形准备三个顶点数据,显然有许多数据是重复的,这样会浪费大量的内存和系统带宽.为了解决这一问题,可以先创建一个顶点缓冲区,将不重复的顶点数据写入顶点缓冲区,然后创建一个顶点索引缓冲区(index buffer),存放各个三角形的顶点索引信息,最后通过顶点索引和顶点数据共同完成图形绘制. 在Direct3D中一个顶点的索引只需要用一个16位或32位的整数表示,因此当多边形的顶点有较

坐标系与基本图元(4)

坐标系与基本图元(4) 各种基本图元的绘制 上面使用顶点缓冲区绘制的是三角形列表图元,前面介绍过Direct3D支持点列表,线段列表.线段条带.三角形列表.三角形条带.三角扇形6种基本图元.下面通过示例程序BasicPrimitive演示各种基本图元的绘制.该示例程序使用同一个顶点缓冲区绘制不同类型的图元,程序中通过一个全局变量标识当前绘制的图元类型,通过单击键盘上的"1" ~ "6"数字键可以在各图元类型之间进行切换,单击空格键可以在线框模式和实体模式之间切换.

坐标系与基本图元(6)

坐标系与基本图元(6) 灵活顶点格式 灵活顶点格式(Flexible Vertex Format, FVF)用来描述在顶点缓冲区中的顶点存储格式中包含了哪些属性.Direct3D应用程序可以用几种不同的方式定义灵活顶点格式.灵活顶点格式使应用程序只使用它需要的顶点数据,排除那些它不需要的组成成分.这样,应用程序可以节省内存空间,减少系统带宽.通过D3DFVF的组合,可以描述图元顶点的格式.灵活顶点格式指定的格式包括点的大小,用D3DFVF_PSIZE指定,该大小在投影机空间用来表示未经变换的顶点

Qt5图形视图框架之概念篇(1)

本章将主要简述Graphics View框架结构的特点.主要包含的元素及坐标系统. 1.Graphics View框架结构的特点: (1)系统可以利用Qt绘图系统的反锯齿.OpenGL工具来改善绘图性能. (2)其支持事件传播结构,可以使图元在场景中的交互能力提高一倍,凸缘可以处理键盘事件和鼠标事件. (3)通过BSP提供快速的图元查找,可以实现实时显示包含数百万图元的大场景. 2.Graphics View的三元素: (1)场景类(QGraphicsScene):本身不可见,是一个放置图元的容