Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ms291052

Particle System Problem! ("Pretty" screenshots too)

This topic is 5219 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 recently rewrote my particle system code because the coloring was done badly and wasn't working right but now my problem is far worse. It seems to me like the particles are ENORMOUS (they shouldn't be) and the texture and coloring aren't working properly. I hope someone can help me with this problem.
SCREENSHOTS: http://24.167.180.51/programming/problem.php
CODE: Particle.h
#ifndef PARTICLE__H
#define PARTICLE__H
#include <d3dx9.h>
class PARTICLEEMITTER;
class PARTICLE
{
public:
	D3DXVECTOR3      m_Position;
	D3DXVECTOR3      m_Velocity;
	D3DCOLOR         m_Color;
	float            m_Age;
	float            m_Life;
	PARTICLEEMITTER* m_Parent;
	PARTICLE();
};
#endif
Particle.cpp
   
#include <d3dx9.h>
#include "particle.h"
float Rand(float min = 0.0f, float max = 1.0f);
PARTICLE::PARTICLE()
{
	m_Position = m_Velocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	m_Color = D3DCOLOR_XRGB(255, 255, 255);
	m_Parent = NULL;
	m_Age = 0.0f;
	m_Life = 1.0f;
}

ParticleEmitter.h
#include "particle.h"
class PARTICLEEMITTER
{
public:
	LPDIRECT3DVERTEXBUFFER9 m_pVB;
	LPDIRECT3DTEXTURE9      m_pTexture;

	D3DXVECTOR3 m_Position;
	D3DXVECTOR3 m_Velocity;
	PARTICLE*   m_Particles;
	DWORD       m_NumParticles;
	PARTICLE*   m_DeadParticles;
	DWORD       m_NumDeadParticles;
	PARTICLEEMITTER();
	~PARTICLEEMITTER();
	void Init(DWORD);
	void Update(float);
	void Render();
};

ParticleEmitter.cpp
#include <d3dx9.h>
#include <iostream>
#include "particleemitter.h"
using namespace std;
#define SAFE_RELEASE(x) if(x != NULL) x->Release(); x=NULL;
float Rand(float, float);
extern LPDIRECT3DDEVICE9 g_pd3dDevice;
DWORD  FtoDW(float f);// {return *((DWORD*)&f);}

struct PARTICLEVERTEX
{
	D3DXVECTOR3 pos;
	D3DCOLOR   diffuse;
};
#define D3DFVF_PARTICLEVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
PARTICLEEMITTER::PARTICLEEMITTER()
{
	m_DeadParticles = m_Particles = NULL;
	m_pTexture = NULL;
	m_pVB = NULL;
	m_Position = m_Velocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
}

PARTICLEEMITTER::~PARTICLEEMITTER()
{
	SAFE_RELEASE(m_pVB);
	SAFE_RELEASE(m_pTexture);
}

void PARTICLEEMITTER::Init(DWORD NumParticles)
{
	m_NumParticles = NumParticles;
	m_Particles = new PARTICLE[NumParticles];
	D3DXCreateTextureFromFileEx(g_pd3dDevice, "star.bmp", 0, 0, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, NULL, NULL, NULL, &m_pTexture);
	//g_pd3dDevice->CreateVertexBuffer(NumParticles*sizeof(PARTICLEVERTEX), D3DUSAGE_POINTS, D3DFVF_PARTICLEVERTEX, D3DPOOL_MANAGED, &m_pVB, NULL);

	for(DWORD i=0; i < m_NumParticles; i++)
	{
		m_Particles[i].m_Color = D3DCOLOR_XRGB((int)Rand(0.0f, 255.0f), (int)Rand(0.0f, 255.0f), (int)Rand(0.0f, 255.0f));
		m_Particles[i].m_Position = D3DXVECTOR3(Rand(0.0f, 5.0f), Rand(0.0f, 5.0f), Rand(0.0f, 5.0f));
	}
}

void PARTICLEEMITTER::Update(float elapsed)
{
	PARTICLEVERTEX* pVertices;
	if(m_pVB == NULL)
	{
		cout << "NULL VB for all " << m_NumParticles << " particles!" << endl;
		HRESULT hr = g_pd3dDevice->CreateVertexBuffer(m_NumParticles*sizeof(PARTICLEVERTEX), D3DUSAGE_POINTS/* | D3DUSAGE_DYNAMIC*/, D3DFVF_PARTICLEVERTEX, D3DPOOL_MANAGED, &m_pVB, NULL);
		cout << "Creating new one... hr = " << hr << endl;
		return;
	}
	HRESULT h = m_pVB->Lock(0, 0, (void**)&pVertices, 0);
	if(FAILED(h))
		cout << "Lock Failed" << endl;
	for(DWORD i=0; i < m_NumParticles; i++)
	{
	//	m_Particles[i].m_Age += elapsed;

	//	m_Particles[i].m_Parent = this;

	//	m_Particles[i].m_Position += m_Particles[i].m_Velocity*elapsed;

	//	if(m_Particles[i].m_Age < m_Particles[i].m_Life)

	//	{

		pVertices[i].diffuse = m_Particles[i].m_Color;
		pVertices[i].pos = m_Particles[i].m_Position;
	//	}

	//	else

	//	{

	//		pVertices[i].diffuse = D3DCOLOR_ARGB(0, 0, 0, 0);

	//		pVertices[i].pos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);

	//	}

	}
	m_pVB->Unlock();
	cout << "Update Complete!" << endl;
}

void PARTICLEEMITTER::Render()
{
	if(m_pVB == NULL)
		return;
	g_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
    g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
    g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE );
    g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );

	g_pd3dDevice->SetRenderState( D3DRS_POINTSPRITEENABLE, TRUE );
    g_pd3dDevice->SetRenderState( D3DRS_POINTSCALEENABLE,  TRUE );
    g_pd3dDevice->SetRenderState( D3DRS_POINTSIZE,     FtoDW(0.08f) );
    g_pd3dDevice->SetRenderState( D3DRS_POINTSIZE_MIN, FtoDW(0.00f) );
    g_pd3dDevice->SetRenderState( D3DRS_POINTSCALE_A,  FtoDW(0.00f) );
    g_pd3dDevice->SetRenderState( D3DRS_POINTSCALE_B,  FtoDW(0.00f) );
    g_pd3dDevice->SetRenderState( D3DRS_POINTSCALE_C,  FtoDW(1.00f) );


	g_pd3dDevice->SetStreamSource(0, m_pVB, 0, sizeof(PARTICLEVERTEX));
	g_pd3dDevice->SetFVF(D3DFVF_PARTICLEVERTEX);
	g_pd3dDevice->SetTexture(0, m_pTexture);
	g_pd3dDevice->DrawPrimitive(D3DPT_POINTLIST, 0, m_NumParticles); //This is the line I commented out to change between the two screenshots.



	g_pd3dDevice->SetRenderState( D3DRS_POINTSPRITEENABLE, TRUE );
    g_pd3dDevice->SetRenderState( D3DRS_POINTSCALEENABLE,  TRUE );

	g_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, TRUE );
    g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );

	cout << "Render Complete!" << endl;
}
Thank you so much! [edited by - ms291052 on February 9, 2004 11:10:09 PM]

Share this post


Link to post
Share on other sites
Advertisement
Hmm it maybe a longshot but it looks like that the FVF is set incorrectly. Maybe you changed the FVF for the sprites and didnt change it back to the terrain FVF... !?

Share this post


Link to post
Share on other sites
struct PARTICLEVERTEX
{
D3DXVECTOR3 pos;
D3DCOLOR diffuse;
};
#define D3DFVF_PARTICLEVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)

How is that incorrect?
[edit]
Ah, nevermind, I see what you mean. Since my "engine" only likes .x levels, I have a terrain exporter, and that terrain is in .x format. Thus, it's drawn like standard meshes (so I never explicitly set the FVF).
[/edit]
I suppose I never set D3DRS_POINTSIZE_MAX. According to the docs the default is 64.0f, is that bad?

[edited by - ms291052 on February 9, 2004 4:43:45 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by ms291052
I suppose I never set D3DRS_POINTSIZE_MAX. According to the docs the default is 64.0f, is that bad?


Yeah that''s pretty huge. Try somewhere between 3 and 10...

-=Lohrno

Share this post


Link to post
Share on other sites
sry to off topic here put how you put that gun there that a mesh? or a pic? how you did that.. looks good btw.. bout your particles gonna review how i did mine and ill try to figure your problem out

Share this post


Link to post
Share on other sites
Yeah it looks kinda like the textures got changed around in a bad way...if you didn't set FVF back it would probably give you complete garbage...if you didnt set it, it might cause odd effects...That pattern kind of looks like you're using clamp...I hope you aren't. Either that or the Texture coords are being messed with.

Here, I have a little function that shows renderstates in outputdebugstring, take a look...

-=Lohrno

[edited by - Lohrno on February 9, 2004 8:25:53 PM]

Share this post


Link to post
Share on other sites
switch(UserName)
{
case Lohrno :
{
quote:
Original post by Madprog
sry to off topic here put how you put that gun there that a mesh? or a pic? how you did that.. looks good btw.. bout your particles gonna review how i did mine and ill try to figure your problem out


The guns are (for now) bitmaps that are just rendered on quads and as of now they''re not my artwork :-[. Eventually they will be meshes and my artwork... but that comes after I get my engine 99.9% working.
}
//Notice there is NO break here

case Madprog:
{
Thank you both for looking into my problem. I double checked everything and the FVF and RenderStates appear to be fine (I use about 4-5 different FVFs and none of the others act weird, but I didn''t specifically check them).
} break;
default:
{
Any other ideas?
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!