简体   繁体   English

DirectX:在向量缓冲区中存储多个顶点

[英]DirectX: Storing Multiple Vertices in Vector Buffer

I have just started using DirectX with C++.I have been following DirectX tutorial. 我刚刚开始将DirectX与C ++一起使用。我一直在关注DirectX教程。 I have to draw multiple rectangles on the screen. 我必须在屏幕上绘制多个矩形。 I have created an array to store the 4 vertices of rectangle. 我创建了一个数组来存储矩形的4个顶点。 This array of 4 vertices are placed in each index of another newly created array. 这个4个顶点的数组放置在另一个新创建的数组的每个索引中。 This array is passed to the VectorBuffer. 该数组传递给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. VectorBuffer无法计算传递给它的数组的正确大小,因此调用渲染帧时在屏幕上看不到任何内容。 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 // 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? 还有其他方法可以将多个对象放置在向量缓冲区中吗?

You are creating buffer to hold 8 vertex structs, but then store there just a single pointer by calling memcpy(pVoid, Array, sizeof(Array)); 您正在创建用于容纳8个顶点结构的缓冲区,然后通过调用memcpy(pVoid, Array, sizeof(Array));仅将单个指针存储在此处memcpy(pVoid, Array, sizeof(Array)); . Note that Array is just a plain pointer, despite of its name. 请注意,尽管其名称是Array ,但它只是一个普通的指针。 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) . 或者,您也可以通过强制转换static_cast<CUSTOMVERTEX *>(pVoid)就地初始化这些顶点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM