Antialiasing

This commit is contained in:
Armin Friedl 2022-07-28 20:50:16 +02:00
parent b9862477cd
commit cf5406dbba
5 changed files with 99 additions and 49 deletions

View file

@ -8,4 +8,5 @@ add_executable(rtiww src/main.cpp
src/color.h
src/hittable.h src/hittable_list.h
src/sphere.h
src/camera.h
src/rtweekend.h)

33
rtiww/src/camera.h Normal file
View file

@ -0,0 +1,33 @@
#ifndef CAMERA_H_
#define CAMERA_H_
#include "rtweekend.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_

View file

@ -1,14 +1,24 @@
#ifndef RTIWW_COLOR_H
#define RTIWW_COLOR_H
#include "rtweekend.h"
#include "vec3.h"
#include <iostream>
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 samples_per_pixel) {
auto r = pixel_color.x();
auto g = pixel_color.y();
auto b = pixel_color.z();
auto scale = 1.0 / samples_per_pixel;
r *= scale;
g *= scale;
b *= scale;
out << static_cast<int>(255.999 * clamp(r, 0.0, 0.999)) << ' '
<< static_cast<int>(255.999 * clamp(g, 0.0, 0.999)) << ' '
<< static_cast<int>(255.999 * clamp(b, 0.0, 0.999)) << '\n';
}
#endif // RTIWW_COLOR_H

View file

@ -1,3 +1,4 @@
#include "camera.h"
#include "hittable.h"
#include "rtweekend.h"
@ -23,6 +24,7 @@ int main() {
const auto aspect_ratio = 16.0 / 9.0;
const int image_width = 400;
const int image_height = static_cast<int>(image_width / aspect_ratio);
const int samples_per_pixel = 100;
// World
hittable_list world;
@ -30,14 +32,7 @@ int main() {
world.add(make_shared<sphere>(point3(0, -100.5, -1), 100));
// Camera
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);
camera cam;
// Render
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
@ -45,12 +40,15 @@ int main() {
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);
color pixel_color(0, 0, 0);
for (int s = 0; s < samples_per_pixel; ++s) {
auto u = (i + random_double()) / (image_width - 1);
auto v = (j + random_double()) / (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);
ray r = cam.get_ray(u, v);
pixel_color += ray_color(r, world);
}
write_color(std::cout, pixel_color, samples_per_pixel);
}
}

View file

@ -5,8 +5,8 @@
#include <limits>
#include <memory>
using std::shared_ptr;
using std::make_shared;
using std::shared_ptr;
using std::sqrt;
const double infinity = std::numeric_limits<double>::infinity();
@ -26,6 +26,14 @@ 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;
}
#include "ray.h"
#include "vec3.h"