0
votes

I have just started using DirectX with C++.I have been following DirectX tutorial. I have to draw multiple rectangles on the screen. I have created an array to store the 4 vertices of rectangle. This array of 4 vertices are placed in each index of another newly created array. This array is passed to the VectorBuffer. The VectorBuffer could not calculate the correct size of array passed to it.Due to this when render frame is called nothing can be seen on the screen. How to store multiple objects in single vector buffer.

My code is :

CUSTOMVERTEX** Array = new CUSTOMVERTEX*[2];
CUSTOMVERTEX OurVertices1[] = {
        { 0, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },   //meaning x,y,z,RHW,Dword

    { 100, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },

    { 0, 100, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },


    { 100, 100, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },

    };

    CUSTOMVERTEX OurVertices2[] = {
        { 200, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },

    { 400, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },

    { 200, 400, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },


    { 400, 400, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },

    };

    Array[0] = OurVertices1;// placing vertices in array 0 index
    Array[1] = OurVertices2; // placing vertices in array 1 index


d3ddev->CreateVertexBuffer(8* sizeof(CUSTOMVERTEX),
        0,
        CUSTOMFVF,
        D3DPOOL_MANAGED,
        &v_buffer,
        NULL);
    VOID* pVoid;    // the void* we were talking about

    v_buffer->Lock(0, 0, (void**)&pVoid, 0);    // locks v_buffer, the buffer we made earlier
    memcpy(pVoid, Array,sizeof(Array));    // passing array to vbuffer
    v_buffer->Unlock();    // unlock v_buffer

    d3ddev->SetFVF(CUSTOMFVF);
    d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));

//render_frame

void render_frame(LPDIRECT3DDEVICE9 d3ddev, LPDIRECT3DVERTEXBUFFER9 v_buffer)
{
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3ddev->BeginScene();

    // select which vertex format we are using



    // select the vertex buffer to display
    d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
    d3ddev->SetFVF(CUSTOMFVF);
    // copy the vertex buffer to the back buffer
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);

    // copy the vertex buffer to the back buffer
    //d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);

    d3ddev->EndScene();

    d3ddev->Present(NULL, NULL, NULL, NULL);
}

But could not get two object on the screen. Is there any other method to place multiple object in vector buffer?

1
Do you use an IndexBuffer together with your VertexBuffer?puelo
No I havent used it. Do I need to use it?Developer
Not necessarily. It depends on how you draw the rectangle. (i.e. how does your draw call look)?puelo
@puelo I have placed the code of render_frame where I am drawing rectangle. you can see it nowDeveloper
Looks good. You could try to put all vertices inside one static array instead of wrapping both inside another array. Otherwise you might need to debug drawing process.puelo

1 Answers

1
votes

You are creating buffer to hold 8 vertex structs, but then store there just a single pointer by calling memcpy(pVoid, Array, sizeof(Array));. Note that Array is just a plain pointer, despite of its name. You should initialize vertex buffer with vertex data:

CUSTOMVERTEX OurVertices[] =
{
//  1
    { 0, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },   //meaning x,y,z,RHW,Dword
    { 100, 0, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
    { 0, 100, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 0), },
    { 100, 100, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0), },
//  2
    { 200, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
    { 400, 200, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
    { 200, 400, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), },
    { 400, 400, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255), },
};

memcpy(pVoid, OurVertices, sizeof(OurVertices)); 

Or you can just initialize those vertices in-place by casting static_cast<CUSTOMVERTEX *>(pVoid).