Particle System bugs

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Particle System bugs

Post by GroundUpEngine »

K-Bal wrote:I go into more detail. You should save the particle centers as vertices in a VBO. Then render everything with the point sprite extension. However, since you are not using any textures, you could just render the VBO as GL_POINTS.
GroundUpEngine wrote:
K-Bal wrote:You are using immediate mode which is very slow. You should try using vertex buffer objects with index buffers and object batching. Ask GroundUpEngine :lol:
I would help, but I have no idea how vertex buffer objects work...

Sorry! ;)
Are you serious? :lol: You did your terrain with VBOs and you don't know how they work? ;)
Just kidding... :lol: But seriously, I've only just learned how to use them effectively and I havent tryed anything Dynamic like particles, data mapping ..etc

But here's the sexy tutorial I used ;) http://www.ozone3d.net/tutorials/opengl_vbo.php
User avatar
zeid
Chaos Rift Junior
Chaos Rift Junior
Posts: 201
Joined: Fri Apr 24, 2009 11:58 pm

Re: Particle System bugs

Post by zeid »

Actually if you are creating a particle system I would opt. for point sprites over the use of VBO's.
Here are some samples, go a little down the page.
Axolotl Pop!
Image
Or, try it for free.

For many more free games online visit www.sam-zeid.com
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Particle System bugs

Post by K-Bal »

zeid wrote:I would opt. for point sprites over the use of VBO's.
This is not mutually exclusive. It's actually pretty straight forward to combine both.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Particle System bugs

Post by GroundUpEngine »

K-Bal wrote:
zeid wrote:I would opt. for point sprites over the use of VBO's.
This is not mutually exclusive. It's actually pretty straight forward to combine both.
/agreed
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Particle System bugs

Post by GroundUpEngine »

Double post :P

@mv2112
Here's the basic point sprite technique, it's faster because you only have to pass 1 vertice per Particle, for my particle system I got 1000% performance increase! For this snippet I have used Intermediate mode with one call to glBegin/glEnd. For VBO technique you would have to update the position data buffer every frame using GL_STREAM_DRAW and render using glDrawArrays, or whatever ;)

Code: Select all

glPushMatrix();
	// Properties of Particle
	glPointSize(ParticleSize);
	// Bind Texture
	glBlendFunc(GL_ONE, GL_ONE);
	Texture->Bind();
	// Use Point Sprite
	glEnable(GL_POINT_SPRITE);
	glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);

	// Render Particles (Intermidiate Mode)
	glBegin(GL_POINTS);
	for(int i = 0; i < numParticles; i++) {
		glVertex3f(Particles[i].Pos.x, Particles[i].Pos.y, Particles[i].Pos.z);
	}
	glEnd();

	glDisable(GL_POINT_SPRITE);
	glPointSize(1);
glPopMatrix();
User avatar
zeid
Chaos Rift Junior
Chaos Rift Junior
Posts: 201
Joined: Fri Apr 24, 2009 11:58 pm

Re: Particle System bugs

Post by zeid »

This is not mutually exclusive. It's actually pretty straight forward to combine both.
Nice, I personally haven't looked into using them as a particle system, just new they would be faster (I assumed GL_POINTS was limited to immediate mode as I have only used it for debugging things). Now that you mention it, it seems obvious that you could put the data in a VBO and makes me feel a little silly for saying otherwise. :P
Axolotl Pop!
Image
Or, try it for free.

For many more free games online visit www.sam-zeid.com
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Particle System bugs

Post by K-Bal »

zeid wrote:
This is not mutually exclusive. It's actually pretty straight forward to combine both.
Nice, I personally haven't looked into using them as a particle system, just new they would be faster (I assumed GL_POINTS was limited to immediate mode as I have only used it for debugging things). Now that you mention it, it seems obvious that you could put the data in a VBO and makes me feel a little silly for saying otherwise. :P
If you are using a shader to update positions you don't even have to copy data between CPU and GPU once the particles are emitted. I'm doing this for a thesis and I can handle ~200,000 particles (billboards) with inter-partical collision and ~1,000,000 without at ~60 frames with a gtx8800. Keywords: transform feedback, texture buffer objects, geometry shader ;)
User avatar
zeid
Chaos Rift Junior
Chaos Rift Junior
Posts: 201
Joined: Fri Apr 24, 2009 11:58 pm

Re: Particle System bugs

Post by zeid »

If you are using a shader to update positions you don't even have to copy data between CPU and GPU once the particles are emitted. I'm doing this for a thesis and I can handle ~200,000 particles (billboards) with inter-partical collision and ~1,000,000 without at ~60 frames with a gtx8800. Keywords: transform feedback, texture buffer objects, geometry shader
That is some pretty clever stuff for optimisation, and sounds like an awesome thesis topic.
Axolotl Pop!
Image
Or, try it for free.

