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
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ set(CMAKE_CXX_EXTENSIONS OFF)
enable_testing()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_subdirectory(vendor)
add_subdirectory(libs)
add_subdirectory(src)

Expand Down
93 changes: 93 additions & 0 deletions data/input/scene.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# -----------------------------------------------------------------
# Exemplo de Arquivo de Cena para o Renderizador Prism
# Este arquivo demonstra o uso de múltiplos objetos, materiais
# e transformações para criar uma cena complexa.
# -----------------------------------------------------------------

# Definições reutilizáveis da cena, como materiais.
# Usamos âncoras (&) para definir um material e aliases (*) para reutilizá-lo.
definitions:
materials:
# Material fosco para o chão
chao_cinza: &material_chao
color: [0.8, 0.8, 0.8]
ka: [0.1, 0.1, 0.1] # Pouca reflexão ambiente
ks: [0.1, 0.1, 0.1] # Pouco brilho especular
ns: 10

# Material de plástico vermelho, com brilho moderado
plastico_vermelho: &material_esfera_vermelha
color: [1.0, 0.2, 0.2]
ka: [0.2, 0.05, 0.05]
ks: [0.7, 0.7, 0.7] # Brilho especular forte
ns: 128 # Expoente de brilho alto para um highlight pequeno e intenso

# Material metálico/refletivo para o cubo
metal_azul: &material_cubo_metalico
color: [0.2, 0.3, 1.0]
ka: [0.1, 0.1, 0.2]
ks: [0.9, 0.9, 0.9] # Reflexão especular muito alta
ns: 256

# Material de vidro para o triângulo
vidro_transparente: &material_vidro
color: [0.9, 0.9, 1.0] # Cor levemente azulada
ka: [0.1, 0.1, 0.1]
ks: [0.8, 0.8, 0.8]
ns: 200
ni: 1.5 # Índice de refração (típico para vidro)
d: 0.1 # Opacidade (valor 'd' baixo significa mais transparente)

# -----------------------------------------------------------------
# Configurações da Câmera
# -----------------------------------------------------------------
camera:
image_width: 960
image_height: 540
screen_distance: 1.5
viewport_width: 2.0
viewport_height: 1.125
lookfrom: [0, 2, 8] # Posição da câmera
lookat: [0, 0, 0] # Ponto para onde a câmera olha
vup: [0, 1, 0] # Vetor "para cima"

# -----------------------------------------------------------------
# Lista de Objetos na Cena
# -----------------------------------------------------------------
objects:
- name: Chão
type: plane
point_on_plane: [0, -1, 0] # Ponto que define a altura do plano
normal: [0, 1, 0] # Vetor normal aponta para cima
material: *material_chao # Reutiliza o material do chão definido acima

- name: Esfera Vermelha Principal
type: sphere
center: [-1.5, 0, 0]
radius: 1.0
material: *material_esfera_vermelha # Reutiliza o material de plástico

- name: Malha de Cubo Metálico
type: mesh
path: "./data/input/cubo.obj" # Caminho para o arquivo .obj
material: *material_cubo_metalico # Reutiliza o material metálico
# Múltiplas transformações são aplicadas em ordem
transform:
- type: scaling
factors: [0.7, 0.7, 0.7] # Primeiro, diminui a escala do cubo
- type: rotation
angle: 45 # Em graus (o parser converterá para radianos)
axis: [0, 1, 0] # Rotaciona em torno do eixo Y
- type: translation
vector: [1.5, 0, -1] # Por último, move o cubo para sua posição final

- name: Triângulo de Vidro
type: triangle
# Vértices definidos diretamente no arquivo de cena
p1: [-3, -1, -5]
p2: [3, -1, -5]
p3: [0, 4, -5]
material: *material_vidro # Reutiliza o material de vidro
transform:
- type: translation
vector: [0, 0, 2] # Move o triângulo um pouco para frente
10 changes: 10 additions & 0 deletions libs/Prism/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
include(FetchContent)
FetchContent_Declare(
yaml-cpp
GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
GIT_TAG 0.8.0
)
FetchContent_MakeAvailable(yaml-cpp)

file(GLOB_RECURSE PRISM_SOURCES CONFIGURE_DEPENDS "src/*.cpp")
add_library(Prism SHARED ${PRISM_SOURCES})

Expand All @@ -9,6 +17,8 @@ target_include_directories(Prism PUBLIC
"${CMAKE_CURRENT_BINARY_DIR}"
)

target_link_libraries(Prism PUBLIC yaml-cpp)

