light.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irrlichttypes.h"
  18. /*
  19. Lower level lighting stuff
  20. */
  21. // This directly sets the range of light.
  22. // Actually this is not the real maximum, and this is not the brightest, the
  23. // brightest is LIGHT_SUN.
  24. // If changed, this constant as defined in builtin/game/constants.lua must
  25. // also be changed.
  26. #define LIGHT_MAX 14
  27. // Light is stored as 4 bits, thus 15 is the maximum.
  28. // This brightness is reserved for sunlight
  29. #define LIGHT_SUN 15
  30. inline u8 diminish_light(u8 light)
  31. {
  32. if (light == 0)
  33. return 0;
  34. if (light >= LIGHT_MAX)
  35. return LIGHT_MAX - 1;
  36. return light - 1;
  37. }
  38. inline u8 diminish_light(u8 light, u8 distance)
  39. {
  40. if (distance >= light)
  41. return 0;
  42. return light - distance;
  43. }
  44. inline u8 undiminish_light(u8 light)
  45. {
  46. // We don't know if light should undiminish from this particular 0.
  47. // Thus, keep it at 0.
  48. if (light == 0)
  49. return 0;
  50. if (light == LIGHT_MAX)
  51. return light;
  52. return light + 1;
  53. }
  54. #ifndef SERVER
  55. /**
  56. * \internal
  57. *
  58. * \warning DO NOT USE this directly; it is here simply so that decode_light()
  59. * can be inlined.
  60. *
  61. * Array size is #LIGHTMAX+1
  62. *
  63. * The array is a lookup table to convert the internal representation of light
  64. * (brightness) to the display brightness.
  65. *
  66. */
  67. extern const u8 *light_decode_table;
  68. // 0 <= light <= LIGHT_SUN
  69. // 0 <= return value <= 255
  70. inline u8 decode_light(u8 light)
  71. {
  72. if (light > LIGHT_MAX)
  73. light = LIGHT_MAX;
  74. return light_decode_table[light];
  75. }
  76. // 0.0 <= light <= 1.0
  77. // 0.0 <= return value <= 1.0
  78. inline float decode_light_f(float light_f)
  79. {
  80. s32 i = (u32)(light_f * LIGHT_MAX + 0.5);
  81. if (i <= 0)
  82. return (float)light_decode_table[0] / 255.0;
  83. if (i >= LIGHT_MAX)
  84. return (float)light_decode_table[LIGHT_MAX] / 255.0;
  85. float v1 = (float)light_decode_table[i - 1] / 255.0;
  86. float v2 = (float)light_decode_table[i] / 255.0;
  87. float f0 = (float)i - 0.5;
  88. float f = light_f * LIGHT_MAX - f0;
  89. return f * v2 + (1.0 - f) * v1;
  90. }
  91. void set_light_table(float gamma);
  92. #endif // ifndef SERVER
  93. // 0 <= daylight_factor <= 1000
  94. // 0 <= lightday, lightnight <= LIGHT_SUN
  95. // 0 <= return value <= LIGHT_SUN
  96. inline u8 blend_light(u32 daylight_factor, u8 lightday, u8 lightnight)
  97. {
  98. u32 c = 1000;
  99. u32 l = ((daylight_factor * lightday + (c - daylight_factor) * lightnight)) / c;
  100. if (l > LIGHT_SUN)
  101. l = LIGHT_SUN;
  102. return l;
  103. }