Skip to main content
GameDev.net gamedev.net
🔒 Locked

Wierd VertexBuffer behavior - Please help

Started by Klaser Oct 5, 2004 at 7:44 AM 4 replies 1.2k views
Original Post
Klaser
Klaser
Hi! I have been implementing a particle system using the book: "Special Effect Game Programming with DirecX". After having converted to DirectX9 everything works correct, except changing the size and color of the particles. When I change the size of a particle the color changes instead. When I try changing the color, nothing happens. I think there might be a problem with adressing the VB correctly. I have posted some relevant code:

#define D3DFVF_PARTICLE (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_PSIZE)
typedef struct 
{
    D3DXVECTOR3 position;
    D3DCOLOR    color;
	float       pointsize;
} VERTEX_PARTICLE;

And the render-method:

HRESULT CParticleEmitter::Render(){
	HRESULT hr;
	// Sæt renderstates således at der bruges pointsprites
    // setup alpha blending states
    m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE );
	m_pd3dDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
	m_pd3dDevice->SetRenderState(D3DRS_POINTSCALEENABLE, TRUE);
	m_pd3dDevice->SetRenderState(D3DRS_POINTSIZE_MIN, FtoDW(0.00f));
	m_pd3dDevice->SetRenderState(D3DRS_POINTSCALE_A, FtoDW(0.001f));
	m_pd3dDevice->SetRenderState(D3DRS_POINTSCALE_B, FtoDW(0.001f));
	m_pd3dDevice->SetRenderState(D3DRS_POINTSCALE_C, FtoDW(0.001f));


	// Klargør vertexbufferen
	m_pd3dDevice->SetStreamSource(0, m_vbParticles, 0, sizeof(VERTEX_PARTICLE) );
	m_pd3dDevice->SetFVF(D3DFVF_PARTICLE);
	m_pd3dDevice->SetTexture(0, m_texParticle);
	

	VERTEX_PARTICLE *pVertices;
	DWORD dwNumParticlesToRender = 0;
	if(FAILED(hr = m_vbParticles->Lock(0, m_iVBSize * sizeof(VERTEX_PARTICLE), (VOID **) &pVertices, D3DLOCK_DISCARD))){
		MessageBox(NULL, "Kunne ikke indlæse textur til partikel!", "Error!", MB_OK);
		
		return(hr);
	}


	// Tegn hver enkelt partikle
	for(int q=0; q<NUMPARTICLES; q++){
		if(m_Particles.IsAlive(q)){
			CParticle ∂ = m_Particles.GetAt(q);
			
			pVertices->position = part.m_vPos;
			pVertices->color = (DWORD)part.m_Color;
			pVertices->pointsize = 1.0f; // IF I CHANGE THIS THE COLOR OF THE PARTICLE CHANGES INSTEAD
			pVertices++;

			if(++dwNumParticlesToRender == m_iVBSize){
				// SÅ er vi færdige med at fylde denne del af vertexbuffer - unlock og tegn!
				m_vbParticles->Unlock();
				if(FAILED(hr = m_pd3dDevice->DrawPrimitive(D3DPT_POINTLIST, 0, dwNumParticlesToRender))){
					return(hr);
				}
				if(FAILED(hr = m_vbParticles->Lock(0, m_iVBSize * sizeof(VERTEX_PARTICLE), (VOID **) &pVertices, D3DLOCK_DISCARD))){
					return(hr);
				}
				dwNumParticlesToRender = 0;
			}
		}
	}

	// Tegn det sidste snavs
	m_vbParticles->Unlock();
	if(dwNumParticlesToRender){
		if(FAILED(hr = m_pd3dDevice->DrawPrimitive(D3DPT_POINTLIST, 0, dwNumParticlesToRender))){
			return(hr);
		}
	}

	//Ryd op og nulstill renderstates
	m_pd3dDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, FALSE);
	m_pd3dDevice->SetRenderState(D3DRS_POINTSCALEENABLE, FALSE);
	return(hr);
}

I hope somebody can help me out here! Best regards, Klaser
bmarci
bmarci
Hi,

It appears to me, that the members are not in the correct order in the vertex structure.

According to the DX sdk doc, the vertex point size is before diffuse color.

Se: Using DirectX graphics/Vertex formats/About vertex formats

med venlig hilsen :)
-marci
Klaser
Klaser
Hi!

Thanks for your fast reply : )
I have tried changing the order of the flags so that D3DFVF_PSIZE comes before D3DFVF_DIFFUSE, but it doesn´t change anything.
When I change the order of the declarations in the struct though, all the particles turns into white (the orignal color of the texture) or just disappears...hmmm!?

Klaser

Chris Jungen
Chris Jungen
You have to change the order within the structure
of your vertex format. Changing the order in your define flags
has no effect.

typedef struct
{
D3DXVECTOR3 position;
float pointsize;
D3DCOLOR color;
} VERTEX_PARTICLE;
Klaser
Klaser
Hi!

Thanks for the reply.
I already tried to change the order in the struct to:

typedef struct
{
D3DXVECTOR3 position;
float pointsize;
D3DCOLOR color;
} VERTEX_PARTICLE;

All the particles are white (original color), but I still cant change their size or their color.

Thanks for your help!
Klaser
Klaser
Klaser
Hi!

I found the problem with changing the colors. It was caused by my random generator. The random-method works fine with vectors, but not with colors...hmmm!

I still have a problem with changing the size of the particles though. In my render-method (listed above) I set the values in my VB before rendering, like this:

pVertices->position = part.m_vPos;
pVertices->pointsize = 10.0f; // CHANGING THIS DOES NOT WORK
pVertices->color = (DWORD)part.m_Color;
pVertices++;

I can change the size of the particles by choosing a min size though:
m_pd3dDevice->SetRenderState(D3DRS_POINTSIZE_MIN, FtoDW(10.00f));

Any ideas whats wrong? Your help is much appreciated : )
Best regards,
Klaser

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.