Skip to content

Commit

Permalink
Fix bug with the setCamera function #20
Browse files Browse the repository at this point in the history
Use flat camera parameters in the scene config instead of nested.
  • Loading branch information
smercer10 committed Feb 28, 2024
1 parent 199537f commit 387fbd6
Showing 1 changed file with 34 additions and 42 deletions.
76 changes: 34 additions & 42 deletions src/config.cpp
Original file line number Diff line number Diff line change
@@ -1,62 +1,56 @@
#include "raydiance/config.h"
#include "raydiance/sphere.h"
#include <iostream>

#include <memory>

// TODO: Better error handling
void config::setCamera(const nlohmann::json &j, camera &cam) {
if (j.contains("camera")) {
auto cameraConfig{j["camera"]};

if (cameraConfig.contains("aspectRatio")) {
cam.setAspectRatio(cameraConfig["aspectRatio"]);
}
if (cameraConfig.contains("imgWidth")) {
cam.setImgWidth(cameraConfig["imgWidth"]);
}
if (cameraConfig.contains("samplesPerPixel")) {
cam.setSamplesPerPixel(cameraConfig["samplesPerPixel"]);
}
if (cameraConfig.contains("maxDepth")) {
cam.setMaxDepth(cameraConfig["maxDepth"]);
}
if (cameraConfig.contains("fieldOfView")) {
cam.setFieldOfView(cameraConfig["fieldOfView"]);
}
if (cameraConfig.contains("lookFrom")) {
cam.setLookFrom(point3{cameraConfig["lookFrom"]["x"], cameraConfig["lookFrom"]["y"], cameraConfig["lookFrom"]["z"]});
}
if (cameraConfig.contains("lookAt")) {
cam.setLookAt(point3{cameraConfig["lookAt"]["x"], cameraConfig["lookAt"]["y"], cameraConfig["lookAt"]["z"]});
}
if (cameraConfig.contains("cameraUp")) {
cam.setCameraUp(vec3{cameraConfig["cameraUp"]["x"], cameraConfig["cameraUp"]["y"], cameraConfig["cameraUp"]["z"]});
}
if (cameraConfig.contains("defocusAngle")) {
cam.setDefocusAngle(cameraConfig["defocusAngle"]);
}
if (cameraConfig.contains("focusDistance")) {
cam.setFocusDistance(cameraConfig["focusDistance"]);
}
} else {
std::cout << "No camera configuration found, using default values.\n";
if (j.contains("aspectRatio")) {
cam.setAspectRatio(j["aspectRatio"]);
}
if (j.contains("imgWidth")) {
cam.setImgWidth(j["imgWidth"]);
}
if (j.contains("samplesPerPixel")) {
cam.setSamplesPerPixel(j["samplesPerPixel"]);
}
if (j.contains("maxDepth")) {
cam.setMaxDepth(j["maxDepth"]);
}
if (j.contains("fieldOfView")) {
cam.setFieldOfView(j["fieldOfView"]);
}
if (j.contains("lookFrom")) {
cam.setLookFrom(point3{j["lookFrom"]["x"], j["lookFrom"]["y"], j["lookFrom"]["z"]});
}
if (j.contains("lookAt")) {
cam.setLookAt(point3{j["lookAt"]["x"], j["lookAt"]["y"], j["lookAt"]["z"]});
}
if (j.contains("cameraUp")) {
cam.setCameraUp(vec3{j["cameraUp"]["x"], j["cameraUp"]["y"], j["cameraUp"]["z"]});
}
if (j.contains("defocusAngle")) {
cam.setDefocusAngle(j["defocusAngle"]);
}
if (j.contains("focusDistance")) {
cam.setFocusDistance(j["focusDistance"]);
}
}

// TODO: Better error handling
void config::addObjects(const nlohmann::json &j, scene &world) {
if (j.contains("spheres")) {
for (const auto &sphere: j["spheres"]) {
point3 center{sphere["center"]["x"], sphere["center"]["y"], sphere["center"]["z"]};
point3 centre{sphere["centre"]["x"], sphere["centre"]["y"], sphere["centre"]["z"]};
double radius{sphere["radius"]};
std::shared_ptr<material> mat;

if (sphere["material"]["type"] == "lambertian") {
colour albedo{sphere["material"]["albedo"]["x"], sphere["material"]["albedo"]["y"], sphere["material"]["albedo"]["z"]};
colour albedo{sphere["material"]["colour"]["r"], sphere["material"]["colour"]["g"], sphere["material"]["colour"]["b"]};
mat = std::make_shared<lambertian>(albedo);

} else if (sphere["material"]["type"] == "metal") {
colour albedo{sphere["material"]["albedo"]["x"], sphere["material"]["albedo"]["y"], sphere["material"]["albedo"]["z"]};
colour albedo{sphere["material"]["colour"]["r"], sphere["material"]["colour"]["g"], sphere["material"]["colour"]["b"]};
double fuzz{sphere["material"]["fuzz"]};
mat = std::make_shared<metal>(albedo, fuzz);

Expand All @@ -65,9 +59,7 @@ void config::addObjects(const nlohmann::json &j, scene &world) {
mat = std::make_shared<dielectric>(refIdx);
}

world.add(std::make_shared<class sphere>(center, radius, mat));
world.add(std::make_shared<class sphere>(centre, radius, mat));
}
} else {
std::cout << "No spheres found in the configuration file. Was this intended?\n";
}
}

0 comments on commit 387fbd6

Please sign in to comment.