Skip to content

Commit 93f2a89

Browse files
authored
feat(render): add phong logic to render (#23)
1 parent bee549c commit 93f2a89

File tree

23 files changed

+505
-177
lines changed

23 files changed

+505
-177
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# It sets global settings and includes the sub-projects.
66
# ===================================================================
77
cmake_minimum_required(VERSION 3.22)
8-
project(Prism VERSION 0.1.0 LANGUAGES CXX)
8+
project(Prism VERSION 0.2.0 LANGUAGES CXX)
99

1010
# Set the policy for FetchContent to use modern, safer behavior.
1111
cmake_policy(SET CMP0135 NEW)

demo/data/input/scene.yml

Lines changed: 117 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,143 @@
11
# -----------------------------------------------------------------
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.
2+
# Cena da Cornell Box Final - Com Esferas em Destaque (Avançadas)
53
# -----------------------------------------------------------------
64

7-
# Definições reutilizáveis da cena, como materiais.
8-
# Usamos âncoras (&) para definir um material e aliases (*) para reutilizá-lo.
5+
ambient_light: [0.1, 0.1, 0.1]
6+
7+
# --- Definições de Materiais ---
98
definitions:
109
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
10+
# Materiais das paredes
11+
parede_branca: &branco
12+
color: [0.73, 0.73, 0.73]
13+
ka: [0.73, 0.73, 0.73]
14+
ks: [0.0, 0.0, 0.0]
1615
ns: 10
1716

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
17+
parede_vermelha: &vermelho
18+
color: [0.65, 0.05, 0.05]
19+
ka: [0.65, 0.05, 0.05]
20+
ks: [0.0, 0.0, 0.0]
21+
ns: 10
2422

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
23+
parede_verde: &verde
24+
color: [0.12, 0.45, 0.15]
25+
ka: [0.12, 0.45, 0.15]
26+
ks: [0.0, 0.0, 0.0]
27+
ns: 10
28+
29+
# Materiais para os objetos internos
30+
bloco_azul: &azul
31+
color: [0.1, 0.2, 0.8]
32+
ka: [0.1, 0.2, 0.8]
33+
ks: [0.3, 0.3, 0.3]
34+
ns: 64
3135

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)
36+
bloco_laranja: &laranja
37+
color: [0.9, 0.5, 0.1]
38+
ka: [0.9, 0.5, 0.1]
39+
ks: [0.2, 0.2, 0.2]
40+
ns: 32
41+
42+
esfera_espelho: &espelho
43+
color: [0.0, 0.0, 0.0]
44+
ka: [0.0, 0.0, 0.0]
45+
ks: [0.98, 0.98, 0.98]
46+
ns: 2048
4047

41-
# -----------------------------------------------------------------
42-
# Configurações da Câmera
43-
# -----------------------------------------------------------------
48+
esfera_vidro: &vidro
49+
color: [1.0, 1.0, 1.0]
50+
ka: [1.0, 1.0, 1.0]
51+
ks: [0.1, 0.1, 0.1]
52+
ns: 256
53+
ni: 1.52
54+
d: 0.05
55+
56+
# --- Configurações da Câmera ---
4457
camera:
45-
image_width: 960
46-
image_height: 540
58+
image_width: 800
59+
image_height: 800
4760
screen_distance: 1.5
4861
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"
62+
viewport_height: 2.0
63+
lookfrom: [278, 278, -800]
64+
lookat: [278, 278, 0]
65+
vup: [0, 1, 0]
5366

54-
# -----------------------------------------------------------------
55-
# Lista de Objetos na Cena
56-
# -----------------------------------------------------------------
67+
# --- Fontes de Luz ---
68+
lights:
69+
- name: Luz do Teto
70+
position: [278, 548, 278]
71+
color: [1.0, 1.0, 1.0]
72+
- name: Flash da Câmera
73+
position: [278, 278, -800]
74+
color: [0.5, 0.5, 0.5]
75+
76+
# --- Objetos na Cena ---
5777
objects:
78+
# As 5 paredes da caixa
5879
- name: Chão
5980
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
81+
point_on_plane: [0, 0, 0]
82+
normal: [0, 1, 0]
83+
material: *branco
84+
- name: Teto
85+
type: plane
86+
point_on_plane: [0, 555, 0]
87+
normal: [0, -1, 0]
88+
material: *branco
89+
- name: Parede do Fundo
90+
type: plane
91+
point_on_plane: [0, 0, 555]
92+
normal: [0, 0, -1]
93+
material: *branco
94+
- name: Parede Direita (Verde)
95+
type: plane
96+
point_on_plane: [555, 0, 0]
97+
normal: [-1, 0, 0]
98+
material: *verde
99+
- name: Parede Esquerda (Vermelha)
100+
type: plane
101+
point_on_plane: [0, 0, 0]
102+
normal: [1, 0, 0]
103+
material: *vermelho
69104

