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
8 changes: 4 additions & 4 deletions libs/Prism/include/Prism.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "Prism/Colormap.hpp"
#include "Prism/ObjReader.hpp"
#include "Prism/camera.hpp"
#include "Prism/color.hpp"
#include "Prism/material.hpp"
#include "Prism/matrix.hpp"
#include "Prism/mesh.hpp"
#include "Prism/objects.hpp"
#include "Prism/plane.hpp"
#include "Prism/point.hpp"
#include "Prism/ray.hpp"
#include "Prism/scene.hpp"
#include "Prism/sphere.hpp"
#include "Prism/triangle.hpp"
#include "Prism/utils.hpp"
#include "Prism/vector.hpp"
#include "Prism/triangle.hpp"
#include "Prism/ObjReader.hpp"
#include "Prism/Colormap.hpp"
#include "Prism/mesh.hpp"
33 changes: 16 additions & 17 deletions libs/Prism/include/Prism/Colormap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Classe de leitura de arquivos .mtl, que guarda cores e propriedades de materiais
A saber que:
- kd = Difuso (Cor do objeto)
- ks = Specular (Reflexivo)
- ke = Emissivo
- ke = Emissivo
- ka = Ambiente
- ns = Brilho
- ni = Índice de refração
Expand All @@ -17,26 +17,26 @@ A saber que:
A classe precisa ser instânciada passando o caminho do arquivo .mtl correspondente
*/

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <map>
#include "Prism/vector.hpp"
#include "Prism/material.hpp"
#include "Prism/vector.hpp"
#include "prism_export.h"
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

