You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.2 KiB

  1. #version 330
  2. // Input vertex attributes (from vertex shader)
  3. in vec2 fragTexCoord;
  4. in vec3 fragNormal;
  5. // Input uniform values
  6. uniform sampler2D texture0;
  7. uniform vec4 colDiffuse;
  8. // Output fragment color
  9. out vec4 finalColor;
  10. // NOTE: Add here your custom variables
  11. // Light uniform values
  12. uniform vec3 lightAmbientColor = vec3(0.6, 0.3, 0.0);
  13. uniform vec3 lightDiffuseColor = vec3(1.0, 0.5, 0.0);
  14. uniform vec3 lightSpecularColor = vec3(0.0, 1.0, 0.0);
  15. uniform float lightIntensity = 1.0;
  16. uniform float lightSpecIntensity = 1.0;
  17. // Material uniform values
  18. uniform vec3 matAmbientColor = vec3(1.0, 1.0, 1.0);
  19. uniform vec3 matSpecularColor = vec3(1.0, 1.0, 1.0);
  20. uniform float matGlossiness = 50.0;
  21. // World uniform values
  22. uniform vec3 lightPosition;
  23. uniform vec3 cameraPosition;
  24. // Fragment shader output data
  25. out vec4 fragColor;
  26. // Calculate ambient lighting component
  27. vec3 AmbientLighting()
  28. {
  29. return (matAmbientColor*lightAmbientColor);
  30. }
  31. // Calculate diffuse lighting component
  32. vec3 DiffuseLighting(in vec3 N, in vec3 L)
  33. {
  34. // Lambertian reflection calculation
  35. float diffuse = clamp(dot(N, L), 0, 1);
  36. return (colDiffuse.xyz*lightDiffuseColor*lightIntensity*diffuse);
  37. }
  38. // Calculate specular lighting component
  39. vec3 SpecularLighting(in vec3 N, in vec3 L, in vec3 V)
  40. {
  41. float specular = 0.0;
  42. // Calculate specular reflection only if the surface is oriented to the light source
  43. if (dot(N, L) > 0)
  44. {
  45. // Calculate half vector
  46. vec3 H = normalize(L + V);
  47. // Calculate specular intensity
  48. specular = pow(dot(N, H), 3 + matGlossiness);
  49. }
  50. return (matSpecularColor*lightSpecularColor*lightSpecIntensity*specular);
  51. }
  52. void main()
  53. {
  54. // Normalize input vectors
  55. vec3 L = normalize(lightPosition);
  56. vec3 V = normalize(cameraPosition);
  57. vec3 N = normalize(fragNormal);
  58. // Calculate lighting components
  59. vec3 ambient = AmbientLighting();
  60. vec3 diffuse = DiffuseLighting(N, L);
  61. vec3 specular = SpecularLighting(N, L, V);
  62. // Texel color fetching from texture sampler
  63. vec4 texelColor = texture(texture0, fragTexCoord);
  64. // Calculate final fragment color
  65. finalColor = vec4(texelColor.rgb*(ambient + diffuse + specular), texelColor.a);
  66. }