利用D3D接口绘制简单几何体
顶点缓存和索引缓存
两者用法相似
顶点缓存是一个包含内存数据的连续内存空间
索引缓存是一个包含索引数据的XXXXX
用它们的原因是他们存放数据的地方是显存,在绘制的时候,使用现存数据比内存数据会快的多
代码中,顶点缓存用接口 IDirect3DVertexBuffer9
索引是 IDirect3DIndexBuffer9
(directx9版本)
创建分别是:
HRESULT CreateVertexBuffer(
[in] UINT Length,
[in] DWORD Usage,
[in] DWORD FVF,
[in] D3DPOOL Pool,
[out, retval] IDirect3DVertexBuffer9 **ppVertexBuffer,
[in] HANDLE *pSharedHandle
);
HRESULT CreateIndexBuffer(
[in] UINT Length,
[in] DWORD Usage,
[in] D3DFORMAT Format,
[in] D3DPOOL Pool,
[out, retval] IDirect3DIndexBuffer9 **ppIndexBuffer,
[in] HANDLE *pSharedHandle
);
参数挺像的
从上往下分别是
缓存大小,字节数
附加属性
灵活顶点格式,索引格式(索引大小 16位/32位)
内存池
接收缓存的指针
预留的不使用,设为0,可以被用来在 windows的vista版本共享资源(这个特性从vista开始有,就是把资源在设备和进程间共享,具体的做法就是在资源创建的时候,就为其设定一个共享资源句柄,就是这个参数,这个资源被存放在默认内存池中
D3DPOOL_DEFAULT
这里比较有用的一个是 Usage参数
它有1个参数是 D3DUSAGE_DYNAMIC
如果使用了它,所创建的缓存就是动态缓存,动态缓存被放在AGP存储区中,特点是其内容可以迅速更新,适合需要频繁更新缓存内容的情况
例如粒子系统(根据时间和物理信息改变状态)
如果不使用,缓存就是静态缓存,静态缓存被直接放在显存中,好处是绘制效率最高,但是直接放在显存中不利于修改,显存读写速度比AGP慢
因此这种适合长时间不需要改变的数据,例如地形和城市
这个在开发中需要注意使用
*对显存和AGP存储区进行读操作非常慢,如果需要在运行时读取几何数据,最好在内存中保留一份副本,需要时读取
但是
AGP已经是过去时
现在AGP已经被淘汰,替换它的是PCIe
要说这个要从头说
首先,是pci,它是pc interface, 用来连接计算机的设备,最开始的时候它也连显卡,但是后来带宽不足,于是AGP诞生
全称Accelerated Graphics Port 图形加速接口
AGP的工作原理,详见http://computer.howstuffworks.com/agp4.htm
简单说就是,AGP会建立一块AGP存储区(系统内存中的块),用来弥补显卡显存的不足,集成显卡没有显存就直接用这个
DX的动态存储和静态存储的动态就存在这个AGP存储区,也就是内存里
至于修改内存数据和修改显存数据之间的速度关系
我的理解是这样的,要修改数据都是有CPU发出的指令,修改内存的直接就访问了,修改显存的资源要先载入内存再进入显存,所以会慢
再后来AGP技术就被PCIe替换了 PCIe全称是 pci express 就是扩展型的pci,它的设计初衷就是利用串行的优势获得更高的传输速度
曾经还有个pcie和agp同时出现的时代
访问缓存内容
每个buffer接口中都有lock函数,它会返回缓存指针,得到缓存指针之后就可以对缓存进行读写
操作完毕后需要unlock
顶点缓存和索引缓存的这两个函数用法同,参数同
获取缓存信息,缓存信息就是我们创建缓存的时候输入的那些参数
为了管理缓存信息,DX有一个成员和创建缓存函数几乎相同的结构体
typedef struct D3DVERTEXBUFFER_DESC {
D3DFORMAT Format;
D3DRESOURCETYPE Type;
DWORD Usage;
D3DPOOL Pool;
UINT Size;
DWORD FVF;
} D3DVERTEXBUFFER_DESC, *LPD3DVERTEXBUFFER_DESC;
顶点缓存信息结构体
再调用创建的缓存自带的函数 GetDesc就可以获得信息
绘制的精髓是
D3D设备接口中的SetRenderState函数
许多特效就是利用渲染状态改变来实现的
要实现设定的顶点缓存和索引缓存的绘制
需要先把这两个缓存传给设备,还需要指明的还有顶点格式
之后,调用DrawPrimitive把当前设备中的几何信息通过绘制流水线传输
这个是基本的图元绘制函数,不使用索引,可以画三角形,点和线
HRESULT DrawPrimitive(
[in] D3DPRIMITIVETYPE PrimitiveType,
[in] UINT StartVertex,
[in] UINT PrimitiveCount
);
还有个使用索引的
HRESULT DrawIndexedPrimitive(
[in] D3DPRIMITIVETYPE Type,
[in] INT BaseVertexIndex,
[in] UINT MinIndex,
[in] UINT NumVertices,
[in] UINT StartIndex,
[in] UINT PrimitiveCount
);
这是DX9里的版本
在DX10和11里已经简化了
void DrawIndexed(
[in] UINT IndexCount,
[in] UINT StartIndexLocation,
[in] INT BaseVertexLocation
);
龙书里提到了
BaseVertexIndex
的用法
它代表一个偏移量
想将多个几何体的顶点合并到一个全局缓存,对于每个物体必须重新计算索引
DX这里的做法是给每个索引信息添加一个物体顶点信息在全局顶点缓存中的偏移量。
标明这个几何体的索引信息是从哪里开始的,这个偏移用顶点数量来度量
*这个机制在dx9里有,在新的dx10和11标准里可能已经去掉,以后接触到了留意一下
所有绘制语句需要写在
BeginScene和EndScene之间
//
//
// File: cube.cpp
//
// Author: Frank Luna (C) All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0
//
// Desc: Renders a spinning cube in wireframe mode. Demonstrates vertex and
// index buffers, world and view transformations, render states and
// drawing commands.
//
//
#include "d3dUtility.h"
//
// Globals
//
IDirect3DDevice9* Device = 0;
const int Width = 640;
const int Height = 480;
IDirect3DVertexBuffer9* VB = 0;
IDirect3DIndexBuffer9* IB = 0;
//
// Classes and Structures
//
struct Vertex
{
Vertex(){}
Vertex(float x, float y, float z)
{
_x = x; _y = y; _z = z;
}
float _x, _y, _z;
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ;
//
// Framework Functions
//
bool Setup()
{
//
// Create vertex and index buffers.
//
Device->CreateVertexBuffer(
8 * sizeof(Vertex),
D3DUSAGE_WRITEONLY,
Vertex::FVF,
D3DPOOL_MANAGED,
&VB,
0);
Device->CreateIndexBuffer(
36 * sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&IB,
0);
//
// Fill the buffers with the cube data.
//
// define unique vertices:
Vertex* vertices;
VB->Lock(0, 0, (void**)&vertices, 0);
// vertices of a unit cube
vertices[0] = Vertex(-1.0f, -1.0f, -1.0f);
vertices[1] = Vertex(-1.0f, 1.0f, -1.0f);
vertices[2] = Vertex( 1.0f, 1.0f, -1.0f);
vertices[3] = Vertex( 1.0f, -1.0f, -1.0f);
vertices[4] = Vertex(-1.0f, -1.0f, 1.0f);
vertices[5] = Vertex(-1.0f, 1.0f, 1.0f);
vertices[6] = Vertex( 1.0f, 1.0f, 1.0f);
vertices[7] = Vertex( 1.0f, -1.0f, 1.0f);
VB->Unlock();
// define the triangles of the cube:
WORD* indices = 0;
IB->Lock(0, 0, (void**)&indices, 0);
// front side
indices[0] = 0; indices[1] = 1; indices[2] = 2;
indices[3] = 0; indices[4] = 2; indices[5] = 3;
// back side
indices[6] = 4; indices[7] = 6; indices[8] = 5;
indices[9] = 4; indices[10] = 7; indices[11] = 6;
// left side
indices[12] = 4; indices[13] = 5; indices[14] = 1;
indices[15] = 4; indices[16] = 1; indices[17] = 0;
// right side
indices[18] = 3; indices[19] = 2; indices[20] = 6;
indices[21] = 3; indices[22] = 6; indices[23] = 7;
// top
indices[24] = 1; indices[25] = 5; indices[26] = 6;
indices[27] = 1; indices[28] = 6; indices[29] = 2;
// bottom
indices[30] = 4; indices[31] = 0; indices[32] = 3;
indices[33] = 4; indices[34] = 3; indices[35] = 7;
IB->Unlock();
//
// Position and aim the camera.
//
D3DXVECTOR3 position(0.0f, 0.0f, -5.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &position, &target, &up);
Device->SetTransform(D3DTS_VIEW, &V);
//
// Set the projection matrix.
//
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)Width / (float)Height,
1.0f,
1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);
//
// Switch to wireframe mode.
//
Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
return true;
}
void Cleanup()
{
d3d::Release<IDirect3DVertexBuffer9*>(VB);
d3d::Release<IDirect3DIndexBuffer9*>(IB);
}
bool Display(float timeDelta)
{
if( Device )
{
//
// spin the cube:
//
D3DXMATRIX Rx, Ry;
// rotate 45 degrees on x-axis
D3DXMatrixRotationX(&Rx, 3.14f / 4.0f);
// incremement y-rotation angle each frame
static float y = 0.0f;
D3DXMatrixRotationY(&Ry, y);
y += timeDelta;
// reset angle to zero when angle reaches 2*PI
if( y >= 6.28f )
y = 0.0f;
// combine x- and y-axis rotation transformations.
D3DXMATRIX p = Rx * Ry;
Device->SetTransform(D3DTS_WORLD, &p);
//
// draw the scene:
//
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
Device->BeginScene();
Device->SetStreamSource(0, VB, 0, sizeof(Vertex));
Device->SetIndices(IB);
Device->SetFVF(Vertex::FVF);
// Draw cube.
Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
Device->EndScene();
Device->Present(0, 0, 0, 0);
}
return true;
}
//
// WndProc
//
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
case WM_DESTROY:
::PostQuitMessage(0);
break;
case WM_KEYDOWN:
if( wParam == VK_ESCAPE )
::DestroyWindow(hwnd);
break;
}
return ::DefWindowProc(hwnd, msg, wParam, lParam);
}
//
// WinMain
//
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE prevInstance,
PSTR cmdLine,
int showCmd)
{
if(!d3d::InitD3D(hinstance,
Width, Height, true, D3DDEVTYPE_HAL, &Device))
{
::MessageBox(0, "InitD3D() - FAILED", 0, 0);
return 0;
}
if(!Setup())
{
::MessageBox(0, "Setup() - FAILED", 0, 0);
return 0;
}
d3d::EnterMsgLoop( Display );
Cleanup();
Device->Release();
return 0;
}
附上一段源码,运行结果是一个转动的立方体
源码还需要两个文件支持,是龙书里封装好的dx初始化相关