Skip to content

Commit d4c3c41

Browse files
committed
Fix green texmap artifacts on earth render
Turns out the problem has to do with the conversion to gamma-corrected intensities from the [0,1] values read in from the STB image library. I thought the range would be [0,1), but instead it's [0,1]. Thus, intensity values of 1.0 were mapped to 256, which then converted to 0 for unsigned 8-bit chars. Thankfully, I typo'ed the conversion in the latest book 2 progression, and multiplied intensities by _257_, which made the problem worse, and enough to to hit me. This fix makes the conversion bullet-proof, and zero or negative intensities are now mapped to 0, and values greater than or equal to 1.0 are now mapped to 255. Resolves #1485
1 parent ed69218 commit d4c3c41

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

books/RayTracingTheNextWeek.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,14 @@
17971797
return high - 1;
17981798
}
17991799

1800+
static unsigned char float_to_byte(float value) {
1801+
if (value <= 0.0)
1802+
return 0;
1803+
if (1.0 <= value)
1804+
return 255;
1805+
return static_cast< unsigned char >(256.0 * value);
1806+
}
1807+
18001808
void convert_to_bytes() {
18011809
// Convert the linear floating point pixel data to bytes, storing the resulting byte
18021810
// data in the `bdata` member.
@@ -1810,7 +1818,7 @@
18101818
auto *bptr = bdata;
18111819
auto *fptr = fdata;
18121820
for (auto i=0; i < total_bytes; i++, fptr++, bptr++)
1813-
*bptr = static_cast< unsigned char >(*fptr * 256.0);
1821+
*bptr = float_to_byte(*fptr);
18141822
}
18151823
};
18161824

src/TheNextWeek/rtw_stb_image.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ class rtw_image {
102102
return high - 1;
103103
}
104104

105+
static unsigned char float_to_byte(float value) {
106+
if (value <= 0.0)
107+
return 0;
108+
if (1.0 <= value)
109+
return 255;
110+
return static_cast< unsigned char >(256.0 * value);
111+
}
112+
105113
void convert_to_bytes() {
106114
// Convert the linear floating point pixel data to bytes, storing the resulting byte
107115
// data in the `bdata` member.
@@ -115,7 +123,7 @@ class rtw_image {
115123
auto *bptr = bdata;
116124
auto *fptr = fdata;
117125
for (auto i=0; i < total_bytes; i++, fptr++, bptr++)
118-
*bptr = static_cast<unsigned char>(*fptr * 256.0);
126+
*bptr = float_to_byte(*fptr);
119127
}
120128
};
121129

0 commit comments

Comments
 (0)