2020-06-01 02:49:50 +02:00
|
|
|
#pragma once
|
|
|
|
|
2020-06-06 22:11:09 +02:00
|
|
|
#include "rtweekend.h"
|
2020-06-01 02:49:50 +02:00
|
|
|
|
2023-02-17 23:55:31 +01:00
|
|
|
// for writing to stdout
|
2020-06-01 02:49:50 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2023-02-17 23:55:31 +01:00
|
|
|
// for writing to socket
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-02-18 19:35:15 +01:00
|
|
|
const colour pink(254.0/255.0, 226.0/255.0, 170.0/255.0);
|
|
|
|
const colour grey(0.133, 0.133, 0.133);
|
|
|
|
|
2023-02-17 23:55:31 +01:00
|
|
|
void correct_gamma(colour& pixel_colour, int samples)
|
2020-06-01 02:49:50 +02:00
|
|
|
{
|
2023-02-17 23:55:31 +01:00
|
|
|
double r = pixel_colour.x();
|
|
|
|
double g = pixel_colour.y();
|
|
|
|
double b = pixel_colour.z();
|
2020-06-06 22:11:09 +02:00
|
|
|
|
2023-02-17 23:55:31 +01:00
|
|
|
// divide the colour total by the number of samples and gamma-correct for gamma=2.0
|
|
|
|
auto scale = 1.0 / samples;
|
2020-06-07 00:01:46 +02:00
|
|
|
r = sqrt(scale * r);
|
|
|
|
g = sqrt(scale * g);
|
|
|
|
b = sqrt(scale * b);
|
2023-02-17 23:55:31 +01:00
|
|
|
|
|
|
|
pixel_colour = colour(r, g, b);
|
|
|
|
}
|
|
|
|
|
2023-02-19 20:55:42 +01:00
|
|
|
int format_component(double component)
|
|
|
|
{
|
|
|
|
return int(256 * math::clamp(component, 0.0, 0.999));
|
|
|
|
}
|
|
|
|
|
2023-02-17 23:55:31 +01:00
|
|
|
void write_colour_to_stream(std::ostream &out, colour pixel_colour, int samples_per_pixel)
|
|
|
|
{
|
|
|
|
correct_gamma(pixel_colour, samples_per_pixel);
|
|
|
|
|
|
|
|
auto r = pixel_colour.x();
|
|
|
|
auto g = pixel_colour.y();
|
|
|
|
auto b = pixel_colour.z();
|
2020-06-06 22:11:09 +02:00
|
|
|
|
2020-06-01 02:49:50 +02:00
|
|
|
// write the translated [0,255] value of each colour component.
|
2023-02-19 20:55:42 +01:00
|
|
|
out << format_component(r) << ' '
|
|
|
|
<< format_component(g) << ' '
|
|
|
|
<< format_component(b) << '\n';
|
2023-02-17 23:55:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void write_colour_to_socket(int sockfd, colour pixel_colour, int samples_per_pixel)
|
|
|
|
{
|
|
|
|
correct_gamma(pixel_colour, samples_per_pixel);
|
|
|
|
|
|
|
|
int r = format_component(pixel_colour.x());
|
|
|
|
int g = format_component(pixel_colour.y());
|
|
|
|
int b = format_component(pixel_colour.z());
|
|
|
|
|
|
|
|
// pack values
|
|
|
|
int len = 3;
|
|
|
|
char s[len];
|
|
|
|
sprintf(s, "%c%c%c", r, g, b);
|
|
|
|
|
|
|
|
int written = write(sockfd, s, len);
|
|
|
|
if (written < 0)
|
|
|
|
{
|
|
|
|
error("ERROR writing colour to socket");
|
|
|
|
}
|
|
|
|
}
|