install(TARGETS Prism
RUNTIME DESTINATION bin # Para .dll no Windows
LIBRARY DESTINATION lib # Para .so no Linux, .dylib no macOS
Expand Down
2 changes: 2 additions & 0 deletions libs/Prism/include/Prism.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
#include "Prism/triangle.hpp"
#include "Prism/utils.hpp"
#include "Prism/vector.hpp"
#include "Prism/scene_parser.hpp"
#include "Prism/style.hpp"
7 changes: 4 additions & 3 deletions libs/Prism/include/Prism/Colormap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ A classe precisa ser instânciada passando o caminho do arquivo .mtl corresponde
#include <sstream>
#include <string>
#include <vector>
#include "Prism/style.hpp"
using namespace std;

namespace Prism {
Expand All @@ -43,7 +44,7 @@ class colormap {
std::ifstream mtlFile(input);

if (!mtlFile.is_open()) {
std::cerr << "erro abrindo arquivo cores.mtl\n";
Style::logError("erro abrindo arquivo cores.mtl");
}

string line, currentMaterial;
Expand Down Expand Up @@ -98,7 +99,7 @@ class colormap {
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";
Style::logError("Cor " + s + " indefinida no arquivo .mtl");
return Vector3(0, 0, 0);
}
}
Expand All @@ -107,7 +108,7 @@ class colormap {
if (mp.find(s) != mp.end()) {
return mp[s];
} else {
cerr << "Error: Cor " << s << " indefinida no arquivo .mtl\n";
Style::logError("Cor " + s + " indefinida no arquivo .mtl");
return Material();
}
}
Expand Down
3 changes: 2 additions & 1 deletion libs/Prism/include/Prism/ObjReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Prism/point.hpp"
#include "Prism/triangle.hpp"
#include "Prism/vector.hpp"
#include "Prism/style.hpp"
#include "prism_export.h"
#include <array>
#include <fstream>
Expand All @@ -26,7 +27,7 @@ class ObjReader {
ObjReader(const std::string& filename) {
file.open(filename);
if (!file.is_open()) {
std::cerr << "Erro ao abrir o arquivo: " << filename << std::endl;
Style::logError("Erro ao abrir o arquivo: " + filename);
return;
}

Expand Down
37 changes: 37 additions & 0 deletions libs/Prism/include/Prism/scene_parser.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef PRISM_SCENEPARSER_HPP_
#define PRISM_SCENEPARSER_HPP_

#include "Prism/scene.hpp"
#include "prism_export.h"
#include <string>

namespace Prism {

/**
* @class SceneParser
* @brief Reads and parses a scene description from a YAML file.
* The SceneParser class is responsible for reading a YAML file that describes a 3D scene, including
* camera settings, materials, and objects. It constructs a Scene object that can be rendered.
*/
class PRISM_EXPORT SceneParser {
public:
/**
* @brief Constructor that initializes the SceneParser with the path to the scene file.
* @param sceneFilePath The path to the YAML file containing the scene description.
*/
explicit SceneParser(const std::string& sceneFilePath);

/**
* @brief Parses the scene file and constructs a Scene object.
* @return A Scene object containing the parsed camera, materials, and objects.
* @throws std::runtime_error if there is an error reading or parsing the YAML file.
*/
Scene parse();

private:
std::string filePath; ///< The path to the YAML file containing the scene description
};

} // namespace Prism

#endif // PRISM_SCENEPARSER_HPP_
95 changes: 95 additions & 0 deletions libs/Prism/include/Prism/style.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#ifndef PRISM_STYLE_HPP_
#define PRISM_STYLE_HPP_

#include "prism_export.h"
#include <string>
#include <iostream>

namespace Prism {

/**
* @namespace Style
* @brief Provides constants for colored console output.
*
* This namespace centralizes ANSI escape codes for styling text in the terminal,
* ensuring a consistent look and feel for all engine logs.
*/
namespace Style {

// --- Basic Colors ---
const std::string RESET = "\033[0m";
const std::string YELLOW = "\033[0;33m";
const std::string GREEN = "\033[0;32m";
const std::string CYAN = "\033[0;36m";
const std::string GRAY = "\033[0;90m";
const std::string RED = "\033[0;31m";

// --- Bold Variants ---
const std::string BOLD_CYAN = "\033[1;36m";
const std::string BOLD_GREEN = "\033[1;32m";
const std::string BOLD_RED = "\033[1;31m";
const std::string BOLD_YELLOW= "\033[1;33m";

/**
* @brief Logs a formatted informational message to std::clog.
* @param message The message to display.
*/
inline void logInfo(const std::string& message) {
std::clog << YELLOW << "[INFO] " << RESET << message << std::endl;
}

/**
* @brief Logs a formatted completion message to std::clog.
* @param message The message to display.
*/
inline void logDone(const std::string& message) {
std::clog << GREEN << "[DONE] " << RESET << message << std::endl;
}

/**
* @brief Logs a formatted error message to std::cerr.
* @param message The error message to display.
*/
inline void logError(const std::string& message) {
std::cerr << BOLD_RED << "[ERROR] " << RESET
<< RED << message << RESET << std::endl;
}

/**
* @brief Logs a formatted warning message to std::clog.
* @param message The warning message to display.
*/
inline void logWarning(const std::string& message) {
std::clog << BOLD_YELLOW << "[WARNING] " << RESET
<< YELLOW << message << RESET << std::endl;
}

/**
* @brief Displays a dynamic status bar on the current line in std::clog.
* @param progress A value between 0.0 and 1.0 indicating completion.
* @param width The total character width of the bar itself.
*/
inline void logStatusBar(double progress, int width = 25) {
if (progress < 0.0) progress = 0.0;
if (progress > 1.0) progress = 1.0;

int bar_fill = static_cast<int>(progress * width);
int percentage = static_cast<int>(progress * 100.0);

// \r (carriage return) move o cursor para o início da linha
std::clog << GREEN << "\rProgress: [" << RESET;
for (int i = 0; i < width; ++i) {
if (i < bar_fill) std::clog << "=";
else std::clog << " ";
}
std::clog << GREEN << "] " << percentage << "%" << RESET << std::flush;

if (progress >= 1.0) {
std::clog << '\n' << std::endl;
}
}

} // namespace Style
} // namespace Prism

#endif // PRISM_STYLE_HPP_
20 changes: 20 additions & 0 deletions libs/Prism/src/init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "Prism/style.hpp"
#include <iostream>

namespace Prism {

/**
* @struct PrismInitializer
* @brief A helper class whose sole purpose is to run code in its
* constructor, before the program's 'main'.
*/
struct PrismInitializer {
PrismInitializer() {
// This code will run once when the library is loaded.
std::clog << Style::BOLD_CYAN << "\n[PRISM RENDER ENGINE]\n" << Style::RESET << std::endl;
}
};

static PrismInitializer initializer;

} // namespace Prism
Loading