Advertisement
Guest User

opengl fragmentshader

a guest
Aug 16th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #version 330 core
  2.  
  3. in vec2 UV;
  4. in vec3 Position_worldspace;
  5. in vec3 Normal_cameraspace;
  6. in vec3 EyeDirection_cameraspace;
  7. in vec3 LightDirection_cameraspace;
  8.  
  9. out vec3 color;
  10.  
  11. uniform sampler2D textureSampler;
  12. uniform mat4 modelViewMatrix;
  13. uniform vec3 lightPosition_worldspace;
  14.  
  15. void main() {
  16. vec3 lightColor = vec3(1, 1, 1);
  17. float lightPower = 50.0f;
  18.  
  19. vec3 materialDiffuseColor = texture2D(textureSampler, UV).rgb;
  20. vec3 materialAmbientColor = vec3(0.1, 0.1, 0.1) * materialDiffuseColor;
  21. vec3 materialSpecularColor = vec3(0.3, 0.3, 0.3);
  22.  
  23. float distance = length(lightPosition_worldspace - Position_worldspace);
  24.  
  25. vec3 n = normalize(Normal_cameraspace);
  26. vec3 l = normalize(LightDirection_cameraspace);
  27.  
  28. float cosTheta = clamp(dot(n, l), 0, 1);
  29.  
  30. vec3 eye = normalize(EyeDirection_cameraspace);
  31. vec3 reflection = reflect(-l, n);
  32.  
  33. float cosAlpha = clamp(dot(eye, reflection), 0, 1);
  34.  
  35. color = materialAmbientColor +
  36. materialDiffuseColor * lightColor * lightPower * cosTheta / (distance * distance) +
  37. materialSpecularColor * lightColor * lightPower * pow(cosAlpha, 5) / (distance * distance);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement