DirectX only drawing a single primitive.

Started by
6 comments, last by onetwothreegone 10 years, 6 months ago

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();
}
Advertisement

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

Also, where is this being called?

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.)

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!

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.

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


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 ;)

Well thanks for you time!

This topic is closed to new replies.

Advertisement