Skip to content

Commit

Permalink
empty cornell box
Browse files Browse the repository at this point in the history
  • Loading branch information
alysssssa committed May 8, 2023
1 parent bb46e29 commit 4b8398a
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 4 deletions.
87 changes: 87 additions & 0 deletions raytracer/aarect.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,46 @@ class xy_rect : public hittable {
double x0, x1, y0, y1, k;
};

class xz_rect : public hittable {
public:
xz_rect() {}

xz_rect(double _x0, double _x1, double _z0, double _z1, double _k, shared_ptr<material> mat) : x0(_x0), x1(_x1), z0(_z0), z1(_z1), k(_k), mp(mat) {};

virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const override;

virtual bool bounding_box(double time0, double time1, aabb& output_box) const override {
// The bounding box must have non-zero width in each dimension, so pad the Y
// dimension a small amount.
output_box = aabb(point3(x0, k-0.0001, z0), point3(x1, k+0.0001, z1));
return true;
}

public:
shared_ptr<material> mp;
double x0, x1, z0, z1, k;
};

class yz_rect : public hittable {
public:
yz_rect() {}

yz_rect(double _y0, double _y1, double _z0, double _z1, double _k, shared_ptr<material> mat) : y0(_y0), y1(_y1), z0(_z0), z1(_z1), k(_k), mp(mat) {};

virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const override;

virtual bool bounding_box(double time0, double time1, aabb& output_box) const override {
// The bounding box must have non-zero width in each dimension, so pad the X
// dimension a small amount.
output_box = aabb(point3(k-0.0001, y0, z0), point3(k+0.0001, y1, z1));
return true;
}

public:
shared_ptr<material> mp;
double y0, y1, z0, z1, k;
};

bool xy_rect::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
auto t = (k - r.origin().z()) / r.direction().z();

Expand All @@ -49,5 +89,52 @@ bool xy_rect::hit(const ray& r, double t_min, double t_max, hit_record& rec) con
return true;
}

bool xz_rect::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
auto t = (k - r.origin().y()) / r.direction().y();

if (t < t_min || t > t_max)
return false;

auto x = r.origin().x() + t*r.direction().x();
auto z = r.origin().z() + t*r.direction().z();

if (x < x0 || x > x1 || z < z0 || z > z1)
return false;

rec.u = (x-x0) / (x1-x0);
rec.v = (z-z0) / (z1-z0);
rec.t = t;

auto outward_normal = vec3(0, 1, 0);
rec.set_face_normal(r, outward_normal);
rec.mat_ptr = mp;
rec.p = r.at(t);

return true;
}

bool yz_rect::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
auto t = (k - r.origin().x()) / r.direction().x();

if (t < t_min || t > t_max)
return false;

auto y = r.origin().y() + t*r.direction().y();
auto z = r.origin().z() + t*r.direction().z();

if (y < y0 || y > y1 || z < z0 || z > z1)
return false;

rec.u = (y-y0) / (y1-y0);
rec.v = (z-z0) / (z1-z0);
rec.t = t;

auto outward_normal = vec3(1, 0, 0);
rec.set_face_normal(r, outward_normal);
rec.mat_ptr = mp;
rec.p = r.at(t);

return true;
}

#endif // AARECT_H
35 changes: 31 additions & 4 deletions raytracer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,32 @@ hittable_list simple_light() {
return objects;
}

hittable_list cornell_box() {
hittable_list objects;

auto red = make_shared<lambertian>(colour(.65, .05, .05));
auto white = make_shared<lambertian>(colour(.73, .73, .73));
auto green = make_shared<lambertian>(colour(.12, .45, .15));
auto light = make_shared<diffuse_light>(colour(15, 15, 15));

objects.add(make_shared<yz_rect>(0, 555, 0, 555, 555, green));
objects.add(make_shared<yz_rect>(0, 555, 0, 555, 0, red));
objects.add(make_shared<xz_rect>(213, 343, 227, 332, 554, light));
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 0, white));
objects.add(make_shared<xz_rect>(0, 555, 0, 555, 555, white));
objects.add(make_shared<xy_rect>(0, 555, 0, 555, 555, white));

return objects;
}

int main() {

// write output image in ppm
const auto aspect_ratio = 16.0 / 9.0;
const int image_width = 400;
const auto aspect_ratio = 1.0;
// const auto aspect_ratio = 16.0 / 9.0;
const int image_width = 600;
const int image_height = static_cast<int>(image_width / aspect_ratio);
const int samples_per_pixel = 400;
const int samples_per_pixel = 200;
const int max_depth = 50;

// world
Expand Down Expand Up @@ -183,14 +202,22 @@ int main() {
vfov = 20.0;
break;

default:
case 5:
background = colour(0,0,0);
world = simple_light();
lookfrom = point3(26,3,6);
lookat = point3(0,2,0);
vfov = 20.0;
break;

default:
case 6:
world = cornell_box();
background = colour(0,0,0);
lookfrom = point3(278, 278, -800);
lookat = point3(278, 278, 0);
vfov = 40.0;
break;
}

// camera with depth of field
Expand Down

0 comments on commit 4b8398a

Please sign in to comment.