Implement motion blur
This commit is contained in:
@@ -9,32 +9,76 @@ int main(void) {
|
||||
// World.
|
||||
hittableList world;
|
||||
|
||||
auto groundMaterial = make_shared<lambertian>(colour(0.1, 0.0, 0.0));
|
||||
auto centreMaterial = make_shared<lambertian>(colour(0.1, 0.2, 0.5));
|
||||
auto leftMaterial = make_shared<dielectric>(1.50);
|
||||
auto bubbleMaterial = make_shared<dielectric>(1.00 / 1.50);
|
||||
auto rightMaterial = make_shared<metal>(colour(1.0, 0.0, 0.0), 1.0);
|
||||
// auto groundMaterial = make_shared<lambertian>(colour(0.1, 0.0, 0.0));
|
||||
// auto centreMaterial = make_shared<lambertian>(colour(0.1, 0.2, 0.5));
|
||||
// auto leftMaterial = make_shared<dielectric>(1.50);
|
||||
// auto bubbleMaterial = make_shared<dielectric>(1.00 / 1.50);
|
||||
// auto rightMaterial = make_shared<metal>(colour(1.0, 0.0, 0.0), 1.0);
|
||||
|
||||
world.add(make_shared<sphere>(point3( 0.0, -100.5, -1.0), 100.0, groundMaterial));
|
||||
world.add(make_shared<sphere>(point3( 0.0, 0.0, -1.2), 0.5, centreMaterial));
|
||||
world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), 0.5, leftMaterial));
|
||||
world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), 0.4, bubbleMaterial));
|
||||
world.add(make_shared<sphere>(point3( 1.0, 0.0, -1.0), 0.5, rightMaterial));
|
||||
// world.add(make_shared<sphere>(point3( 0.0, -100.5, -1.0), 100.0, groundMaterial));
|
||||
// world.add(make_shared<sphere>(point3( 0.0, 0.0, -1.2), 0.5, centreMaterial));
|
||||
// world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), 0.5, leftMaterial));
|
||||
// world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), 0.4, bubbleMaterial));
|
||||
// world.add(make_shared<sphere>(point3( 1.0, 0.0, -1.0), 0.5, rightMaterial));
|
||||
|
||||
// Code from the book.
|
||||
auto ground_material = make_shared<lambertian>(colour(0.5, 0.5, 0.5));
|
||||
world.add(make_shared<sphere>(point3(0,-1000,0), 1000, ground_material));
|
||||
|
||||
for (int a = -11; a < 11; a++) {
|
||||
for (int b = -11; b < 11; b++) {
|
||||
auto choose_mat = randomDouble();
|
||||
point3 center(a + 0.9*randomDouble(), 0.2, b + 0.9*randomDouble());
|
||||
|
||||
if ((center - point3(4, 0.2, 0)).length() > 0.9) {
|
||||
shared_ptr<material> sphere_material;
|
||||
|
||||
if (choose_mat < 0.8) {
|
||||
// diffuse
|
||||
auto albedo = colour::random() * colour::random();
|
||||
sphere_material = make_shared<lambertian>(albedo);
|
||||
world.add(make_shared<sphere>(center, 0.2, sphere_material));
|
||||
} else if (choose_mat < 0.95) {
|
||||
// metal
|
||||
auto albedo = colour::random(0.5, 1);
|
||||
auto fuzz = randomDouble(0, 0.5);
|
||||
sphere_material = make_shared<metal>(albedo, fuzz);
|
||||
world.add(make_shared<sphere>(center, 0.2, sphere_material));
|
||||
|
||||
auto center2 = center + vec3(0, randomDouble(0,.5), 0);
|
||||
world.add(make_shared<sphere>(center, center2, 0.2, sphere_material));
|
||||
} else {
|
||||
// glass
|
||||
sphere_material = make_shared<dielectric>(1.5);
|
||||
world.add(make_shared<sphere>(center, 0.2, sphere_material));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto material1 = make_shared<dielectric>(1.5);
|
||||
world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, material1));
|
||||
|
||||
auto material2 = make_shared<lambertian>(colour(0.4, 0.2, 0.1));
|
||||
world.add(make_shared<sphere>(point3(-4, 1, 0), 1.0, material2));
|
||||
|
||||
auto material3 = make_shared<metal>(colour(0.7, 0.6, 0.5), 0.0);
|
||||
world.add(make_shared<sphere>(point3(4, 1, 0), 1.0, material3));
|
||||
|
||||
// Camera.
|
||||
camera cam;
|
||||
cam.aspectRatio = 16.0 / 9.0;
|
||||
cam.imageWidth = 1200;
|
||||
cam.samplesPerPixel = 500;
|
||||
cam.imageWidth = 400;
|
||||
cam.samplesPerPixel = 100;
|
||||
cam.maxDepth = 50;
|
||||
|
||||
cam.vFieldOfView = 90;
|
||||
cam.lookFrom = point3(-2,2,1);
|
||||
cam.lookAt = point3(0,0,-1);
|
||||
cam.vFieldOfView = 20;
|
||||
cam.lookFrom = point3(13,2,3);
|
||||
cam.lookAt = point3(0,0,0);
|
||||
cam.vUp = vec3(0,1,0);
|
||||
|
||||
cam.defocusAngle = 0.6;
|
||||
cam.focusDistance = 3.4;
|
||||
cam.focusDistance = 10;
|
||||
|
||||
cam.render(world);
|
||||
|
||||
|
Binary file not shown.
BIN
build/Raytracer
BIN
build/Raytracer
Binary file not shown.
4
camera.h
4
camera.h
@@ -99,8 +99,8 @@ class camera {
|
||||
|
||||
auto rayOrigin = (defocusAngle <= 0) ? centre : defocusDiskSample();
|
||||
auto rayDirection = pixelSample - rayOrigin;
|
||||
|
||||
return ray(rayOrigin, rayDirection);
|
||||
auto rayTime = randomDouble();
|
||||
return ray(rayOrigin, rayDirection, rayTime);
|
||||
}
|
||||
|
||||
vec3 sampleSquare() const {
|
||||
|
@@ -28,7 +28,7 @@ class lambertian : public material {
|
||||
// Catch degenerate scatter direction.
|
||||
if (scatterDirection.nearZero()) scatterDirection = rec.normal;
|
||||
|
||||
scattered = ray(rec.p, scatterDirection);
|
||||
scattered = ray(rec.p, scatterDirection, rIn.time());
|
||||
attenuation = albedo;
|
||||
return true;
|
||||
}
|
||||
@@ -45,7 +45,7 @@ class metal : public material {
|
||||
bool scatter(const ray& rIn, const hitRecord& rec, colour& attenuation, ray& scattered) const override {
|
||||
vec3 reflected = reflect(rIn.direction(), rec.normal);
|
||||
reflected = unitVector(reflected) + (fuzz * randomUnitVector());
|
||||
scattered = ray(rec.p, reflected);
|
||||
scattered = ray(rec.p, reflected, rIn.time());
|
||||
attenuation = albedo;
|
||||
return (dot(scattered.direction(), rec.normal) > 0);
|
||||
}
|
||||
@@ -82,7 +82,7 @@ class dielectric : public material {
|
||||
direction = refract(unitDirection, rec.normal, ri);
|
||||
}
|
||||
|
||||
scattered = ray(rec.p, direction);
|
||||
scattered = ray(rec.p, direction, rIn.time());
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
7
ray.h
7
ray.h
@@ -7,12 +7,15 @@ class ray {
|
||||
private:
|
||||
point3 orig;
|
||||
vec3 dir;
|
||||
double tm;
|
||||
|
||||
public:
|
||||
ray() {}
|
||||
|
||||
ray(const point3& origin, const vec3& direction) : orig(origin), dir(direction) {}
|
||||
|
||||
ray(const point3& origin, const vec3& direction, double time) : orig(origin), dir(direction), tm(time) {}
|
||||
|
||||
const point3& origin() const {
|
||||
return orig;
|
||||
}
|
||||
@@ -21,6 +24,10 @@ class ray {
|
||||
return dir;
|
||||
}
|
||||
|
||||
double time() const {
|
||||
return tm;
|
||||
}
|
||||
|
||||
point3 at(double t) const {
|
||||
return orig + t * dir;
|
||||
}
|
||||
|
19
sphere.h
19
sphere.h
@@ -6,15 +6,28 @@
|
||||
|
||||
class sphere : public hittable {
|
||||
private:
|
||||
point3 centre;
|
||||
point3 centre1;
|
||||
double radius;
|
||||
shared_ptr<material> mat;
|
||||
bool isMoving;
|
||||
vec3 centreVec;
|
||||
|
||||
point3 sphereCentre(double time) const {
|
||||
// Linearly interpolate from centre1 to centre2 accoedingf to time, where t=0 yields centre1 and t=1 yields centre2.
|
||||
return centre1 + time * centreVec;
|
||||
}
|
||||
|
||||
public:
|
||||
sphere(const point3& centre, double radius, shared_ptr<material> mat)
|
||||
: centre(centre), radius(fmax(0,radius)), mat(mat) {}
|
||||
// Stationary sphere.
|
||||
sphere(const point3& centre, double radius, shared_ptr<material> mat) : centre1(centre), radius(fmax(0, radius)), mat(mat), isMoving(false) {}
|
||||
|
||||
// Moving sphere.
|
||||
sphere(const point3& centre1, const point3& centre2, double radius, shared_ptr<material> mat) : centre1(centre1), radius(fmax(0, radius)), mat(mat), isMoving(true) {
|
||||
centreVec = centre2 - centre1;
|
||||
}
|
||||
|
||||
bool hit(const ray& r, interval rayT, hitRecord& rec) const override {
|
||||
point3 centre = isMoving ? sphereCentre(r.time()) : centre1;
|
||||
vec3 oc = centre - r.origin();
|
||||
auto a = r.direction().lengthSquared();
|
||||
auto h = dot(r.direction(), oc);
|
||||
|
Reference in New Issue
Block a user