Skip to content

Commit

Permalink
Changed setup of modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenz Reithmayr committed May 10, 2024
1 parent 0e28d62 commit 6a4b0e0
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 94 deletions.
48 changes: 28 additions & 20 deletions include/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@
namespace cannoli {
class Camera {
public:
Camera(float ar, float f_length, float vert_fov, PointXYZ origin, Vec3f view_dir, Vec3f vec_up) :
m_aspectRatio(ar),
m_focalLength(f_length),
m_verticalFOV(vert_fov),
m_origin(origin),
m_viewDir(view_dir),
m_vecUp(vec_up) {
InitializeCameraParams();
}
Camera() = default;

void InitializeCameraParams() {
void initialize() {
float theta = m_verticalFOV * 0.017453;
float h = tan(theta * 0.5);
m_viewportHeight = 2.0 * h;
Expand All @@ -33,38 +25,54 @@ class Camera {
m_viewportLowerLeft = m_origin - m_horizontal * 0.5 - m_vertical * 0.5 - w;
}

[[nodiscard]] PointXYZ GetOrigin() const {
void setAspectRatio(float aspect_ratio) {
m_aspectRatio = aspect_ratio;
}

void setFocalLength(float focal_length) {
m_focalLength = focal_length;
}

void setVerticalFOV(float vert_fov) {
m_verticalFOV = vert_fov;
}

[[nodiscard]] PointXYZ getOrigin() const {
return m_origin;
}

void SetOrigin(const PointXYZ &origin) {
void setOrigin(const PointXYZ &origin) {
m_origin = origin;
}

[[nodiscard]] Vec3f GetViewDirection() const {
[[nodiscard]] Vec3f getViewDirection() const {
return m_viewDir;
}

void SetViewDirection(const Vec3f &view_dir) {
void setViewDirection(const Vec3f &view_dir) {
m_viewDir = view_dir;
}

[[nodiscard]] PointXYZ GetViewportLLC() const {
void setUp(Vec3f up){
m_vecUp = up;
}

[[nodiscard]] PointXYZ getViewportLLC() const {
return m_viewportLowerLeft;
}

[[nodiscard]] Vec3f GetHorizontal() const {
[[nodiscard]] Vec3f getHorizontal() const {
return m_horizontal;
}

[[nodiscard]] Vec3f GetVertical() const {
[[nodiscard]] Vec3f getVertical() const {
return m_vertical;
}

private:
const float m_aspectRatio;
const float m_focalLength;
const float m_verticalFOV;
float m_aspectRatio{};
float m_focalLength{};
float m_verticalFOV{};

float m_viewportHeight{};
float m_viewportWidth{};
Expand Down
33 changes: 27 additions & 6 deletions include/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,33 @@
#define CANNOLI_INCLUDE_CANVAS_H_

namespace cannoli {
struct Canvas {
Canvas(float ar, int w) : aspect_ratio(ar), width(w) {};
class Canvas {
public:
Canvas() = default;
void initialize() {
m_height = static_cast<int>(m_width / m_aspectRatio);
}

const float aspect_ratio;
const int width;
const int height = static_cast<int>(width / aspect_ratio);
void setAspectRation(float aspect_ratio) {
m_aspectRatio = aspect_ratio;
}

void setWidth(float width) {
m_width = width;
}

[[nodiscard]] int getHeight() const {
return m_height;
}

[[nodiscard]] int getWidth() const {
return m_width;
}

private:
float m_aspectRatio{};
int m_width{};
int m_height{};
};
}
} // namespace cannoli
#endif //CANNOLI_INCLUDE_CANVAS_H_
25 changes: 22 additions & 3 deletions include/ray_tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,35 @@ class RayTracer {
static LightRay EmitRay(const Vec3f &origin, const Vec3f &direction);
static cannoli::ColorRGB PaintBackground(cannoli::LightRay &ray);

void loadScene(Scene scene) {
m_scene = std::move(scene);
}
void setCamera(const Camera &camera) {
m_camera = camera;
}
void setCanvas(const Canvas &canvas) {
m_canvas = canvas;
}
void setOutputPath(std::string &out_fn) {
m_outFile = out_fn;
}
void setSamples(int samples) {
m_samples = samples;
}
void setMaxBounces(int max_bounces) {
m_maxBounces = max_bounces;
}

private:
std::ofstream m_ppmImage{};
Scene m_scene;
Canvas m_canvas;
Camera m_camera;
ColorRGB m_pixelColor{};
bool m_stopTrace;
bool m_stopTrace{false};
std::string m_outFile;
int m_samples;
int m_maxBounces;
int m_samples{5};
int m_maxBounces{5};

bool m_hitBackground{false};
};
Expand Down
34 changes: 32 additions & 2 deletions include/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CANNOLI_INCLUDE_SCENE_H_

#include "mesh.h"
#include "metal_brdf.h"

#include <vector>
#include <memory>
Expand All @@ -11,11 +12,40 @@ class Scene {
public:
Scene() = default;

void Add(const std::shared_ptr<Mesh> &mesh) {
void load(std::string &path) {
// Load all meshes from the .obj file
objl::Loader loader;
bool loadout = loader.LoadFile(path);
if (loadout) {
for (auto &msh : loader.LoadedMeshes) {
float rand = cannoli::random_float(0.1, 1.0);
auto random_color = cannoli::ColorRGB::random(0.2, 0.9);
auto random_fuzz = cannoli::random_float(0.2, 0.9);

if (rand > 0.5) {
std::shared_ptr<cannoli::Material> mat = std::make_shared<cannoli::LambertianBRDF>(random_color);
cannoli::Mesh mesh(msh, mat);
addMesh(std::make_shared<cannoli::Mesh>(mesh));
} else {
std::shared_ptr<cannoli::Material> mat = std::make_shared<cannoli::MetalBRDF>(random_color, random_fuzz);
cannoli::Mesh mesh(msh, mat);
addMesh(std::make_shared<cannoli::Mesh>(mesh));
}

fmt::print(" Random Color: {} \n", random_color);
fmt::print(" Random Fuzz: {} \n", random_fuzz);
}

} else {
fmt::print(stderr, "Failed to load mesh!\n");
}
};

void addMesh(const std::shared_ptr<Mesh> &mesh) {
m_meshesInScene.emplace_back(mesh);
}

[[nodiscard]] std::vector<std::shared_ptr<Mesh>> GetMeshesInScene() const {
[[nodiscard]] std::vector<std::shared_ptr<Mesh>> getMeshesInScene() const {
return m_meshesInScene;
};

Expand Down
5 changes: 3 additions & 2 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <fmt/format.h>
#include <cmath>
#include <limits>
#include <numbers>
#include <random>
#include <iostream>

Expand All @@ -26,13 +27,13 @@ void progressBar(auto progress) {
}
}
std::cout << "] " << static_cast<int>(progress * 100.0) << " %\r";
std::cout.flush();
// std::cout.flush();
}

// Constants

const float infinity = std::numeric_limits<double>::infinity();
const float pi = 3.1415926535897932385;
const float pi = std::numbers::pi;

// Utility Functions

Expand Down
108 changes: 47 additions & 61 deletions src/cannoli_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,74 +5,60 @@
#include <iostream>
#include <cmath>
#include <memory>
#include <chrono>
#include <ratio>
#include <fmt/chrono.h>

cannoli::Scene LoadScene(std::string &fpath) {
cannoli::Scene scene;

// Load all meshes from the .obj file
objl::Loader loader;
bool loadout = loader.LoadFile(fpath);
if (loadout) {
for (auto &msh : loader.LoadedMeshes) {
float rand = cannoli::random_float(0.1, 1.0);
auto random_color = cannoli::ColorRGB::random(0.2, 0.9);
auto random_fuzz = cannoli::random_float(0.2, 0.9);

if (rand > 0.5) {
std::shared_ptr<cannoli::Material> mat = std::make_shared<cannoli::LambertianBRDF>(random_color);
cannoli::Mesh mesh(msh, mat);
scene.Add(std::make_shared<cannoli::Mesh>(mesh));
} else {
std::shared_ptr<cannoli::Material> mat = std::make_shared<cannoli::MetalBRDF>(random_color, random_fuzz);
cannoli::Mesh mesh(msh, mat);
scene.Add(std::make_shared<cannoli::Mesh>(mesh));
}

fmt::print(" Random Color: {} \n", random_color);
fmt::print(" Random Fuzz: {} \n", random_fuzz);
}

} else {
fmt::print(stderr, "Failed to load mesh!\n");
}
return scene;
}

// Camera parameters
constexpr float ASPECT_RATIO = 3.0 / 2.0;
constexpr float FOCAL_LENGTH = 1.0;
constexpr float VFOV = 30.0;
int main() {
// Camera parameters
constexpr float ASPECT_RATIO = 3.0 / 2.0;
constexpr float FOCAL_LENGTH = 1.0;
constexpr float VFOV = 30.0;

// Canvas parameters
constexpr int CANVAS_WIDTH = 800;
// Canvas parameters
constexpr int CANVAS_WIDTH = 800;

// Ray tracer parameters
constexpr int SAMPLES = 300;
constexpr int MAX_BOUNCES = 8;
// Ray tracer parameters
constexpr int SAMPLES = 30;
constexpr int MAX_BOUNCES = 4;

int main() {
std::string obj_fpath = "../models/blocks.obj";
// Scene parameters
std::string scene_name = "blocks.obj";
std::string scene_path = fmt::format("../models/{}", scene_name);
std::string out_path = fmt::format("../images/cannoli_output_{}_samples-{}_maxBounces-{}.ppm", scene_name, SAMPLES, MAX_BOUNCES);

#if AABB_INT
// std::string out_path = "../images/triIndex_output_bbox.ppm";
std::string out_path = fmt::format("../images/cannoli_output_samples-{}_maxBounces-{}.ppm", SAMPLES, MAX_BOUNCES);
#else
std::string out_path = "../images/triIndex_output.ppm";
#endif
// Initialize the camera
cannoli::Camera camera;
camera.setAspectRatio(ASPECT_RATIO);
camera.setFocalLength(FOCAL_LENGTH);
camera.setVerticalFOV(VFOV);
camera.setOrigin(cannoli::PointXYZ(20, 10, 10));
camera.setViewDirection(cannoli::PointXYZ(0, 3, 0));
camera.setUp(cannoli::Vec3f(0, 1, 0));
camera.initialize();

// Create a camera and a canvas on which to render the scene
cannoli::Camera camera(ASPECT_RATIO,
FOCAL_LENGTH,
VFOV,
cannoli::PointXYZ(20, 10, 10),
cannoli::PointXYZ(0, 3, 0),
cannoli::Vec3f(0, 1, 0));
// Initialize the canvas
cannoli::Canvas canvas;
canvas.setAspectRation(ASPECT_RATIO);
canvas.setWidth(CANVAS_WIDTH);
canvas.initialize();

cannoli::Canvas canvas(ASPECT_RATIO, CANVAS_WIDTH);
// Construct the scene
cannoli::Scene scene;
scene.load(scene_path);

cannoli::Scene scene = LoadScene(obj_fpath);
// Initialize the ray tracer
cannoli::RayTracer rt;
rt.loadScene(scene);
rt.setCamera(camera);
rt.setCanvas(canvas);
rt.setOutputPath(out_path);
rt.setSamples(SAMPLES);
rt.setMaxBounces(MAX_BOUNCES);

// Pass the scene, camera and canvas to the ray tracer and trace away
cannoli::RayTracer rt(scene, camera, canvas, out_path, SAMPLES, MAX_BOUNCES);
rt.Trace();
const auto start = std::chrono::steady_clock::now();
rt.trace();
const auto end = std::chrono::steady_clock::now();
auto dur = std::chrono::duration<double>(end - start);
fmt::print("Time taken: {} seconds", dur.count());
}

0 comments on commit 6a4b0e0

Please sign in to comment.