Advertisement
thecplusplusguy

GLSL tutorial 5 - fragment.frag

Aug 9th, 2012
1,336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //normalmapping (bump-mapping) fragment.frag file (fragment-shader)
  3. #version 120
  4.  
  5. uniform sampler2D img;
  6. uniform sampler2D normalmap;
  7. varying vec2 texcoord;
  8.  
  9. varying vec3 position;
  10. varying vec3 normal;
  11.  
  12. uniform vec3 lightPos;
  13.  
  14. uniform vec3 mambient;  //gl_FrontMaterial
  15. uniform vec3 mdiffuse;
  16. uniform vec3 mspecular;
  17. uniform float shininess;
  18.  
  19. uniform vec3 lambient;  //gl_LightSource[0]
  20. uniform vec3 ldiffuse;
  21. uniform vec3 lspecular;
  22.  
  23. varying vec3 tangentSurface2light;
  24. varying vec3 tangentSurface2view;
  25.  
  26.  
  27. void main()
  28. {
  29.     vec3 lightPos2=vec3(gl_ModelViewMatrix*vec4(lightPos,1.0));
  30.     vec3 texcolor=vec3(texture2D(img,texcoord));
  31.     float dist=length(position-lightPos2);
  32.     float att=1.0/(1.0+0.01*dist+0.001*dist*dist);
  33.     vec3 ambient=texcolor*lambient;
  34.    
  35.     vec3 surf2light=normalize(tangentSurface2light);
  36.     vec3 norm=normalize(texture2D(normalmap,texcoord).xyz*2.0-1.0);
  37.     float dcont=max(0.0,dot(norm,surf2light));
  38.     vec3 diffuse=dcont*(texcolor*ldiffuse);
  39.    
  40.     vec3 surf2view=normalize(tangentSurface2view);
  41.     vec3 reflection=reflect(-surf2light,norm);
  42.    
  43.     float scont=pow(max(0.0,dot(surf2view,reflection)),shininess);
  44.     vec3 specular=scont*lspecular*mspecular;
  45.    
  46.     gl_FragColor=vec4((ambient+diffuse+specular)*att,1.0);
  47.     //gl_FragColor=vec4(tangentSurface2view,1.0);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement