0

I need to create a vertex buffer out of vertices. This tutorial in MSDN is splendid.

But this one holds good for DirectX10+. I am using DirectX9. How to accomplish the same here?

Thanks.

Jordan.J.D
  • 7,999
  • 11
  • 48
  • 78
bdhar
  • 21,619
  • 17
  • 70
  • 86

1 Answers1

0
//Definitions
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL;
struct SimpleVertexCombined{
    D3DXVECTOR3 Pos;  
    SimpleVertexCombined(FLOAT X, FLOAT Y, FLOAT Z):Pos(X, Y, Z){}  
};
d3ddev->CreateVertexBuffer(8*sizeof(SimpleVertexCombined),
                           0,
                           0,
                           D3DPOOL_MANAGED,
                           &v_buffer,
                           NULL);
SimpleVertexCombined* cube = 0;
v_buffer->Lock(0, 0, (void**)&cube, 0);
    cube[0] = SimpleVertexCombined(-1.0f, -1.0f, -1.0f);
    cube[1] = SimpleVertexCombined(-1.0f,  1.0f, -1.0f);
    cube[2] = SimpleVertexCombined( 1.0f,  1.0f, -1.0f);
    cube[3] = SimpleVertexCombined( 1.0f, -1.0f, -1.0f);
    cube[4] = SimpleVertexCombined(-1.0f, -1.0f,  1.0f);
    cube[5] = SimpleVertexCombined(-1.0f,  1.0f,  1.0f);
    cube[6] = SimpleVertexCombined( 1.0f,  1.0f,  1.0f);
    cube[7] = SimpleVertexCombined( 1.0f, -1.0f,  1.0f);
v_buffer->Unlock();

I think this should work, First we create the v_buffer using CreateVertexBuffer(), the first value is its size so remember to change that number if you're adding more vertices, and then we use the pointer cube to transfer data to the buffer. You can also use memcpy() between the lock and unlock if you want to transfer data from a pre-existing array, like this:

SimpleVertexCombined verticesCombo[] = {
    D3DXVECTOR3( 0.0f, 0.5f, 0.5f ),
    D3DXVECTOR3( 0.0f, 0.0f, 0.5f ),
    D3DXVECTOR3( 0.5f, -0.5f, 0.5f ),
    D3DXVECTOR3( 0.5f, 0.0f, 0.0f ),
    D3DXVECTOR3( -0.5f, -0.5f, 0.5f ),
    D3DXVECTOR3( 0.0f, 0.5f, 0.0f ),
};
VOID* pVoid;
v_buffer->Lock(0, 0, (void**)&cube, 0);
    memcpy(pVoid, verticesCombo, sizeof(verticesCombo));
v_buffer->Unlock();

All of this is done without colour, if you want colour you have to add it to the constructor, also if you want to render you have to create a D3DVERTEXELEMENT9 and a declaration to it.

HonestHeuristic
  • 336
  • 2
  • 14