daynightratio.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #ifndef DAYNIGHTRATIO_HEADER
  17. #define DAYNIGHTRATIO_HEADER
  18. inline u32 time_to_daynight_ratio(float time_of_day, bool smooth)
  19. {
  20. float t = time_of_day;
  21. if(t < 0)
  22. t += ((int)(-t)/24000)*24000;
  23. if(t >= 24000)
  24. t -= ((int)(t)/24000)*24000;
  25. if(t > 12000)
  26. t = 24000 - t;
  27. float values[][2] = {
  28. {4250+125, 150},
  29. {4500+125, 150},
  30. {4750+125, 250},
  31. {5000+125, 350},
  32. {5250+125, 500},
  33. {5500+125, 675},
  34. {5750+125, 875},
  35. {6000+125, 1000},
  36. {6250+125, 1000},
  37. };
  38. if(!smooth){
  39. float lastt = values[0][0];
  40. for(u32 i=1; i<sizeof(values)/sizeof(*values); i++){
  41. float t0 = values[i][0];
  42. float switch_t = (t0 + lastt) / 2;
  43. lastt = t0;
  44. if(switch_t <= t)
  45. continue;
  46. return values[i][1];
  47. }
  48. return 1000;
  49. } else {
  50. for(u32 i=0; i<sizeof(values)/sizeof(*values); i++){
  51. if(values[i][0] <= t)
  52. continue;
  53. if(i == 0)
  54. return values[i][1];
  55. float td0 = values[i][0] - values[i-1][0];
  56. float f = (t - values[i-1][0]) / td0;
  57. return f * values[i][1] + (1.0 - f) * values[i-1][1];
  58. }
  59. return 1000;
  60. }
  61. }
  62. #endif