Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

LandFragmentShader.glsl 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #version 410 core
  19. in FragmentData
  20. {
  21. vec3 position;
  22. vec2 normalTexCoord;
  23. vec2 terrainTexCoord;
  24. vec4 terrainInfo1;
  25. vec4 terrainInfo2;
  26. vec4 terrainInfo3;
  27. vec4 terrainInfo4;
  28. vec4 terrainInfo5;
  29. };
  30. out vec4 fragColor;
  31. #include "graphics/shaders/LandCommon.glsl"
  32. vec3 phong()
  33. {
  34. vec3 n = texture(normalTex, normalTexCoord).xyz - vec3(0.5);
  35. n = normalize(normalMatrix * n);
  36. vec3 s = normalize(lightPosition - position);
  37. vec3 v = normalize(-position);
  38. vec3 h = normalize(v + s);
  39. float cosine = max(dot(s, n), 0.0);
  40. float gapped_cosine = cosine * 0.5 + floor(cosine * 3.0) * 0.25;
  41. vec3 ambient = Ka;
  42. vec3 diffuse = Kd * gapped_cosine;
  43. vec3 specular = Ks * pow(max(dot(h, n), 0.0), shininess);
  44. return lightIntensity * (ambient + diffuse + specular);
  45. }
  46. vec3 linearize(vec3 color)
  47. {
  48. return pow(color, vec3(2.2));
  49. }
  50. // Filmic tonemapping operators
  51. // Also applies gamma correction
  52. // http://filmicgames.com/archives/75
  53. vec3 hejl(vec3 color)
  54. {
  55. vec3 x = max(vec3(0.0), color - vec3(0.004));
  56. return (x * (6.2 * x + 0.5)) / (x * (6.2 * x + 1.7) + 0.06);
  57. }
  58. float alpha(vec3 texCoord)
  59. {
  60. vec3 realTexCoord = vec3(texCoord.s, texCoord.t, fract(texCoord.p / 128.0) * 128.0);
  61. float a = texture(blendTex, realTexCoord).r;
  62. float z = floor(texCoord.p / 128.0);
  63. return a + z - 2.0 * a * z;
  64. }
  65. void main()
  66. {
  67. vec3 tc1 = linearize(texture(terrainTex, vec3(terrainTexCoord.st, terrainInfo1.q)).rgb);
  68. vec3 tc2 = linearize(texture(terrainTex, vec3(terrainTexCoord.st, terrainInfo2.q)).rgb);
  69. vec3 tc3 = linearize(texture(terrainTex, vec3(terrainTexCoord.st, terrainInfo3.q)).rgb);
  70. vec3 tc4 = linearize(texture(terrainTex, vec3(terrainTexCoord.st, terrainInfo4.q)).rgb);
  71. vec3 tc5 = linearize(texture(terrainTex, vec3(terrainTexCoord.st, terrainInfo5.q)).rgb);
  72. float ba2 = alpha(terrainInfo2.stp);
  73. float ba3 = alpha(terrainInfo3.stp);
  74. float ba4 = alpha(terrainInfo4.stp);
  75. float ba5 = alpha(terrainInfo5.stp);
  76. vec3 tc = mix(tc2, tc1, ba2);
  77. tc = mix(tc3, tc, ba3);
  78. tc = mix(tc4, tc, ba4);
  79. tc = mix(tc5, tc, ba5);
  80. fragColor = vec4(hejl(tc) * phong(), 1.0);
  81. }