我刚刚开始在C++中使用DirectX。我一直在学习DirectX教程。我必须在屏幕上绘制多个矩形。我创建了一个数组来存储矩形的4个顶点。此4个顶点的数组放置在另一个新创建数组的每个索引中。此数组被传递到VectorBuffer。VectorBuffer无法计算传递给它的数组的正确大小。因此,当调用渲染帧时,屏幕上看不到任何内容。如何在单个向量缓冲区中存储多个对象。
我的代码是:
CUSTOMVERTEX** Array = new CUSTOMVERTEX*[2];
CUSTOMVERTEX OurVertices1[] = {
{ 0, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), }, //meaning x,y,z,RHW,Dword
{ 100, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 0, 100, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
{ 100, 100, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
};
CUSTOMVERTEX OurVertices2[] = {
{ 200, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 400, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 200, 400, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
{ 400, 400, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
};
Array[0] = OurVertices1;// placing vertices in array 0 index
Array[1] = OurVertices2; // placing vertices in array 1 index
d3ddev->CreateVertexBuffer(8* sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
VOID* pVoid; // the void* we were talking about
v_buffer->Lock(0, 0, (void**)&pVoid, 0); // locks v_buffer, the buffer we made earlier
memcpy(pVoid, Array,sizeof(Array)); // passing array to vbuffer
v_buffer->Unlock(); // unlock v_buffer
d3ddev->SetFVF(CUSTOMFVF);
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
//render\u帧
void render_frame(LPDIRECT3DDEVICE9 d3ddev, LPDIRECT3DVERTEXBUFFER9 v_buffer)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
// select which vertex format we are using
// select the vertex buffer to display
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
d3ddev->SetFVF(CUSTOMFVF);
// copy the vertex buffer to the back buffer
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);
// copy the vertex buffer to the back buffer
//d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
但无法在屏幕上获取两个对象。有没有其他方法可以将多个对象放置在向量缓冲区中?