Sign in to follow this  

Particlesystem Tips and some more!

This topic is 3863 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

godday! i have been working on my particle system now and its not going to good... so i gona re build it and want som tips. im currently using this code, and just give me tips on how you shuld do to get this one working.
class Para
{
private:
	typedef struct {
		float X,Y,Z;
		D3DCOLOR color;
		float U,V;
	} QUAD;
	VOID *pQuad;
	D3DXMATRIX QuadFinal;
	D3DXMATRIX QuadTrans;
	static const DWORD QUAD_FVF = D3DFVF_XYZ|D3DFVF_DIFFUSE | D3DFVF_TEX1;
	FLOAT X,Y,Z;
	int Number;
public:
	void Init(int R, int G, int B, int A,const char* filename, int numberofpara);
	void SetPara(float Xpos, float Ypos, float Zpos);

};

void Para::Init(int R, int G, int B, int A, const char*filename, int numberofpara)
{
	Number=numberofpara;
	QUAD ourQuad[] =
	{
		{-1.0f,1.0f,0.0f,D3DCOLOR_ARGB(A,R,G,B),1,0,},
		{1.0f,1.0f,0.0f,D3DCOLOR_ARGB(A,R,G,B),0,0,},
		{1.0f,-1.0f,0.0f,D3DCOLOR_ARGB(A,R,G,B),1,1,},
		{-1.0f,-1.0f,0.0f,D3DCOLOR_ARGB(A,R,G,B),0,1,},
	};
	D3DXCreateTextureFromFile(pd3dDevice,filename,&texture_1); 
	pd3dDevice->SetTexture(0,texture_1);
	pd3dDevice->CreateVertexBuffer((numberofpara*4)*sizeof(QUAD),0,QUAD_FVF,D3DPOOL_MANAGED,&QuadBuff,NULL);
	QuadBuff->Lock(0,0,(void**)&pQuad,0);
	memcpy(pQuad,ourQuad,sizeof(ourQuad));
	QuadBuff->Unlock();
	pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,true);
}
void Para::SetPara(float Xpos, float Ypos, float Zpos)
{

	X=Xpos,Y=Ypos,Z=Zpos;
	D3DXMatrixIdentity(&QuadFinal);
	D3DXMatrixTranslation(&QuadTrans, X,Y,Z);
	D3DXMatrixMultiply(&QuadFinal, &QuadFinal, &QuadTrans);
	pd3dDevice->SetTransform(D3DTS_WORLD, &QuadFinal);
	pd3dDevice->SetFVF(QUAD_FVF);
	pd3dDevice->SetStreamSource(0,QuadBuff,0,sizeof(QUAD));
	for(int i=0; i<Number;i+=4)
	{
        pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, i, 2);
	}
}


Share this post


Link to post
Share on other sites
You need to use a dynamic vertex buffer to improve your performance; there's a number of articles on how to do this correctly. The real problem is that you're drawing 2 (TWO!) triangles per DrawPrimitive call. You WANT to be drawing your entire particle system in a single DrawPrimitive call -- then you'll get good performance. This is called batching and it's crucial to D3D; in order to do it, you'll have to transform the particles yourself because you won't be able to stop and set a world matrix, render a particle, etc. which is what you're doing now (and is realllllllly slow.)

Share this post


Link to post
Share on other sites
In what way is it "not going to good"?

The one obvious thing I can see if you're doing a DrawPrimitive() call for each particle. You'll usually want to done one call for the whole particulae system, and render the whole thing as an [indexed] triangle list, or by using point sprites (But they have some other limitations)

Share this post


Link to post
Share on other sites
well its notgoing good becus, icant set my random correctly and i want to set etc that every Quad will have its own orgin.
and i want to get a random chois as XMax and XMin so i can easy set it´s random orign example i want to have random values between 10.0f and 1.0f!
and i want to give all the quad´s its own orgin!

Share this post


Link to post
Share on other sites
Quote:
Original post by snoddas14
well its notgoing good becus, icant set my random correctly and i want to set etc that every Quad will have its own orgin.
and i want to get a random chois as XMax and XMin so i can easy set it´s random orign example i want to have random values between 10.0f and 1.0f!
and i want to give all the quad´s its own orgin!

None of this has anything to do with performance. You need to use a dynamic VB and you need to render LOT OF PARTICLES at once or else your performance will not be acceptable!

Share this post


Link to post
Share on other sites
Quote:
Original post by MasterWorks
you won't be able to stop and set a world matrix, render a particle, etc. which is what you're doing now

My mistake, this isnt exactly how you have it coded. Note that even if you render all your particles in one drawPrimitive, you don't have to give up any of the functionality you have now. You can still use your matrices and you can still have random particle positions or whatever you are worried about. All you have to do is change your code to something like this:

-create really big vertex buffer (dynamic!!)

Then loop thru each particle and:

-tack on each particles's coordinates to the next available slots in the vertex buffer
-set up your renderstates/transformations
-render the vertex buffer in one drawprimitive
-clear the vertex buffer

You can improve on this using WRITEONLY/DISCARD vertex buffer locking flags combination and/or using an index buffer alongside your vertex buffer, but the basic idea is the same.

Share this post


Link to post
Share on other sites

This topic is 3863 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this