I'm following this book, Computer Graphics from scratch: A programmer's introduction to 3D rendering, to build a simple raytracer and rendering the scene to a bitmap image. I've run into a problem when doing the recursive reflections. When the program performs the recursion, the image ends up having corrupted pixel/color data (not sure how else to explain it). I've run into a similar problem earlier when I added specular reflections because I forgot to saturate the color values. However, now I'm not sure where the problem is.

The first image is what it currently renders. The second is what it should look like (aside from the image size).

enter image description here

enter image description here

This is the output without any recursive reflections for reference: enter image description here

Here is my code (c++):

Defines.h (where I define the bitmap header data as well as other things)

I'm not familiar with bitmap formatting. I used this as a reference.

main.cpp (where all the functions related to the raytracing algorithm are defined and used and where I write the data to the .bmp file)

You can assume the math functions work. If needed I can provide the math library in an edit.

I noticed if I removed lighting or just scaled the color by a constant value but still do the recursion I get this result:

recursion depth 2 no lighting

recursion depth 3 no lighting

I don't know if the problem lies with my implementation of the algorithm or how I'm handling writing the data to a bitmap file. I've looked over the algorithm in the book and compared it to my implementation and I don't see anything different, but maybe another set of eyes can help.


  • $\begingroup$ Hi! I didn't have time to read your code, but the problem in the first image can be caused by incorrect image buffer handling (e.g. incorrect line padding/align) or by incorrect file output implementation. I have no explanation for the latter problems :-/ $\endgroup$
    – ivokabel
    Apr 4, 2022 at 9:16
  • $\begingroup$ Anyway, it's always a good idea to simplify your debugging scenario as much as possible and move to more complex after fixing it. $\endgroup$
    – ivokabel
    Apr 4, 2022 at 9:18
  • $\begingroup$ Hi @ivokabel, thanks for the response. The image is 1920x1080 and after some testing there doesn't seem to be a padding issue. I've double-checked that the header data is correct and being written correctly. As for simplifying the debugging process, I've tried just rendering one sphere at a time. When rendering the smaller spheres (red, green, blue) with the recursion there doesn't seem to be anything visually wrong (no distortion/corruption). However, when rendering the larger yellow sphere with recursion I start to get some artifacts on the sphere. You can see this in the last two images. $\endgroup$
    – JayDee17
    Apr 5, 2022 at 0:11
  • $\begingroup$ @ivokabel: a padding/alignment problem would be seen on every row, which is clearly not the case. $\endgroup$
    – user1703
    May 7 at 8:03
  • $\begingroup$ @YvesDaoust, that sounds like a reasonable assessment. $\endgroup$
    – ivokabel
    Jul 25 at 10:44

1 Answer 1


I was able to solve my problem, I realized I wasn't using the std::ios::binary flag when writing to the file, but adding it seemed to solve my problem.

  • $\begingroup$ So it was a file storage problem. Very interesting. Thanks for sharing the experience. Might be helpful for others. $\endgroup$
    – ivokabel
    Jul 25 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.