light.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include "light.h"
  17. #include <algorithm>
  18. #include <cmath>
  19. #include "util/numeric.h"
  20. #include "settings.h"
  21. #ifndef SERVER
  22. static u8 light_LUT[LIGHT_SUN + 1];
  23. // The const ref to light_LUT is what is actually used in the code
  24. const u8 *light_decode_table = light_LUT;
  25. struct LightingParams {
  26. float a, b, c; // Lighting curve polynomial coefficients
  27. float boost, center, sigma; // Lighting curve parametric boost
  28. float gamma; // Lighting curve gamma correction
  29. };
  30. static LightingParams params;
  31. float decode_light_f(float x)
  32. {
  33. if (x >= 1.0f) // x is often 1.0f
  34. return 1.0f;
  35. x = std::fmax(x, 0.0f);
  36. float brightness = ((params.a * x + params.b) * x + params.c) * x;
  37. brightness += params.boost *
  38. std::exp(-0.5f * sqr((x - params.center) / params.sigma));
  39. if (brightness <= 0.0f) // May happen if parameters are extreme
  40. return 0.0f;
  41. if (brightness >= 1.0f)
  42. return 1.0f;
  43. return powf(brightness, 1.0f / params.gamma);
  44. }
  45. // Initialize or update the light value tables using the specified gamma
  46. void set_light_table(float gamma)
  47. {
  48. // Lighting curve bounding gradients
  49. const float alpha = rangelim(g_settings->getFloat("lighting_alpha"), 0.0f, 3.0f);
  50. const float beta = rangelim(g_settings->getFloat("lighting_beta"), 0.0f, 3.0f);
  51. // Lighting curve polynomial coefficients
  52. params.a = alpha + beta - 2.0f;
  53. params.b = 3.0f - 2.0f * alpha - beta;
  54. params.c = alpha;
  55. // Lighting curve parametric boost
  56. params.boost = rangelim(g_settings->getFloat("lighting_boost"), 0.0f, 0.4f);
  57. params.center = rangelim(g_settings->getFloat("lighting_boost_center"), 0.0f, 1.0f);
  58. params.sigma = rangelim(g_settings->getFloat("lighting_boost_spread"), 0.0f, 0.4f);
  59. // Lighting curve gamma correction
  60. params.gamma = rangelim(gamma, 0.33f, 3.0f);
  61. // Boundary values should be fixed
  62. light_LUT[0] = 0;
  63. light_LUT[LIGHT_SUN] = 255;
  64. for (size_t i = 1; i < LIGHT_SUN; i++) {
  65. float brightness = decode_light_f((float)i / LIGHT_SUN);
  66. // Strictly speaking, rangelim is not necessary here—if the implementation
  67. // is conforming. But we don’t want problems in any case.
  68. light_LUT[i] = rangelim((s32)(255.0f * brightness), 0, 255);
  69. // Ensure light brightens with each level
  70. if (i > 0 && light_LUT[i] <= light_LUT[i - 1]) {
  71. light_LUT[i] = std::min((u8)254, light_LUT[i - 1]) + 1;
  72. }
  73. }
  74. }
  75. #endif