//============================================================================= // // Project: SharpImage // Module: shader-vmt-phong-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 sampler3D sam3Tex3; // Sampler for tags image uniform float fSamplingRate; // Current sampling rate uniform float fNormalOffset; // Offset for normal calculataions void main() { // Interpolate images float value = vec4( texture3D(sam3Tex1, gl_TexCoord[1]) ).a; float grad = vec4( texture3D(sam3Tex2, gl_TexCoord[2]) ).a; float tag = vec4( texture3D(sam3Tex3, gl_TexCoord[3]) ).a; tag = step( 0.5, tag ); /* // Interpolate tag vec3 size = vec3( 256, 256, 110 ); vec3 spacing = vec3( 1.0, 1.0, 1.0 ); vec3 vec3TagXL = vec3( gl_TexCoord[3] ); vec3TagXL.x = floor( gl_TexCoord[3].x*size.x ) / size.x; vec3 vec3TagXR = vec3( gl_TexCoord[3] ); vec3TagXR.x = ceil ( gl_TexCoord[3].x*size.x ) / size.x; vec3 vec3TagYL = vec3( gl_TexCoord[3] ); vec3TagYL.y = floor( gl_TexCoord[3].y*size.y ) / size.y; vec3 vec3TagYR = vec3( gl_TexCoord[3] ); vec3TagYR.y = ceil ( gl_TexCoord[3].y*size.y ) / size.y; vec3 vec3TagZL = vec3( gl_TexCoord[3] ); vec3TagZL.z = floor( gl_TexCoord[3].z*size.z ) / size.z; vec3 vec3TagZR = vec3( gl_TexCoord[3] ); vec3TagZR.z = ceil ( gl_TexCoord[3].z*size.z ) / size.z; float fTagXL = vec4( texture3D(sam3Tex3, vec3TagXL) ).a; float fTagXR = vec4( texture3D(sam3Tex3, vec3TagXR) ).a; float fTagYL = vec4( texture3D(sam3Tex3, vec3TagYL) ).a; float fTagYR = vec4( texture3D(sam3Tex3, vec3TagYR) ).a; float fTagZL = vec4( texture3D(sam3Tex3, vec3TagZL) ).a; float fTagZR = vec4( texture3D(sam3Tex3, vec3TagZR) ).a; float fTag = vec4( texture3D(sam3Tex3, gl_TexCoord[3]) ).a; float tag = ( fTagXL*(gl_TexCoord[3].x - vec3TagXL.x)*size.x + fTagXR*(vec3TagXR.x - gl_TexCoord[3].x)*size.x ) + ( fTagYL*(gl_TexCoord[3].y - vec3TagYL.y)*size.y + fTagYR*(vec3TagYR.x - gl_TexCoord[3].y)*size.y ) + ( fTagZL*(gl_TexCoord[3].z - vec3TagZL.z)*size.z + fTagZR*(vec3TagZR.x - gl_TexCoord[3].z)*size.z ); //tag /= 3.0; // float tag = ( fTagXL*(gl_TexCoord[3].x - vec3TagXL.x)*size.x // + fTagXR*(vec3TagXR.x - gl_TexCoord[3].x)*size.x ); */ // Index transfer function const float LAYERS = 8.0; const float LAYER0 = 0.0 / ( LAYERS - 1.0 ); const float LAYER1 = 1.0 / ( LAYERS - 1.0 ); const float LAYER2 = 2.0 / ( LAYERS - 1.0 ); const float LAYER3 = 3.0 / ( LAYERS - 1.0 ); vec3 pos3TfAll = vec3( value, 1.0-grad, LAYER0 ); vec3 pos3TfTag1 = vec3( value, 1.0-grad, LAYER1 ); vec4 val4TfBack = texture3D( sam3Tex0, pos3TfAll ); vec4 val4TfTag1 = texture3D( sam3Tex0, pos3TfTag1 ); // Adjust alpha for sampling rate val4TfBack.a = 1.0 - pow( 1.0 - val4TfBack.a, 1.0 / fSamplingRate ); val4TfTag1.a = 1.0 - pow( 1.0 - val4TfTag1.a, 1.0 / fSamplingRate ); // Mix tags const float TAG0 = 0.0; const float TAG1 = 1.0; vec4 val4Mix = float( tag == TAG0 ) * val4TfBack + float( tag == TAG1 ) * val4TfTag1; if ( val4Mix.a <= 0.0 ) discard; // 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 lighting terms float fLdotN = dot( vec4L, vec4N ); vec4 vec4R = normalize( (2.0 * fLdotN * vec4N) - vec4L ); float fRdotL = abs( dot(vec4R, vec4L) ); // Compute lighting contributions gl_LightSourceParameters light = gl_LightSource[0]; vec4 vec4A = light.ambient; vec4 vec4D = light.diffuse * abs( fLdotN ); vec4 vec4S = light.specular * pow( fRdotL, light.spotExponent ); // Apply lighting gl_FragColor = val4Mix * ( vec4A + vec4D + vec4S ); }