//============================================================================= // // Project: SharpImage // Module: shader-vm-enhance-simple.frag // Language: OpenGL Shading Language (GLSL) // Author: Dan Mueller // Date: $Date: 2007-09-26 15:20:34 +1000 (Wed, 26 Sep 2007) $ // Revision: $Revision: 26 $ // // 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 uniform float fSamplingRate; // Current sampling rate uniform float fNormalOffset; // Offset for normal calculataions uniform float fFactor1; // Enhancement factor uniform float fFactor2; // Enhancement factor uniform float fFactor3; // Enhancement factor void main() { // Interpolate images float value = vec4( texture3D(sam3Tex1, gl_TexCoord[1]) ).a; float grad = vec4( texture3D(sam3Tex2, gl_TexCoord[2]) ).a; // Interpolate transfer function vec3 pos3Tf = vec3( value, 1.0-grad, 0.0 ); vec4 val4Tf = texture3D( sam3Tex0, pos3Tf ); // Discard if no contribution if (val4Tf.a <= 0.0) discard; // Adjust alpha for sampling rate val4Tf.a = 1.0 - pow( 1.0 - val4Tf.a, 1.0 / fSamplingRate ); // Compute the normal vec4 vec4G = vec4( 0.0 ); for ( int i=0; i<3; i++ ) { vec3 pos3L = vec3( gl_TexCoord[1] ); pos3L[i] -= fNormalOffset; vec3 pos3R = vec3( gl_TexCoord[1] ); pos3R[i] += fNormalOffset; float valL = texture3D( sam3Tex1, pos3L ).a; float valR = texture3D( sam3Tex1, pos3R ).a; vec4G[i] = (valL - valR) / 2.0; } vec4 vec4N = normalize( vec4G ); // Compute light/viewing 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 and apply enhancements vec4 black = vec4( 0.0, 0.0, 0.0, 1.0 ); float fLdotN = dot( vec4L, vec4N ); float boundary = (fFactor1 <= 0.0) ? 1.0 : pow( value, fFactor1 ); float silhouette = (fFactor1 <= 0.0) ? 0.0 : pow( 1.0 - abs( fLdotN ), fFactor1 ); if ( silhouette > fFactor2 ) { val4Tf = mix( black, val4Tf, fFactor3 ); val4Tf.a *= silhouette; } // Set color gl_FragColor = val4Tf; }