Chapter 11&12: Defocus Blur & Final Scene
在 Camera.h 中修改如下:
#pragma once
#define _USE_MATH_DEFINES
#include "Ray.h"
#include
Vec3 RandomInUnitDisk() {
Vec3 p;
do {
p = 2.0*Vec3((rand() % (100) / (float)(100)), (rand() % (100) / (float)(100)), 0) - Vec3(1, 1, 0);
} while (dot(p, p) >= 1.0);
return p;
}
class Camera {
public:
Camera(Vec3 lookfrom, Vec3 lookat, Vec3 vup, float vfov, float aspect, float aperture, float focus_dist)
{
lens_radius = aperture / 2;
float theta = vfov*M_PI / 180;
float half_height = tan(theta / 2);
float half_width = aspect * half_height;
origin = lookfrom;
w = unit_vector(lookfrom - lookat);
u = unit_vector(cross(vup, w));
v = cross(w, u);
lower_left_corner = origin - half_width*focus_dist*u - half_height*focus_dist*v - focus_dist*w;
horizOntal= focus_dist * 2 * half_width*u;
vertical = focus_dist * 2 * half_height*v;
}
Ray getRay(float s, float t)
{
Vec3 rd = lens_radius * RandomInUnitDisk();
Vec3 offset = u * rd.x() + v * rd.y();
return Ray(origin + offset, lower_left_corner + s*horizontal + t*vertical - origin - offset);
}
Vec3 lower_left_corner;
Vec3 origin;
Vec3 horizontal;
Vec3 vertical;
Vec3 u, v, w;
float lens_radius;
};
Main.cpp 中:
Hitable *RandomScene() {
int n = 500;
Hitable **list = new Hitable *[n + 1];
list[0] = new Sphere(Vec3(0, -1000, 0), 1000, new Lambertian(Vec3(0.5, 0.5, 0.5)));
int i = 1;
for (int a = -11; a <11; a++) {
for (int b = -11; b <11; b++) {
float choose_mat = (rand() % (100) / (float)(100));
Vec3 center(a + 0.9*(rand() % (100) / (float)(100)), 0.2,b + 0.9*(rand() % (100) / (float)(100)));
if ((center - Vec3(4, 0.2, 0)).length() > 0.9) {
if (choose_mat <0.8) {
list[i++] = new Sphere(center, 0.2,
new Lambertian(Vec3(
(rand() % (100) / (float)(100))*(rand() % (100) / (float)(100)),
(rand() % (100) / (float)(100))*(rand() % (100) / (float)(100)),
(rand() % (100) / (float)(100))*(rand() % (100) / (float)(100)))));
}
else if (choose_mat <0.95) {
list[i++] = new Sphere(center, 0.2,
new Metal(Vec3(0.5*(1 + (rand() % (100) / (float)(100))),
0.5*(1 + (rand() % (100) / (float)(100))),
0.5*(1 + (rand() % (100) / (float)(100)))),
0.5*(1 + (rand() % (100) / (float)(100)))));
}
else {
list[i++] = new Sphere(center, 0.2, new Dielectric(1.5));
}
}
}
}
list[i++] = new Sphere(Vec3(0, 1, 0), 1.0, new Dielectric(1.5));
list[i++] = new Sphere(Vec3(-4, 1, 0), 1.0, new Lambertian(Vec3(0.4, 0.2, 0.1)));
list[i++] = new Sphere(Vec3(4, 1, 0), 1.0, new Metal(Vec3(0.7, 0.6, 0.5), 0.0));
return new HitableList(list, i);
}
int main()
{
ofstream outfile;
outfile.open("ch12_HighImage.ppm");
int nx = 2000;
int ny = 1000;
int ns = 100;
outfile <<"P3\n" <" " <"\n255\n";
Hitable *world = RandomScene();
Vec3 lookfrom(13.0f, 2.0f, 3.0f);
Vec3 lookat(0.0f, 0.0f, 0.0f);
float dist_to_focus = 10.0f;
float aperture = 0.1f;
Camera cam(lookfrom, lookat , Vec3(0.0f, 1.0f, 0.0f), 20, float(nx) / float(ny), aperture, dist_to_focus);
default_random_engine reng;
uniform_real_distribution<float> uni_dist(0.0f, 1.0f);
for (int j = ny - 1; j >= 0; j--)
{
for (int i = 0; i {
Vec3 col(0.0f, 0.0f, 0.0f);
for (int s = 0; s {
float u = float(i + uni_dist(reng)) / float(nx);
float v = float(j + uni_dist(reng)) / float(ny);
Ray r = cam.getRay(u,v);
col += Color(r, world, 0);
}
col /= float(ns);
col = Vec3(sqrt(col[0]), sqrt(col[1]), sqrt(col[2]));
int ir = int(255.99*col[0]);
int ig = int(255.99*col[1]);
int ib = int(255.99*col[2]);
outfile <" " <" " <"\n";
}
}
outfile.close();
return 0;
}
最终效果图:(2000px*2000px高清版,渲染了近30个小时)