Commit c771c9bc by Mathias Goldau

### [CHANGE] Outsourced distancePointLineSegment shader function into WGEUtils.glsl

parent d4ebabce
 ... ... @@ -111,5 +111,41 @@ vec4 scaleMaxToOne( vec4 point ) return point / maxC; } /** * Computes the distance from a line segment and a point. * * \param start Definig the start of the line segment. * \param end Defining the end of the line segment. * \param point For which the distance should be computed. * * \return Distance from given line segment (start,end) and point. */ float distancePointLineSegment( vec3 point, vec3 start, vec3 end ) { float segmentLength = length( start - end ); if( segmentLength == 0.0 ) { return distance( point, start ); } // Consider the line extending the segment, parameterized as start + t (end - start): // We find projection of point p onto the line: t = [(p-v) . (w-v)] / |w-v|^2 float t = dot( point - start, end - start ) / ( segmentLength * segmentLength ); if( t < 0.0 ) // Beyond the 'v' end of the segment { return distance( point, start ); } else if( t > 1.0 ) // Beyond the 'w' end of the segment { return distance( point, end ); } else { vec3 projection = start + t * ( end - start ); // Projection falls on the segment return distance( point, projection ); } } #endif // WGEUTILS_GLSL
 ... ... @@ -24,6 +24,8 @@ #version 120 #include "WGEUtils.glsl" /** * Color of the fiber stipples. This will be further combined with tract probability. */ ... ... @@ -59,41 +61,10 @@ uniform vec3 middlePoint_tex = vec3( 0.5, 0.5, 0.0 ); */ varying float probability; uniform float u_glyphThickness; /** * Computes the minimal distance from segment vw and point p. * * \param v start point of the segment * \param w end point of the segment * \param p point for which the minimal distance should be computed * * \return minimal distance from segment vw to point p. * Scale the radius of the glyphs (aka stipples). */ float minimum_distance( vec3 v, vec3 w, vec3 p ) { // Return minimum distance between line segment vw and point p float len = length( v - w ); if( len == 0.0 ) // v == w case { return distance( p, v ); } // Consider the line extending the segment, parameterized as v + t (w - v). // We find projection of point p onto the line. // It falls where t = [(p-v) . (w-v)] / |w-v|^2 float t = dot( p - v, w - v ) / ( len * len ); if( t < 0.0 ) // Beyond the 'v' end of the segment { return distance( p, v ); } else if( t > 1.0 ) // Beyond the 'w' end of the segment { return distance( p, w ); } vec3 projection = v + t * ( w - v ); // Projection falls on the segment return distance( p, projection ); } uniform float u_glyphThickness; void main() { ... ... @@ -112,13 +83,13 @@ void main() float r2 = p2 - sqrt( p2 * p2 + q ); float radius = max( r1, r2 ) * u_glyphThickness; if( minimum_distance( scaledFocalPoint1, scaledFocalPoint2, gl_TexCoord[1].xyz ) < radius ) if( distancePointLineSegment( gl_TexCoord[1].xyz, scaledFocalPoint1, scaledFocalPoint2 ) < radius ) { gl_FragColor = u_color * probability; } else { // if( minimum_distance( scaledFocalPoint1, scaledFocalPoint2, gl_TexCoord[1].xyz ) < ( radius + 0.01 ) ) // if( distancePointLineSegment( gl_TexCoord[1].xyz, scaledFocalPoint1, scaledFocalPoint2 ) < ( radius + 0.01 ) ) // { // gl_FragColor = vec4( 1.0, 1.0, 1.0, gl_Color.w ); // } ... ... @@ -136,7 +107,7 @@ void main() // // Color debugging facilities // if( minimum_distance( focalPoint1, focalPoint2, gl_TexCoord[1].xyz ) < 0.01 ) // if( distancePointLineSegment( gl_TexCoord[1].xyz, focalPoint1, focalPoint2 ) < 0.01 ) // { // if( l <= 1.1 ) // { ... ...
 ... ... @@ -25,6 +25,7 @@ #version 120 #include "WIsoline-varyings.glsl" #include "WGEUtils.glsl" // for distancePointLineSegment /** * Line width. ... ... @@ -36,43 +37,6 @@ uniform float u_lineWidth; */ uniform vec4 u_color = vec4( 1.0, 0.0, 0.0, 1.0 ); // TODO(math): this function was copyied over from fiberStipple fragment shader. So please either replace it with // a library function: look in GLSL as well as WGE! Or make it a WGE shader helper function or similar. Last but // not least ask Seb about it! /** * Computes the minimal distance from segment vw and point p. * * \param v start point of the segment * \param w end point of the segment * \param p point for which the minimal distance should be computed * * \return minimal distance from segment vw to point p. */ float minimum_distance( vec3 v, vec3 w, vec3 p ) { // Return minimum distance between line segment vw and point p float len = length( v - w ); if( len == 0.0 ) // v == w case { return distance( p, v ); } // Consider the line extending the segment, parameterized as v + t (w - v). // We find projection of point p onto the line. // It falls where t = [(p-v) . (w-v)] / |w-v|^2 float t = dot( p - v, w - v ) / ( len * len ); if( t < 0.0 ) // Beyond the 'v' end of the segment { return distance( p, v ); } else if( t > 1.0 ) // Beyond the 'w' end of the segment { return distance( p, w ); } vec3 projection = v + t * ( w - v ); // Projection falls on the segment return distance( p, projection ); } /** * Draws a line from point p1 to point p2 with the given width and color. * ... ... @@ -87,7 +51,7 @@ void drawLine( vec3 p1, vec3 p2, float width, vec4 color ) // gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 ); // draw actually the line if( minimum_distance( p1, p2, gl_TexCoord[1].xyz ) < width ) if( distancePointLineSegment( gl_TexCoord[1].xyz, p1, p2 ) < width ) { gl_FragColor = u_color; } ... ...
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!