在Direct X中绘制一个矩形

时间:2013-04-07 19:50:32

标签: c++ directx-9 vertex

在这里使用教程,我设法在屏幕上显示红色三角形:http://www.directxtutorial.com/Lesson.aspx?lessonid=9-4-4

CUSTOMVERTEX OurVertices[] =
{
    { 0, 0, 0, 1.0f, D3DCOLOR_XRGB( 127, 0, 0 ) },
    { WIDTH, 0, 0, 1.0f, D3DCOLOR_XRGB( 127, 0, 0 ) },
    { 0, 300, 0, 1.0f, D3DCOLOR_XRGB( 127, 0, 0 ) },
    { WIDTH, 300, 0, 1.0f, D3DCOLOR_XRGB( 127, 0, 0 ) }
};

d3dDevice->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),
    0,
    CUSTOMFVF,
    D3DPOOL_MANAGED,
    &vBuffer,
    NULL);

VOID* pVoid;    // the void* we were talking about

vBuffer->Lock(0, 0, (void**)&pVoid, 0);    // locks v_buffer, the buffer we made earlier
memcpy(pVoid, OurVertices, sizeof(OurVertices));    // copy vertices to the vertex buffer
vBuffer->Unlock();    // unlock v_buffer

d3dDevice->SetFVF(CUSTOMFVF);
d3dDevice->SetStreamSource(0, vBuffer, 0, sizeof(CUSTOMVERTEX));
d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

但是你可以看到我真的想画一个矩形。

我已经更改了Primitive以绘制2个三角形并将缓冲区大小扩展为我自定义顶点的4 *大小但是我不能说我理解如何从我的三角形到我想要的矩形:

enter image description here

有没有更好的方法来绘制矩形而不是使用四边形,因为我只想在它上面放一些文字:

http://1.bp.blogspot.com/-6HjFVnrVM94/TgRq8oP4U-I/AAAAAAAAAKk/i8N0OZU999E/s1600/monkey_island_screen.jpg

1 个答案:

答案 0 :(得分:9)

我必须释放缓冲区以允许4个顶点数组大小:

d3dDevice->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX),
    0,
    CUSTOMFVF,
    D3DPOOL_MANAGED,
    &vBuffer,
    NULL);

然后将绘图基元从TRIANGLELIST更改为STRIP,将绘制的三角形数量扩展为2

d3dDevice->DrawPrimitive (D3DPT_TRIANGLESTRIP, 0, 2 );

来源:http://www.mdxinfo.com/tutorials/tutorial4.php