Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actualpowly k

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

#3powly k

Posted 19 January 2013 - 03:28 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 shadersmooth out vec3 pos;void main() {  pos = gl_Vertex.xyz;  gl_Position = gl_ModelViewMatrix * gl_Vertex;}
//fragment shadersmooth 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 shadersmooth out float alpha;in float opaque;void main(){  alpha = opaque;  gl_Position = gl_ModelViewMatrix * gl_Vertex;}
//fragment shadersmooth 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.

#2powly k

Posted 19 January 2013 - 03:27 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 shadersmooth out vec3 pos;void main() {  pos = gl_Vertex.xyz;  gl_Position = gl_ModelViewMatrix * gl_Vertex;}
//fragment shadersmooth 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 shadersmooth out float alpha;in float opaque;void main(){  alpha = opaque;  gl_Position = gl_ModelViewMatrix * gl_Vertex;}
//fragment shadersmooth 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.

#1powly k

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 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.


PARTNERS