70-
- name: Malha de Cubo Metálico
105+
# Blocos com tamanho e posição originais
106+
- name: Bloco Alto (Laranja)
71107
type: mesh
72-
path: "./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
108+
path: "cubo.obj"
109+
material: *laranja
75110
transform:
76111
- type: scaling
77-
factors: [0.7, 0.7, 0.7] # Primeiro, diminui a escala do cubo
112+
factors: [82.5, 165, 82.5]
78113
- type: rotation
79-
angle: 45 # Em graus (o parser converterá para radianos)
80-
axis: [0, 1, 0] # Rotaciona em torno do eixo Y
114+
angle: 18
115+
axis: [0, 1, 0]
81116
- type: translation
82-
vector: [1.5, 0, -1] # Por último, move o cubo para sua posição final
117+
vector: [180, 165, 250]
83118

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
119+
- name: Bloco Baixo (Azul)
120+
type: mesh
121+
path: "cubo.obj"
122+
material: *azul
91123
transform:
124+
- type: scaling
125+
factors: [82.5, 82.5, 82.5]
126+
- type: rotation
127+
angle: -20
128+
axis: [0, 1, 0]
92129
- type: translation
93-
vector: [0, 0, 2] # Move o triângulo um pouco para frente
130+
vector: [370, 82.5, 150]
131+
132+
# ESFERAS COM POSIÇÃO AJUSTADA (AINDA MAIS PERTO)
133+
- name: Esfera de Vidro
134+
type: sphere
135+
center: [380, 90, -140] # Z foi de 120 para 80
136+
radius: 90
137+
material: *vidro
138+
139+
- name: Esfera de Espelho
140+
type: sphere
141+
center: [180, 90, -200] # Z foi de 180 para 100
142+
radius: 90
143+
material: *espelho

