Thread: Vertex buffer hell

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

    Vertex buffer hell

    First, read this: http://ahlukalt.blogspot.com/ (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:

    Code:
    struct CUSTOMVERTEX
    {
      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:

    Code:
    // 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); 
      m_VB4.SetIndexBuffer(&m_IB); 
    }
    The rendering:

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

    Code:
    void bgVertexBuffer::Render(LPDIRECT3DDEVICE9 pDevice, UINT uNumPrimitives,
                                D3DPRIMITIVETYPE primitiveType)
    {
      if (!pDevice)
        return;
    
      pDevice->SetStreamSource(0, m_pVB, 0, m_uVertexSize);
      pDevice->SetFVF(m_dwFVF);
    
      // Got milk? Oops, I mean, Got an index buffer?
      if (m_pIB)
      {
        pDevice->SetIndices(m_pIB->GetBuffer());
        pDevice->DrawIndexedPrimitive(primitiveType, 0, 0, m_uNumVertices, 0, uNumPrimitives);
      }
      else
        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
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Since you are wrapping the vertex buffer and index buffer it's impossible for me to help.

    Code:
    IDirect3DVertexBuffer9 *m_pVB=NULL
    
    m_pDevice->CreateVertexBuffer(length*sizeof(VertexStruct::FVF),D3DUSAGE_WRITEONLY,VertexStruct::FVF,D3DPOOL_MANAGED,&m_pVB);
    
    VertexStruct *pVerts=NULL;
    m_pVB->Lock(0,0,(void **)&pVerts,0);
    
    //Write to vertex buffer
    
    m_pVB->Unlock();
    ...
    ...
    
    m_pDevice->SetFVF(VertexStruct::FVF);
    m_pDevice->SetStreamSource(0,m_pVB,0,sizeof(VertexStruct));
    
    //Render
    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
    Posts
    1,709
    I really shouldn't do this late at night. You know what the problem was?
    I wasn't doing this:

    Code:
      D3DXMATRIX id;
      D3DXMatrixIdentity(&id);
      pDevice->SetTransform(D3DTS_WORLD, &id);
      pDevice->SetTransform(D3DTS_VIEW, &id);
    
      D3DXMATRIX projection;
      D3DXMatrixIdentity(&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