Skip to content

Commit c5f28b4

Browse files
committed
background color #25
1 parent 51021d0 commit c5f28b4

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

apps/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ int main(int argc, char* argv[]) {
4444
std::shared_ptr<obj::Camera> cam =
4545
std::make_shared<obj::Camera>(obj::Camera(scene_config.at("camera")));
4646
debug("camera created");
47+
4748
obj::Scene scene = obj::Scene(cam);
4849
debug("empty scene created");
4950

@@ -52,6 +53,7 @@ int main(int argc, char* argv[]) {
5253

5354
Image image = Image();
5455
RenderingEngine r(config);
56+
r.set_background(scene_config.at("background").get<std::string>());
5557

5658
// render scene on image
5759
r.render(image, scene);

apps/scene.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"view_dir": [0, 0, -1]
99
},
1010

11+
"background": "day",
12+
1113
"materials": [
1214
{
1315
"name": "white_metal",

include/image.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <filesystem>
99
#include <iostream>
1010
#include <memory>
11+
#include <string>
1112

1213
#define INIT_WIDTH 600
1314
#define INIT_HEIGHT 400
@@ -85,9 +86,11 @@ namespace render {
8586
int max_bounces;
8687
double view_distance;
8788
int sample_per_pixel;
89+
std::string _background;
8890

8991
protected:
90-
Color send_ray(Ray r, int depth);
92+
Color send_ray(Ray, int depth) const;
93+
Color background(Ray&) const;
9194

9295
public:
9396
RenderingEngine(json::Json const&);
@@ -97,6 +100,7 @@ namespace render {
97100
void add_filter(std::shared_ptr<filters::Filter> f) {
98101
_filters.push_back(f);
99102
}
103+
void set_background(std::string style) { _background = style; }
100104
};
101105
} // namespace render
102106

src/display/render.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ using render::RenderingEngine;
1414
*/
1515

1616
RenderingEngine::RenderingEngine(json::Json const& config)
17-
: sample_per_pixel(1) {
17+
: sample_per_pixel(1), _background("day") {
1818
max_bounces = config.at("max_bounces").get<int>();
1919
if (max_bounces < 0)
2020
throw std::invalid_argument("max_bounces need to be a positive integer");
@@ -79,7 +79,7 @@ void RenderingEngine::apply_filters(Image& image) const {
7979
}
8080
}
8181

82-
Color RenderingEngine::send_ray(Ray r, int depth) {
82+
Color RenderingEngine::send_ray(Ray r, int depth) const {
8383
hit_record hit;
8484
// Initialize max time
8585
hit.t = view_distance;
@@ -103,10 +103,19 @@ Color RenderingEngine::send_ray(Ray r, int depth) {
103103
return send_ray(new_ray, --depth);
104104
}
105105

106+
return background(r);
107+
}
108+
109+
Color RenderingEngine::background(Ray& r) const {
106110
double t = 0.5 * (r.dir().y() + 1.0); // normalisation
107-
r.apply((1 - t) * maths::Vect3D(1.0, 1.0, 1.0)
108-
+ t * maths::Vect3D(0.5, 0.7, 1.0));
109-
return r.colour(); // Sky color from white to cyan
111+
if (_background == "day") // Sky color from white to cyan
112+
r.apply((1 - t) * maths::Vect3D(1) + t * maths::Vect3D(0.5, 0.7, 1.0));
113+
else if (_background == "night") {
114+
r.apply((1 - t) * maths::Vect3D(0.2, 0.2, 0.2) + t * maths::Vect3D(0));
115+
} else if (_background == "dark") {
116+
r.apply(maths::Vect3D(0));
117+
} else throw std::invalid_argument("background style is not a valid name.");
118+
return r.colour();
110119
}
111120

112121
/*

0 commit comments

Comments
 (0)