• Advertisement
Sign in to follow this  

[DirectX] Creating Multiple Objects (Triangles)

This topic is 3517 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just started learning DirectX. I am currently on this chapter of the DirectX Tutorial: http://www.directxtutorial.com/Tutorial9/B-Direct3DBasics/dx9B4.aspx. My question is simple enough but the answer will probably be complex. Please note that I am very new to directX and am only a beginner in c++. What I'm wondering is how I can make multiple triangles using the method showed in that tutorial. Like I said I'm new to DirectX and some of the things DirectX uses are very overwhelming for me. I do understand most of it and I have tried creating a second triangle using the same method shown in the tutorial (creating a new CUSTOMVERTEX, filling it up, going through the stages of creating a new buffer, etc.) The part I got stuck on was when I went to draw the triangle. I couldn't find a way to implement the new triangle into one scene:
// this is the function used to render a single frame
void render_frame(void)
{
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3ddev->BeginScene();

        // select which vertex format we are using
        d3ddev->SetFVF(CUSTOMFVF);

        // select the vertex buffer to display
        d3ddev->SetStreamSource(0, t_buffer, 0, sizeof(CUSTOMVERTEX));

        // copy the vertex buffer to the back buffer
        d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

    d3ddev->EndScene();

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

    return;
}
I tried adding this line after
        d3ddev->SetStreamSource(0, t_buffer, 0, sizeof(CUSTOMVERTEX));
:
        d3ddev->SetStreamSource(0, t_buffer2, 0, sizeof(CUSTOMVERTEX2));
I had already set up the new buffer and customvertex so I didn't get any errors. However, when I tried running it, I just got the second triangle. I know I am doing this wrong because there can only be one StreamSource (I think). So my question is this: how would I create a second triangle to fit this format?

Share this post


Link to post
Share on other sites
Advertisement
The actual drawing happens with DrawPrimitive in this case. The function SetStreamSource merely tells D3D what to draw.

So to get your second triangle drawn you need a second call to DrawPrimitive.

Share this post


Link to post
Share on other sites
Do I call another SetStreamSource, too? And when do I do this? Do I draw it right after the first DrawPrimitive or do I do it after that scene is over. That's what I'm confused about, the location.

Share this post


Link to post
Share on other sites
To draw two triangles, instead of creative two VertexBuffers just create one with space for two triangles (6 verts if you doing normal triangle lists).

CUSTOMVERTEX* v;
vbuf->Lock (..., (LPVOID*) &v);

// set first triangle details
v[0].x = 0;
v[0].y = 0;
v[0].z = 0;

v[1].x = 10;
v[1].y = 0;
v[1].z = 0;

v[2].x = 10;
v[2].y = 10;
v[2].z = 0;

// set second triangle details
v[3].x = 0;
v[3].y = 0;
v[3].z = 0;

v[4].x = 10;
v[4].y = 10;
v[4].z = 0;

v[5].x = 0;
v[5].y = 10;
v[5].z = 0;

d3d->SetFVF (CUSTOMVERTEX::FVF);
d3d->SetStreamSource (0, vbuf, 0, sizeof (CUSTOMVERTEX));
d3d->DrawPrimitive (D3DPT_TRIANGLELIST, 0, 2);





As for why its not working, essentially you have to call Draw* everytime you want the currently set stream to be drawn to the rendering context.

So if you want to draw two different meshes you have to call Set/Draw on the first then Set/Draw on the second mesh.

begin scene

# object 1
set transform
set stream Obj1
draw

# object 2
set transform
set stream Obj2
draw

# etc etc

end scene
present

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement