• Create Account

## drawing triangle using dx9

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1phil67rpg  Members

215
Like
2Likes
Like

Posted 06 January 2013 - 06:24 PM

I am studying the directx9tutorial.com web site.one of their exercises is to change the shape of a triangle during runtime. Here is some of the code given from this tutorial.
// 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, v_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);
}

// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{
v_buffer->Release();    // close and release the vertex buffer
d3ddev->Release();    // close and release the 3D device
d3d->Release();    // close and release Direct3D
}

// this is the function that puts the 3D models into video RAM
void init_graphics(void)
{
// create the vertices using the CUSTOMVERTEX struct
CUSTOMVERTEX vertices[] =
{
{ 400.0f, 62.5f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 650.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 150.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
};

// create a vertex buffer interface called v_buffer
d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);

VOID* pVoid;    // a void pointer

// lock v_buffer and load the vertices into it
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer->Unlock();

### #2Postie  Members

1459
Like
2Likes
Like

Posted 06 January 2013 - 08:25 PM

Nice code, but did you have a question?

Currently working on an open world survival RPG - For info check out my Development blog:

### #3phil67rpg  Members

215
Like
0Likes
Like

Posted 06 January 2013 - 08:43 PM

yes I have drawn a triangle but I want to change it to a square during runtime.

### #4Morphex  Members

298
Like
0Likes
Like

Posted 06 January 2013 - 08:52 PM

Just few tips, a triangle has 3 vertices a square has 4 at least (you may need more).

Find where you declare vertices,

You will need a switch to check if you are in square mode or triangle mode, and to a way to change that switch at runtime. (By switch I mean a Bool variable, True, False)

Check out my new blog: Morphexe

### #5Khatharr  Members

7695
Like
0Likes
Like

Posted 06 January 2013 - 11:36 PM

Phil got the code tags working! +1!

Let us know how it turns out, bud.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #6Endurion  Members

5168
Like
1Likes
Like

Posted 07 January 2013 - 12:15 AM

Just few tips, a triangle has 3 vertices a square has 4 at least (you may need more).

Find where you declare vertices,

You will need a switch to check if you are in square mode or triangle mode, and to a way to change that switch at runtime. (By switch I mean a Bool variable, True, False)

There is no square mode.

A square is simply a list of two triangles instead of 1. The simplest way would be to add the 3 vertices of the second triangle to your vertex array , change the "3*" in the CreateVertexBuffer call to "6*" and change the "1" in the DrawPrimitive call to "2".

Get that working first, don't bother with the other primitive types yet.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

### #7phil67rpg  Members

215
Like
0Likes
Like

Posted 07 January 2013 - 04:14 PM

thanks a lot I got it working also thanks for the rep points

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.