• 13
• 18
• 19
• 27
• 10

# Changing shape of an triangle during runtime

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

## Recommended Posts

So I'm stuck on an exercise again and I have to: Get the triangle to change shape during runtime. I thought it would be as easy as changing the color of a background during runtime but not so much. I tried diffrent syntax and I get compile errors on all of them. Yeah I just deleted the code I was going to show it just makes me look like an idiot when posting my code. The code doesn't work so it's useless posting it anyways. But here is my struct for my triangle:
CUSTOMVERTEX t_vert[] =
{
{ 100.0f, 20.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 520.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 120.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
};
My question is though how do I access those vertices in the correct syntax?

##### Share on other sites
Quote:
 Original post by Roberts91So I'm stuck on an exercise again and I have to:Get the triangle to change shape during runtime.I thought it would be as easy as changing the color of a background during runtime but not so much. I tried diffrent syntax and I get compile errors on all of them. Yeah I just deleted the code I was going to show it just makes me look like an idiot when posting my code. The code doesn't work so it's useless posting it anyways. But here is my struct for my triangle:CUSTOMVERTEX t_vert[] = { { 100.0f, 20.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), }, { 520.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), }, { 120.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), }, };My question is though how do I access those vertices in the correct syntax?

t_vert[0].x = 123.0f;

To actually update the version of the triangle D3D has, you'll have to Lock() your vertex buffer again, copy the vertices back into it, and Unlock() again. If you're doing that often (More than once a second or so), you'll probably want to make sure your vertex buffer is dynamic, or you'll have some performance issues.

##### Share on other sites
Thank you for some reason I thought I had to have syntax like this t_vert[0][0] = 50.0f;

##### Share on other sites
Well for some reason this code isnt changing the triangle shape during runtime:

void init_graphics(void){		CUSTOMVERTEX t_vert[] =	{			{ 100.0f, 20.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },		{ 520.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },		{ 120.0f, 400.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },	};    // create a vertex buffer interface called t_buffer    d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),                               0,                               CUSTOMFVF,                               D3DPOOL_MANAGED,                               &t_buffer,                               NULL);		 VOID* pVoid;    // a void pointer   	if(((GetTickCount()/1000) % 2) == 0)	{		t_vert[0].X = 10.0f;		t_vert[0].Y = 100.0f;	}	else	{		t_vert[0].X = 350.0f;		t_vert[0].Y = 200.0f;	}	t_buffer->Lock(0, 0, (void**)&pVoid, 0);	memcpy(pVoid, t_vert, sizeof(t_vert));	t_buffer->Unlock();	    return;}

Doesn't make any sense

##### Share on other sites
Presumably init_graphics() is only called once at application startup. You'll want to update the vertices every tick before rendering. You can miss out the CreateVertexBuffer() call when rendering, then just update the vertices, lock, memcpy, unlock then render.

##### Share on other sites
Yea your right initgraphics() is only called once so I just slapped the function into render_frame function and it worked = )

##### Share on other sites
Quote:
 Original post by Roberts91Yea your right initgraphics() is only called once so I just slapped the function into render_frame function and it worked = )
So long as you didn't include the CreateVertexBuffer call, that should be fine (Since if you did, you'll have a memory leak).

It may be worth looking into dynamic vertex buffers if you're going to be changing the vertices every frame now, but so long as it works that's the main thing (for now...) [smile]

##### Share on other sites
Meh theres no other way to do it without making the program crash unless you just got rid of the initgraphics function all together. Or atleast no other way i can think of.

##### Share on other sites
Quote:
 Original post by Roberts91Meh theres no other way to do it without making the program crash unless you just got rid of the initgraphics function all together. Or atleast no other way i can think of.

init_graphics() just needs to create the vertex buffer, and at render time you can just lock, memcpy and unlock it.

If that crashes, we'll need to see the code with an explanation of what you mean by a crash (What's the exact error message(s)), and the line the crash happens on.