Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

vmd.frag

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2011 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 /***************************************************************************
00009  * RCS INFORMATION:
00010  *
00011  *      $RCSfile: vmd.frag,v $
00012  *      $Author: johns $        $Locker:  $             $State: Exp $
00013  *      $Revision: 1.54 $       $Date: 2010/12/16 04:10:13 $
00014  *
00015  ***************************************************************************
00016  * DESCRIPTION:
00017  *  This file contains the VMD OpenGL fragment shader implementing 
00018  *  per-pixel lighting with phong highlights etc.
00019  ***************************************************************************/
00020 
00021 // requires GLSL version 1.10
00022 #version 110
00023 
00024 //
00025 // Fragment shader varying and uniform variable definitions for data 
00026 // supplied by VMD and/or the vertex shader
00027 //
00028 varying vec3 oglnormal;       // interpolated normal from the vertex shader
00029 varying vec3 oglcolor;        // interpolated color from the vertex shader
00030 varying vec3 V;               // view direction vector
00031 uniform vec3 vmdlight0;       // VMD directional lights
00032 uniform vec3 vmdlight1;
00033 uniform vec3 vmdlight2;
00034 uniform vec3 vmdlight3;
00035 
00036 uniform vec3 vmdlight0H;      // Blinn halfway vectors for all four lights
00037 uniform vec3 vmdlight1H;
00038 uniform vec3 vmdlight2H;
00039 uniform vec3 vmdlight3H;
00040 
00041 uniform vec4 vmdlightscale;   // VMD light on/off state for all 4 VMD lights,
00042                               // represented as a scaling constant.  Could be
00043                               // done with on/off flags but ATI doesn't deal
00044                               // well with branching constructs, so this value
00045                               // is simply multiplied by the light's 
00046                               // contribution.  Hacky, but it works for now.
00047 
00048 uniform vec4 vmdmaterial;     // VMD material properties
00049                               // [0] is ambient (white ambient light only)
00050                               // [1] is diffuse
00051                               // [2] is specular
00052                               // [3] is shininess
00053 
00054 uniform float vmdopacity;     // VMD global alpha value
00055 
00056 uniform float vmdoutline;     // VMD outline shading
00057 
00058 uniform float vmdoutlinewidth;// VMD outline shading width
00059 
00060 uniform int vmdtransmode;     // VMD transparency mode
00061 
00062 uniform int vmdfogmode;       // VMD depth cueing / fog mode
00063 
00064 uniform int vmdtexturemode;   // VMD texture mode 0=off 1=modulate 2=replace
00065 uniform sampler3D vmdtex0;    // active 3-D texture map
00066 
00067 //
00068 // VMD Fragment Shader
00069 //
00070 void main(void) {
00071   vec3 texcolor;                    // texture color if needed
00072 
00073   // perform texturing operations for volumetric data start texture
00074   // fetch as early as possible to hide memory latency
00075   if (vmdtexturemode != 0) {
00076     texcolor = vec3(texture3D(vmdtex0, gl_TexCoord[0].xyz));
00077   }
00078   
00079   // Flip the surface normal if it is facing away from the viewer,
00080   // determined by polygon winding order provided by OpenGL.
00081   vec3 N = normalize(oglnormal);
00082   if (!gl_FrontFacing) {
00083     N = -N;
00084   }
00085 
00086   // beginning of shading calculations
00087   float ambient = vmdmaterial[0];   // ambient
00088   float diffuse = 0.0;
00089   float specular = 0.0;
00090   float shininess = vmdmaterial[3]; // shininess 
00091 
00092   // calculate diffuse lighting contribution
00093   diffuse += max(0.0, dot(N, vmdlight0)) * vmdlightscale[0];
00094   diffuse += max(0.0, dot(N, vmdlight1)) * vmdlightscale[1];
00095   diffuse += max(0.0, dot(N, vmdlight2)) * vmdlightscale[2];
00096   diffuse += max(0.0, dot(N, vmdlight3)) * vmdlightscale[3];
00097   diffuse *= vmdmaterial[1]; // diffuse scaling factor
00098 
00099   // compute edge outline if enabled
00100   if (vmdoutline > 0.0) {
00101     float edgefactor = dot(N,V);
00102     edgefactor = 1.0 - (edgefactor*edgefactor);
00103     edgefactor = 1.0 - pow(edgefactor, (1.0-vmdoutlinewidth)*32.0);
00104     diffuse = mix(diffuse, diffuse * edgefactor, vmdoutline);
00105   }
00106 
00107   // calculate specular lighting contribution with Phong highlights, based
00108   // on Blinn's halfway vector variation of Phong highlights
00109   specular += pow(max(0.0, dot(N, vmdlight0H)), shininess) * vmdlightscale[0];
00110   specular += pow(max(0.0, dot(N, vmdlight1H)), shininess) * vmdlightscale[1];
00111   specular += pow(max(0.0, dot(N, vmdlight2H)), shininess) * vmdlightscale[2];
00112   specular += pow(max(0.0, dot(N, vmdlight3H)), shininess) * vmdlightscale[3];
00113   specular *= vmdmaterial[2]; // specular scaling factor
00114 
00115   // Fog computations
00116   const float Log2E = 1.442695; // = log2(2.718281828)
00117   float fog = 1.0;
00118   if (vmdfogmode == 1) {
00119     // linear fog
00120     fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;
00121   } else if (vmdfogmode == 2) {
00122     // exponential fog
00123     fog = exp2(-gl_Fog.density * gl_FogFragCoord * Log2E);
00124   } else if (vmdfogmode == 3) { 
00125     // exponential-squared fog
00126     fog = exp2(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord * Log2E);
00127   }
00128   fog = clamp(fog, 0.0, 1.0);       // clamp the final fog parameter [0->1)
00129 
00130   vec3 objcolor = oglcolor * vec3(diffuse);         // texturing is disabled
00131   if (vmdtexturemode == 1) {
00132     objcolor = oglcolor * texcolor * vec3(diffuse); // emulate GL_MODULATE
00133   } else if (vmdtexturemode == 2) {
00134     objcolor = texcolor;                            // emulate GL_REPLACE
00135   } 
00136 
00137   vec3 color = objcolor + vec3(ambient + specular);
00138 
00139   float alpha = vmdopacity;
00140 
00141   // Emulate Raster3D's angle-dependent surface opacity if enabled
00142   if (vmdtransmode==1) {
00143     alpha = 1.0 + cos(3.1415926 * (1.0-alpha) * dot(N,V));
00144     alpha = alpha*alpha * 0.25;
00145   }
00146 
00147   gl_FragColor = vec4(mix(vec3(gl_Fog.color), color, fog), alpha);
00148 }
00149 
00150 

Generated on Thu May 24 01:51:36 2012 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002