2
0

opengl_vertex.glsl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. uniform mat4 mWorldViewProj;
  2. uniform mat4 mInvWorld;
  3. uniform mat4 mTransWorld;
  4. uniform mat4 mWorld;
  5. uniform float dayNightRatio;
  6. uniform vec3 eyePosition;
  7. uniform float animationTimer;
  8. varying vec3 vPosition;
  9. varying vec3 worldPosition;
  10. varying vec3 eyeVec;
  11. varying vec3 lightVec;
  12. varying vec3 tsEyeVec;
  13. varying vec3 tsLightVec;
  14. varying float area_enable_parallax;
  15. varying float disp;
  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. #if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES) || (MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS)
  44. vec4 pos2 = mWorld * gl_Vertex;
  45. float tOffset = (pos2.x + pos2.y) * 0.001 + pos2.z * 0.002;
  46. disp = (smoothTriangleWave(animationTimer * 31.0 + tOffset) +
  47. smoothTriangleWave(animationTimer * 29.0 + tOffset) +
  48. smoothTriangleWave(animationTimer * 13.0 + tOffset)) - 0.9;
  49. #endif
  50. #if (MATERIAL_TYPE == TILE_MATERIAL_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_LIQUID_OPAQUE) && ENABLE_WAVING_WATER
  51. vec4 pos = gl_Vertex;
  52. pos.y -= 2.0;
  53. float posYbuf = (pos.z / WATER_WAVE_LENGTH + animationTimer * WATER_WAVE_SPEED * WATER_WAVE_LENGTH);
  54. pos.y -= sin(posYbuf) * WATER_WAVE_HEIGHT + sin(posYbuf / 7.0) * WATER_WAVE_HEIGHT;
  55. gl_Position = mWorldViewProj * pos;
  56. #elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES
  57. vec4 pos = gl_Vertex;
  58. pos.x += disp * 0.1;
  59. pos.y += disp * 0.1;
  60. pos.z += disp;
  61. gl_Position = mWorldViewProj * pos;
  62. #elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS
  63. vec4 pos = gl_Vertex;
  64. if (gl_TexCoord[0].y < 0.05) {
  65. pos.z += disp;
  66. }
  67. gl_Position = mWorldViewProj * pos;
  68. #else
  69. gl_Position = mWorldViewProj * gl_Vertex;
  70. #endif
  71. vPosition = gl_Position.xyz;
  72. worldPosition = (mWorld * gl_Vertex).xyz;
  73. // Don't generate heightmaps when too far from the eye
  74. float dist = distance (vec3(0.0, 0.0 ,0.0), vPosition);
  75. if (dist > 150.0) {
  76. area_enable_parallax = 0.0;
  77. }
  78. vec3 sunPosition = vec3 (0.0, eyePosition.y * BS + 900.0, 0.0);
  79. vec3 normal, tangent, binormal;
  80. normal = normalize(gl_NormalMatrix * gl_Normal);
  81. tangent = normalize(gl_NormalMatrix * gl_MultiTexCoord1.xyz);
  82. binormal = normalize(gl_NormalMatrix * gl_MultiTexCoord2.xyz);
  83. vec3 v;
  84. lightVec = sunPosition - worldPosition;
  85. v.x = dot(lightVec, tangent);
  86. v.y = dot(lightVec, binormal);
  87. v.z = dot(lightVec, normal);
  88. tsLightVec = normalize (v);
  89. eyeVec = -(gl_ModelViewMatrix * gl_Vertex).xyz;
  90. v.x = dot(eyeVec, tangent);
  91. v.y = dot(eyeVec, binormal);
  92. v.z = dot(eyeVec, normal);
  93. tsEyeVec = normalize (v);
  94. vec4 color;
  95. float day = gl_Color.r;
  96. float night = gl_Color.g;
  97. float light_source = gl_Color.b;
  98. float rg = mix(night, day, dayNightRatio);
  99. rg += light_source * 2.5; // Make light sources brighter
  100. float b = rg;
  101. // Moonlight is blue
  102. b += (day - night) / 13.0;
  103. rg -= (day - night) / 23.0;
  104. // Emphase blue a bit in darker places
  105. // See C++ implementation in mapblock_mesh.cpp finalColorBlend()
  106. b += max(0.0, (1.0 - abs(b - 0.13)/0.17) * 0.025);
  107. // Artificial light is yellow-ish
  108. // See C++ implementation in mapblock_mesh.cpp finalColorBlend()
  109. rg += max(0.0, (1.0 - abs(rg - 0.85)/0.15) * 0.065);
  110. color.r = rg;
  111. color.g = rg;
  112. color.b = b;
  113. color.a = gl_Color.a;
  114. gl_FrontColor = gl_BackColor = clamp(color,0.0,1.0);
  115. }