Drawing a rectangle in Direct X
Asked Answered
C

1

5

Using the tutorial here, I have managed to get a red triangle up on my screen: 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);

But you can see that I really want to be drawing a rectangle.

I have changed the Primitive to draw 2 triangles and extended the buffer size to 4*size of my custom vertex but I can't really say I understand how to get it from my triangle to my rectangle I would like:

enter image description here

Is there a better way of drawing a rectangle rather than using a quad considering I just want to sling some text on top of it something like this:

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

Cima answered 7/4, 2013 at 19:50 Comment(7)
Correct me if I'm wrong, but you should have four vertices and an index buffer containing the proper indices for two triangles to make your rectangle.Roguish
So 4 vertices = the 4 points of a square and an indices is the order at which to draw them? I obviously am completely fresh to this vertex idea, could I have a bit of a hand getting over this hurdle? Or maybe an example of specifically a rectangle?Cima
So in your vertex buffer, you'd have, e.g., {(-2, 2, 0), (2, 2, 0), (2, 1, 0), (-2, 1, 0)} (top left, top right, bottom right, bottom left) and your index buffer might contain {(0, 1, 3), (3, 1, 2)} (TL, TR, BL, followed by BL, TR, BR). The index buffer just tells it which vertices come in which order. You should be aware of whether it's CW or CCW as well for culling. You have to do some addition SetIndexBuffer etc. with it as well. Forgive me, since it's been a while since I've done that, and I never liked those as much as the premade mesh functions.Roguish
Ah, wise one, what are these premade mesh functions you talk of, I am but a padawan. And I am going to look at this index buffer thingCima
Well, I'm sure vertex and index buffers are how people normally do anything more complex than the built-in ones, and less complex than what they want to load from a separate model file, but the built-in ones I'm talking about let you easily create meshes for common shapes. Off the top of my head, there's D3DXCreateSphere and I know there's a cube and a teapot one as well. It would probably be easier just to make a cube mesh with no depth (if it lets you; otherwise a very small depth) and live with the extra ten triangles, but I'd say it's worth learning how to do right, and I'm no expert.Roguish
No worries found the answer! phew, it's easier than I hoped for.. I'll put as answerCima
Wow, I forgot about that (it has been a while). Index buffers work well, but moreso for more complicated things than this :pRoguish
C
9

I had to exend my buffer to allow for 4 vertex array size:

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

And then changed the draw primitive from TRIANGLELIST to STRIP extending the amount of triangles drawn to 2

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

Source: http://www.mdxinfo.com/tutorials/tutorial4.php

Cima answered 7/4, 2013 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.