Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


GLSL Coloring per pixel?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Konrad Jablonski   Members   -  Reputation: 483

Like
0Likes
Like

Posted 19 January 2013 - 10:45 AM

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, 19 January 2013 - 10:48 AM.


Sponsor:

#2 Geometrian   Crossbones+   -  Reputation: 1582

Like
0Likes
Like

Posted 19 January 2013 - 12:31 PM

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.


And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.

#3 powly k   Members   -  Reputation: 653

Like
1Likes
Like

Posted 19 January 2013 - 03:24 PM

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, 19 January 2013 - 03:29 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS