Line data Source code
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 <math.h>
22 : #include "util/numeric.h"
23 :
24 : #ifndef SERVER
25 :
26 : // Length of LIGHT_MAX+1 means LIGHT_MAX is the last value.
27 : // LIGHT_SUN is read as LIGHT_MAX from here.
28 :
29 : u8 light_LUT[LIGHT_MAX+1] =
30 : {
31 : /* Middle-raised variation of a_n+1 = a_n * 0.786
32 : * Length of LIGHT_MAX+1 means LIGHT_MAX is the last value.
33 : * LIGHT_SUN is read as LIGHT_MAX from here.
34 : */
35 : 8,
36 : 11+2,
37 : 14+7,
38 : 18+10,
39 : 22+15,
40 : 29+20,
41 : 37+20,
42 : 47+15,
43 : 60+10,
44 : 76+7,
45 : 97+5,
46 : 123+2,
47 : 157,
48 : 200,
49 : 255,
50 : };
51 :
52 : const u8 *light_decode_table = light_LUT;
53 :
54 : /** Initialize or update the light value tables using the specified \p gamma.
55 : * If \p gamma == 1.0 then the light table is linear. Typically values for
56 : * gamma range between 1.8 and 2.2.
57 : *
58 : * @note The value for gamma will be restricted to the range 1.1 <= gamma <= 3.0.
59 : *
60 : * @note This function is not, currently, a simple linear to gamma encoding
61 : * because adjustments are made so that a gamma of 1.8 gives the same
62 : * results as those hardcoded for use by the server.
63 : */
64 1 : void set_light_table(float gamma)
65 : {
66 : static const float brightness_step = 255.0f / (LIGHT_MAX + 1);
67 :
68 : /* These are adjustment values that are added to the calculated light value
69 : * after gamma is applied. Currently they are used so that given a gamma
70 : * of 1.8 the light values set by this function are the same as those
71 : * hardcoded in the initalizer list for the declaration of light_LUT.
72 : */
73 : static const int adjustments[LIGHT_MAX + 1] = {
74 : 7,
75 : 7,
76 : 7,
77 : 5,
78 : 2,
79 : 0,
80 : -7,
81 : -20,
82 : -31,
83 : -39,
84 : -43,
85 : -45,
86 : -40,
87 : -25,
88 : 0
89 : };
90 :
91 1 : gamma = rangelim(gamma, 1.0, 3.0);
92 :
93 1 : float brightness = brightness_step;
94 :
95 15 : for (size_t i = 0; i < LIGHT_MAX; i++) {
96 14 : light_LUT[i] = (u8)(255 * powf(brightness / 255.0f, gamma));
97 14 : light_LUT[i] = rangelim(light_LUT[i] + adjustments[i], 0, 255);
98 14 : if (i > 1 && light_LUT[i] < light_LUT[i-1])
99 0 : light_LUT[i] = light_LUT[i-1] + 1;
100 14 : brightness += brightness_step;
101 : }
102 1 : light_LUT[LIGHT_MAX] = 255;
103 1 : }
104 : #endif
105 :
106 :
107 :
108 : #if 0
109 : /*
110 : Made using this and:
111 : - adding 220 as the second last one
112 : - replacing the third last one (212) with 195
113 :
114 : #!/usr/bin/python
115 :
116 : from math import *
117 : from sys import stdout
118 :
119 : # We want 0 at light=0 and 255 at light=LIGHT_MAX
120 : LIGHT_MAX = 14
121 : #FACTOR = 0.69
122 : #FACTOR = 0.75
123 : FACTOR = 0.83
124 : START_FROM_ZERO = False
125 :
126 : L = []
127 : if START_FROM_ZERO:
128 : for i in range(1,LIGHT_MAX+1):
129 : L.append(int(round(255.0 * FACTOR ** (i-1))))
130 : L.append(0)
131 : else:
132 : for i in range(1,LIGHT_MAX+1):
133 : L.append(int(round(255.0 * FACTOR ** (i-1))))
134 : L.append(255)
135 :
136 : L.reverse()
137 : for i in L:
138 : stdout.write(str(i)+",\n")
139 : */
140 : u8 light_decode_table[LIGHT_MAX+1] =
141 : {
142 : 23,
143 : 27,
144 : 33,
145 : 40,
146 : 48,
147 : 57,
148 : 69,
149 : 83,
150 : 100,
151 : 121,
152 : 146,
153 : 176,
154 : 195,
155 : 220,
156 : 255,
157 : };
158 : #endif
159 :
160 : #if 0
161 : // This is good
162 : // a_n+1 = a_n * 0.786
163 : // Length of LIGHT_MAX+1 means LIGHT_MAX is the last value.
164 : // LIGHT_SUN is read as LIGHT_MAX from here.
165 : u8 light_decode_table[LIGHT_MAX+1] =
166 : {
167 : 8,
168 : 11,
169 : 14,
170 : 18,
171 : 22,
172 : 29,
173 : 37,
174 : 47,
175 : 60,
176 : 76,
177 : 97,
178 : 123,
179 : 157,
180 : 200,
181 : 255,
182 : };
183 : #endif
184 :
185 : #if 0
186 : // Use for debugging in dark
187 : u8 light_decode_table[LIGHT_MAX+1] =
188 : {
189 : 58,
190 : 64,
191 : 72,
192 : 80,
193 : 88,
194 : 98,
195 : 109,
196 : 121,
197 : 135,
198 : 150,
199 : 167,
200 : 185,
201 : 206,
202 : 229,
203 : 255,
204 : };
205 : #endif
206 :
207 : // This is reasonable with classic lighting with a light source
208 : /*u8 light_decode_table[LIGHT_MAX+1] =
209 : {
210 : 2,
211 : 3,
212 : 4,
213 : 6,
214 : 9,
215 : 13,
216 : 18,
217 : 25,
218 : 32,
219 : 35,
220 : 45,
221 : 57,
222 : 69,
223 : 79,
224 : 255
225 : };*/
226 :
227 :
228 : // As in minecraft, a_n+1 = a_n * 0.8
229 : // NOTE: This doesn't really work that well because this defines
230 : // LIGHT_MAX as dimmer than LIGHT_SUN
231 : // NOTE: Uh, this has had 34 left out; forget this.
232 : /*u8 light_decode_table[LIGHT_MAX+1] =
233 : {
234 : 8,
235 : 11,
236 : 14,
237 : 17,
238 : 21,
239 : 27,
240 : 42,
241 : 53,
242 : 66,
243 : 83,
244 : 104,
245 : 130,
246 : 163,
247 : 204,
248 : 255,
249 : };*/
250 :
251 : // This was a quick try of more light, manually quickly made
252 : /*u8 light_decode_table[LIGHT_MAX+1] =
253 : {
254 : 0,
255 : 7,
256 : 11,
257 : 15,
258 : 21,
259 : 29,
260 : 42,
261 : 53,
262 : 69,
263 : 85,
264 : 109,
265 : 135,
266 : 167,
267 : 205,
268 : 255,
269 : };*/
270 :
271 : // This was used for a long time, manually made
272 : /*u8 light_decode_table[LIGHT_MAX+1] =
273 : {
274 : 0,
275 : 6,
276 : 8,
277 : 11,
278 : 14,
279 : 19,
280 : 26,
281 : 34,
282 : 45,
283 : 61,
284 : 81,
285 : 108,
286 : 143,
287 : 191,
288 : 255,
289 : };*/
290 :
291 : /*u8 light_decode_table[LIGHT_MAX+1] =
292 : {
293 : 0,
294 : 3,
295 : 6,
296 : 10,
297 : 18,
298 : 25,
299 : 35,
300 : 50,
301 : 75,
302 : 95,
303 : 120,
304 : 150,
305 : 185,
306 : 215,
307 : 255,
308 : };*/
309 : /*u8 light_decode_table[LIGHT_MAX+1] =
310 : {
311 : 0,
312 : 5,
313 : 12,
314 : 22,
315 : 35,
316 : 50,
317 : 65,
318 : 85,
319 : 100,
320 : 120,
321 : 140,
322 : 160,
323 : 185,
324 : 215,
325 : 255,
326 : };*/
327 : // LIGHT_MAX is 14, 0-14 is 15 values
328 : /*u8 light_decode_table[LIGHT_MAX+1] =
329 : {
330 : 0,
331 : 9,
332 : 12,
333 : 14,
334 : 16,
335 : 20,
336 : 26,
337 : 34,
338 : 45,
339 : 61,
340 : 81,
341 : 108,
342 : 143,
343 : 191,
344 : 255,
345 : };*/
346 :
347 : #if 0
348 : /*
349 : #!/usr/bin/python
350 :
351 : from math import *
352 : from sys import stdout
353 :
354 : # We want 0 at light=0 and 255 at light=LIGHT_MAX
355 : LIGHT_MAX = 14
356 : #FACTOR = 0.69
357 : FACTOR = 0.75
358 :
359 : L = []
360 : for i in range(1,LIGHT_MAX+1):
361 : L.append(int(round(255.0 * FACTOR ** (i-1))))
362 : L.append(0)
363 :
364 : L.reverse()
365 : for i in L:
366 : stdout.write(str(i)+",\n")
367 : */
368 : u8 light_decode_table[LIGHT_MAX+1] =
369 : {
370 : 0,
371 : 6,
372 : 8,
373 : 11,
374 : 14,
375 : 19,
376 : 26,
377 : 34,
378 : 45,
379 : 61,
380 : 81,
381 : 108,
382 : 143,
383 : 191,
384 : 255,
385 : };
386 : #endif
387 :
388 :
|