home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / tools_install / gDEBugger-2_1_book.msi / tpFragmentShader.glsl < prev    next >
Encoding:
Text File  |  2005-06-19  |  2.2 KB  |  66 lines

  1. //------------------------------ tpFragmentShader.glsl ------------------------------
  2.  
  3. // --------------------------------------------------------
  4. //  ⌐ 2004-2005 Graphic Remedy. All Rights Reserved.
  5. // --------------------------------------------------------
  6.  
  7. // Controls texture influence [0,1]:
  8. uniform float textureInfluence;
  9.  
  10. // Allows access to our texture:
  11. uniform sampler2D textureSampler;
  12.  
  13. // Contains the surface normal:
  14. varying vec3 surfaceNormal;
  15.  
  16. // Contains the vertex interpulated position:
  17. varying vec3 vertexPosition;
  18.  
  19.  
  20. // ---------------------------------------------------------------------------
  21. // Name:        Teapot application fragment shader main function
  22. // ---------------------------------------------------------------------------
  23. void main (void)
  24. {
  25.     // Calculate the Phong model eye, L and R vectors:
  26.     vec3 eyeVec = normalize(-vertexPosition); 
  27.     vec3 L = normalize(gl_LightSource[0].position.xyz - vertexPosition); 
  28.     vec3 R = normalize(-reflect(L, surfaceNormal)); 
  29.  
  30.     // Calculate ambient term:
  31.     vec4 ambientTerm = gl_FrontLightProduct[0].ambient;
  32.  
  33.     // Calculate diffuse term:
  34.     vec4 diffuseTerm = vec4(0.0, 0.0, 0.0, 1.0);
  35.     float dotSurfaceNormalAndL = dot(surfaceNormal, L);
  36.     if (dotSurfaceNormalAndL > 0.0)
  37.     {
  38.         diffuseTerm = gl_FrontLightProduct[0].diffuse * max(dotSurfaceNormalAndL, 0.0);
  39.     }
  40.     
  41.     // Calculate specular term:
  42.     vec4 specularTerm = vec4(0.0, 0.0, 0.0, 1.0);
  43.     float dotRAndEyeVec = dot(R, eyeVec);
  44.     if (dotRAndEyeVec > 0.0)
  45.     {
  46.         specularTerm = gl_FrontLightProduct[0].specular * pow(max(dotRAndEyeVec, 0.0), 0.3 * gl_FrontMaterial.shininess);
  47.     }
  48.  
  49.     // Get the texel color:
  50.     vec4 texelColor = texture2D(textureSampler, gl_TexCoord[0].st);
  51.  
  52.     // Apply alpha component on the texture color:
  53.     float alpha = texelColor.a;
  54.     vec4 newTexelColor = texelColor * alpha;
  55.     newTexelColor.a = 1.0;
  56.  
  57.     // Mix texture color and ambient color:
  58.     vec4 texAndAmbientColor = ((1.0 - textureInfluence) * ambientTerm) + (textureInfluence * newTexelColor);
  59.  
  60.     // Calculate final color:
  61.     vec4 finalColor = gl_FrontLightModelProduct.sceneColor + texAndAmbientColor + diffuseTerm + specularTerm; 
  62.     
  63.     // Write final color:
  64.     gl_FragColor = finalColor;
  65. }
  66.