Skip to content
Merged

. #24

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,18 @@ graph TD;
SCENE["🎬 Scene"];
CAMERA["📷 Camera"];
SCENE_PARSER["📄 SceneParser"];
LIGHT["💡 Light"];
end

subgraph "External Dependency"
YAML_CPP["📄 yaml-cpp"];
end

SCENE --> CAMERA;
SCENE --> LIGHT;

SCENE_PARSER --> CAMERA;
SCENE_PARSER --> YAML_CPP;
SCENE_PARSER --> SCENE;

```
12 changes: 5 additions & 7 deletions src/include/Prism/core/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ class PRISM_EXPORT Color {
*/
Color(const Color& other);

Color operator* (Color other) const;
Color operator*(Color other) const;

Color operator* (double scalar) const;

Color operator+ (Color other) const;
Color operator*(double scalar) const;

Color operator+= (Color other);

Color& clamp();
Color operator+(Color other) const;

Color operator+=(Color other);

Color& clamp();

double r; ///< Red component of the color (0.0 to 1.0)
double g; ///< Green component of the color (0.0 to 1.0)
Expand Down
5 changes: 2 additions & 3 deletions src/include/Prism/core/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ Vector3 PRISM_EXPORT refract(const Vector3& uv, const Vector3& n, double etai_ov

/**
* @brief Calcs the reflectance using Schlick's approximation.
* This function computes the reflectance based on the angle of incidence and the index of refraction
* using Schlick's approximation.
* This function computes the reflectance based on the angle of incidence and the index of
* refraction using Schlick's approximation.
* @param cosine The cosine of the angle between the incident vector and the normal.
* @param ref_idx The index of refraction of the material.
* @return The reflectance value as a double, which is the probability of reflection at the
Expand All @@ -65,7 +65,6 @@ Vector3 PRISM_EXPORT refract(const Vector3& uv, const Vector3& n, double etai_ov
*/
double PRISM_EXPORT schlick(double cosine, double ref_idx);


} // namespace Prism

#endif // PRISM_UTILS_HPP_
4 changes: 2 additions & 2 deletions src/include/Prism/objects/ObjReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

#include "Prism/objects/Colormap.hpp"

#include <algorithm>
#include <array>
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>

namespace Prism {

Expand All @@ -27,7 +27,7 @@ class ObjReader {
std::vector<std::array<double, 3>> vertices;
std::vector<std::array<double, 3>> normals;
std::vector<FaceIndices> faces;

ObjReader(const std::string& filename) {
curMaterial = std::make_shared<Material>();

Expand Down
4 changes: 2 additions & 2 deletions src/include/Prism/objects/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include "Prism/objects/triangle.hpp"

#include <array>
#include <filesystem>
#include <memory>
#include <vector>
#include <filesystem>

namespace Prism {

Expand Down Expand Up @@ -59,7 +59,7 @@ class PRISM_EXPORT Mesh : public Object {
void setMaterial(std::shared_ptr<Material> new_material);

private:
std::vector<std::shared_ptr<Point3>> points; ///< Points that define the vertices of the mesh
std::vector<std::shared_ptr<Point3>> points; ///< Points that define the vertices of the mesh
std::vector<std::shared_ptr<Vector3>> normals; ///< Normals for each vertex in the mesh
std::vector<MeshTriangle>
mesh; ///< Triangles that make up the mesh, each defined by three points
Expand Down
6 changes: 3 additions & 3 deletions src/include/Prism/objects/triangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ class PRISM_EXPORT MeshTriangle {
* @param n3 The normal vector at the third point.
* This constructor initializes the MeshTriangle with the specified points.
*/
MeshTriangle(std::shared_ptr<Point3> p1, std::shared_ptr<Point3> p2,
std::shared_ptr<Point3> p3, std::shared_ptr<Vector3> n1,
std::shared_ptr<Vector3> n2, std::shared_ptr<Vector3> n3);
MeshTriangle(std::shared_ptr<Point3> p1, std::shared_ptr<Point3> p2, std::shared_ptr<Point3> p3,
std::shared_ptr<Vector3> n1, std::shared_ptr<Vector3> n2,
std::shared_ptr<Vector3> n3);

/**
* @brief Constructs a MeshTriangle given three Point3 objects.
Expand Down
12 changes: 7 additions & 5 deletions src/include/Prism/scene/light.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#define PRISM_LIGHT_HPP_

#include "prism_export.h"
#include "Prism/core/point.hpp"

#include "Prism/core/color.hpp"
#include "Prism/core/point.hpp"

namespace Prism {

Expand All @@ -12,9 +13,10 @@ namespace Prism {
* @brief Represents a point light source in the scene.
*/
class PRISM_EXPORT Light {
public:
public:
Point3 position; ///< The position of the light in 3D space.
Color color; ///< The color of the light, typically used to determine how it illuminates objects.
Color
color; ///< The color of the light, typically used to determine how it illuminates objects.

/**
* @brief Default constructor that initializes the light with default values.
Expand All @@ -23,8 +25,8 @@ class PRISM_EXPORT Light {
* - Color: White (1, 1, 1)
* - Intensity: 1.0
*/
Light(const Point3& pos, const Color& col)
: position(pos), color(col) {}
Light(const Point3& pos, const Color& col) : position(pos), color(col) {
}
};

} // namespace Prism
Expand Down
6 changes: 3 additions & 3 deletions src/include/Prism/scene/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

#include "prism_export.h"

#include "Prism/core/color.hpp"
#include "Prism/objects/objects.hpp"
#include "Prism/scene/camera.hpp"
#include "Prism/scene/light.hpp"
#include "Prism/core/color.hpp"

#include <filesystem>
#include <memory>
Expand Down Expand Up @@ -66,8 +66,8 @@ class PRISM_EXPORT Scene {
Color trace(const Ray& ray, int depth) const;

std::vector<std::unique_ptr<Object>> objects_; ///< Collection of objects in the scene
std::vector<std::unique_ptr<Light>> lights_; ///< Collection of light sources in the scene
Color ambient_color_ = Color(0.1, 0.1, 0.1); ///< Ambient color for the scene
std::vector<std::unique_ptr<Light>> lights_; ///< Collection of light sources in the scene
Color ambient_color_ = Color(0.1, 0.1, 0.1); ///< Ambient color for the scene
Camera camera_; ///< The camera used to view the scene
};
} // namespace Prism
Expand Down
9 changes: 5 additions & 4 deletions src/src/core/color.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Prism/core/color.hpp"

#include <algorithm>

namespace Prism {
Expand Down Expand Up @@ -34,10 +35,10 @@ Color Color::operator+=(Color other) {
}

Color& Color::clamp() {
r = std::max(0.0,std::min(r, 1.0));
g = std::max(0.0,std::min(g, 1.0));
b = std::max(0.0,std::min(b, 1.0));
r = std::max(0.0, std::min(r, 1.0));
g = std::max(0.0, std::min(g, 1.0));
b = std::max(0.0, std::min(b, 1.0));

return *this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/src/core/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Vector3 refract(const Vector3& uv, const Vector3& n, double etai_over_etat) {
if (sqr(r_out_perp.magnitude()) > 1.0) {
return Vector3(0, 0, 0);
}

return r_out_perp + r_out_parallel;
}

Expand Down
23 changes: 7 additions & 16 deletions src/src/objects/mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Prism/objects/mesh.hpp"

#include "Prism/core/matrix.hpp"

#include <cmath>

namespace Prism {
Expand All @@ -19,14 +20,9 @@ Mesh::Mesh(std::filesystem::path& path) {
}

for (auto& face : reader.faces) {
mesh.emplace_back(
points[face.vertex_indices[0]],
points[face.vertex_indices[1]],
points[face.vertex_indices[2]],
normals[face.normal_indices[0]],
normals[face.normal_indices[1]],
normals[face.normal_indices[2]]
);
mesh.emplace_back(points[face.vertex_indices[0]], points[face.vertex_indices[1]],
points[face.vertex_indices[2]], normals[face.normal_indices[0]],
normals[face.normal_indices[1]], normals[face.normal_indices[2]]);
}
};

Expand All @@ -41,14 +37,9 @@ Mesh::Mesh(ObjReader& reader) : material(std::move(reader.curMaterial)) {
}

for (auto& face : reader.faces) {
mesh.emplace_back(
points[face.vertex_indices[0]],
points[face.vertex_indices[1]],
points[face.vertex_indices[2]],
normals[face.normal_indices[0]],
normals[face.normal_indices[1]],
normals[face.normal_indices[2]]
);
mesh.emplace_back(points[face.vertex_indices[0]], points[face.vertex_indices[1]],
points[face.vertex_indices[2]], normals[face.normal_indices[0]],
normals[face.normal_indices[1]], normals[face.normal_indices[2]]);
}
};

Expand Down
7 changes: 4 additions & 3 deletions src/src/objects/triangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ bool Triangle::hit(const Ray& ray, double t_min, double t_max, HitRecord& rec) c
MeshTriangle::MeshTriangle(std::shared_ptr<Point3> p1, std::shared_ptr<Point3> p2,
std::shared_ptr<Point3> p3, std::shared_ptr<Vector3> n1,
std::shared_ptr<Vector3> n2, std::shared_ptr<Vector3> n3)
: point1(std::move(p1)), point2(std::move(p2)), point3(std::move(p3)),
normal1(std::move(n1)), normal2(std::move(n2)), normal3(std::move(n3)) {
: point1(std::move(p1)), point2(std::move(p2)), point3(std::move(p3)), normal1(std::move(n1)),
normal2(std::move(n2)), normal3(std::move(n3)) {
}

MeshTriangle::MeshTriangle(Point3 p1, Point3 p2, Point3 p3)
Expand Down Expand Up @@ -127,7 +127,8 @@ bool MeshTriangle::hit(const Ray& ray, double t_min, double t_max, HitRecord& re

if (t > t_min && t < t_max) {

const double w = 1.0 - u - v;;
const double w = 1.0 - u - v;
;
rec.t = t;
rec.normal = ((*normal1 * w) + (*normal2 * u) + (*normal3 * v)).normalize();
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/src/scene/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ Color Scene::trace(const Ray& ray, int depth) const {
refraction_color = trace(refracted_ray, depth - 1);
}

Color trasmited_color = reflection_color * reflectance + refraction_color * (1.0 - reflectance);
Color trasmited_color =
reflection_color * reflectance + refraction_color * (1.0 - reflectance);
final_color = final_color * opacity + trasmited_color * (1.0 - opacity);
}
else if (mat->ks.r > 0 || mat->ks.g > 0 || mat->ks.b > 0) {
} else if (mat->ks.r > 0 || mat->ks.g > 0 || mat->ks.b > 0) {
Vector3 reflect_dir = ray.direction() - rec.normal * 2 * ray.direction().dot(rec.normal);
Ray reflection_ray(rec.p, reflect_dir);
final_color += mat->ks * trace(reflection_ray, depth - 1);
Expand Down
3 changes: 1 addition & 2 deletions src/src/scene/scene_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ Scene SceneParser::parse() {
Color color(color_vec.x, color_vec.y, color_vec.z);
scene.addLight(std::make_unique<Light>(pos, color));
}
}
else {
} else {
Style::logWarning("'lights' node not found or is not a list. No lights will be added.");
}

Expand Down