Fix rgbe conversion routines thanks to Charles Bloom. http://cbloomrants.blogspot.com/2020/06/widespread-error-in-radiance-hdr-rgbe.html
This commit is contained in:
parent
3e034d2de1
commit
0f98a936f8
@ -25,24 +25,25 @@ Vector3 nv::r11g11b10_to_vector3(FloatR11G11B10 v) {
|
|||||||
FloatR11G11B10 nv::vector3_to_r11g11b10(const Vector3 & v) {
|
FloatR11G11B10 nv::vector3_to_r11g11b10(const Vector3 & v) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// These are based on:
|
// Implementation based on on:
|
||||||
// http://www.graphics.cornell.edu/~bjw/rgbe/rgbe.c
|
// https://cbloomrants.blogspot.com/2020/06/widespread-error-in-radiance-hdr-rgbe.html
|
||||||
// While this may not be the best way to encode/decode RGBE8, I'm not making any changes to maintain compatibility.
|
|
||||||
FloatRGBE8 nv::vector3_to_rgbe8(const Vector3 & v) {
|
FloatRGBE8 nv::vector3_to_rgbe8(const Vector3 & v) {
|
||||||
|
|
||||||
float m = max3(v.x, v.y, v.z);
|
float m = max3(v.x, v.y, v.z);
|
||||||
|
|
||||||
FloatRGBE8 rgbe;
|
FloatRGBE8 rgbe;
|
||||||
|
|
||||||
if (m < 1e-32) {
|
if (m < 1e-32f) {
|
||||||
|
// Exponent byte = 0 is a special encoding that makes RGB output = 0
|
||||||
rgbe.v = 0;
|
rgbe.v = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
int e;
|
int e;
|
||||||
float scale = frexpf(m, &e) * 256.0f / m;
|
frexpf(m, &e);
|
||||||
rgbe.r = U8(ftoi_round(v.x * scale));
|
float scale = ldexpf(1.0f, -exponent + 8);
|
||||||
rgbe.g = U8(ftoi_round(v.y * scale));
|
rgbe.r = U8(v.x * scale);
|
||||||
rgbe.b = U8(ftoi_round(v.z * scale));
|
rgbe.g = U8(v.y * scale);
|
||||||
|
rgbe.b = U8(v.z * scale);
|
||||||
rgbe.e = U8(e + 128);
|
rgbe.e = U8(e + 128);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,8 +53,8 @@ FloatRGBE8 nv::vector3_to_rgbe8(const Vector3 & v) {
|
|||||||
|
|
||||||
Vector3 nv::rgbe8_to_vector3(FloatRGBE8 v) {
|
Vector3 nv::rgbe8_to_vector3(FloatRGBE8 v) {
|
||||||
if (v.e != 0) {
|
if (v.e != 0) {
|
||||||
float scale = ldexpf(1.0f, v.e-(int)(128+8)); // +8 to divide by 256. @@ Shouldn't we divide by 255 instead?
|
float scale = ldexpf(1.0f, (int)v.e-128+8); // +8 to divide by 256.
|
||||||
return scale * Vector3(float(v.r), float(v.g), float(v.b));
|
return scale * Vector3(float(v.r + 0.5f), float(v.g + 0.5f), float(v.b + 0.5f));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Vector3(0);
|
return Vector3(0);
|
||||||
|
Loading…
Reference in New Issue
Block a user