phong_fragment.glsl 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. // Combine all texture colors
  16. vec4 texCombined = vec4(1);
  17. #if MAT_TEXTURES>0
  18. for (int i = 0; i < MAT_TEXTURES; i++) {
  19. if (MatTexVisible(i) == false) {
  20. continue;
  21. }
  22. vec4 texcolor = texture(MatTexture[i], FragTexcoord * MatTexRepeat(i) + MatTexOffset(i));
  23. if (i == 0) {
  24. texCombined = texcolor;
  25. } else {
  26. texCombined = mix(texCombined, texcolor, texcolor.a);
  27. }
  28. }
  29. #endif
  30. // Combine material with texture colors
  31. vec4 matDiffuse = vec4(MatDiffuseColor, MatOpacity) * texCombined;
  32. vec4 matAmbient = vec4(MatAmbientColor, MatOpacity) * texCombined;
  33. // Inverts the fragment normal if not FrontFacing
  34. vec3 fragNormal = Normal;
  35. if (!gl_FrontFacing) {
  36. fragNormal = -fragNormal;
  37. }
  38. // Calculates the Ambient+Diffuse and Specular colors for this fragment using the Phong model.
  39. vec3 Ambdiff, Spec;
  40. phongModel(Position, fragNormal, CamDir, vec3(matAmbient), vec3(matDiffuse), Ambdiff, Spec);
  41. // Final fragment color
  42. FragColor = min(vec4(Ambdiff + Spec, matDiffuse.a), vec4(1.0));
  43. }