daynightratio.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Minetest
  3. Copyright (C) 2010-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. inline u32 time_to_daynight_ratio(float time_of_day, bool smooth)
  18. {
  19. float t = time_of_day;
  20. if (t < 0.0f)
  21. t += ((int)(-t) / 24000) * 24000.0f;
  22. if (t >= 24000.0f)
  23. t -= ((int)(t) / 24000) * 24000.0f;
  24. if (t > 12000.0f)
  25. t = 24000.0f - t;
  26. const float values[9][2] = {
  27. {4250.0f + 125.0f, 175.0f},
  28. {4500.0f + 125.0f, 175.0f},
  29. {4750.0f + 125.0f, 250.0f},
  30. {5000.0f + 125.0f, 350.0f},
  31. {5250.0f + 125.0f, 500.0f},
  32. {5500.0f + 125.0f, 675.0f},
  33. {5750.0f + 125.0f, 875.0f},
  34. {6000.0f + 125.0f, 1000.0f},
  35. {6250.0f + 125.0f, 1000.0f},
  36. };
  37. if (!smooth) {
  38. float lastt = values[0][0];
  39. for (u32 i = 1; i < 9; i++) {
  40. float t0 = values[i][0];
  41. float switch_t = (t0 + lastt) / 2.0f;
  42. lastt = t0;
  43. if (switch_t <= t)
  44. continue;
  45. return values[i][1];
  46. }
  47. return 1000;
  48. }
  49. if (t <= 4625.0f) // 4500 + 125
  50. return values[0][1];
  51. else if (t >= 6125.0f) // 6000 + 125
  52. return 1000;
  53. for (u32 i = 0; i < 9; i++) {
  54. if (values[i][0] <= t)
  55. continue;
  56. float td0 = values[i][0] - values[i - 1][0];
  57. float f = (t - values[i - 1][0]) / td0;
  58. return f * values[i][1] + (1.0f - f) * values[i - 1][1];
  59. }
  60. return 1000;
  61. }