Jump to content
  • Advertisement
Sign in to follow this  
onetwothreegone

DirectX only drawing a single primitive.

This topic is 2108 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 am trying to get DirectX to draw a triangle in a single function. The function works, but I can only draw a single time, any ideas? I don't really know what other information to provide.

 

Code:

void graphics::fillTriangle(float x, float y, float x2, float y2, float x3, float y3, int r, int g, int b, int a)
{
	Vertex vertexs[] =
	{
		{ x, y, 1.0f, 1.0f, D3DCOLOR_RGBA(r, g, b, a) },
		{ x2, y, 1.0f, 1.0f, D3DCOLOR_RGBA(r, g, b, a) },
		{ x2, y2, 1.0f, 1.0f, D3DCOLOR_RGBA(r, g, b, a) },
	};

	VOID* pVoid;

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

	vBuffer->Lock(0, 0, (void**)&pVoid, 0);
	memcpy(pVoid, vertexs, sizeof(vertexs));
	vBuffer->Unlock();

	d3dDevice->SetFVF(CUSTOMFVF);
	d3dDevice->SetStreamSource(0, vBuffer, 0, sizeof(Vertex));
	d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

	vBuffer->Release();
}

Share this post


Link to post
Share on other sites
Advertisement

Just out of curiosity, what are the x3 and y3 parameters supposed to be used to accomplish?

Also, where is this being called?

Edited by Ectara

Share this post


Link to post
Share on other sites

What are you trying to achieve?

 

What you have there is just a VERY basic piece of code which could serve only as a starting point for anything bigger. In that code you create a vertex buffer for 3 vertices, fill it with data, draw it and release it.

In Direct3D you need to render everything every frame and in this case you would be recreating and releasing the buffer every frame, too, which is bad. You'll have to create and fill your resources at the start of the application, then just draw them every frame as needed and release them before the application exits.

(You can lock-fill-unlock dynamic buffers every frame if you need to change their contents, but definitely not create them every frame.)

Share this post


Link to post
Share on other sites

Typically most of the program content is static. That means that you'll create a vertex buffer(s) as required and fill them once when loading meshes, after you don't touch the data. 

 

Of course sometimes the geometry of an object may be subject to a change (a terrain with LOD for example) and you'll need to use a dynamic buffer which you'll fill once and a while. Even here, the buffer may be created _once_ at the program start up.

 

The code you presented _may_ be expanded to draw multiple triangles (I strongly suggest to move the VB creation code else where). Just make it accept an array of positions for example and then use every 3 positions to form a triangle. 

 

Cheers!

Share this post


Link to post
Share on other sites

Huh, it looks like you are trying to reproduce immediate mode with vertex buffers. Though I wouldn't recommend you using it anymore (since its slower, and quite outdated), if all you really want is to have a function "here is a triangle, draw it now!" you can use DrawPrimitiveUp so you don't need to use any vertex buffer at all. Otherwise, just like the others say, you have to create a vertexbuffer once with appropriate size, and for each triangle fill the next three vertices of that buffer, than eigther draw in one go or with multiple draw calls over the 3 vertex range.

Share this post


Link to post
Share on other sites

Thanks, primitive drawing works now. To Juliean, how much performance does DrawPrimitiveUp save and how do I use it? I just started DirectX and have only used OpenGL in the past.

 

Thanks,

Philip

Share this post


Link to post
Share on other sites


To Juliean, how much performance does DrawPrimitiveUp save and how do I use it? I just started DirectX and have only used OpenGL in the past.

 

No no, you got me wrong, DrawPrimitiveUp is the one that is way slower than the other methods. If you already got it to work, there is no point in it anyway, so you better forget that DrawPrimitiveUp even exists and that I showed it to you ;)

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!