Sign in to follow this  
Rozik

Problem with PointSprites

Recommended Posts

Hi everyone, I just got started on Pointsprites and i already ran into trouble: This is my code to display one sprite:
m_NumParticles = NumParticles;
		m_Particles = new FBSParticle[m_NumParticles];
		m_pVB = NULL;
		m_Texture = NULL;
		Device->CreateVertexBuffer(m_NumParticles*sizeof(PARTICLECUSTOMVERTEX), 0, D3DFVF_PARTICLEVERTEX, D3DPOOL_DEFAULT, &m_pVB, NULL);
		D3DXCreateTextureFromFile(Device, "Sprite.bmp", &m_Texture);

PARTICLECUSTOMVERTEX *vertices = new PARTICLECUSTOMVERTEX[m_NumParticles];

	

			vertices[0].x = 0;
			vertices[0].y = 0;
			vertices[0].z = 0;
		

		VOID* pVertices = NULL;
		m_pVB->Lock( 0, sizeof(PARTICLECUSTOMVERTEX) * m_NumParticles, (void**)&pVertices, 0 );
		memcpy( pVertices, vertices, sizeof(vertices) );
		m_pVB->Unlock();

		FLOAT MinSize, Scale;

		MinSize = Scale = 0;
		g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, false);
		g_pd3dDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
		g_pd3dDevice->SetRenderState(D3DRS_POINTSCALEENABLE, TRUE);
		g_pd3dDevice->SetRenderState(D3DRS_POINTSIZE_MIN, *((DWORD*)&MinSize));
		g_pd3dDevice->SetRenderState(D3DRS_POINTSCALE_A,  *((DWORD*)&Scale));
		g_pd3dDevice->SetRenderState(D3DRS_POINTSCALE_B,  *((DWORD*)&Scale));
		g_pd3dDevice->SetRenderState(D3DRS_POINTSCALE_C,  *((DWORD*)&Scale));

			// use alpha from texture
	g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
	g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);

	g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
	g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);


        g_pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof(PARTICLECUSTOMVERTEX) );
        g_pd3dDevice->SetFVF( D3DFVF_PARTICLEVERTEX );
		g_pd3dDevice->SetTexture(0, m_Texture);
        g_pd3dDevice->DrawPrimitive( D3DPT_POINTLIST, 0, m_NumParticles );

I have two problems with this: first all i see is a black square even though the bmp i load is not black next problem is when i move the camera back and forth the sprite stays the same size shouldnt it shrink when i move away from it? i would be happy if anyone had any hints for me:) thx a lot

Share this post


Link to post
Share on other sites
If you see the particles black if think it may be one of this two issues:

1. If you are setting lighting to false on render states, you should set ambient light to white on render states: IDirect3DDevice9::SetRenderState (D3DRS_AMBIENT, 0xffffffff);. This way the white light affect the particles

2. You are using a normal vector value in the FVF and they are pointing to the same direction of the direction of the lights

About the size of the particles relative to distance, you are setting: IDirect3DDevice9::SetRenderState (D3DRS_POINTSCALEENABLE, TRUE); this means that direct3d is calculating size of the point sprites by some function, but you are anulating that function by setting the parameters A B C to 0. They cant be 0 all at the same time.

A will give you a size no dependent to distance, B is dependent to distance lineally and C is dependent to distance cuadratic.

So if you want the particles to be smaller if they are far away from the camera, try A=0 B=0 C=2


Share this post


Link to post
Share on other sites
Sorry still doesnt work:(

This

SetRenderState (D3DRS_AMBIENT, 0xffffffff);

didnt do anything

and when i tried this:

A=0 B=0 C=2
the sprite disapeared

then i tried to move the sprite like this

vertices[0].x = 0;
vertices[0].y = 10;
vertices[0].z = 0;

but it doesnt even move at all:(

Share this post


Link to post
Share on other sites
That was the issue i work around some time ago and the information i provided you was the solution for me.

About the lighting im not 100% sure.. may be there are other render states messing up.. but about the function to calculate the point sprite size im pretty sure im not wrong.

Im just understanding directx like you so i think some expert answer is required here.

Share this post


Link to post
Share on other sites

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