Simple (?) problem rendering a vertex buffer

This is a discussion on Simple (?) problem rendering a vertex buffer within the Game Programming forums, part of the General Programming Boards category; Not sure what I am doing wrong here. Probably a simple mistake but I can't seem to find it. I ...

  1. #1
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174

    Simple (?) problem rendering a vertex buffer

    Not sure what I am doing wrong here. Probably a simple mistake but I can't seem to find it. I plan on having a CObject class that represents a single instance of an object; a mesh (.X file), vertex buffer, ect. This way I can load the object and create instances of it that will be held in seperate CObject's. Everything seems to be working fine except that nothing is being rendered to the display. I am still working on it and it is still a bit messy but here is what I have so far.

    CObject contains a struct that defines what kind of object it is.
    Code:
    typedef enum D3DDATATYPE
    {
        D3DOBJECTTYPE_NOOBJECT     = 0x000,
        D3DOBJECTTYPE_SIMPLEMESH   = 0x001,
        D3DOBJECTTYPE_SKINNEDMESH  = 0x002,
        D3DOBJECTTYPE_VERTEXBUFFER = 0x003
    } D3DDATATYPE;
    typedef struct D3DOBJECTTYPE
    {
        D3DDATATYPE Type;
        union
        {
            CMesh         *pSimpleMesh;
            CSkinnedMesh  *pSkinnedMesh;
            CVertexBuffer *pVertexBuffer;
        };
    } D3DOBJECTTYPE;
    I create a vertex buffer and an index buffer
    Code:
    CGameApp::CreateDevice()
    ...
            cstmVertex::PositionColor vertices[] =
            {
                {-1.0f, -1.0f, 0.0f, D3DCOLOR_XRGB( 255,   0,   0 ) },
                {-1.0f,  1.0f, 0.0f, D3DCOLOR_XRGB( 255, 255,   0 ) },
                { 1.0f, -1.0f, 0.0f, D3DCOLOR_XRGB(   0, 255,   0 ) },
                { 1.0f,  1.0f, 0.0f, D3DCOLOR_XRGB(   0,   0, 255 ) }
            };
            UINT indices[] = { 0, 1, 2, 1, 3, 2 };
            m_IndexBuffer.CreateBuffer(pDevice, 6, D3DFMT_INDEX16);
            m_IndexBuffer.SetData(6, indices);
            m_VertexBuffer.CreateBuffer(pDevice,                           // The D3DDevice
                                        4,                                 // num verts
                                        sizeof(cstmVertex::PositionColor), // vert size
                                        2,                                 // num primitives
                                        D3DPT_TRIANGLELIST,                // primitive type
                                        D3DFVF_XYZ | D3DFVF_DIFFUSE,       // FVF
                                        false,                             // dynamic
                                        (void*)vertices,                   // vertices
                                        &m_IndexBuffer);                   // CIndexBuffer
            m_pCube = new (CObject);
            m_VertexBuffer.CreateInstance(pDevice, m_pCube);
            D3DVERTEXELEMENT9 VertexElement[] =
            {
                { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
                { 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
                D3DDECL_END()
            };
            LPDIRECT3DVERTEXDECLARATION9 VertexDeclaration = 0;
            pDevice->CreateVertexDeclaration(VertexElement, &VertexDeclaration);
            pDevice->SetVertexDeclaration(VertexDeclaration);
    ...
     
     
    bool CVertexBuffer::CreateBuffer(LPDIRECT3DDEVICE9 pDevice,
                                     UINT              NumVertices,
                                     UINT              VertexSize,
                                     UINT              NumPrimitives,
                                     D3DPRIMITIVETYPE  PrimitiveType,
                                     DWORD             FVF,
                                     bool              dynamic,
                                     void             *pVertexData,
                                     CIndexBuffer     *pIB)
    {
        if (m_pVB) Release();
        m_NumVertices   = NumVertices;
        m_VertexSize    = VertexSize;
        m_NumPrimitives = NumPrimitives;
        m_PrimitiveType = PrimitiveType;
        m_FVF           = FVF;
        // Dynamic buffers can't be in D3DPOOL_MANAGED
        D3DPOOL pool = dynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
        DWORD usage  = dynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : D3DUSAGE_WRITEONLY;
        if (FAILED(pDevice->CreateVertexBuffer(m_NumVertices * m_VertexSize,
                                               usage,
                                               m_FVF,
                                               pool,
                                               &m_pVB,
                                               NULL ) ) )
        {
            SHOW_ERROR("CreateVertexBuffer failed.");
            return false;
        }
        // If there is an index buffer, set it
        if (pIB)
            m_pIB = pIB;
        // Set the vertex data
        char *pData;
        // Lock the vertex buffer
        if (FAILED(m_pVB->Lock( 0, 0, (void**)&pData, D3DLOCK_DISCARD) ) )
        {
            SHOW_ERROR("IDirect3DVertexBuffer9::Lock failed.");
            return false;
        }
        // Copy vertex data to the vertex buffer
        memcpy(pData, pVertexData, m_NumVertices * m_VertexSize);
        // Unlock vertex buffer
        if (FAILED(m_pVB->Unlock() ) )
        {
            SHOW_ERROR("IDirect3DVertexBuffer9::Unlock failed.");
            return false;
        }
        return true;
    }
    And I create an instance (CObject)
    Code:
    bool CVertexBuffer::CreateInstance(LPDIRECT3DDEVICE9 pDevice,
                                       CObject *pObjToCreate)
    {
        if (!pObjToCreate)
            return false;
        pObjToCreate->SetObjRef(pDevice,
                                D3DOBJECTTYPE_VERTEXBUFFER,
                                (void*)this);
        return true;
    }
     
     
    void CObject::SetObjRef(LPDIRECT3DDEVICE9 pDevice,
                            D3DDATATYPE Type,
                            void *pObject)
    {
        Release();
        if (Type == D3DOBJECTTYPE_VERTEXBUFFER)
        {
            m_Object.Type          = D3DOBJECTTYPE_VERTEXBUFFER;
            m_Object.pVertexBuffer = (CVertexBuffer*)pObject;
        }
        return;
    }
    and then I render it
    Code:
    CGameApp::Render()
    ...
            m_camera.Update();
            pDevice->SetTransform(D3DTS_VIEW, m_camera.GetViewMatrix() );
            pDevice->Clear(0,
                           0,
                           D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                           D3DCOLOR_XRGB(0, 0, 0),
                           1.0f,
                           0);
            pDevice->BeginScene();
            m_pCube->Render(pDevice);
            pDevice->EndScene();
            pDevice->Present(0, 0, 0, 0);
    ...
     
     
    void CObject::Render(LPDIRECT3DDEVICE9 pDevice) {
     pDevice->SetTransform(D3DTS_WORLD, GetTransform() );
     switch (m_Object.Type)
     {
            case D3DOBJECTTYPE_VERTEXBUFFER :
            {
                m_Object.pVertexBuffer->Render(pDevice, NULL);
            }
            break;
        }
        return;
    }
     
    void CVertexBuffer::Render(LPDIRECT3DDEVICE9 pDevice,
                               CObject *pObject)
    {
        pDevice->SetStreamSource(0, m_pVB, 0, m_VertexSize);
        pDevice->SetFVF(m_FVF);
        if (m_pIB)
        {
            pDevice->SetIndices(m_pIB->GetBuffer() );
            pDevice->DrawIndexedPrimitive(m_PrimitiveType,
                                          0,
                                          0,
                                          m_NumVertices,
                                          0,
                                          m_NumPrimitives);
        } else
        {
            pDevice->DrawPrimitive(m_PrimitiveType, 0, m_NumPrimitives);
        }
        return;
    }
    Anyone see anything I am missing that would cause this not to display anything?
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  2. #2
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    #$!@, Just realized I posted in the wrong forum. Can a Mod please move this to the Game Programming forum? Thanks.
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Moved as requested.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Dude your Heroes link is broken.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    Sorry, link fixed.

    I am starting to think this might be more of a C++ question than a DX question.

    Anyone have any ideas? I am still lost on this one.
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  2. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  3. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  4. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21