如何绘制2个立方体而不是一个? (DirectX9)

时间:2019-07-18 18:54:57

标签: c++ visual-c++ directx

这里我的代码绘制了一个立方体:

void CreateCube(
    LPDIRECT3DDEVICE9 &ptr_dxDevice, 
    LPDIRECT3DINDEXBUFFER9 &ptr_indexBuffer, 
    LPDIRECT3DVERTEXBUFFER9 &ptr_vertexBuffer,
    int cubeNumber
)
{
    // create the vertices using the CUSTOMVERTEX struct
    CUSTOMVERTEX vertices[] =
    {
        { -0.2f,  0.2f, -0.2f, 0xffff0000,},
        {  0.2f,  0.2f, -0.2f, 0xff00ff00,},
        { -0.2f, -0.2f, -0.2f, 0xffff0000,},
        {  0.2f, -0.2f, -0.2f, 0xff00ff00,},
        { -0.2f,  0.2f,  0.2f, 0xffff0000,},
        {  0.2f,  0.2f,  0.2f, 0xff00ff00,},
        { -0.2f, -0.2f,  0.2f, 0xffff0000,},
        {  0.2f, -0.2f,  0.2f, 0xff00ff00,},
    };

    // create a vertex buffer interface called v_buffer
    ptr_dxDevice->CreateVertexBuffer(
        8 * sizeof(CUSTOMVERTEX),
        0,
        drawFVF,
        D3DPOOL_MANAGED,
        &ptr_vertexBuffer,
        NULL);

    VOID* pVoid;    // a void pointer

    // lock v_buffer and load the vertices into it
    ptr_vertexBuffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, vertices, sizeof(vertices));
    ptr_vertexBuffer->Unlock();

    // create the indices using an int array
    short indices[] =
    {
        0, 1, 2,    // side 1
        2, 1, 3,
        4, 0, 6,    // side 2
        6, 0, 2,
        7, 5, 6,    // side 3
        6, 5, 4,
        3, 1, 7,    // side 4
        7, 1, 5,
        4, 5, 0,    // side 5
        0, 5, 1,
        3, 7, 2,    // side 6
        2, 7, 6,
    };

    // create an index buffer interface called i_buffer
    ptr_dxDevice->CreateIndexBuffer(36 * sizeof(short),
        0,
        D3DFMT_INDEX16,
        D3DPOOL_MANAGED,
        &ptr_indexBuffer,
        NULL);


    // lock i_buffer and load the indices into it
    ptr_indexBuffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, indices, sizeof(indices));
    ptr_indexBuffer->Unlock();

    ptr_dxDevice->SetFVF(drawFVF);

    // select the vertex and index buffers to use
    ptr_dxDevice->SetStreamSource(0, vertexBuffer, 0, sizeof(CUSTOMVERTEX));
    ptr_dxDevice->SetIndices(ptr_indexBuffer);

    // draw the cube
    ptr_dxDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
}

当我尝试使用此代码创建2个或更多多维数据集时,出现内存冲突错误。我正在使用顶点bufefr和索引缓冲区。

请帮我修复此代码-为了能够绘制2个或更多的多维数据集,我该怎么办?

谢谢

P.S。 使用DirectX9 Visual Studio 2017 今天是星期四

0 个答案:

没有答案