opengl_vertex.glsl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. uniform mat4 mWorldViewProj;
  2. uniform mat4 mWorld;
  3. // Color of the light emitted by the sun.
  4. uniform vec3 dayLight;
  5. uniform vec3 eyePosition;
  6. uniform float animationTimer;
  7. varying vec3 vPosition;
  8. varying vec3 worldPosition;
  9. varying vec3 eyeVec;
  10. varying vec3 lightVec;
  11. varying vec3 tsEyeVec;
  12. varying vec3 tsLightVec;
  13. varying float area_enable_parallax;
  14. // Color of the light emitted by the light sources.
  15. const vec3 artificialLight = vec3(1.04, 1.04, 1.04);
  16. const float e = 2.718281828459;
  17. const float BS = 10.0;
  18. float smoothCurve(float x)
  19. {
  20. return x * x * (3.0 - 2.0 * x);
  21. }
  22. float triangleWave(float x)
  23. {
  24. return abs(fract(x + 0.5) * 2.0 - 1.0);
  25. }
  26. float smoothTriangleWave(float x)
  27. {
  28. return smoothCurve(triangleWave(x)) * 2.0 - 1.0;
  29. }
  30. void main(void)
  31. {
  32. gl_TexCoord[0] = gl_MultiTexCoord0;
  33. //TODO: make offset depending on view angle and parallax uv displacement
  34. //thats for textures that doesnt align vertically, like dirt with grass
  35. //gl_TexCoord[0].y += 0.008;
  36. //Allow parallax/relief mapping only for certain kind of nodes
  37. //Variable is also used to control area of the effect
  38. #if (DRAW_TYPE == NDT_NORMAL || DRAW_TYPE == NDT_LIQUID || DRAW_TYPE == NDT_FLOWINGLIQUID)
  39. area_enable_parallax = 1.0;
  40. #else
  41. area_enable_parallax = 0.0;
  42. #endif
  43. float disp_x;
  44. float disp_z;
  45. #if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES) || (MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS)
  46. vec4 pos2 = mWorld * gl_Vertex;
  47. float tOffset = (pos2.x + pos2.y) * 0.001 + pos2.z * 0.002;
  48. disp_x = (smoothTriangleWave(animationTimer * 23.0 + tOffset) +
  49. smoothTriangleWave(animationTimer * 11.0 + tOffset)) * 0.4;
  50. disp_z = (smoothTriangleWave(animationTimer * 31.0 + tOffset) +
  51. smoothTriangleWave(animationTimer * 29.0 + tOffset) +
  52. smoothTriangleWave(animationTimer * 13.0 + tOffset)) * 0.5;
  53. #endif
  54. #if (MATERIAL_TYPE == TILE_MATERIAL_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_LIQUID_OPAQUE) && ENABLE_WAVING_WATER
  55. vec4 pos = gl_Vertex;
  56. pos.y -= 2.0;
  57. float posYbuf = (pos.z / WATER_WAVE_LENGTH + animationTimer * WATER_WAVE_SPEED * WATER_WAVE_LENGTH);
  58. pos.y -= sin(posYbuf) * WATER_WAVE_HEIGHT + sin(posYbuf / 7.0) * WATER_WAVE_HEIGHT;
  59. gl_Position = mWorldViewProj * pos;
  60. #elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES
  61. vec4 pos = gl_Vertex;
  62. pos.x += disp_x;
  63. pos.y += disp_z * 0.1;
  64. pos.z += disp_z;
  65. gl_Position = mWorldViewProj * pos;
  66. #elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS
  67. vec4 pos = gl_Vertex;
  68. if (gl_TexCoord[0].y < 0.05) {
  69. pos.x += disp_x;
  70. pos.z += disp_z;
  71. }
  72. gl_Position = mWorldViewProj * pos;
  73. #else
  74. gl_Position = mWorldViewProj * gl_Vertex;
  75. #endif
  76. vPosition = gl_Position.xyz;
  77. worldPosition = (mWorld * gl_Vertex).xyz;
  78. // Don't generate heightmaps when too far from the eye
  79. float dist = distance (vec3(0.0, 0.0, 0.0), vPosition);
  80. if (dist > 150.0) {
  81. area_enable_parallax = 0.0;
  82. }
  83. vec3 sunPosition = vec3 (0.0, eyePosition.y * BS + 900.0, 0.0);
  84. vec3 normal, tangent, binormal;
  85. normal = normalize(gl_NormalMatrix * gl_Normal);
  86. tangent = normalize(gl_NormalMatrix * gl_MultiTexCoord1.xyz);
  87. binormal = normalize(gl_NormalMatrix * gl_MultiTexCoord2.xyz);
  88. vec3 v;
  89. lightVec = sunPosition - worldPosition;
  90. v.x = dot(lightVec, tangent);
  91. v.y = dot(lightVec, binormal);
  92. v.z = dot(lightVec, normal);
  93. tsLightVec = normalize (v);
  94. eyeVec = -(gl_ModelViewMatrix * gl_Vertex).xyz;
  95. v.x = dot(eyeVec, tangent);
  96. v.y = dot(eyeVec, binormal);
  97. v.z = dot(eyeVec, normal);
  98. tsEyeVec = normalize (v);
  99. // Calculate color.
  100. // Red, green and blue components are pre-multiplied with
  101. // the brightness, so now we have to multiply these
  102. // colors with the color of the incoming light.
  103. // The pre-baked colors are halved to prevent overflow.
  104. vec4 color;
  105. // The alpha gives the ratio of sunlight in the incoming light.
  106. float nightRatio = 1 - gl_Color.a;
  107. color.rgb = gl_Color.rgb * (gl_Color.a * dayLight.rgb +
  108. nightRatio * artificialLight.rgb) * 2;
  109. color.a = 1;
  110. // Emphase blue a bit in darker places
  111. // See C++ implementation in mapblock_mesh.cpp finalColorBlend()
  112. float brightness = (color.r + color.g + color.b) / 3;
  113. color.b += max(0.0, 0.021 - abs(0.2 * brightness - 0.021) +
  114. 0.07 * brightness);
  115. gl_FrontColor = gl_BackColor = clamp(color, 0.0, 1.0);
  116. }