]> git.lizzy.rs Git - dragonfireclient.git/blob - src/light.cpp
Modernize various files (src/k*, src/l*)
[dragonfireclient.git] / src / light.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "light.h"
21 #include <cmath>
22 #include "util/numeric.h"
23 #include "settings.h"
24
25 #ifndef SERVER
26
27 // Length of LIGHT_MAX+1 means LIGHT_MAX is the last value.
28 // LIGHT_SUN is read as LIGHT_MAX from here.
29
30 u8 light_LUT[LIGHT_MAX+1];
31
32 // the const ref to light_LUT is what is actually used in the code.
33 const u8 *light_decode_table = light_LUT;
34
35 /** Initialize or update the light value tables using the specified \p gamma.
36  */
37 void set_light_table(float gamma)
38 {
39 // lighting curve derivatives
40         const float alpha = g_settings->getFloat("lighting_alpha");
41         const float beta  = g_settings->getFloat("lighting_beta");
42 // lighting curve coefficients
43         const float a = alpha + beta - 2;
44         const float b = 3 - 2 * alpha - beta;
45         const float c = alpha;
46 // gamma correction
47         gamma = rangelim(gamma, 0.5, 3.0);
48
49         for (size_t i = 0; i < LIGHT_MAX; i++) {
50                 float x = i;
51                 x /= LIGHT_MAX;
52                 float brightness = a * x * x * x + b * x * x + c * x;
53                 brightness = powf(brightness, 1.0 / gamma);
54                 light_LUT[i] = rangelim((u32)(255 * brightness), 0, 255);
55                 if (i > 1 && light_LUT[i] <= light_LUT[i - 1])
56                         light_LUT[i] = light_LUT[i - 1] + 1;
57         }
58         light_LUT[LIGHT_MAX] = 255;
59 }
60 #endif
61