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.55 $ $Date: 2020年02月24日 21:25:51 $ 00014 * 00015 ***************************************************************************/ 00021 00023 #version 110 00024 00025 // 00026 // Fragment shader varying and uniform variable definitions for data 00027 // supplied by VMD and/or the vertex shader 00028 // 00029 varying vec3 oglnormal; 00030 varying vec3 oglcolor; 00031 varying vec3 V; 00032 uniform vec3 vmdlight0; 00033 uniform vec3 vmdlight1; 00034 uniform vec3 vmdlight2; 00035 uniform vec3 vmdlight3; 00036 00037 uniform vec3 vmdlight0H; 00038 uniform vec3 vmdlight1H; 00039 uniform vec3 vmdlight2H; 00040 uniform vec3 vmdlight3H; 00041 00042 uniform vec4 vmdlightscale; 00043 00044 00045 00046 00047 00048 00049 uniform vec4 vmdmaterial; 00050 00051 00052 00053 00054 00055 uniform float vmdopacity; 00056 00057 uniform float vmdoutline; 00058 00059 uniform float vmdoutlinewidth; 00060 00061 uniform int vmdtransmode; 00062 00063 uniform int vmdfogmode; 00064 00065 uniform int vmdtexturemode; 00066 uniform sampler3D vmdtex0; 00067 00071 void main(void) { 00072 vec3 texcolor; 00073 00074 // perform texturing operations for volumetric data start texture 00075 // fetch as early as possible to hide memory latency 00076 if (vmdtexturemode != 0) { 00077 texcolor = vec3(texture3D(vmdtex0, gl_TexCoord[0].xyz)); 00078 } 00079 00080 // Flip the surface normal if it is facing away from the viewer, 00081 // determined by polygon winding order provided by OpenGL. 00082 vec3 N = normalize(oglnormal); 00083 if (!gl_FrontFacing) { 00084 N = -N; 00085 } 00086 00087 // beginning of shading calculations 00088 float ambient = vmdmaterial[0]; // ambient 00089 float diffuse = 0.0; 00090 float specular = 0.0; 00091 float shininess = vmdmaterial[3]; // shininess 00092 00093 // calculate diffuse lighting contribution 00094 diffuse += max(0.0, dot(N, vmdlight0)) * vmdlightscale[0]; 00095 diffuse += max(0.0, dot(N, vmdlight1)) * vmdlightscale[1]; 00096 diffuse += max(0.0, dot(N, vmdlight2)) * vmdlightscale[2]; 00097 diffuse += max(0.0, dot(N, vmdlight3)) * vmdlightscale[3]; 00098 diffuse *= vmdmaterial[1]; // diffuse scaling factor 00099 00100 // compute edge outline if enabled 00101 if (vmdoutline > 0.0) { 00102 float edgefactor = dot(N,V); 00103 edgefactor = 1.0 - (edgefactor*edgefactor); 00104 edgefactor = 1.0 - pow(edgefactor, (1.0-vmdoutlinewidth)*32.0); 00105 diffuse = mix(diffuse, diffuse * edgefactor, vmdoutline); 00106 } 00107 00108 // calculate specular lighting contribution with Phong highlights, based 00109 // on Blinn's halfway vector variation of Phong highlights 00110 specular += pow(max(0.0, dot(N, vmdlight0H)), shininess) * vmdlightscale[0]; 00111 specular += pow(max(0.0, dot(N, vmdlight1H)), shininess) * vmdlightscale[1]; 00112 specular += pow(max(0.0, dot(N, vmdlight2H)), shininess) * vmdlightscale[2]; 00113 specular += pow(max(0.0, dot(N, vmdlight3H)), shininess) * vmdlightscale[3]; 00114 specular *= vmdmaterial[2]; // specular scaling factor 00115 00116 // Fog computations 00117 const float Log2E = 1.442695; // = log2(2.718281828) 00118 float fog = 1.0; 00119 if (vmdfogmode == 1) { 00120 // linear fog 00121 fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale; 00122 } else if (vmdfogmode == 2) { 00123 // exponential fog 00124 fog = exp2(-gl_Fog.density * gl_FogFragCoord * Log2E); 00125 } else if (vmdfogmode == 3) { 00126 // exponential-squared fog 00127 fog = exp2(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord * Log2E); 00128 } 00129 fog = clamp(fog, 0.0, 1.0); // clamp the final fog parameter [0->1) 00130 00131 vec3 objcolor = oglcolor * vec3(diffuse); // texturing is disabled 00132 if (vmdtexturemode == 1) { 00133 objcolor = oglcolor * texcolor * vec3(diffuse); // emulate GL_MODULATE 00134 } else if (vmdtexturemode == 2) { 00135 objcolor = texcolor; // emulate GL_REPLACE 00136 } 00137 00138 vec3 color = objcolor + vec3(ambient + specular); 00139 00140 float alpha = vmdopacity; 00141 00142 // Emulate Raster3D's angle-dependent surface opacity if enabled 00143 if (vmdtransmode==1) { 00144 alpha = 1.0 + cos(3.1415926 * (1.0-alpha) * dot(N,V)); 00145 alpha = alpha*alpha * 0.25; 00146 } 00147 00148 gl_FragColor = vec4(mix(vec3(gl_Fog.color), color, fog), alpha); 00149 } 00150 00151