//============================================================================= // // Project: SharpImage // Module: shader-vm-enhance-phong.frag // Language: OpenGL Shading Language (GLSL) // Author: Dan Mueller // Date: $Date: 2007-07-06 10:57:00 +1000 (Fri, 06 Jul 2007) $ // Revision: $Revision: 2 $ // // Copyright (c) Queensland University of Technology (QUT) 2007. // All rights reserved. // // This software is distributed WITHOUT ANY WARRANTY; without even // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. See the copyright notices for more information. // //============================================================================= uniform sampler3D sam3Tex0; // Sampler for transfer function uniform sampler3D sam3Tex1; // Sampler for value image uniform sampler3D sam3Tex2; // Sampler for gradient image varying vec3 pos3Tex1; // Current coord of value image varying vec3 pos3Tex2; // Current coord of gradient image uniform float fSamplingRate; // Current sampling rate uniform float fNormalOffset; // Offset for normal calculataions uniform float fEnhanceFactor1; // Enhancement factor uniform float fEnhanceFactor2; // Enhancement factor uniform float fEnhanceFactor3; // Enhancement factor // Adjust the alpha value for the current sampling rate float adjustAlphaForSamplingRate( float a ) { return 1.0 - pow( 1.0 - a, 1.0 / fSamplingRate ); } // Compute the gradient using central differences vec4 gradientFromCentralDifferences( ) { vec4 gradient = vec4( 0.0 ); for ( int i=0; i<3; i++ ) { vec3 pos3L = vec3( pos3Tex1 ); pos3L[i] -= fNormalOffset; vec3 pos3R = vec3( pos3Tex1 ); pos3R[i] += fNormalOffset; float valL = texture3D( sam3Tex1, pos3L ).a; float valR = texture3D( sam3Tex1, pos3R ).a; gradient[i] = 0.5*valL - 0.5*valR; } return gradient; } // Compute the normal dot viewing vector float computeNdotV( vec4 vec4N ) { vec4 vec4V = vec4( 0.0, 0.0, -1.0, 0.0 ); vec4V *= gl_ModelViewMatrix; vec4V = normalize( vec4(vec4V.x, vec4V.y, -vec4V.z, 0.0) ); return dot( vec4N, vec4V ); } // Compute the silhouette enhancement float enhanceSilhouette( vec4 vec4N ) { if (fEnhanceFactor1 <= 0.0) return 0.0; return pow( 1.0 - abs( computeNdotV(vec4N) ), fEnhanceFactor1 ); } // Compute the boundary enhancement float enhanceBoundary( float value ) { if (fEnhanceFactor1 <= 0.0) return 1.0; return pow( value, fEnhanceFactor1 ); } // Apply the Phong lighting model vec4 phong( vec4 vec4N, gl_LightSourceParameters light ) { // Compute light/view vector vec4 vec4L = vec4( 0.0, 0.0, -1.0, 0.0 ); vec4L *= gl_ModelViewMatrix; vec4L = normalize( vec4(vec4L.x, vec4L.y, -vec4L.z, 0.0) ); // Compute terms float fLdotN = dot( vec4L, vec4N ); vec4 vec4R = normalize( (2.0 * fLdotN * vec4N) - vec4L ); float fRdotL = abs( dot(vec4R, vec4L) ); // Compute contributions vec4 vec4A = light.ambient; vec4 vec4D = light.diffuse * abs( fLdotN ); vec4 vec4S = light.specular * pow( fRdotL, light.spotExponent ); // Add and return return (vec4A + vec4D + vec4S); } void main() { // Interpolate images float value = vec4( texture3D(sam3Tex1, pos3Tex1) ).a; float gradmag = vec4( texture3D(sam3Tex2, pos3Tex2) ).a; // Interpolate transfer function vec3 pos3Tf = vec3( value, 1.0-gradmag, 0.0 ); vec4 val4Tf = texture3D( sam3Tex0, pos3Tf ); // Discard if no contribution if (val4Tf.a <= 0.0) discard; // Adjust alpha for sampling rate val4Tf.a = adjustAlphaForSamplingRate( val4Tf.a ); // Compute the normal vec4 vec4G = gradientFromCentralDifferences( ); vec4 vec4N = normalize( vec4G ); // Apply enhancements vec4 black = vec4( 0.0, 0.0, 0.0, 1.0 ); float silhouette = enhanceSilhouette( vec4N ); //float boundary = enhanceBoundary( gradmag ); if ( silhouette > fEnhanceFactor3 ) { val4Tf = mix( black, val4Tf, fEnhanceFactor2 ); val4Tf.a *= silhouette; } // Apply phong lighting gl_FragColor = val4Tf * phong( vec4N, gl_LightSource[0] ); }