Jump to content
  • Advertisement
Sign in to follow this  
iLoveGameProgramming

GLSL Coloring per pixel?

This topic is 2074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

First of all thank you for taking time to answer my question.
I am very new to shaders.

I am trying to fade out my lines using shaders,
this picture shows exacly what I need:

 

whatIreallyreallywant%20copy.png




My Fragment Shader:
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

My Vertex Shader:
void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

This works, and colors them red. Is there a way to check position of the pixel I am coloring. So then I can compare it to the distance away from the beginning of the line. Using that value to calculate the Alpha of the FragColor?
Unless there is another way?

Thank you smile.png

Edited by iLoveGameProgramming

Share this post


Link to post
Share on other sites
Advertisement

Yes. The way is with gl_FragCoord. However, the standard way to so this is by coloring each vertex and then interpolating the color between them.

Share this post


Link to post
Share on other sites
So, in GLSL, you can pass variables from vertex shader to pixel shader. For those variables, you say "in" or "out" in their name, like this:
//vertex shader
smooth out vec3 pos;
void main() {
  pos = gl_Vertex.xyz;
  gl_Position = gl_ModelViewMatrix * gl_Vertex;
}
//fragment shader
smooth in vec3 pos;
void main() {
  gl_FragColor = vec4(pos, 1.0f);
}
This would cause the fragments to be coloured with the world space coordinates - sometimes nice for debugging, but not a generally good-looking idea! So for example the red component would be the x component. As you might notice, theres another specifier before the "out" and "in" ones - "smooth". What that means is that the variable is smoothly interpolated in the 3D space. For example if you render a triangle, the "pos" is evaluated at the corners (the vertex shader is run per every vertex/corner) and interpolated in between. ... or if it's a line, it's smoothly interpolated between the two ends!

To make this best fit your case, you might want to add another attribute to your vertices, which is - of course - the value you want to interpolate, the alpha value of the line. For one end, it will be 1.0 (completely opaque), and for the other one, 0.0 (completely invisible), and GL will do the interpolation like magic. Here's a simple example:
//vertex shader
smooth out float alpha;
in float opaque;
void main(){
  alpha = opaque;
  gl_Position = gl_ModelViewMatrix * gl_Vertex;
}
//fragment shader
smooth in float alpha;
uniform vec3 color;
void main() {
  gl_FragColor = vec4(color, alpha);
}
You can also do "alpha = gl_Vertex.a" and store your opaqueness attribute into the fourth coordinate of the vertices in your lines. This can sometimes be more convenient - setting stuff up in GL is way more tedious than shader programming itself. As a suggestion for the latter, doing some smoothstep on the alpha might look better than just a straight lerp :)

As a final note, the name of both "smooth in" and "smooth out" is "varying", so it'll be "varying float alpha;" etc. in older versions of GLSL which I assume you're using, because you have the now deprecated gl_ModelViewProjectionMatrix there. I really suggest using at least OpenGL 3.0, it clarifies a lot of things. Edited by powly k

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!