GBufferGlobal.frag 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. // Request GLSL 3.3
  9. #version 330
  10. // Inputs from vertex shader
  11. // Tex coord
  12. in vec2 fragTexCoord;
  13. // This corresponds to the output color to the color buffer
  14. layout(location = 0) out vec4 outColor;
  15. // Different textures from G-buffer
  16. uniform sampler2D uGDiffuse;
  17. uniform sampler2D uGNormal;
  18. uniform sampler2D uGWorldPos;
  19. // Create a struct for directional light
  20. struct DirectionalLight
  21. {
  22. // Direction of light
  23. vec3 mDirection;
  24. // Diffuse color
  25. vec3 mDiffuseColor;
  26. // Specular color
  27. vec3 mSpecColor;
  28. };
  29. // Uniforms for lighting
  30. // Camera position (in world space)
  31. uniform vec3 uCameraPos;
  32. // Ambient light level
  33. uniform vec3 uAmbientLight;
  34. // Directional Light
  35. uniform DirectionalLight uDirLight;
  36. void main()
  37. {
  38. vec3 gbufferDiffuse = texture(uGDiffuse, fragTexCoord).xyz;
  39. vec3 gbufferNorm = texture(uGNormal, fragTexCoord).xyz;
  40. vec3 gbufferWorldPos = texture(uGWorldPos, fragTexCoord).xyz;
  41. // Surface normal
  42. vec3 N = normalize(gbufferNorm);
  43. // Vector from surface to light
  44. vec3 L = normalize(-uDirLight.mDirection);
  45. // Vector from surface to camera
  46. vec3 V = normalize(uCameraPos - gbufferWorldPos);
  47. // Reflection of -L about N
  48. vec3 R = normalize(reflect(-L, N));
  49. // Compute phong reflection
  50. vec3 Phong = uAmbientLight;
  51. float NdotL = dot(N, L);
  52. if (NdotL > 0)
  53. {
  54. vec3 Diffuse = uDirLight.mDiffuseColor * dot(N, L);
  55. Phong += Diffuse;
  56. }
  57. // Clamp light between 0-1 RGB values
  58. Phong = clamp(Phong, 0.0, 1.0);
  59. // Final color is texture color times phong light (alpha = 1)
  60. outColor = vec4(gbufferDiffuse * Phong, 1.0);
  61. }