inline Color operator * (const Color& c, float a) {
Color color;
color.red = c.red * a;
color.green = c.green * a;
color.blue = c.blue * a;
return color;
}
So my question is: Is there something wrong with any of these operator overloading? If not, where might it be the cause (fell free to ask for more code)? I know by fact that the main issue that I have is caused by the + operator, since if I remove it, the problem vanishes.
Thanks for the answers. Duthomas, are you sure about using "+=" ? I ask it because my "raytracing how-to book." uses it: https://www.scratchapixel.com/code.php?id=32&origin=/lessons/3d-basic-rendering/phong-shader-BRDF (line 565). To my understanding, it makes sense. I have to accumulate the contributions of several light sources, so += seems to make sense. On the diffuse light is also += and in that case it does work as intended.
a [ctr] = min (final_color.blue*255.0f,255.0f);
a [ctr+1] = min (final_color.green*255.0f,255.0f);
a [ctr+2] = min (final_color.red*255.0f,255.0f);
but I guess it is already too late. Not sure I would recognize if the value overflowed while still being calculated as a float. How I would know what is an acceptable value? I mean, I am currently printing the values of two different pixels on the terminal's screen for two different images.
For the first test case, to swap "+=" by "= "does not produce any change for one pixel, for the other the value goes from 0 to 22 (for all the colors). In the second, the specular component goes from 1.6 to 0 for the first pixel. However, in the second, it goes from about 1500 to 0! I realize that being 0 is likely correct, since the specular only affects a few pixels
Also, still intrigued by Duthomas comment about += being wrong.
Anyway link to two test images with += and only with +. Still different issues with "+", although obviously smaller: https://postimg.cc/gallery/di05ynmw/
are you working in float for byte element colors just because of pow?
if so, your code will be faster and give better results if you stick to bytes and write yourself an integer power routine. If not, carry on.
The final color is, yes, a composite of the contributing colors.
Be careful how you combine them. Right now you are treating them as equal contributors and simply adding their full values, which is blowing the RGB bounds and manifesting as weird colors.
could you provide a minimal testcase that does reproduce your issue?
Complicated, I think it requires so many things that a striped up version would require so many effort and would still be huge for a testcase. I could without so many effort create a makefile and update it to my gitlab to make it easy to compile it. Plus to add the current problematic version
are you working in float for byte element colors just because of pow?
if so, your code will be faster and give better results if you stick to bytes and write yourself an integer power routine. If not, carry on.
Not sure if I understood, but I want working with float because most variables that take part of the color calculations (diffuse and specular) are float. So, as far as I can imagine, int would result in wrongs values, hence colors that would be incorrect.
Be careful how you combine them. Right now you are treating them as equal contributors and simply adding their full values, which is blowing the RGB bounds and manifesting as weird colors.