phong.fs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #version 330
  2. // Vertex shader input data
  3. in vec2 fragTexCoord;
  4. in vec3 fragNormal;
  5. // Diffuse data
  6. uniform sampler2D texture0;
  7. uniform vec4 fragTintColor;
  8. // Light attributes
  9. uniform vec3 light_ambientColor = vec3(0.6, 0.3, 0.0);
  10. uniform vec3 light_diffuseColor = vec3(1.0, 0.5, 0.0);
  11. uniform vec3 light_specularColor = vec3(0.0, 1.0, 0.0);
  12. uniform float light_intensity = 1.0;
  13. uniform float light_specIntensity = 1.0;
  14. // Material attributes
  15. uniform vec3 mat_ambientColor = vec3(1.0, 1.0, 1.0);
  16. uniform vec3 mat_specularColor = vec3(1.0, 1.0, 1.0);
  17. uniform float mat_glossiness = 50.0;
  18. // World attributes
  19. uniform vec3 lightPos;
  20. uniform vec3 cameraPos;
  21. // Fragment shader output data
  22. out vec4 fragColor;
  23. vec3 AmbientLighting()
  24. {
  25. return (mat_ambientColor*light_ambientColor);
  26. }
  27. vec3 DiffuseLighting(in vec3 N, in vec3 L)
  28. {
  29. // Lambertian reflection calculation
  30. float diffuse = clamp(dot(N, L), 0, 1);
  31. return (fragTintColor.xyz*light_diffuseColor*light_intensity*diffuse);
  32. }
  33. vec3 SpecularLighting(in vec3 N, in vec3 L, in vec3 V)
  34. {
  35. float specular = 0.0;
  36. // Calculate specular reflection only if the surface is oriented to the light source
  37. if (dot(N, L) > 0)
  38. {
  39. // Calculate half vector
  40. vec3 H = normalize(L + V);
  41. // Calculate specular intensity
  42. specular = pow(dot(N, H), 3 + mat_glossiness);
  43. }
  44. return (mat_specularColor*light_specularColor*light_specIntensity*specular);
  45. }
  46. void main()
  47. {
  48. // Normalize input vectors
  49. vec3 L = normalize(lightPos);
  50. vec3 V = normalize(cameraPos);
  51. vec3 N = normalize(fragNormal);
  52. vec3 ambient = AmbientLighting();
  53. vec3 diffuse = DiffuseLighting(N, L);
  54. vec3 specular = SpecularLighting(N, L, V);
  55. // Get base color from texture
  56. vec4 textureColor = texture(texture0, fragTexCoord);
  57. vec3 finalColor = textureColor.rgb;
  58. fragColor = vec4(finalColor * (ambient + diffuse + specular), textureColor.a);
  59. }