2020-06-02 22:06:53 +00:00
|
|
|
#ifndef COLOR_H
|
|
|
|
#define COLOR_H
|
|
|
|
|
2020-06-07 01:28:59 +00:00
|
|
|
#include <vector>
|
2020-06-02 22:06:53 +00:00
|
|
|
#include <iostream>
|
2020-06-06 12:57:55 +00:00
|
|
|
#include <cmath>
|
2020-06-07 01:28:59 +00:00
|
|
|
#include <cstddef>
|
2020-06-02 22:06:53 +00:00
|
|
|
|
|
|
|
#include "vec3.h"
|
2020-06-03 22:39:14 +00:00
|
|
|
#include "util.h"
|
2020-06-02 22:06:53 +00:00
|
|
|
|
2020-06-03 22:39:14 +00:00
|
|
|
void write_color(std::ostream &out, Color pixel_color, int sample_per_pixel) {
|
|
|
|
auto r = pixel_color.x();
|
|
|
|
auto g = pixel_color.y();
|
|
|
|
auto b = pixel_color.z();
|
|
|
|
|
|
|
|
auto scale = 1.0 / sample_per_pixel;
|
2020-06-06 12:57:55 +00:00
|
|
|
r = std::sqrt(scale * r);
|
|
|
|
g = std::sqrt(scale * g);
|
|
|
|
b = std::sqrt(scale * b);
|
2020-06-03 22:39:14 +00:00
|
|
|
|
|
|
|
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";
|
2020-06-02 22:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 01:28:59 +00:00
|
|
|
void write_color_vec(std::vector<unsigned char>& vec, std::size_t i, Color pixel_color, int sample_per_pixel) {
|
|
|
|
auto r = pixel_color.x();
|
|
|
|
auto g = pixel_color.y();
|
|
|
|
auto b = pixel_color.z();
|
|
|
|
|
|
|
|
auto scale = 1.0 / sample_per_pixel;
|
|
|
|
r = std::sqrt(scale * r);
|
|
|
|
g = std::sqrt(scale * g);
|
|
|
|
b = std::sqrt(scale * b);
|
|
|
|
|
|
|
|
vec[i+0] = static_cast<unsigned char>(256 * clamp(r, 0.0, 0.999));
|
|
|
|
vec[i+1] = static_cast<unsigned char>(256 * clamp(g, 0.0, 0.999));
|
|
|
|
vec[i+2] = static_cast<unsigned char>(256 * clamp(b, 0.0, 0.999));
|
|
|
|
}
|
|
|
|
|
2020-06-02 22:06:53 +00:00
|
|
|
#endif // COLOR_H
|