Add enviromental samples per pixel

This commit is contained in:
Phireh 2021-08-21 18:26:22 +02:00
commit f622a55c5c
4 changed files with 93 additions and 20 deletions

View file

@ -6,6 +6,7 @@
#include "color.hpp"
#include "hittable_list.hpp"
#include "sphere.hpp"
#include "camera.hpp"
color ray_color(const ray& r, const hittable& world);
double hit_sphere(const point3& center, double radius, const ray& r);
@ -38,10 +39,21 @@ double hit_sphere(const point3& center, double radius, const ray& r)
int main()
{
// Image
double aspect_ratio = 16.0 / 9;
const int32_t image_width = 400;
const int32_t image_height = (int32_t) (image_width / aspect_ratio);
int32_t samples_per_pixel = 100;
if (getenv("SPP"))
{
samples_per_pixel = strtol(getenv("SPP"), NULL, 10);
}
// World
hittable_list world;
@ -49,14 +61,7 @@ int main()
world.add(std::make_shared<sphere>(point3(0,-100.5,-1), 100));
// Camera
double viewport_height = 2.0;
double viewport_width = aspect_ratio * viewport_height;
double focal_length = 1.0;
point3 origin = point3(0,0,0);
vec3 horizontal = vec3(viewport_width, 0, 0);
vec3 vertical = vec3(0, viewport_height, 0);
vec3 lower_left_corner = origin - horizontal/2 - vertical/2 - vec3(0, 0, focal_length);
camera cam;
// Render
printf("P3\n%d %d\n255\n", image_width, image_height);
@ -67,12 +72,17 @@ int main()
fflush(stderr);
for (int32_t i = 0; i < image_width; ++i)
{
double u = (double)i / (image_width-1);
double v = (double)j / (image_height-1);
ray r = ray(origin, lower_left_corner + u*horizontal + v*vertical - origin);
color pixel_color = ray_color(r, world);
color pixel_color = color(0,0,0);
for (int32_t s = 0; s < samples_per_pixel; ++s)
{
double u = ((i + random_double()) / (image_width-1));
double v = ((j + random_double()) / (image_height-1));
ray r = cam.get_ray(u,v);
pixel_color += ray_color(r, world);
}
write_color(stdout, pixel_color);
write_color(stdout, pixel_color, samples_per_pixel);
}
}
fprintf(stderr, "\nDone\n");