-
Notifications
You must be signed in to change notification settings - Fork 865
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #510 from RayTracing/write-color
Move vec3::write_color to standalone helper
- Loading branch information
Showing
7 changed files
with
123 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef COLOR_H | ||
#define COLOR_H | ||
//============================================================================================== | ||
// Originally written in 2020 by Peter Shirley <[email protected]> | ||
// | ||
// To the extent possible under law, the author(s) have dedicated all copyright and related and | ||
// neighboring rights to this software to the public domain worldwide. This software is | ||
// distributed without any warranty. | ||
// | ||
// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication | ||
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. | ||
//============================================================================================== | ||
|
||
#include "vec3.h" | ||
|
||
#include <iostream> | ||
|
||
|
||
void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) { | ||
auto r = pixel_color.x(); | ||
auto g = pixel_color.y(); | ||
auto b = pixel_color.z(); | ||
|
||
// Replace NaN components with zero. See explanation in Ray Tracing: The Rest of Your Life. | ||
if (r != r) r = 0.0; | ||
if (g != g) g = 0.0; | ||
if (b != b) b = 0.0; | ||
|
||
// Divide the color by the number of samples and gamma-correct for gamma=2.0. | ||
auto scale = 1.0 / samples_per_pixel; | ||
r = sqrt(scale * r); | ||
g = sqrt(scale * g); | ||
b = sqrt(scale * b); | ||
|
||
// Write the translated [0,255] value of each color component. | ||
out << static_cast<int>(256 * clamp(r, 0.0, 0.999)) << ' ' | ||
<< static_cast<int>(256 * clamp(g, 0.0, 0.999)) << ' ' | ||
<< static_cast<int>(256 * clamp(b, 0.0, 0.999)) << '\n'; | ||
} | ||
|
||
|
||
#endif |