feat: add depth-of-field blur
This commit is contained in:
parent
0b5c3c20cb
commit
e7552d1a8a
27
camera.h
27
camera.h
@ -12,25 +12,34 @@ class camera {
|
||||
point3 lookat,
|
||||
vec3 vup,
|
||||
double vfov, // vertical field-of-view in degrees,
|
||||
double aspect_ratio
|
||||
double aspect_ratio,
|
||||
double aperture,
|
||||
double focus_dist
|
||||
) {
|
||||
auto theta = degrees_to_radians(vfov);
|
||||
auto h = tan(theta/2);
|
||||
auto viewport_height = 2.0 * h;
|
||||
auto viewport_width = aspect_ratio * viewport_height;
|
||||
|
||||
auto w = unit_vector(lookfrom - lookat);
|
||||
auto u = unit_vector(cross(vup, w));
|
||||
auto v = cross(w, u);
|
||||
w = unit_vector(lookfrom - lookat);
|
||||
u = unit_vector(cross(vup, w));
|
||||
v = cross(w, u);
|
||||
|
||||
origin = lookfrom;
|
||||
horizontal = viewport_width * u;
|
||||
vertical = viewport_height * v;
|
||||
lower_left_corner = origin - horizontal/2 - vertical/2 - w;
|
||||
horizontal = focus_dist * viewport_width * u;
|
||||
vertical = focus_dist * viewport_height * v;
|
||||
lower_left_corner = origin - horizontal/2 - vertical/2 - focus_dist*w;
|
||||
|
||||
lens_radius = aperture/2;
|
||||
}
|
||||
|
||||
ray get_ray(double s, double t) const {
|
||||
return ray(origin, lower_left_corner + s*horizontal + t*vertical - origin);
|
||||
vec3 rd = lens_radius * random_in_unit_sphere();
|
||||
vec3 offset = u* rd.x() + v * rd.y();
|
||||
return ray(
|
||||
origin + offset,
|
||||
lower_left_corner + s*horizontal + t*vertical - origin - offset
|
||||
);
|
||||
}
|
||||
|
||||
private:
|
||||
@ -38,6 +47,8 @@ class camera {
|
||||
point3 lower_left_corner;
|
||||
vec3 horizontal;
|
||||
vec3 vertical;
|
||||
vec3 u, v, w;
|
||||
double lens_radius;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
7
main.cc
7
main.cc
@ -52,7 +52,12 @@ int main() {
|
||||
world.add(make_shared<sphere>(point3( 1, 0, -1), 0.5, material_right));
|
||||
|
||||
// Camera
|
||||
camera cam(point3(-2, 2, 1), point3(0, 0, -1), vec3(0, 1, 0), 70, aspect_ratio);
|
||||
point3 lookfrom(3, 3, 2);
|
||||
point3 lookat(0, 0, -1);
|
||||
vec3 vup(0, 1, 0);
|
||||
auto dist_to_focus = (lookfrom-lookat).length();
|
||||
auto aperture = 2.0;
|
||||
camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus);
|
||||
|
||||
// Render
|
||||
std::cout<<"P3\n" <<image_width<<" "<<image_height<<"\n255\n";
|
||||
|
Loading…
Reference in New Issue
Block a user