|
Post by pixelboystm on Oct 17, 2021 16:37:11 GMT
Hello I was working through the book and everything worked just fine until at the end of chapter eight these weird spirals in transformed spheres occour. I've read in other threads that is due to floating point precission but I cannot find where I hav made a mistake the t value for the hit at pixel(0,0) is something very small 0,005 so I think its hitting itself but my floats are much more precise. Does soemone has an idea where the problem might be?
|
|
|
Post by Jamis on Oct 17, 2021 18:09:11 GMT
Wow, that's trippy!
Try simplifying the scene, making the simplest possible scene the duplicates the issue. Can you do it with just one sphere? Once you have a simple scene that exhibits the problem, you can pick one pixel in your image and then step through the code when it starts rendering that pixel. It's tedious work, I know---I had to do this multiple times in the development of the book. :/
- Jamis
|
|
|
Post by icolomby on Oct 28, 2021 4:05:29 GMT
Hi, The reason for what you are seeing is a floating point precision error. Your program uses floats which is causing a rounding error. I simplified your scene to only render the middle sphere, and shrunk the final image down to 384x216. At this size the pixel at (133, 79) was one not being rendered properly. The precision error shows up in the sphere intersection method for that point when calculating the discriminant. When using floats the following values are calculated: a = 9697.292 b = -108322.375 c = 302500.3 d = 1024 When I run my code which uses doubles instead of floats I get the following values in my sphere intersection: a = 9697.290886505943 b = -108322.36388266529 c = 302500.2635228044 d = 2322.6271991729736 The reason for the discrepancy is because a, b & c are large values and when calculating the discriminant the float values loose precision during the calculation. Float b * b = 1.173373 6E+10 4 * a * c = 1.173373 5E+10 Only the final digit before the exponent is different. Double b * b = 1173373 4517.128554 * a * c = 1173373 2194.50135Much better precision So when you perform the final calculation of b * b - 4 * a * c you get a different result than you would using double. This precision error causes a ripple effect, it makes t0 and t1 off as well, which changes the t in the hit intersection, which changes the point and over point in the computations object which ends up causing IsShadowed() to return true instead of false for the pixel in question. If you change your code to use doubles instead of floats your problems will go away and the image will render correctly. Hope this helps, Ian.
|
|