phong_fragment.glsl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // Fragment Shader template
  3. //
  4. // Inputs from vertex shader
  5. in vec4 Position; // Vertex position in camera coordinates.
  6. in vec3 Normal; // Vertex normal in camera coordinates.
  7. in vec3 CamDir; // Direction from vertex to camera
  8. in vec2 FragTexcoord;
  9. #include <lights>
  10. #include <material>
  11. #include <phong_model>
  12. // Final fragment color
  13. out vec4 FragColor;
  14. void main() {
  15. // Mix material color with textures colors
  16. vec4 texMixed = vec4(1);
  17. vec4 texColor;
  18. #if MAT_TEXTURES==1
  19. MIX_TEXTURE(0)
  20. #elif MAT_TEXTURES==2
  21. MIX_TEXTURE(0)
  22. MIX_TEXTURE(1)
  23. #elif MAT_TEXTURES==3
  24. MIX_TEXTURE(0)
  25. MIX_TEXTURE(1)
  26. MIX_TEXTURE(2)
  27. #endif
  28. // Combine material with texture colors
  29. vec4 matDiffuse = vec4(MatDiffuseColor, MatOpacity) * texMixed;
  30. vec4 matAmbient = vec4(MatAmbientColor, MatOpacity) * texMixed;
  31. // Inverts the fragment normal if not FrontFacing
  32. vec3 fragNormal = Normal;
  33. if (!gl_FrontFacing) {
  34. fragNormal = -fragNormal;
  35. }
  36. // Calculates the Ambient+Diffuse and Specular colors for this fragment using the Phong model.
  37. vec3 Ambdiff, Spec;
  38. phongModel(Position, fragNormal, CamDir, vec3(matAmbient), vec3(matDiffuse), Ambdiff, Spec);
  39. // Final fragment color
  40. FragColor = min(vec4(Ambdiff + Spec, matDiffuse.a), vec4(1.0));
  41. }