Skip to content

Commit

Permalink
Added Cornell Box example
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Jorquera committed Mar 25, 2024
1 parent eb26eb7 commit c976952
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ A simple C++ Raytracer based on "Ray Tracing in One Weekend" book.

![render image with diffuse light](docs/render-light.png)

![render cornell box image](docs/render-cornell-box.png)

## Features

* Raytracing sphere geometries with front face detection.
Expand Down
Binary file added docs/render-cornell-box.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ void cornell_box() {
scene->add(make_shared<Quad>(Point(0,0,0), Vector(555,0,0), Vector(0,0,555), white));
scene->add(make_shared<Quad>(Point(555,555,555), Vector(-555,0,0), Vector(0,0,-555), white));
scene->add(make_shared<Quad>(Point(0,0,555), Vector(555,0,0), Vector(0,555,0), white));

scene->add(createBox(Point(130, 0, 65), Point(295, 165, 230), white));
scene->add(createBox(Point(265, 0, 295), Point(430, 330, 460), white));

Camera camera(1.0, 500, 50, 1920, 40.0, Vector(278, 278, -800), Vector(278, 278, 0), Vector(0.0, 1.0, 0.0), Color(0.0, 0.0, 0.0), 0.0);

Expand Down
22 changes: 22 additions & 0 deletions src/quad.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "intersectable.h"
#include "material.h"
#include "aabb.h"
#include "scene.h"

#include <cmath>

Expand Down Expand Up @@ -64,3 +65,24 @@ class Quad : public Intersectable {
}

};

inline std::shared_ptr<Intersectable> createBox(const Point& a, const Point& b, std::shared_ptr<Material> mat)
{
auto sides = std::make_shared<Scene>();

auto min = Point(std::fmin(a.x(), b.x()), fmin(a.y(), b.y()), fmin(a.z(), b.z()));
auto max = Point(std::fmax(a.x(), b.x()), fmax(a.y(), b.y()), fmax(a.z(), b.z()));

auto dx = Vector(max.x() - min.x(), 0, 0);
auto dy = Vector(0, max.y() - min.y(), 0);
auto dz = Vector(0, 0, max.z() - min.z());

sides->add(std::make_shared<Quad>(Point(min.x(), min.y(), max.z()), dx, dy, mat)); // front
sides->add(std::make_shared<Quad>(Point(max.x(), min.y(), max.z()), -dz, dy, mat)); // right
sides->add(std::make_shared<Quad>(Point(max.x(), min.y(), min.z()), -dx, dy, mat)); // back
sides->add(std::make_shared<Quad>(Point(min.x(), min.y(), min.z()), dz, dy, mat)); // left
sides->add(std::make_shared<Quad>(Point(min.x(), max.y(), max.z()), dx, -dz, mat)); // top
sides->add(std::make_shared<Quad>(Point(min.x(), min.y(), min.z()), dx, dz, mat)); // bottom

return sides;
}

0 comments on commit c976952

Please sign in to comment.