Add enviromental samples per pixel
This commit is contained in:
parent
1eeb328b60
commit
f622a55c5c
4 changed files with 93 additions and 20 deletions
36
camera.hpp
Normal file
36
camera.hpp
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
#ifndef CAMERA_H
|
||||||
|
#define CAMERA_H
|
||||||
|
|
||||||
|
#include "rtweekend.hpp"
|
||||||
|
|
||||||
|
struct camera {
|
||||||
|
/* Attributes */
|
||||||
|
point3 origin;
|
||||||
|
point3 lower_left_corner;
|
||||||
|
vec3 horizontal;
|
||||||
|
vec3 vertical;
|
||||||
|
|
||||||
|
/* Constructors */
|
||||||
|
camera()
|
||||||
|
{
|
||||||
|
double aspect_ratio = 16.0 / 9;
|
||||||
|
double viewport_height = 2.0;
|
||||||
|
double viewport_width = aspect_ratio * viewport_height;
|
||||||
|
double focal_length = 1.0;
|
||||||
|
|
||||||
|
origin = vec3(0,0,0);
|
||||||
|
horizontal = vec3(viewport_width, 0, 0);
|
||||||
|
vertical = vec3(0, viewport_height, 0);
|
||||||
|
lower_left_corner = origin - horizontal/2 - vertical/2 - vec3(0, 0, focal_length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Methods */
|
||||||
|
|
||||||
|
ray get_ray(double u, double v) const
|
||||||
|
{
|
||||||
|
return ray(origin, lower_left_corner + u*horizontal + v*vertical - origin);
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
23
color.hpp
23
color.hpp
|
|
@ -1,18 +1,27 @@
|
||||||
#ifndef COLOR_H
|
#ifndef COLOR_H
|
||||||
#define COLOR_H
|
#define COLOR_H
|
||||||
|
|
||||||
#include "vec3.hpp"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "rtweekend.hpp"
|
||||||
|
|
||||||
/* Writes color components as a space-delimited string of numbers in the range [0,255] */
|
/* Writes color components as a space-delimited string of numbers in the range [0,255] */
|
||||||
void write_color(FILE *fp, color c)
|
void write_color(FILE *fp, color c, uint32_t samples_per_pixel)
|
||||||
{
|
{
|
||||||
fprintf(fp, "%d %d %d\n",
|
double scale = 1.0 / samples_per_pixel;
|
||||||
(uint8_t) (255 * c.x),
|
|
||||||
(uint8_t) (255 * c.y),
|
// Divide the color by the number of samples
|
||||||
(uint8_t) (255 * c.z));
|
double r = c.x * scale;
|
||||||
|
double g = c.y * scale;
|
||||||
|
double b = c.z * scale;
|
||||||
|
|
||||||
|
/* Write output */
|
||||||
|
fprintf(fp,
|
||||||
|
"%d %d %d\n",
|
||||||
|
(uint8_t) (255 * clamp(r, 0, 1)),
|
||||||
|
(uint8_t) (255 * clamp(g, 0, 1)),
|
||||||
|
(uint8_t) (255 * clamp(b, 0, 1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
36
main.cpp
36
main.cpp
|
|
@ -6,6 +6,7 @@
|
||||||
#include "color.hpp"
|
#include "color.hpp"
|
||||||
#include "hittable_list.hpp"
|
#include "hittable_list.hpp"
|
||||||
#include "sphere.hpp"
|
#include "sphere.hpp"
|
||||||
|
#include "camera.hpp"
|
||||||
|
|
||||||
color ray_color(const ray& r, const hittable& world);
|
color ray_color(const ray& r, const hittable& world);
|
||||||
double hit_sphere(const point3& center, double radius, const ray& r);
|
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()
|
int main()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Image
|
// Image
|
||||||
double aspect_ratio = 16.0 / 9;
|
double aspect_ratio = 16.0 / 9;
|
||||||
const int32_t image_width = 400;
|
const int32_t image_width = 400;
|
||||||
const int32_t image_height = (int32_t) (image_width / aspect_ratio);
|
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
|
// World
|
||||||
hittable_list world;
|
hittable_list world;
|
||||||
|
|
@ -49,14 +61,7 @@ int main()
|
||||||
world.add(std::make_shared<sphere>(point3(0,-100.5,-1), 100));
|
world.add(std::make_shared<sphere>(point3(0,-100.5,-1), 100));
|
||||||
|
|
||||||
// Camera
|
// Camera
|
||||||
double viewport_height = 2.0;
|
camera cam;
|
||||||
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);
|
|
||||||
|
|
||||||
// Render
|
// Render
|
||||||
printf("P3\n%d %d\n255\n", image_width, image_height);
|
printf("P3\n%d %d\n255\n", image_width, image_height);
|
||||||
|
|
@ -67,12 +72,17 @@ int main()
|
||||||
fflush(stderr);
|
fflush(stderr);
|
||||||
for (int32_t i = 0; i < image_width; ++i)
|
for (int32_t i = 0; i < image_width; ++i)
|
||||||
{
|
{
|
||||||
double u = (double)i / (image_width-1);
|
color pixel_color = color(0,0,0);
|
||||||
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);
|
|
||||||
|
|
||||||
write_color(stdout, pixel_color);
|
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, samples_per_pixel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fprintf(stderr, "\nDone\n");
|
fprintf(stderr, "\nDone\n");
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,24 @@ double degrees_to_radians(double d)
|
||||||
return d * M_PI / 180;
|
return d * M_PI / 180;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Returns a double in the range [0,1) */
|
||||||
|
inline double random_double()
|
||||||
|
{
|
||||||
|
return rand() / RAND_MAX + 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Returns a double in the range [min,max) */
|
||||||
|
inline double random_double(double min, double max)
|
||||||
|
{
|
||||||
|
return min + (max-min) * random_double();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Clamps a value between [min,max] */
|
||||||
|
inline double clamp(double v, double min, double max)
|
||||||
|
{
|
||||||
|
return v < min ? min : v > max ? max : v;
|
||||||
|
}
|
||||||
|
|
||||||
/* Common internal headers */
|
/* Common internal headers */
|
||||||
|
|
||||||
#include "ray.hpp"
|
#include "ray.hpp"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue