light.cpp 3.0 KB

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