Sign in to follow this  
ironlungs

GLSL and fading particles.

Recommended Posts

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;
}

Share this post


Link to post
Share on other sites
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;
}



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

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