Skip to content

Commit 2d9f0d3

Browse files
numberZeroSmallJoker
authored andcommittedDec 12, 2017
Update light decoding table size (#6696)
Fix old undiminish_light bug
1 parent b19241b commit 2d9f0d3

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed
 

‎src/light.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2424

2525
#ifndef SERVER
2626

27-
// Length of LIGHT_MAX + 1 means LIGHT_MAX is the last value.
28-
// LIGHT_SUN is read as LIGHT_MAX from here.
29-
u8 light_LUT[LIGHT_MAX + 1];
27+
static u8 light_LUT[LIGHT_SUN + 1];
3028

3129
// The const ref to light_LUT is what is actually used in the code
3230
const u8 *light_decode_table = light_LUT;
@@ -48,16 +46,16 @@ void set_light_table(float gamma)
4846
// Gamma correction
4947
gamma = rangelim(gamma, 0.5f, 3.0f);
5048

51-
for (size_t i = 0; i < LIGHT_MAX; i++) {
49+
for (size_t i = 0; i < LIGHT_SUN; i++) {
5250
float x = i;
53-
x /= LIGHT_MAX;
51+
x /= LIGHT_SUN;
5452
float brightness = a * x * x * x + b * x * x + c * x;
5553
float boost = d * std::exp(-((x - e) * (x - e)) / (2.0f * f * f));
5654
brightness = powf(brightness + boost, 1.0f / gamma);
5755
light_LUT[i] = rangelim((u32)(255.0f * brightness), 0, 255);
5856
if (i > 1 && light_LUT[i] <= light_LUT[i - 1])
5957
light_LUT[i] = light_LUT[i - 1] + 1;
6058
}
61-
light_LUT[LIGHT_MAX] = 255;
59+
light_LUT[LIGHT_SUN] = 255;
6260
}
6361
#endif

‎src/light.h

+8-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1818
*/
1919

2020
#pragma once
21-
21+
#include <cassert>
2222
#include "irrlichttypes.h"
2323

2424
/*
@@ -54,11 +54,12 @@ inline u8 diminish_light(u8 light, u8 distance)
5454

5555
inline u8 undiminish_light(u8 light)
5656
{
57+
assert(light <= LIGHT_SUN);
5758
// We don't know if light should undiminish from this particular 0.
5859
// Thus, keep it at 0.
5960
if (light == 0)
6061
return 0;
61-
if (light == LIGHT_MAX)
62+
if (light >= LIGHT_MAX)
6263
return light;
6364

6465
return light + 1;
@@ -84,9 +85,9 @@ extern const u8 *light_decode_table;
8485
// 0 <= return value <= 255
8586
inline u8 decode_light(u8 light)
8687
{
87-
if (light > LIGHT_MAX)
88-
light = LIGHT_MAX;
89-
88+
// assert(light <= LIGHT_SUN);
89+
if (light > LIGHT_SUN)
90+
light = LIGHT_SUN;
9091
return light_decode_table[light];
9192
}
9293

@@ -98,8 +99,8 @@ inline float decode_light_f(float light_f)
9899

99100
if (i <= 0)
100101
return (float)light_decode_table[0] / 255.0;
101-
if (i >= LIGHT_MAX)
102-
return (float)light_decode_table[LIGHT_MAX] / 255.0;
102+
if (i >= LIGHT_SUN)
103+
return (float)light_decode_table[LIGHT_SUN] / 255.0;
103104

104105
float v1 = (float)light_decode_table[i - 1] / 255.0;
105106
float v2 = (float)light_decode_table[i] / 255.0;

0 commit comments

Comments
 (0)
Please sign in to comment.