Skip to content
Merged
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
41 changes: 41 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CMake on multiple platforms

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ${{ matrix.config.os }}
strategy:
fail-fast: false
matrix:
preset: [debug, release]

config:
- { os: ubuntu-latest, c_compiler: gcc, cpp_compiler: g++ }
- { os: ubuntu-latest, c_compiler: clang, cpp_compiler: clang++ }
- { os: windows-latest, c_compiler: cl, cpp_compiler: cl }

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set compiler environment variables
shell: bash
run: |
echo "CC=${{ matrix.config.c_compiler }}" >> $GITHUB_ENV
echo "CXX=${{ matrix.config.cpp_compiler }}" >> $GITHUB_ENV

- name: Configure CMake using preset
run: cmake --preset ${{ matrix.preset }}

- name: Build project
run: cmake --build --preset ${{ matrix.preset }}

- name: Run tests
if: matrix.preset == 'debug'
working-directory: build/${{ matrix.preset }}
run: ctest -C debug --output-on-failure
2 changes: 1 addition & 1 deletion libs/Prism/include/Prism/ObjReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ObjReader {
std::vector<std::array<unsigned int, 3>> triangles;


ObjReader(const std::string& filename) : cmap(cmap) {
ObjReader(const std::string& filename) {
file.open(filename);
if (!file.is_open()) {
std::cerr << "Erro ao abrir o arquivo: " << filename << std::endl;
Expand Down
5 changes: 3 additions & 2 deletions libs/Prism/include/Prism/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ namespace Prism {
class PRISM_EXPORT Color {
public:
Color();
Color(float red, float green, float blue);
Color(double red, double green, double blue);
Color(int red, int green, int blue);
Color(const Color& other);
float r, g, b;
double r, g, b;
};

} // namespace Prism
Expand Down
4 changes: 2 additions & 2 deletions libs/Prism/include/Prism/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class PRISM_EXPORT Matrix {
Matrix(const Matrix& m);

// --- Classes aninhadas para acesso ---
class MatrixRow {
class PRISM_EXPORT MatrixRow {
private:
std::vector<double>& m_row_vector;
public:
MatrixRow(std::vector<double>& row_vec);
double& operator[](int col);
};

class ConstMatrixRow {
class PRISM_EXPORT ConstMatrixRow {
private:
const std::vector<double>& m_row_vector;
public:
Expand Down
2 changes: 1 addition & 1 deletion libs/Prism/include/Prism/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PRISM_EXPORT Mesh : public Object {

private:
std::vector<std::shared_ptr<Point3>> points;
std::vector<Triangle<std::shared_ptr<Point3>>> mesh;
std::vector<MeshTriangle> mesh;
std::shared_ptr<Material> material;
};

Expand Down
17 changes: 6 additions & 11 deletions libs/Prism/include/Prism/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@

namespace Prism {

PRISM_EXPORT inline int convert_color(double f) {
return static_cast<int>(255.999 * f);
}

PRISM_EXPORT inline std::ostream& operator<<(std::ostream& os, const Color& color) {
os << static_cast<int>(convert_color(color.r)) << " "
<< static_cast<int>(convert_color(color.g)) << " "
<< static_cast<int>(convert_color(color.b));
return os;
}

PRISM_EXPORT std::filesystem::path generate_filename();

class PRISM_EXPORT Scene {
Expand All @@ -34,6 +23,12 @@ class PRISM_EXPORT Scene {
*/
Scene(Camera camera);

Scene(const Scene&) = delete;
Scene& operator=(const Scene&) = delete;

Scene(Scene&&) = default;
Scene& operator=(Scene&&) = default;

/**
* @brief Adiciona um objeto à cena.
* A posse do objeto é transferida para a cena.
Expand Down
152 changes: 28 additions & 124 deletions libs/Prism/include/Prism/triangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,142 +4,46 @@
#include "prism_export.h"
#include "Prism/objects.hpp"
#include "Prism/point.hpp"
#include "Prism/vector.hpp"
#include "Prism/ray.hpp"
#include "Prism/utils.hpp"
#include "Prism/material.hpp"
#include <cmath>
#include <memory>
#include <initializer_list>

namespace Prism {
namespace Prism {
class Matrix; // Forward declaration for Matrix
class Ray; // Forward declaration for Ray
class Point3; // Forward declaration for Point3
struct HitRecord; // Forward declaration for HitRecord

template <typename Point> class PRISM_EXPORT Triangle : public Object {
static_assert(std::is_same<Point, Point3>::value || std::is_same<Point, Point3&>::value || std::is_same<Point, std::shared_ptr<Point3>>::value, "T must be Point3 , Point3& or std::shared_ptr<Point3>");
class PRISM_EXPORT Triangle : public Object {
public:
Triangle(Point3 p1, Point3 p2, Point3 p3, std::shared_ptr<Material> mat = std::make_shared<Prism::Material>());

public:
Triangle(Point point1, Point point2, Point point3, std ::shared_ptr<Material> material = std::make_shared<Prism::Material>()):
point1(point1),point2(point2),point3(point3),material(std::move(material)){};
Point3 getPoint1() const;
Point3 getPoint2() const;
Point3 getPoint3() const;

Point3 getPoint1() const{
Point3 point;
if constexpr (std::is_same<Point, std::shared_ptr<Point3>>::value) point = *point1;
else point = point1;
return point;
};

Point3 getPoint2() const{
Point3 point;
if constexpr(std::is_same<Point, std::shared_ptr<Point3>>::value) point = *point2;
else point = point2;
return point;
};

Point3 getPoint3() const{
Point3 point;
if constexpr (std::is_same<Point, std::shared_ptr<Point3>>::value) point = *point3;
else point = point3;
return point;
};

virtual bool hit(const Ray& ray, double t_min, double t_max, HitRecord& rec) const override;
virtual bool hit_in_mesh(const Ray& ray, double t_min, double t_max, HitRecord& rec, const Matrix& mesh_transform, const Matrix& mesh_inverse, const Matrix& mesh_inverseTranspose) const;
bool hit(const Ray& ray, double t_min, double t_max, HitRecord& rec) const override;

private:
Point3 point1, point2, point3;
std::shared_ptr<Material> material;
private:
Point point1;
Point point2;
Point point3;
};

template <typename Point> bool Triangle<Point>::hit(const Ray& ray, double t_min, double t_max, HitRecord& rec) const {
// Passo 1: Criar um raio transformado.
Ray transformed_ray = ray.transform(inverseTransform);

// Passo 2: Realizar o teste de colisão em espaço local (algoritmo Möller-Trumbore).
const double epsilon = std::numeric_limits<double>::epsilon();
const Vector3 ray_direction = transformed_ray.direction();

const Vector3 edge1 = this->getPoint2()- this->getPoint1();
const Vector3 edge2 = this->getPoint3() - this->getPoint1();

const Vector3 h = ray_direction ^ edge2;
const double a = edge1 * h;

if (a > -epsilon && a < epsilon)
return false; // Raio paralelo ao triângulo.

const double f = 1.0 / a;
const Vector3 s = transformed_ray.origin() - this->getPoint1();
const double u = f * (s * h);

if (u < 0.0 || u > 1.0)
return false;

const Vector3 q = s ^ edge1;
const double v = f * (ray_direction * q);

if (v < 0.0 || u + v > 1.0)
return false;

const double t = f * (edge2 * q);

if (t > t_min && t < t_max) {
rec.t = t;
rec.p = transform * transformed_ray.at(t);
Vector3 local_normal = edge1 ^ edge2;
Vector3 world_normal = (inverseTransposeTransform * local_normal).normalize();
rec.set_face_normal(ray, world_normal);
rec.material = material;
return true;
}
class PRISM_EXPORT MeshTriangle {
public:
MeshTriangle(std::shared_ptr<Point3> p1, std::shared_ptr<Point3> p2, std::shared_ptr<Point3> p3);
MeshTriangle(Point3 p1, Point3 p2, Point3 p3);
MeshTriangle(std::initializer_list<Point3> points);

return false;
}
Point3 getPoint1() const;
Point3 getPoint2() const;
Point3 getPoint3() const;

bool hit(const Ray& ray, double t_min, double t_max, HitRecord& rec) const;

template <typename Point> bool Triangle<Point>::hit_in_mesh(const Ray& ray, double t_min, double t_max, HitRecord& rec, const Matrix& mesh_transform, const Matrix& mesh_inverse, const Matrix& mesh_inverseTranspose) const {
// Passo 1: Criar um raio transformado.
Ray transformed_ray = ray.transform(mesh_inverse);

// Passo 2: Realizar o teste de colisão em espaço local (algoritmo Möller-Trumbore).
const double epsilon = std::numeric_limits<double>::epsilon();
const Vector3 ray_direction = transformed_ray.direction();

const Vector3 edge1 = this->getPoint2()- this->getPoint1();
const Vector3 edge2 = this->getPoint3() - this->getPoint1();

const Vector3 h = ray_direction ^ edge2;
const double a = edge1 * h;

if (a > -epsilon && a < epsilon)
return false; // Raio paralelo ao triângulo.

const double f = 1.0 / a;
const Vector3 s = transformed_ray.origin() - this->getPoint1();
const double u = f * (s * h);

if (u < 0.0 || u > 1.0)
return false;

const Vector3 q = s ^ edge1;
const double v = f * (ray_direction * q);

if (v < 0.0 || u + v > 1.0)
return false;

const double t = f * (edge2 * q);

if (t > t_min && t < t_max) {
rec.t = t;
rec.p = mesh_transform * transformed_ray.at(t);
Vector3 local_normal = edge1 ^ edge2;
Vector3 world_normal = (mesh_inverseTranspose * local_normal).normalize();
rec.set_face_normal(ray, world_normal);
rec.material = material;
return true;
}

return false;
}
private:
std::shared_ptr<Point3> point1, point2, point3;
};

} // namespace Prism

Expand Down
4 changes: 2 additions & 2 deletions libs/Prism/src/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Prism {

Color::Color() : r(0), g(0), b(0) {}
Color::Color(float red, float green, float blue) : r(red), g(green), b(blue) {}
// Color::Color(int red, int green, int blue): r(static_cast<float>(red) / 255.0f), g(static_cast<float>(green) / 255.0f), b(static_cast<float>(blue) / 255.0f) {}
Color::Color(double red, double green, double blue) : r(red), g(green), b(blue) {}
Color::Color(int red, int green, int blue): r(static_cast<double>(red) / 255.0), g(static_cast<double>(green) / 255.0), b(static_cast<double>(blue) / 255.0) {}
Color::Color(const Color& other) : r(other.r), g(other.g), b(other.b) {}

} // namespace Prism
4 changes: 2 additions & 2 deletions libs/Prism/src/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ Matrix Matrix::translation(int dimension, std::initializer_list<double> values)

Matrix t = identity(dimension + 1);
auto val_it = values.begin();
for (size_t i = 0; i < dimension; ++i) {
for (size_t i = 0; i < static_cast<size_t>(dimension); ++i) {
t.data_[i][dimension] = *val_it++;
}
return t;
Expand All @@ -280,7 +280,7 @@ Matrix Matrix::scaling(int dimension, std::initializer_list<double> values) {
}
Matrix s = identity(dimension + 1);
auto val_it = values.begin();
for (size_t i = 0; i < dimension; ++i) {
for (size_t i = 0; i < static_cast<size_t>(dimension); ++i) {
s.data_[i][i] = *val_it++;
}
return s;
Expand Down
14 changes: 8 additions & 6 deletions libs/Prism/src/mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Prism/mesh.hpp"
#include "Prism/matrix.hpp"
#include <cmath>

namespace Prism {
Expand All @@ -10,13 +11,10 @@ Mesh::Mesh(ObjReader& reader):material(std::move(reader.curMaterial)){
points.emplace_back(std::make_shared<Point3>(point[0],point[1],point[2]));
}
for(auto& triangle: reader.triangles){
mesh.push_back(
Triangle<std::shared_ptr<Point3>>(
mesh.push_back({
points[triangle[0]],
points[triangle[1]],
points[triangle[2]],
material
)
points[triangle[2]]}
);
}
};
Expand All @@ -27,8 +25,12 @@ bool Mesh::hit(const Ray& ray, double t_min, double t_max, HitRecord& rec) const
bool hit_anything = false;
rec.t = INFINITY;
for (const auto& triangle : mesh) {
if (triangle.hit_in_mesh(ray, 0.001, rec.t, rec, transform, inverseTransform, inverseTransposeTransform)) {
if (triangle.hit(ray, 0.001, rec.t, rec)) {
hit_anything = true;
rec.p = transform * ray.at(rec.t);
Vector3 world_normal = (inverseTransposeTransform * rec.normal).normalize();
rec.set_face_normal(ray, world_normal);
rec.material = material;
}
}
return hit_anything;
Expand Down
16 changes: 8 additions & 8 deletions libs/Prism/src/plane.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#include "Prism/plane.hpp"
#include "Prism/ray.hpp"
#include "Prism/utils.hpp"
#include "Prism/material.hpp"
#include "Prism/point.hpp"
#include "Prism/ray.hpp"
#include "Prism/utils.hpp"
#include "Prism/vector.hpp"
#include "Prism/material.hpp"
#include <cmath>

namespace Prism {

Plane::Plane(Point3 point_on_plane, Vector3 normal, std::shared_ptr<Material>material)
: point_on_plane(point_on_plane), normal(normal), material(std::move(material)) {}

Plane::Plane(Point3 point_on_plane, Vector3 normal, std::shared_ptr<Material> material)
: point_on_plane(point_on_plane), normal(normal), material(std::move(material)) {
}

bool Plane::hit(const Ray& ray, double t_min, double t_max, HitRecord& rec) const {
Ray transformed_ray = ray.transform(inverseTransform);

double denominator = normal.dot(transformed_ray.direction());
double tolerance = 1e-6;

if (std::abs(denominator) <= tolerance) {
return false;
}
Expand All @@ -33,7 +33,7 @@ bool Plane::hit(const Ray& ray, double t_min, double t_max, HitRecord& rec) cons
Vector3 outward_normal_world = (inverseTransposeTransform * this->normal).normalize();
rec.set_face_normal(ray, outward_normal_world); // Usa o raio original
rec.material = material;

return true;
}

Expand Down
Loading