Direct学习3:CreateVertexBuffer

本文介绍了使用Direct3D API创建顶点缓冲区并绘制基本图形的过程。通过创建顶点缓冲区并填充自定义顶点数据,实现了简单的三角形绘制。文章详细展示了使用 IDirect3DDevice9 接口的方法,包括设置流源、顶点格式和调用 DrawPrimitive 函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在环境初始化后,我们要做到就是画简单图形了
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();
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值