• Advertisement
Sign in to follow this  

Calculating screen coordinates (gl_FragCoord in vertex shader

This topic is 2158 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

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 (see quote/link below), but I cant get the calculations for gEndpoints[2] and gPosition (gl_FragCoord?) 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.

Glow With Point-Line Distance

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 (gEndpoints[2] and gPosition). 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.


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


http://prideout.net/blog/?p=61 - link to the page

Share this post

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

  • Advertisement