feat: add antialiasing
This commit is contained in:
parent
88527f91ea
commit
8ec8b93845
31
camera.h
Normal file
31
camera.h
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#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);
|
||||||
|
vertical = vec3(0, viewport_height, 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
|
14
color.h
14
color.h
@ -2,14 +2,20 @@
|
|||||||
#define COLOR_H
|
#define COLOR_H
|
||||||
|
|
||||||
#include "vec3.h"
|
#include "vec3.h"
|
||||||
|
#include "rtweekend.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void write_color(std::ostream &out, color pixel_color) {
|
void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) {
|
||||||
|
auto scale = 1.0 / samples_per_pixel;
|
||||||
|
auto r = pixel_color.x() * scale;
|
||||||
|
auto g = pixel_color.y() * scale;
|
||||||
|
auto b = pixel_color.z() * scale;
|
||||||
|
|
||||||
// Write the translated [0,255] value of each color component
|
// Write the translated [0,255] value of each color component
|
||||||
out << static_cast<int>(255.999 * pixel_color.x()) << " "
|
out << static_cast<int>(256 * clamp(r, 0.0, 0.999)) << " "
|
||||||
<< static_cast<int>(255.999 * pixel_color.y()) << " "
|
<< static_cast<int>(256 * clamp(g, 0.0, 0.999)) << " "
|
||||||
<< static_cast<int>(255.999 * pixel_color.z()) << "\n";
|
<< static_cast<int>(256 * clamp(b, 0.0, 0.999)) << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
24
main.cc
24
main.cc
@ -3,6 +3,7 @@
|
|||||||
#include "color.h"
|
#include "color.h"
|
||||||
#include "hittable_list.h"
|
#include "hittable_list.h"
|
||||||
#include "sphere.h"
|
#include "sphere.h"
|
||||||
|
#include "camera.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
@ -22,6 +23,7 @@ int main() {
|
|||||||
const auto aspect_ratio = 16.0 / 9.0;
|
const auto aspect_ratio = 16.0 / 9.0;
|
||||||
const int image_width = 400;
|
const int image_width = 400;
|
||||||
const int image_height = static_cast<int>(image_width / aspect_ratio);
|
const int image_height = static_cast<int>(image_width / aspect_ratio);
|
||||||
|
const int samples_per_pixel = 10;
|
||||||
|
|
||||||
// World
|
// World
|
||||||
hittable_list world;
|
hittable_list world;
|
||||||
@ -29,25 +31,21 @@ int main() {
|
|||||||
world.add(make_shared<sphere>(point3(0, -100.5, -1), 100));
|
world.add(make_shared<sphere>(point3(0, -100.5, -1), 100));
|
||||||
|
|
||||||
// Camera
|
// Camera
|
||||||
auto viewport_height = 2.0;
|
camera cam;
|
||||||
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);
|
|
||||||
|
|
||||||
// Render
|
// Render
|
||||||
std::cout<<"P3\n"<<image_width<<" "<<image_height<<"\n255\n";
|
std::cout<<"P3\n"<<image_width<<" "<<image_height<<"\n255\n";
|
||||||
for (int j = image_height-1; j >= 0; --j) {
|
for (int j = image_height-1; j >= 0; --j) {
|
||||||
std::cerr<<"\rScanlines remaining: "<<j<<" "<<std::flush;
|
std::cerr<<"\rScanlines remaining: "<<j<<" "<<std::flush;
|
||||||
for (int i = 0; i < image_width; ++i) {
|
for (int i = 0; i < image_width; ++i) {
|
||||||
auto u = double(i) / (image_width-1);
|
color pixel_color(0, 0, 0);
|
||||||
auto v = double(j) / (image_height-1);
|
for (int s = 0; s < samples_per_pixel; ++s) {
|
||||||
ray r(origin, lower_left_corner + u*horizontal + v*vertical - origin);
|
auto u = (i + random_double()) / (image_width-1);
|
||||||
color pixel_color = ray_color(r, world);
|
auto v = (j + random_double()) / (image_height-1);
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cerr<<"\nDone.\n";
|
std::cerr<<"\nDone.\n";
|
||||||
|
17
rtweekend.h
17
rtweekend.h
@ -4,6 +4,7 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
// Usings
|
// Usings
|
||||||
using std::shared_ptr;
|
using std::shared_ptr;
|
||||||
@ -19,6 +20,22 @@ inline double degrees_to_radians(double degrees) {
|
|||||||
return degrees * pi / 180.0;
|
return degrees * pi / 180.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns a random number between (0,1]
|
||||||
|
inline double random_double() {
|
||||||
|
return rand() / (RAND_MAX + 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline double clamp(double x, double min, double max) {
|
||||||
|
if (x < min) return min;
|
||||||
|
if (x > max) return max;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns a random number between (min,max]
|
||||||
|
inline double random_double(double min, double max) {
|
||||||
|
return min + (max - min) * random_double();
|
||||||
|
}
|
||||||
|
|
||||||
// Common headers
|
// Common headers
|
||||||
#include "ray.h"
|
#include "ray.h"
|
||||||
#include "vec3.h"
|
#include "vec3.h"
|
||||||
|
Loading…
Reference in New Issue
Block a user