Sign in to follow this  

Calculating screen coordinates (gl_FragCoord in vertex shader

Recommended Posts

Im making a ray or a "laser" for my game using GL_LINES. I want the laser to have a strong clear light in the middle and to get more and more transparent at the edges. For this I want to do a point - line distance calculation in the fragment shader.

To my vertex shader I send, vertices for the laser, a directional vector for each vertex and also the modelview matrix.

I wish to make something similar to this[i] (see quote/link below)[/i], but I cant get the calculations for [b]gEndpoints[2] [/b]and [b]gPosition [i](gl_FragCoord?) [/i][/b]correct. I wish to translate my vertex coordinates into the same coordinate space as the gl_FragCoord. I've tried numerous variations by googling the problem and some bruteforce matrix multiplications but I just cant get it right. Any help would be greatly appreciated.


[b] Glow With Point-Line Distance[/b]

Here’s my fragment shader for Tron-like glow. For this to link properly, the geometry shader needs to output some screen-space coordinates for the nodes in the line strip ([b]gEndpoints[2][/b] and [b]gPosition[/b]). The fragment shader simply computes a point-line distance, using the result for the pixel’s intensity. It’s actually computing distance as “point-to-segment” rather than “point-to-infinite-line”. If you prefer the latter, you might be able to make an optimization by moving the distance computation up into the geometry shader.

[size=1][size=1] [/size][/size][/left]

uniform vec4 Color;

varying vec2 gEndpoints[2];
varying vec2 gPosition;

uniform float Radius;
uniform mat4 Projection;

// Return distance from point 'p' to line segment 'a b':
float line_distance(vec2 p, vec2 a, vec2 b)
float dist = distance(a,b);
vec2 v = normalize(b-a);
float t = dot(v,p-a);
vec2 spinePoint;
if (t > dist) spinePoint = b;
else if (t > 0.0) spinePoint = a + t*v;
else spinePoint = a;
return distance(p,spinePoint);

void main()
float d = line_distance(gPosition, gEndpoints[0], gEndpoints[1]);
gl_FragColor = vec4(vec3(1.0 - 12.0 * d), 1.0);

[url=""][/url] - link to the page

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