light.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <cassert>
  18. #include "irrlichttypes.h"
  19. /*
  20. Lower level lighting stuff
  21. */
  22. // This directly sets the range of light.
  23. // Actually this is not the real maximum, and this is not the brightest, the
  24. // brightest is LIGHT_SUN.
  25. // If changed, this constant as defined in builtin/game/constants.lua must
  26. // also be changed.
  27. #define LIGHT_MAX 14
  28. // Light is stored as 4 bits, thus 15 is the maximum.
  29. // This brightness is reserved for sunlight
  30. #define LIGHT_SUN 15
  31. #ifndef SERVER
  32. /**
  33. * \internal
  34. *
  35. * \warning DO NOT USE this directly; it is here simply so that decode_light()
  36. * can be inlined.
  37. *
  38. * Array size is #LIGHTMAX+1
  39. *
  40. * The array is a lookup table to convert the internal representation of light
  41. * (brightness) to the display brightness.
  42. *
  43. */
  44. extern const u8 *light_decode_table;
  45. // 0 <= light <= LIGHT_SUN
  46. // 0 <= return value <= 255
  47. inline u8 decode_light(u8 light)
  48. {
  49. // assert(light <= LIGHT_SUN);
  50. if (light > LIGHT_SUN)
  51. light = LIGHT_SUN;
  52. return light_decode_table[light];
  53. }
  54. // 0.0 <= light <= 1.0
  55. // 0.0 <= return value <= 1.0
  56. float decode_light_f(float light_f);
  57. void set_light_table(float gamma);
  58. #endif // ifndef SERVER
  59. // 0 <= daylight_factor <= 1000
  60. // 0 <= lightday, lightnight <= LIGHT_SUN
  61. // 0 <= return value <= LIGHT_SUN
  62. inline u8 blend_light(u32 daylight_factor, u8 lightday, u8 lightnight)
  63. {
  64. u32 c = 1000;
  65. u32 l = ((daylight_factor * lightday + (c - daylight_factor) * lightnight)) / c;
  66. if (l > LIGHT_SUN)
  67. l = LIGHT_SUN;
  68. return l;
  69. }