Jump to content
  • Advertisement
Belfa96

3D Directx9 - Generate mesh from vertex and index buffer

Recommended Posts

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();

 

Share this post


Link to post
Share on other sites
Advertisement

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!