Direct学习3:CreateVertexBuffer

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

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Direct3D是一种用于进行图形渲染的API,可以用于视音频播放。在Direct3D中,可以使用纹理(Texture)来播放RGB格式的视音频。 在这个示例中,我们需要准备一个RGB格式的视频文件,可以是逐帧的图片序列,也可以是实时的摄像头数据。首先,我们需要创建一个Direct3D设备,并设置好它的渲染目标。然后,我们需要创建一个纹理对象,将视频数据加载到纹理中。 创建纹理对象的方法如下: ``` IDirect3DTexture9* texture; HRESULT result = device->CreateTexture(width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &texture, NULL); ``` 然后,将视频数据拷贝到纹理对象中: ``` D3DLOCKED_RECT lockedRect; result = texture->LockRect(0, &lockedRect, NULL, D3DLOCK_DISCARD); memcpy(lockedRect.pBits, videoData, width * height * 4); result = texture->UnlockRect(0); ``` 接下来,我们需要创建一个矩形,将纹理绘制到屏幕上: ``` IDirect3DVertexBuffer9* vertexBuffer; result = device->CreateVertexBuffer(4 * sizeof(Vertex), D3DUSAGE_DYNAMIC, Vertex::FVF, D3DPOOL_DEFAULT, &vertexBuffer, NULL); ``` 然后,设置矩形的顶点信息: ``` Vertex* vertices; result = vertexBuffer->Lock(0, 0, (void**)&vertices, D3DLOCK_DISCARD); vertices[0] = Vertex(-1.0f, 1.0f, 0.0f, 0.0f, 0.0f); vertices[1] = Vertex(1.0f, 1.0f, 0.0f, 1.0f, 0.0f); vertices[2] = Vertex(-1.0f, -1.0f, 0.0f, 0.0f, 1.0f); vertices[3] = Vertex(1.0f, -1.0f, 0.0f, 1.0f, 1.0f); result = vertexBuffer->Unlock(); ``` 最后,我们需要在每一帧中进行渲染和显示: ``` result = device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); result = device->BeginScene(); result = device->SetFVF(Vertex::FVF); result = device->SetStreamSource(0, vertexBuffer, 0, sizeof(Vertex)); result = device->SetTexture(0, texture); result = device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); result = device->EndScene(); result = device->Present(NULL, NULL, NULL, NULL); ``` 通过上述步骤,我们就可以通过Direct3D播放RGB格式的视音频。当然,在实际应用中,还可以进行更多的优化和功能的添加,比如支持音频的播放和控制等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值