Advertisement
thecplusplusguy

GLSL tutorial 4 - fragment.frag

Aug 3rd, 2012
2,012
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //GLSL tutorial - per-fragment (per-pixel lighting)
  3. //fragment.frag
  4. #version 120
  5. varying vec3 position;
  6. varying vec3 normal;
  7.  
  8. uniform vec3 lightPos;
  9.  
  10. uniform vec3 mambient;  //gl_FrontMaterial
  11. uniform vec3 mdiffuse;
  12. uniform vec3 mspecular;
  13. uniform float shininess;
  14.  
  15. uniform vec3 lambient;  //gl_LightSource[0]
  16. uniform vec3 ldiffuse;
  17. uniform vec3 lspecular;
  18.  
  19.  
  20. void main()
  21. {
  22.     float dist=length(position-lightPos);   //distance from light-source to surface
  23.     float att=1.0/(1.0+0.1*dist+0.01*dist*dist);    //attenuation (constant,linear,quadric)
  24.     vec3 ambient=mambient*lambient; //the ambient light
  25.    
  26.     vec3 surf2light=normalize(lightPos-position);
  27.     vec3 norm=normalize(normal);
  28.     float dcont=max(0.0,dot(norm,surf2light));
  29.     vec3 diffuse=dcont*(mdiffuse*ldiffuse);
  30.    
  31.     vec3 surf2view=normalize(-position);
  32.     vec3 reflection=reflect(-surf2light,norm);
  33.    
  34.     float scont=pow(max(0.0,dot(surf2view,reflection)),shininess);
  35.     vec3 specular=scont*lspecular*mspecular;
  36.    
  37.     gl_FragColor=vec4((ambient+diffuse+specular)*att,1.0);  //<- don't forget the paranthesis (ambient+diffuse+specular)
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement