在环境初始化后,我们要做到就是画简单图形了
HRESULT CreateVertexBuffer(
UINT Length,
DWORD Usage,
DWORD FVF,
D3DPOOL Pool,
IDirect3DVertexBuffer9** ppVertexBuffer,
HANDLE* pSharedHandle
);
此函数是用来创建一段VertexBuffer的,就是在D3D里创建一段内存,用来存储Vertex的。其中Lenth参数是指
这段内存存储多少个Vertex,Usage可以为0,意味着没有值。如果要想复杂使用,请参考D3DUSAGE。而FVF则指的是
VERTEX的格式。D3DFVF里定义了各种格式以及使用方法。D3DPOOL是用来指示内存的管理方式,D3DPOOL 里有各种
枚举具体定义和使用方法。
ppVertexBuffer就是这个函数传出的地址,就是分配出来的内存的首地址。最后一个参数为保留参数。
有了点几何的缓存后,我们就要把数据装入这片内存了。
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
{
return E_FAIL;
}
//创建点集合内存块
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices
VOID* pVertices;
//给该片内存上锁,不允许其他活动访问破坏
if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
return E_FAIL;
//利用内存拷贝函数将点几何vertics拷贝到pVertices(vertices定义而构造)
memcpy( pVertices, vertices, sizeof(vertices) );
g_pVB->Unlock();
附带
struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw; // The transformed position for the vertex
DWORD color; // The vertex color
};
CUSTOMVERTEX vertices[] =
{
{ 150.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
{ 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },
{ 50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },
};
这个是Direct9例子带的示例,不知道是否能定义其他样式的点,如何识别?Direct3D如何要求输入格式?
画图。将已经放入到内存的点几何用指定形式化出来
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
// Draw the triangles in the vertex buffer. This is broken into a few
// steps. We are passing the vertices down a "stream", so first we need
// to specify the source of that stream, which is our vertex buffer. Then
// we need to let D3D know what vertex shader to use. Full, custom vertex
// shaders are an advanced topic, but in most cases the vertex shader is
// just the FVF, so that D3D knows what type of vertices we are dealing
// with. Finally, we call DrawPrimitive() which does the actual rendering
// of our geometry (in this case, just one triangle).
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );//放入显示卡设备
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );//设置点的样式
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );//以三角形形式画出
// End the scene
g_pd3dDevice->EndScene();
}
Direct学习3:CreateVertexBuffer
最新推荐文章于 2023-10-05 12:46:20 发布