Thread: Vertex buffer hell

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005

    Vertex buffer hell

    First, read this: (The latest post, "Vertex Buffers").

    They're out to get me.

    Here's my vertex struct along with the array of them I'm using:

      float x, y, z;
      DWORD color;;
    CUSTOMVERTEX g_4Verts[] = 
      {-1.0f, -1.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0)},
      {-1.0f,  1.0f, 5.0f, D3DCOLOR_XRGB(255, 255, 0)},
      { 1.0f, -1.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0)},
      { 1.0f,  1.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255)}
    USHORT g_Indices[] = { 0, 1, 2, 1, 3, 2 };
    So first I was locking the vertex buffer with D3DLOCK_DISCARD only to find out "Direct3D9: (ERROR) :Can specify D3DLOCK_DISCARD or D3DLOCK_NOOVERWRITE for only Vertex Buffers created with D3DUSAGE_DYNAMIC".

    So I set the lock flag to 0 (since I don't know what else to set it to) and now the Lock() calls succeed but still nothing is drawn.

    Here's where I create the vertex / index buffers and fill 'em up. I think it's pretty self explanatory; If you want me to post up the vertex / index buffer classes, tell me:

    // The 0 (3rd arg) to SetData() is setting the Lock() flags to 0
    void Game::OnCreateDevice(LPDIRECT3DDEVICE9 pDevice)
      m_VB4.CreateBuffer(pDevice, 4, D3DFVF_XYZ | D3DFVF_DIFFUSE, sizeof(CUSTOMVERTEX));
      m_VB4.SetData(4, g_4Verts, 0); 
      m_IB.CreateBuffer(pDevice, 6, D3DFMT_INDEX16); 
      m_IB.SetData(6, g_Indices, 0); 
    The rendering:

    void Game::OnRenderFrame(LPDIRECT3DDEVICE9 pDevice)
      pDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 200),
                     1.0f, 0);
      m_VB4.Render(pDevice, 2, D3DPT_TRIANGLELIST);
      pDevice->Present(0, 0, 0, 0);
    Here's the Render() method itself:

    void bgVertexBuffer::Render(LPDIRECT3DDEVICE9 pDevice, UINT uNumPrimitives,
                                D3DPRIMITIVETYPE primitiveType)
      if (!pDevice)
      pDevice->SetStreamSource(0, m_pVB, 0, m_uVertexSize);
      // Got milk? Oops, I mean, Got an index buffer?
      if (m_pIB)
        pDevice->DrawIndexedPrimitive(primitiveType, 0, 0, m_uNumVertices, 0, uNumPrimitives);
        pDevice->DrawPrimitive(primitiveType, 0, uNumPrimitives);
    While debugging I don't get any DirectX error messages in the output window so I'm fairly stumped. It's nothing to do with setting the Lock() flags to 0 is it?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Since you are wrapping the vertex buffer and index buffer it's impossible for me to help.

    IDirect3DVertexBuffer9 *m_pVB=NULL
    VertexStruct *pVerts=NULL;
    m_pVB->Lock(0,0,(void **)&pVerts,0);
    //Write to vertex buffer
    For index buffers you just create the buffer, lock it, write to it, unlock it, set the index buffer, and render.

    If you fail to unlock your vertex or index buffers nothing will be rendered.

    Also you really need to get a book. Tutorials only go so far. A book will explain to you how Direct3D and
    DirectX all fit together and work. A tutorial can never teach you everything. What you will get is
    code that renders something but you won't have any idea why.
    Last edited by VirtualAce; 12-02-2006 at 08:25 PM.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    I really shouldn't do this late at night. You know what the problem was?
    I wasn't doing this:

      D3DXMATRIX id;
      pDevice->SetTransform(D3DTS_WORLD, &id);
      pDevice->SetTransform(D3DTS_VIEW, &id);
      D3DXMATRIX projection;
      float aspect = (float) m_pFramework->GetWidth() / (float) m_pFramework->GetHeight();
      D3DXMatrixPerspectiveFovLH(&projection, D3DX_PI / 3, aspect, 1.0f, 1000.0f);
      pDevice->SetTransform(D3DTS_PROJECTION, &projection);
    I am such an idiot.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple (?) problem rendering a vertex buffer
    By Dark_Phoenix in forum Game Programming
    Replies: 4
    Last Post: 08-11-2007, 07:32 PM
  2. error help making no sense
    By tunerfreak in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2007, 07:55 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM
  5. Using D3DFVF_XYZRHW flag for a Vertex Buffer
    By lightatdawn in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2001, 02:48 PM