GLSL and fading particles.

Started by
4 comments, last by ironlungs 16 years, 4 months ago
Hi All. I'm trying to understand some basic GLSL by having particles use emission and fade over time. The only problem is they don't appear to fade out. Particles just abruptly disappear instead of fading to nothingness when they die. I'm not sure what is going on since I am a newbie. Any help would be appreciated! The code defined in the update() portion of my code is as follows:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glShadeModel(GL_SMOOTH);
...
static float color[] = { 0.0f, 0.0f, 1.0f, it->getLife() };
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
...

"it" is an iterator that loops through all of my particles and gets there life which is set as the alpha value for the color. My shaders are rather simple. Vertex:

void main()
{
	
	gl_FrontColor = gl_FrontMaterial.emission;
	gl_Position = ftransform();
}

Pixel:

void main()
{
	
	gl_FragColor = gl_FrontMaterial.emission;
}

Advertisement
I'm not sure if gl_FrontMaterial can be accessed in the fragment shader (I've never done this myself, and I don't have my OpenGL book for reference at the moment either). But, you could try putting the gl_FrontMaterial value in a varying variable. So your shaders would look as following:

Vertex:
varying vec4 emission;void main(){		emission = gl_FrontMaterial.emission;	gl_Position = ftransform();}


Fragment:
varying vec4 emission;void main(){		gl_FragColor = emission;}
while (tired) DrinkCoffee();
polymorphed, thanks for the reply.

I tried your suggestion and the result is the same. I don't have any other lights defined so I'm not sure why the particles would still be visible and then abruptly disappear instead of fading out.
Try to isolate down to the part which is causing the problem.
The first thing I'd do is disable GL_BLEND, and see if it works.
And you can just create a uniform variable in your shader, it does the same thing as using glMaterial, but it's "easier" in my opinion.

Vertex:
uniform vec4 uni_fade;varying vec4 var_fade;void main(){	var_fade = uni_fade;	gl_Position = ftransform();}



Fragment:
varying vec4 var_fade;void main(){        gl_FragColor = var_fade;}


And to upload the colors in the uniform variable:
(You can only use glUniform outside a glBegin()..glEnd() block)
glUniform4f(glGetUniformLocation(handle_to_shader, "uni_fade"), red, green, blue, alpha);


And lastly, make sure that your getLife() function is returning correct values :)

I hope this helps.
while (tired) DrinkCoffee();
Make sure getLife is between 0 and 1. Disable lighting and try glColor4f() and use gl_FrontColor.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

I managed to fix my problem. After going over all the suggestions from you guys, and fiddling with code here and there it eventually worked. I don't really know what I was doing wrong. I think having glEnable(GL_BLEND) inside of a glBegin() might have been the problem...silly me.

Again, thank you dpadam450 and polymorphed.

This topic is closed to new replies.

Advertisement