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

vmdspheresprite.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: vmdspheresprite.frag,v $
00012  *      $Author: johns $        $Locker:  $             $State: Exp $
00013  *      $Revision: 1.2 $       $Date: 2011/06/03 17:55:54 $
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 oglcolor;        // interpolated color from the vertex shader
00029 varying vec3 V;               // view direction vector
00030 
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   // Texture coords for point sprites range from 0,0 to 1,1, so we use this
00072   // to compute which pixels fall on the sphere (circle)
00073   vec2 pos = gl_TexCoord[0].xy - vec2(0.5, 0.5);
00074   float r2 = pos.x*pos.x + pos.y*pos.y;
00075   if (r2 > 0.25)
00076     discard; // Discard fragments outside of the sphere (circle)
00077 
00078 #if 0
00079   vec3 texcolor;                    // texture color if needed
00080   // perform texturing operations for volumetric data start texture
00081   // fetch as early as possible to hide memory latency
00082   if (vmdtexturemode != 0) {
00083     texcolor = vec3(texture3D(vmdtex0, gl_TexCoord[0].xyz));
00084   }
00085 #endif
00086   
00087   // Compute the surface normal for fragments on the sphere
00088   //  vec3 N = normalize(vec3(pos.x, -pos.y, sqrt(0.25 - r2)));
00089   vec3 N = 2.0*vec3(pos.x, -pos.y, sqrt(0.25 - r2));
00090 
00091   // beginning of shading calculations
00092   float ambient = vmdmaterial[0];   // ambient
00093   float diffuse = 0.0;
00094   float specular = 0.0;
00095   float shininess = vmdmaterial[3]; // shininess 
00096 
00097   // calculate diffuse lighting contribution
00098   diffuse += max(0.0, dot(N, vmdlight0)) * vmdlightscale[0];
00099   diffuse += max(0.0, dot(N, vmdlight1)) * vmdlightscale[1];
00100   diffuse += max(0.0, dot(N, vmdlight2)) * vmdlightscale[2];
00101   diffuse += max(0.0, dot(N, vmdlight3)) * vmdlightscale[3];
00102   diffuse *= vmdmaterial[1]; // diffuse scaling factor
00103 
00104   // compute edge outline if enabled
00105   if (vmdoutline > 0.0) {
00106     float edgefactor = dot(N,V);
00107     edgefactor = 1.0 - (edgefactor*edgefactor);
00108     edgefactor = 1.0 - pow(edgefactor, (1.0-vmdoutlinewidth)*32.0);
00109     diffuse = mix(diffuse, diffuse * edgefactor, vmdoutline);
00110   }
00111 
00112   // calculate specular lighting contribution with Phong highlights, based
00113   // on Blinn's halfway vector variation of Phong highlights
00114   specular += pow(max(0.0, dot(N, vmdlight0H)), shininess) * vmdlightscale[0];
00115   specular += pow(max(0.0, dot(N, vmdlight1H)), shininess) * vmdlightscale[1];
00116   specular += pow(max(0.0, dot(N, vmdlight2H)), shininess) * vmdlightscale[2];
00117   specular += pow(max(0.0, dot(N, vmdlight3H)), shininess) * vmdlightscale[3];
00118   specular *= vmdmaterial[2]; // specular scaling factor
00119 
00120   // Fog computations
00121   const float Log2E = 1.442695; // = log2(2.718281828)
00122   float fog = 1.0;
00123   if (vmdfogmode == 1) {
00124     // linear fog
00125     fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;
00126   } else if (vmdfogmode == 2) {
00127     // exponential fog
00128     fog = exp2(-gl_Fog.density * gl_FogFragCoord * Log2E);
00129   } else if (vmdfogmode == 3) { 
00130     // exponential-squared fog
00131     fog = exp2(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord * Log2E);
00132   }
00133   fog = clamp(fog, 0.0, 1.0);       // clamp the final fog parameter [0->1)
00134 
00135   vec3 objcolor = oglcolor * vec3(diffuse);         // texturing is disabled
00136 #if 0
00137   if (vmdtexturemode == 1) {
00138     objcolor = oglcolor * texcolor * vec3(diffuse); // emulate GL_MODULATE
00139   } else if (vmdtexturemode == 2) {
00140     objcolor = texcolor;                            // emulate GL_REPLACE
00141   } 
00142 #endif
00143 
00144   vec3 color = objcolor + vec3(ambient + specular);
00145 
00146   float alpha = vmdopacity;
00147 
00148   // Emulate Raster3D's angle-dependent surface opacity if enabled
00149   if (vmdtransmode==1) {
00150     alpha = 1.0 + cos(3.1415926 * (1.0-alpha) * dot(N,V));
00151     alpha = alpha*alpha * 0.25;
00152   }
00153 
00154   gl_FragColor = vec4(mix(vec3(gl_Fog.color), color, fog), alpha);
00155 }
00156 
00157 

Generated on Sat May 26 01:48:35 2012 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002