Easy question (making a quad)

Grand Nagus

Member
Joined
Jan 7, 2004
Messages
10
Oh boy! An active DirectX forum! :D

Hello, all. Im a seriously newbie DirectX coder (using Visual C++ .net), and Im probably going to ask some ridiculously simple questions every now and then (ya can pick on me, but please dont bash me for not knowing anything).

Heres my dilemma:
First off, I dont know much of anything about DirectX programming. So far, I can kickstart DirectX in windowed and fullscreen at a set resolution and color mode (oooooh, impressive).
Ive played around with Microsofts tutorials to see how to do the basics, and it seemed easy enough. But Ive spent a couple of days trying to modify this tutorial to get it to render a quad (square) instead of a triangle. Heres what Ive done to it, but it doesnt work:
I changed this:
Code:
    // Initialize three vertices for rendering a triangle
    CUSTOMVERTEX vertices[] =
    {
        { 150.0f,  50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
        { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },
        {  50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },
    };
to this (added an extra vertex, arranged coords to reflect a square instead of a triangle):
Code:
    // Initialize three vertices for rendering a triangle
    CUSTOMVERTEX vertices[] =
    {
        { 50.0f,  50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
        { 50.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },
        {  250.0f, 50.0f, 0.5f, 1.0f, 0xff00ffff, },
        {  250.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },
    };

Then, I changed this:
Code:
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
to this (increased buffer size to take into account the extra vertex):
Code:
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 2*3*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )

Finally, I changed this:
Code:
        g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
to this (make a triangle strip instead of a triangle list, and make two polys):
Code:
        g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

That shoulda defined 4 vertices, put em into the vertex buffer, and then rendered em as a triangle strip instead of a triangle list. But when the programs run, it only shows a blue screen (the initialized DirectX screens background color). When I look at the code it looks like it should work, so I cant figure out where the problems at... Im sure its something really simple, but what am I doing wrong or omitting thats keeping this code from rendering a quad? :confused:

Thanks in advance for the help!
 
shouldnt
Code:
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 2*3*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )

be
Code:
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),    // change on this line
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
as you are only adding one extra vertex to define a triangle strip.
 
Thanks for the response, PlausiblyDamp.

I gave your idea a shot and it compiled fine, but I still only get a blue Direct3d screen (no quad). Gads, I wish I could figure this thing out. Its frustratin!

Heres a link to the code (zipped 91k), if you want to d/l it and see why its doing what it is (or isnt). Its the Vertices tut that Microsoft provides with the DirectX sdk, and it already has my edits in it. Just download it and extract it into its own folder, then startup Vertices.vcproj in VisualC++.net:
Quad dont work

Thanks again!
 
Yippee, I got it to work! It had to do with the order of which I had the vertices... Direct3D is picky, picky, picky.

Thanks for the help! Maybe Ill have a slightly more challenging question another time. ;)
 
Back
Top