src/include/Prism/core/color.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ class PRISM_EXPORT Color {
4242
*/
4343
Color(const Color& other);
4444

45+
Color operator* (Color other) const;
46+
47+
Color operator* (double scalar) const;
48+
49+
Color operator+ (Color other) const;
50+
51+
Color operator+= (Color other);
52+
53+
Color& clamp();
54+
55+
56+
4557
double r; ///< Red component of the color (0.0 to 1.0)
4658
double g; ///< Green component of the color (0.0 to 1.0)
4759
double b; ///< Blue component of the color (0.0 to 1.0)

src/include/Prism/core/material.hpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,26 @@ class PRISM_EXPORT Material {
2222
* @brief
2323
* Default constructor that initializes the material with default values.
2424
* The default values are:
25-
* - Color: Black (0, 0, 0)
26-
* - Ambient reflectivity (ka): (0, 0, 0)
25+
* - Color: white (1,1,1)
26+
* - Ambient reflectivity (ka): (0.1, 0.1, 0.1)
2727
* - Specular reflectivity (ks): (0, 0, 0)
2828
* - Emissive color (ke): (0, 0, 0)
29-
* - Shininess (ns): 0
30-
* - Index of refraction (ni): 0
31-
* - Transparency (d): 0
29+
* - Shininess (ns): 1
30+
* - Index of refraction (ni): 1
31+
* - Transparency (d): 1
3232
*/
33-
Material(Color color = Color(), Vector3 ka = Vector3(), Vector3 ks = Vector3(),
34-
Vector3 ke = Vector3(), double ns = 0, double ni = 0, double d = 0)
33+
Material(Color color = Color(1, 1, 1), Color ka = Color(0.1, 0.1, 0.1),
34+
Color ks = Color(0, 0, 0), Color ke = Color(0, 0, 0), double ns = 1.0, double ni = 1.0,
35+
double d = 1.0)
3536
: color(color), ka(ka), ks(ks), ke(ke), ns(ns), ni(ni), d(d) {
3637
}
3738

3839
Color color; ///< The color of the material, typically used for diffuse reflection.
39-
Vector3 ka; ///< Ambient reflectivity of the material, representing how much ambient light it
40+
Color ka; ///< Ambient reflectivity of the material, representing how much ambient light it
4041
///< reflects.
41-
Vector3 ks; ///< Specular reflectivity of the material, representing how much specular light it
42+
Color ks; ///< Specular reflectivity of the material, representing how much specular light it
4243
///< reflects.
43-
Vector3
44+
Color
4445
ke; ///< Emissive color of the material, representing light emitted by the material itself.
4546
double ns; ///< Shininess factor of the material, affecting the size and intensity of specular
4647
///< highlights.

src/include/Prism/core/point.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ class PRISM_EXPORT Point3 {
5757
*/
5858
bool operator==(const Point3& p) const;
5959

60+
/**
61+
* @brief Negates the point.
62+
* @return A new point that is the negation of this point.
63+
*/
64+
Point3 operator-() const;
65+
6066
/**
6167
* @brief Gets a vector from this point to another point.
6268
* @param p The point to get the vector to.

src/include/Prism/core/utils.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ template <typename T> T sqr(const T& value) {
4242
return value * value;
4343
}
4444

45+
/**
46+
* @brief Computes the refracted vector based on Snell's law.
47+
* This function calculates the refracted vector given an incident vector, a normal vector,
48+
* and the ratio of indices of refraction.
49+
* @param uv The incident vector (unit vector).
50+
* @param n The normal vector at the point of incidence (unit vector).
51+
* @param etai_over_etat The ratio of indices of refraction (etai/etat).
52+
* @return The refracted vector as a Vector3.
53+
*/
54+
Vector3 PRISM_EXPORT refract(const Vector3& uv, const Vector3& n, double etai_over_etat);
55+
56+
/**
57+
* @brief Calcs the reflectance using Schlick's approximation.
58+
* This function computes the reflectance based on the angle of incidence and the index of refraction
59+
* using Schlick's approximation.
60+
* @param cosine The cosine of the angle between the incident vector and the normal.
61+
* @param ref_idx The index of refraction of the material.
62+
* @return The reflectance value as a double, which is the probability of reflection at the
63+
* interface.
64+
* This value is used in ray tracing to determine how much light is reflected versus refracted.
65+
*/
66+
double PRISM_EXPORT schlick(double cosine, double ref_idx);
67+
68+
4569
} // namespace Prism
4670

4771
#endif // PRISM_UTILS_HPP_

src/include/Prism/core/vector.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ class PRISM_EXPORT Vector3 {
6767
*/
6868
Vector3& operator=(const Vector3& v);
6969

70+
/**
71+
* @brief Negates the vector.
72+
* @return A new vector that is the negation of this vector.
73+
*/
74+
Vector3 operator-() const;
75+
7076
/**
7177
* @brief Adds two vectors.
7278
* @param v The vector to add.

src/include/Prism/objects/Colormap.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,19 @@ class colormap {
7272
double ksR, ksG, ksB;
7373
iss >> ksR >> ksG >> ksB;
7474
if (!currentMaterial.empty()) {
75-
mp[currentMaterial].ks = Vector3(ksR, ksG, ksB);
75+
mp[currentMaterial].ks = Color(ksR, ksG, ksB);
7676
}
7777
} else if (keyword == "Ke") {
7878
double keR, keG, keB;
7979
iss >> keR >> keG >> keB;
8080
if (!currentMaterial.empty()) {
81-
mp[currentMaterial].ke = Vector3(keR, keG, keB);
81+
mp[currentMaterial].ke = Color(keR, keG, keB);
8282
}
8383
} else if (keyword == "Ka") {
8484
double kaR, kaG, kaB;
8585
iss >> kaR >> kaG >> kaB;
8686
if (!currentMaterial.empty()) {
87-
mp[currentMaterial].ka = Vector3(kaR, kaG, kaB);
87+
mp[currentMaterial].ka = Color(kaR, kaG, kaB);
8888
}
8989
} else if (keyword == "Ns") {
9090
iss >> mp[currentMaterial].ns;

0 commit comments

Comments
 (0)