For many more free games online visit www.sam-zeid.com
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Particle System bugs

Post by mv2112 »

Ok, here is how it's set up, it still lags more than the SDL version does.
Does anything stand out that could be fixed or that is causing lag?

ParticleSystem Draw:
This is called in main.

Code: Select all

void ParticleSystem::Draw()
{
	glLoadIdentity();
	glClear(GL_COLOR_BUFFER_BIT);

	glEnable( GL_BLEND );
	glBlendFunc(GL_ONE, GL_ONE);
	glPointSize(s);
	glEnable(GL_POINT_SPRITE);
	glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
	glBegin(GL_POINTS);
	for(int i=0;i<num;i++)
	{
		Particles[i].Draw();
	}
	glEnd();
	glDisable(GL_POINT_SPRITE);
	glDisable( GL_BLEND );
	
}
Particle Draw:

Code: Select all

void Particle::Draw()
{	  
	glColor3f(Color.r,Color.g,Color.b); //Color is mvColor{float r;float g; float b};
	glVertex3f(Coordinates.Get(0),Coordinates.Get(1),0); 
        //Coordinates is a really crappy vector template i put together, ingeniously making it's members private for un-easy access
}
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Particle System bugs

Post by short »

Iterators instead of for loops may help a little bit.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Particle System bugs

Post by K-Bal »

The first thing is, you are using PointSprites without texturing and with probably very small particles. So consider not rendering sprites but only points. Next thing is, turn of blending if you don't use alpha values.

For a whole lot of particles it would be better to batch particles with the same color. And put vertex data and color data in one array each rather than using a class for particles, it saves a 10000 function calls (good thing). Now you are able to use glDrawArrays (see also glVertexPointer, glColorPointer, glEnableClientState), which will give a good performance boost (since it's saving about 20000 function calls, vertex and color for each particle). I'm talking about 10000 particles here.

Furthermore, maybe your graphics card is not that fancy as your CPU.
short wrote:Iterators instead of for loops may help a little bit.
Iterators in an standard array? ;) He also implied that the rendering is making trouble.

Edit: Just saw you have a vector class with Get functions, that's another 20000 function calls each frame. Make x and y public.
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Particle System bugs

Post by mv2112 »

Ok, so i've started learning openGL a little bit now. I FINALY was able to make a VBO and store the particle positions in it, however it is SLOW AS HELL! I'm new at this so this code may be a little screwed up. First, i generate the buffer in the particle system constructor, and delete it in the destructor. This is my render function:

Code: Select all

	glBindBuffer(GL_ARRAY_BUFFER,PB); //PB is the VBO
	glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*pos.size(),&pos[0],GL_STATIC_DRAW); //pos is the vector that holds the x and y's
	glBindBuffer(1,PB);

	glLoadIdentity();
	glClear(GL_COLOR_BUFFER_BIT);
	glPointSize(s);
	glVertexPointer(2,GL_FLOAT,0,BUFFER_OFFSET(0));
	glEnableClientState(GL_VERTEX_ARRAY);

	for(int i=0;i<pos.size()/2;i++)
	{
		glDrawArrays(GL_POINTS,i,1);
	}

	glDisableClientState(GL_VERTEX_ARRAY);
	pos.clear(); //pos gets refilled when the particles are updated
EDIT: SUPER slow in debug mode, slower than my SDL version in release mode
Why would this be so slow?
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Particle System bugs

Post by mv2112 »

I took the draw arrays out of the for loop and it runs faster now. Does anyone know any good optimization tips?
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Particle System bugs

Post by K-Bal »

Draw the whole buffer with ONE call to glDrawArrays and very much more important: don't copy the data into you buffer every frame. You are grilling your bus ;) Get yourself a pointer to the buffer memory and update only what you need to update. Or use shaders to update the buffer on the GPU.

I know from my own experience that the latter is highly performant. Here is a little demo I put together:

<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/-3MqRjlx81E&hl ... ram><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/-3MqRjlx81E&hl=de_DE&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>[/youtube]

30000 is not even the limit and remember that I'm simultaneously capturing 30 full frames per second of video.

If you don't want to have the data on the GPU, use glVertexPointer to point to your raw data and draw this with glDrawArrays. Otherwise you won't gain anything from the VBO.
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Particle System bugs

Post by mv2112 »

K-Bal wrote:Draw the whole buffer with ONE call to glDrawArrays and very much more important: don't copy the data into you buffer every frame. You are grilling your bus ;) Get yourself a pointer to the buffer memory and update only what you need to update. Or use shaders to update the buffer on the GPU.
I made it use one call to glDrawArrays and it is alot faster now, but how would i not copy the data into the buffer each frame when all the particles are constantly changing position? I would need to update every particle's vertices every frame, right?
Post Reply