Skip to content

Commit 812064d

Browse files
authored
feat(scene): Add YAML parser and integrate scene loading (#19)
1 parent 15c3446 commit 812064d

File tree

14 files changed

+487
-90
lines changed

14 files changed

+487
-90
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ set(CMAKE_CXX_EXTENSIONS OFF)
1010
enable_testing()
1111

1212
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
13-
add_subdirectory(vendor)
1413
add_subdirectory(libs)
1514
add_subdirectory(src)
1615

data/input/scene.yml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# -----------------------------------------------------------------
2+
# Exemplo de Arquivo de Cena para o Renderizador Prism
3+
# Este arquivo demonstra o uso de múltiplos objetos, materiais
4+
# e transformações para criar uma cena complexa.
5+
# -----------------------------------------------------------------
6+
7+
# Definições reutilizáveis da cena, como materiais.
8+
# Usamos âncoras (&) para definir um material e aliases (*) para reutilizá-lo.
9+
definitions:
10+
materials:
11+
# Material fosco para o chão
12+
chao_cinza: &material_chao
13+
color: [0.8, 0.8, 0.8]
14+
ka: [0.1, 0.1, 0.1] # Pouca reflexão ambiente
15+
ks: [0.1, 0.1, 0.1] # Pouco brilho especular
16+
ns: 10
17+
18+
# Material de plástico vermelho, com brilho moderado
19+
plastico_vermelho: &material_esfera_vermelha
20+
color: [1.0, 0.2, 0.2]
21+
ka: [0.2, 0.05, 0.05]
22+
ks: [0.7, 0.7, 0.7] # Brilho especular forte
23+
ns: 128 # Expoente de brilho alto para um highlight pequeno e intenso
24+
25+
# Material metálico/refletivo para o cubo
26+
metal_azul: &material_cubo_metalico
27+
color: [0.2, 0.3, 1.0]
28+
ka: [0.1, 0.1, 0.2]
29+
ks: [0.9, 0.9, 0.9] # Reflexão especular muito alta
30+
ns: 256
31+
32+
# Material de vidro para o triângulo
33+
vidro_transparente: &material_vidro
34+
color: [0.9, 0.9, 1.0] # Cor levemente azulada
35+
ka: [0.1, 0.1, 0.1]
36+
ks: [0.8, 0.8, 0.8]
37+
ns: 200
38+
ni: 1.5 # Índice de refração (típico para vidro)
39+
d: 0.1 # Opacidade (valor 'd' baixo significa mais transparente)
40+
41+
# -----------------------------------------------------------------
42+
# Configurações da Câmera
43+
# -----------------------------------------------------------------
44+
camera:
45+
image_width: 960
46+
image_height: 540
47+
screen_distance: 1.5
48+
viewport_width: 2.0
49+
viewport_height: 1.125
50+
lookfrom: [0, 2, 8] # Posição da câmera
51+
lookat: [0, 0, 0] # Ponto para onde a câmera olha
52+
vup: [0, 1, 0] # Vetor "para cima"
53+
54+
# -----------------------------------------------------------------
55+
# Lista de Objetos na Cena
56+
# -----------------------------------------------------------------
57+
objects:
58+
- name: Chão
59+
type: plane
60+
point_on_plane: [0, -1, 0] # Ponto que define a altura do plano
61+
normal: [0, 1, 0] # Vetor normal aponta para cima
62+
material: *material_chao # Reutiliza o material do chão definido acima
63+
64+
- name: Esfera Vermelha Principal
65+
type: sphere
66+
center: [-1.5, 0, 0]
67+
radius: 1.0
68+
material: *material_esfera_vermelha # Reutiliza o material de plástico
69+
70+
- name: Malha de Cubo Metálico
71+
type: mesh
72+
path: "./data/input/cubo.obj" # Caminho para o arquivo .obj
73+
material: *material_cubo_metalico # Reutiliza o material metálico
74+
# Múltiplas transformações são aplicadas em ordem
75+
transform:
76+
- type: scaling
77+
factors: [0.7, 0.7, 0.7] # Primeiro, diminui a escala do cubo
78+
- type: rotation
79+
angle: 45 # Em graus (o parser converterá para radianos)
80+
axis: [0, 1, 0] # Rotaciona em torno do eixo Y
81+
- type: translation
82+
vector: [1.5, 0, -1] # Por último, move o cubo para sua posição final
83+
84+
- name: Triângulo de Vidro
85+
type: triangle
86+
# Vértices definidos diretamente no arquivo de cena
87+
p1: [-3, -1, -5]
88+
p2: [3, -1, -5]
89+
p3: [0, 4, -5]
90+
material: *material_vidro # Reutiliza o material de vidro
91+
transform:
92+
- type: translation
93+
vector: [0, 0, 2] # Move o triângulo um pouco para frente

libs/Prism/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
include(FetchContent)
2+
FetchContent_Declare(
3+
yaml-cpp
4+
GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
5+
GIT_TAG 0.8.0
6+
)
7+
FetchContent_MakeAvailable(yaml-cpp)
8+
19
file(GLOB_RECURSE PRISM_SOURCES CONFIGURE_DEPENDS "src/*.cpp")
210
add_library(Prism SHARED ${PRISM_SOURCES})
311

@@ -9,6 +17,8 @@ target_include_directories(Prism PUBLIC
917
"${CMAKE_CURRENT_BINARY_DIR}"
1018
)
1119

20+
target_link_libraries(Prism PUBLIC yaml-cpp)
21+
1222
install(TARGETS Prism
1323
RUNTIME DESTINATION bin # Para .dll no Windows
1424
LIBRARY DESTINATION lib # Para .so no Linux, .dylib no macOS

libs/Prism/include/Prism.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
#include "Prism/triangle.hpp"
1515
#include "Prism/utils.hpp"
1616
#include "Prism/vector.hpp"
17+
#include "Prism/scene_parser.hpp"
18+
#include "Prism/style.hpp"

libs/Prism/include/Prism/Colormap.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ A classe precisa ser instânciada passando o caminho do arquivo .mtl corresponde
2626
#include <sstream>
2727
#include <string>
2828
#include <vector>
29+
#include "Prism/style.hpp"
2930
using namespace std;
3031

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

4546
if (!mtlFile.is_open()) {
46-
std::cerr << "erro abrindo arquivo cores.mtl\n";
47+
Style::logError("erro abrindo arquivo cores.mtl");
4748
}
4849

4950
string line, currentMaterial;
@@ -98,7 +99,7 @@ class colormap {
9899
if (mp.find(s) != mp.end()) {
99100
return Vector3(mp[s].color.r, mp[s].color.g, mp[s].color.b);
100101
} else {
101-
cerr << "Error: cor " << s << " indefinida no arquivo .mtl\n";
102+
Style::logError("Cor " + s + " indefinida no arquivo .mtl");
102103
return Vector3(0, 0, 0);
103104
}
104105
}
@@ -107,7 +108,7 @@ class colormap {
107108
if (mp.find(s) != mp.end()) {
108109
return mp[s];
109110
} else {
110-
cerr << "Error: Cor " << s << " indefinida no arquivo .mtl\n";
111+
Style::logError("Cor " + s + " indefinida no arquivo .mtl");
111112
return Material();
112113
}
113114
}

libs/Prism/include/Prism/ObjReader.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Prism/point.hpp"
77
#include "Prism/triangle.hpp"
88
#include "Prism/vector.hpp"
9+
#include "Prism/style.hpp"
910
#include "prism_export.h"
1011
#include <array>
1112
#include <fstream>
@@ -26,7 +27,7 @@ class ObjReader {
2627
ObjReader(const std::string& filename) {
2728
file.open(filename);
2829
if (!file.is_open()) {
29-
std::cerr << "Erro ao abrir o arquivo: " << filename << std::endl;
30+
Style::logError("Erro ao abrir o arquivo: " + filename);
3031
return;
3132
}
3233

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef PRISM_SCENEPARSER_HPP_
2+
#define PRISM_SCENEPARSER_HPP_
3+
4+
#include "Prism/scene.hpp"
5+
#include "prism_export.h"
6+
#include <string>
7+
8+
namespace Prism {
9+
10+
/**
11+
* @class SceneParser
12+
* @brief Reads and parses a scene description from a YAML file.
13+
* The SceneParser class is responsible for reading a YAML file that describes a 3D scene, including
14+
* camera settings, materials, and objects. It constructs a Scene object that can be rendered.
15+
*/
16+
class PRISM_EXPORT SceneParser {
17+
public:
18+
/**
19+
* @brief Constructor that initializes the SceneParser with the path to the scene file.
20+
* @param sceneFilePath The path to the YAML file containing the scene description.
21+
*/
22+
explicit SceneParser(const std::string& sceneFilePath);
23+
24+
/**
25+
* @brief Parses the scene file and constructs a Scene object.
26+
* @return A Scene object containing the parsed camera, materials, and objects.
27+
* @throws std::runtime_error if there is an error reading or parsing the YAML file.
28+
*/
29+
Scene parse();
30+
31+
private:
32+
std::string filePath; ///< The path to the YAML file containing the scene description
33+
};
34+
35+
} // namespace Prism
36+
37+
#endif // PRISM_SCENEPARSER_HPP_

libs/Prism/include/Prism/style.hpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#ifndef PRISM_STYLE_HPP_
2+
#define PRISM_STYLE_HPP_
3+
4+
#include "prism_export.h"
5+
#include <string>
6+
#include <iostream>
7+
8+
namespace Prism {
9+
10+
/**
11+
* @namespace Style
12+
* @brief Provides constants for colored console output.
13+
*
14+
* This namespace centralizes ANSI escape codes for styling text in the terminal,
15+
* ensuring a consistent look and feel for all engine logs.
16+
*/
17+
namespace Style {
18+
19+
// --- Basic Colors ---
20+
const std::string RESET = "\033[0m";
21+
const std::string YELLOW = "\033[0;33m";
22+
const std::string GREEN = "\033[0;32m";
23+
const std::string CYAN = "\033[0;36m";
24+
const std::string GRAY = "\033[0;90m";
25+
const std::string RED = "\033[0;31m";
26+
27+
// --- Bold Variants ---
28+
const std::string BOLD_CYAN = "\033[1;36m";
29+
const std::string BOLD_GREEN = "\033[1;32m";
30+
const std::string BOLD_RED = "\033[1;31m";
31+
const std::string BOLD_YELLOW= "\033[1;33m";
32+
33+
/**
34+
* @brief Logs a formatted informational message to std::clog.
35+
* @param message The message to display.
36+
*/
37+
inline void logInfo(const std::string& message) {
38+
std::clog << YELLOW << "[INFO] " << RESET << message << std::endl;
39+
}
40+
41+
/**
42+
* @brief Logs a formatted completion message to std::clog.
43+
* @param message The message to display.
44+
*/
45+
inline void logDone(const std::string& message) {
46+
std::clog << GREEN << "[DONE] " << RESET << message << std::endl;
47+
}
48+
49+
/**
50+
* @brief Logs a formatted error message to std::cerr.
51+
* @param message The error message to display.
52+
*/
53+
inline void logError(const std::string& message) {
54+
std::cerr << BOLD_RED << "[ERROR] " << RESET
55+
<< RED << message << RESET << std::endl;
56+
}
57+
58+
/**
59+
* @brief Logs a formatted warning message to std::clog.
60+
* @param message The warning message to display.
61+
*/
62+
inline void logWarning(const std::string& message) {
63+
std::clog << BOLD_YELLOW << "[WARNING] " << RESET
64+
<< YELLOW << message << RESET << std::endl;
65+
}
66+
67+
/**
68+
* @brief Displays a dynamic status bar on the current line in std::clog.
69+
* @param progress A value between 0.0 and 1.0 indicating completion.
70+
* @param width The total character width of the bar itself.
71+
*/
72+
inline void logStatusBar(double progress, int width = 25) {
73+
if (progress < 0.0) progress = 0.0;
74+
if (progress > 1.0) progress = 1.0;
75+
76+
int bar_fill = static_cast<int>(progress * width);
77+
int percentage = static_cast<int>(progress * 100.0);
78+
79+
// \r (carriage return) move o cursor para o início da linha
80+
std::clog << GREEN << "\rProgress: [" << RESET;
81+
for (int i = 0; i < width; ++i) {
82+
if (i < bar_fill) std::clog << "=";
83+
else std::clog << " ";
84+
}
85+
std::clog << GREEN << "] " << percentage << "%" << RESET << std::flush;
86+
87+
if (progress >= 1.0) {
88+
std::clog << '\n' << std::endl;
89+
}
90+
}
91+
92+
} // namespace Style
93+
} // namespace Prism
94+
95+
#endif // PRISM_STYLE_HPP_

libs/Prism/src/init.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "Prism/style.hpp"
2+
#include <iostream>
3+
4+
namespace Prism {
5+
6+
/**
7+
* @struct PrismInitializer
8+
* @brief A helper class whose sole purpose is to run code in its
9+
* constructor, before the program's 'main'.
10+
*/
11+
struct PrismInitializer {
12+
PrismInitializer() {
13+
// This code will run once when the library is loaded.
14+
std::clog << Style::BOLD_CYAN << "\n[PRISM RENDER ENGINE]\n" << Style::RESET << std::endl;
15+
}
16+
};
17+
18+
static PrismInitializer initializer;
19+
20+
} // namespace Prism

0 commit comments

Comments
 (0)