Add camera class and sampling.
This commit is contained in:
parent
881141a912
commit
be4e32755a
2
Makefile
2
Makefile
@ -2,7 +2,7 @@ CXX = g++
|
||||
|
||||
CXXFLAGS = -Wall -Wextra -O2 -std=c++14
|
||||
|
||||
DEPS = util.h vec3.h color.h ray.h hittable.h hittable_list.h sphere.h
|
||||
DEPS = util.h vec3.h color.h ray.h camera.h hittable.h hittable_list.h sphere.h
|
||||
OBJ = wtracer.o
|
||||
|
||||
TARGET = wtracer
|
||||
|
31
camera.h
Normal file
31
camera.h
Normal file
@ -0,0 +1,31 @@
|
||||
#ifndef CAMERA_H
|
||||
#define CAMERA_H
|
||||
|
||||
#include "vec3.h"
|
||||
|
||||
class Camera {
|
||||
public:
|
||||
Camera() {
|
||||
auto aspect_ratio = 16.0 / 9.0;
|
||||
auto viewport_height = 2.0;
|
||||
auto viewport_width = aspect_ratio * viewport_height;
|
||||
auto focal_length = 1.0;
|
||||
|
||||
origin = Point3(0, 0, 0);
|
||||
horizontal = Vec3(viewport_width, 0.0, 0.0);
|
||||
vertical = Vec3(0.0, viewport_height, 0.0);
|
||||
lower_left_corner = origin - horizontal / 2 - vertical / 2 - Vec3(0, 0, focal_length);
|
||||
}
|
||||
|
||||
Ray get_ray(double u, double v) const {
|
||||
return Ray(origin, lower_left_corner + u * horizontal + v * vertical - origin);
|
||||
}
|
||||
|
||||
private:
|
||||
Point3 origin;
|
||||
Point3 lower_left_corner;
|
||||
Vec3 horizontal;
|
||||
Vec3 vertical;
|
||||
};
|
||||
|
||||
#endif // CAMERA_H
|
18
color.h
18
color.h
@ -4,11 +4,21 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "vec3.h"
|
||||
#include "util.h"
|
||||
|
||||
void write_color(std::ostream &out, Color pixel_color) {
|
||||
out << static_cast<int>(255.999 * pixel_color.x()) << " "
|
||||
<< static_cast<int>(255.999 * pixel_color.y()) << " "
|
||||
<< static_cast<int>(255.999 * pixel_color.z()) << "\n";
|
||||
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 *= scale;
|
||||
g *= scale;
|
||||
b *= scale;
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
#endif // COLOR_H
|
||||
|
17
util.h
17
util.h
@ -2,6 +2,7 @@
|
||||
#define UTIL_H
|
||||
|
||||
#include <limits>
|
||||
#include <random>
|
||||
|
||||
const double infinity = std::numeric_limits<double>::infinity();
|
||||
const double pi = 3.1415926535897932385;
|
||||
@ -10,4 +11,20 @@ inline double degrees_to_radians(double degrees) {
|
||||
return degrees * pi / 180;
|
||||
}
|
||||
|
||||
inline double random_double() {
|
||||
static std::uniform_real_distribution<double> distribution(0.0, 1.0);
|
||||
static std::mt19937 generator;
|
||||
return distribution(generator);
|
||||
}
|
||||
|
||||
inline double random_double(double min, double max) {
|
||||
return min + (max-min) * random_double();
|
||||
}
|
||||
|
||||
inline double clamp(double x, double min, double max) {
|
||||
if (x < min) return min;
|
||||
if (x > max) return max;
|
||||
return x;
|
||||
}
|
||||
|
||||
#endif // UTIL_H
|
||||
|
27
wtracer.cpp
27
wtracer.cpp
@ -7,6 +7,7 @@
|
||||
|
||||
#include "hittable_list.h"
|
||||
#include "sphere.h"
|
||||
#include "camera.h"
|
||||
|
||||
Color ray_color(const Ray& r, const Hittable& world) {
|
||||
hit_record rec;
|
||||
@ -21,31 +22,29 @@ Color ray_color(const Ray& r, const Hittable& world) {
|
||||
int main() {
|
||||
const auto aspect_ratio = 16.0 / 9.0;
|
||||
const int image_width = 1280;
|
||||
//const int image_width = 384;
|
||||
const int image_height = static_cast<int>(image_width / aspect_ratio);
|
||||
const int samples_per_pixel = 100;
|
||||
|
||||
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
|
||||
|
||||
auto viewport_height = 2.0;
|
||||
auto viewport_width = aspect_ratio * viewport_height;
|
||||
auto focal_length = 1.0;
|
||||
|
||||
auto origin = Point3(0, 0, 0);
|
||||
auto horizontal = Vec3(viewport_width, 0, 0);
|
||||
auto vertical = Vec3(0, viewport_height, 0);
|
||||
auto lower_left_corner = origin - horizontal/2 - vertical/2 - Vec3(0, 0, focal_length);
|
||||
|
||||
Hittable_list world;
|
||||
world.add(std::make_shared<Sphere>(Point3(0, 0, -1), 0.5));
|
||||
world.add(std::make_shared<Sphere>(Point3(0, -100.5, -1), 100));
|
||||
|
||||
Camera cam;
|
||||
|
||||
for (int j = image_height - 1; j >= 0; --j) {
|
||||
std::cerr << "\rScanlines remaining: " << j << " " << std::flush;
|
||||
for (int i = 0; i < image_width; ++i) {
|
||||
auto u = double(i) / (image_width - 1);
|
||||
auto v = double(j) / (image_height - 1);
|
||||
Ray r(origin, lower_left_corner + u*horizontal + v*vertical - origin);
|
||||
Color pixel_color = ray_color(r, world);
|
||||
write_color(std::cout, pixel_color);
|
||||
Color pixel_color(0, 0, 0);
|
||||
for (int s = 0; s<samples_per_pixel; ++s) {
|
||||
auto u = double(i + random_double()) / (image_width - 1);
|
||||
auto v = double(j + random_double()) / (image_height - 1);
|
||||
Ray r = cam.get_ray(u, v);
|
||||
pixel_color += ray_color(r, world);
|
||||
}
|
||||
write_color(std::cout, pixel_color, samples_per_pixel);
|
||||
}
|
||||
}
|
||||
std::cerr << "\nDone.\n";
|
||||
|
Loading…
Reference in New Issue
Block a user