Skip to main content
edited tags
Link
user1430
user1430
Source Link

Directx9 - Generate mesh from vertex and index buffer

I've got a single vertex buffer and an index buffer for a cube in Directx9. I now want to generate a LPD3DXMESH object from them, so that I can manage mouse picking and other stuff with the cube model. How can I achieve this? Here's the code of my vertex and index buffers:

CUSTOMVERTEX vertices[] =
{
    { -3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 0), },
    { 3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 0), },
    { -3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 0), },
    { 3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 0), },
    { -3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(0, 0, 0), },
    { 3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(0, 0, 0), },
    { -3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 0, 0), },
    { 3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 0, 0), },
};

d3ddev->CreateVertexBuffer(8 * sizeof(CUSTOMVERTEX),
                           0,
                           CUSTOMFVF,
                           D3DPOOL_MANAGED,
                           &vertex_buffer,
                           NULL);

VOID* pVoid;
vertex_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
vertex_buffer->Unlock();

short indices[] =
{
    0, 1, 2,
    2, 1, 3,
    4, 0, 6,
    6, 0, 2,
    7, 5, 6,
    6, 5, 4,
    3, 1, 7,
    7, 1, 5,
    4, 5, 0,
    0, 5, 1,
    3, 7, 2,
    2, 7, 6,
};

d3ddev->CreateIndexBuffer(36 * sizeof(short),
                          0,
                          D3DFMT_INDEX16,
                          D3DPOOL_MANAGED,
                          &index_buffer,
                          NULL);

index_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, indices, sizeof(indices));
index_buffer->Unlock();