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

CC BY-SA 4.0
2023-07-25T10:42:57.527Z
So it was a file storage problem. Very interesting. Thanks for sharing the experience. Might be helpful for others.
ivokabel
CC BY-SA 4.0
2022-04-11T18:09:20.797Z
2022-04-11T18:09:20.797Z
JayDee17
<p>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.</p>
<p>The first image is what it currently renders. The second is what it should look like (aside from the image size).</p>
<p><a href="https://i.sstatic.net/iivcA.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/iivcA.png" alt="enter image description here" /></a></p>
<p><a href="https://i.sstatic.net/MnMqk.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/MnMqk.png" alt="enter image description here" /></a></p>
<p>This is the output without any recursive reflections for reference:
<a href="https://i.sstatic.net/zB0HP.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/zB0HP.png" alt="enter image description here" /></a></p>
<p>Here is my code (c++):</p>
<p><a href="https://pastebin.com/iskhCZzZ" rel="nofollow noreferrer">Defines.h</a>
(where I define the bitmap header data as well as other things)</p>
<p>I'm not familiar with bitmap formatting. I used this as a <a href="https://dev.to/muiz6/c-how-to-write-a-bitmap-image-from-scratch-1k6m" rel="nofollow noreferrer">reference</a>.</p>
<p><a href="https://pastebin.com/th40YG3e" rel="nofollow noreferrer">main.cpp</a>
(where all the functions related to the raytracing algorithm are defined and used and where I write the data to the .bmp file)</p>
<p>You can assume the math functions work. If needed I can provide the math library in an edit.</p>
<p>I noticed if I removed lighting or just scaled the color by a constant value but still do the recursion I get this result:</p>
<p><a href="https://i.sstatic.net/KsYKl.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/KsYKl.png" alt="recursion depth 2 no lighting" /></a></p>
<p><a href="https://i.sstatic.net/pwwai.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/pwwai.png" alt="recursion depth 3 no lighting" /></a></p>
<p>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.</p>
<p>Thanks.</p>

CC BY-SA 4.0
2022-04-04T09:16:10.817Z
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 :-/
ivokabel
CC BY-SA 4.0
2022-04-04T09:18:47.067Z
Anyway, it's always a good idea to simplify your debugging scenario as much as possible and move to more complex after fixing it.
ivokabel
CC BY-SA 4.0
2022-04-05T00:11:29.153Z
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.
JayDee17
CC BY-SA 4.0
2023-07-25T10:44:21.320Z
@YvesDaoust, that sounds like a reasonable assessment.
ivokabel
<raytracing><bitmap-graphics>
Problem with Corrupted Pixel/color data on bitmap image in Raytracer
JayDee17
# Problem with Corrupted Pixel/color data on bitmap image in Raytracer

(Asked by: username_0 on 2022-04-02)

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](https://i.sstatic.net/iivcA.png)](https://i.sstatic.net/iivcA.png)

[![enter image description here](https://i.sstatic.net/MnMqk.png)](https://i.sstatic.net/MnMqk.png)

This is the output without any recursive reflections for reference:
[![enter image description here](https://i.sstatic.net/zB0HP.png)](https://i.sstatic.net/zB0HP.png)

Here is my code (c++):

[Defines.h](https://pastebin.com/iskhCZzZ)
(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](https://dev.to/muiz6/c-how-to-write-a-bitmap-image-from-scratch-1k6m).

[main.cpp](https://pastebin.com/th40YG3e)
(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](https://i.sstatic.net/KsYKl.png)](https://i.sstatic.net/KsYKl.png)

[![recursion depth 3 no lighting](https://i.sstatic.net/pwwai.png)](https://i.sstatic.net/pwwai.png)

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.

Thanks.

**Tags:** raytracing, bitmap-graphics

> **Comments:**
> **username_1**: 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 :-/  
> **username_1**: Anyway, it's always a good idea to simplify your debugging scenario as much as possible and move to more complex after fixing it.  
> **username_0**: Hi @username_1, 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.  
> **username_1**: @YvesDaoust, that sounds like a reasonable assessment.  

---

## 1 answers

### Answer by username_0 on 2022-04-11. Score: 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.

> **Comments:**
> **username_1**: So it was a file storage problem. Very interesting. Thanks for sharing the experience. Might be helpful for others.  

---

