toytracer/color.h
2020-06-07 03:28:59 +02:00

43 lines
1.2 KiB
C++

#ifndef COLOR_H
#define COLOR_H
#include <vector>
#include <iostream>
#include <cmath>
#include <cstddef>
#include "vec3.h"
#include "util.h"
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;
r = std::sqrt(scale * r);
g = std::sqrt(scale * g);
b = std::sqrt(scale * b);
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";
}
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));
}
#endif // COLOR_H