namespace Prism{
namespace Prism {
class colormap {

public:
public:
map<string, Material> mp;

//Construtor
// Construtor
colormap(){};
colormap(string input){
colormap(string input) {

// construtor: lê arquivo cores.mtl e guarda valores RGB associados a cada nome

Expand Down Expand Up @@ -94,24 +94,23 @@ class colormap {
mtlFile.close();
}

Vector3 getColor(string& s){
Vector3 getColor(string& s) {
if (mp.find(s) != mp.end()) {
return Vector3(mp[s].color.r, mp[s].color.g, mp[s].color.b);
} else {
cerr << "Error: cor " << s << " indefinida no arquivo .mtl\n";
return Vector3(0,0,0);
return Vector3(0, 0, 0);
}
}

Material getMaterial(string& s){
Material getMaterial(string& s) {
if (mp.find(s) != mp.end()) {
return mp[s];
} else {
cerr << "Error: Cor " << s << " indefinida no arquivo .mtl\n";
return Material();
}
}

};
}
} // namespace Prism
#endif
45 changes: 21 additions & 24 deletions libs/Prism/include/Prism/ObjReader.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#ifndef PRISM_OBJREADER_HPP_
#define PRISM_OBJREADER_HPP_

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <memory>
#include <array>
#include "Prism/vector.hpp"
#include "Prism/point.hpp"
#include "Prism/Colormap.hpp"
#include "Prism/triangle.hpp"
#include "Prism/material.hpp"
#include "Prism/point.hpp"
#include "Prism/triangle.hpp"
#include "Prism/vector.hpp"
#include "prism_export.h"
#include <array>
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>

namespace Prism {

Expand All @@ -22,15 +22,13 @@ class ObjReader {
std::shared_ptr<Material> curMaterial;
std::vector<std::array<double, 3>> vertices;
std::vector<std::array<unsigned int, 3>> triangles;


ObjReader(const std::string& filename) {
file.open(filename);
if (!file.is_open()) {
std::cerr << "Erro ao abrir o arquivo: " << filename << std::endl;
return;
}

}

std::string line, mtlfile, colorname, filename_mtl;
while (std::getline(file, line)) {
Expand All @@ -47,14 +45,12 @@ class ObjReader {
iss >> colorname;
auto mtlProps = cmap.getMaterial(colorname);
curMaterial = std::make_shared<Material>(
Color(mtlProps.color.r, mtlProps.color.g, mtlProps.color.b),
mtlProps.ka, mtlProps.ks, mtlProps.ke,
mtlProps.ns, mtlProps.ni, mtlProps.d
);
Color(mtlProps.color.r, mtlProps.color.g, mtlProps.color.b), mtlProps.ka,
mtlProps.ks, mtlProps.ke, mtlProps.ns, mtlProps.ni, mtlProps.d);
} else if (prefix == "v") {
double x, y, z;
iss >> x >> y >> z;
vertices.push_back({x,y,z});
vertices.push_back({x, y, z});
} else if (prefix == "f") {
unsigned int vi[3], ni[3];
char slash;
Expand All @@ -71,9 +67,9 @@ class ObjReader {
file.close();
}

private:
std::ifstream file;
colormap cmap;
private:
std::ifstream file;
colormap cmap;

// /**
// * @brief Prints each triangle’s vertices to the console
Expand All @@ -82,9 +78,10 @@ class ObjReader {
// int i = 0;
// for (const auto& tri : triangles) {
// std::clog << "Face " << (++i) << ": ";
// std::cout << "(" << tri.getPoint1().x << ", " << tri.getPoint1().y << ", " << tri.getPoint1().z << ") ";
// std::cout << "(" << tri.getPoint2().x << ", " << tri.getPoint2().y << ", " << tri.getPoint2().z << ") ";
// std::cout << "(" << tri.getPoint3().x << ", " << tri.getPoint3().y << ", " << tri.getPoint3().z << ") ";
// std::cout << "(" << tri.getPoint1().x << ", " << tri.getPoint1().y << ", " <<
// tri.getPoint1().z << ") "; std::cout << "(" << tri.getPoint2().x << ", " <<
// tri.getPoint2().y << ", " << tri.getPoint2().z << ") "; std::cout << "(" <<
// tri.getPoint3().x << ", " << tri.getPoint3().y << ", " << tri.getPoint3().z << ") ";
// std::cout << std::flush;
// std::clog << std::endl;
// }
Expand Down
94 changes: 68 additions & 26 deletions libs/Prism/include/Prism/camera.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef PRISM_CAMERA_HPP_
#define PRISM_CAMERA_HPP_

#include "Prism/matrix.hpp"
#include "Prism/point.hpp"
#include "Prism/ray.hpp"
#include "Prism/vector.hpp"
#include "Prism/matrix.hpp"
#include "prism_export.h"
#include <initializer_list>
#include <iterator>
Expand All @@ -29,10 +29,25 @@ class PRISM_EXPORT Camera {
* @param image_height The height of the image in pixels.
* @param image_width The width of the image in pixels.
*/
Camera(const Point3& position, const Point3& target, const Vector3& upvec,
double distance, double viewport_height, double viewport_width,
int image_height, int image_width);
Camera(const Point3& position, const Point3& target, const Vector3& upvec, double distance,
double viewport_height, double viewport_width, int image_height, int image_width);

/**
* @brief Constructs a Camera with default parameters.
* This constructor initializes the camera at the origin, looking down the negative z-axis,
* with the up vector pointing in the positive y direction.
*/
Camera()
: Camera(Point3(0, 0, 0), Point3(0, 0, -1), Vector3(0, 1, 0), 1.0, 1.0, 1.0, 480, 640) {
}

/**
* @class CameraIterator
* @brief An iterator for traversing the pixels of the camera's view.
*
* This iterator allows iteration over the rays that correspond to each pixel in the camera's
* view. It provides a way to access each pixel's ray in a forward manner.
*/
class CameraIterator {
public:
using iterator_category = std::forward_iterator_tag;
Expand All @@ -41,16 +56,30 @@ class PRISM_EXPORT Camera {
using pointer = Ray*;
using reference = Ray&;

/**
* @brief Constructs a CameraIterator for the given camera, starting at pixel (y, x).
* @param cam Pointer to the Camera object.
* @param y The starting row (y-coordinate) of the pixel.
* @param x The starting column (x-coordinate) of the pixel.
*/
CameraIterator(const Camera* cam, int y, int x) : camera(cam), current_y(y), current_x(x) {
}

/**
* @brief Dereferences the iterator to get the Ray corresponding to the current pixel.
* @return A Ray object representing the ray for the current pixel.
*/
Ray operator*() const {
Point3 pixel_center = camera->pixel_00_loc + (camera->pixel_delta_u * current_x) -
(camera->pixel_delta_v * current_y);

return Ray(camera->pos, pixel_center);
}

/**
* @brief Post-increment operator to move the iterator to the next pixel.
* @return A reference to the updated CameraIterator.
*/
CameraIterator& operator++() {
current_x++;
if (current_x >= camera->pixel_width) {
Expand All @@ -60,47 +89,60 @@ class PRISM_EXPORT Camera {
return *this;
}

/**
* @brief Difference operator to compare two CameraIterator objects.
* @param other The other CameraIterator to compare with.
* @return True if the current iterator is not equal to the other, false otherwise.
*/
bool operator!=(const CameraIterator& other) const {
return current_y != other.current_y || current_x != other.current_x;
}

private:
const Camera* camera;
int current_y;
int current_x;
const Camera* camera; ///< Pointer to the Camera object being iterated over
int current_y; ///< Current row (y-coordinate) of the pixel
int current_x; ///< Current column (x-coordinate) of the pixel
};

CameraIterator begin() {
return CameraIterator(this, 0, 0);
}
CameraIterator end() {
return CameraIterator(this, pixel_height, 0);
}

/**
* @brief Returns a const iterator to the beginning of the camera's pixel rays.
* @return A CameraIterator pointing to the first pixel ray.
*
* This function allows iteration over the rays corresponding to each pixel
* in the camera's view. It works for both const and non-const Camera objects.
*/
CameraIterator begin() const {
return CameraIterator(this, 0, 0);
}

/**
* @brief Returns a const iterator to the end of the camera's pixel rays.
* @return A CameraIterator pointing to one past the last pixel ray.
*/
CameraIterator end() const {
return CameraIterator(this, pixel_height, 0);
}

Point3 pos;
Point3 aim;
Vector3 up;
Point3 pos; ///< Camera position in 3D space
Point3 aim; ///< Point the camera is looking at (the target point)
Vector3 up; ///< Up vector indicating the upward direction from the camera

Matrix coordinate_basis;
Matrix coordinate_basis; ///< Coordinate basis matrix for the camera, defining its orientation
///< in space

double screen_distance;
double screen_height;
double screen_width;
double screen_distance; ///< Distance from the camera to the projection screen (view plane)
double screen_height; ///< Height of the projection screen (view plane)
double screen_width; ///< Width of the projection screen (view plane)

int pixel_height;
int pixel_width;
int pixel_height; ///< Height of the image in pixels
int pixel_width; ///< Width of the image in pixels

private:
Point3 pixel_00_loc;
Vector3 pixel_delta_u;
Vector3 pixel_delta_v;
Point3 pixel_00_loc; ///< Location of the pixel at (0, 0) in the camera's view
Vector3 pixel_delta_u; ///< Vector representing the change in position along the u direction
///< (horizontal)
Vector3 pixel_delta_v; ///< Vector representing the change in position along the v direction
///< (vertical)
};

} // namespace Prism
Expand Down
Loading