diff options
| author | hachem <im@hachem.wtf> | 2025-08-14 18:21:00 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2025-08-14 18:21:00 +0200 |
| commit | 3d8d3259e1cf5a7d88e5d1e530cc8c9bf9140734 (patch) | |
| tree | 450b674caaf60327104aa76375c045f1f8f77076 /src/engine/scene.cpp | |
| parent | 79045760ce6100b4c87846d793f9dceeba4c9c36 (diff) | |
feat: I am going to throw my computer out a window
Diffstat (limited to 'src/engine/scene.cpp')
| -rw-r--r-- | src/engine/scene.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/engine/scene.cpp b/src/engine/scene.cpp new file mode 100644 index 0000000..bfac1aa --- /dev/null +++ b/src/engine/scene.cpp @@ -0,0 +1,57 @@ +#include "scene.h" + +namespace Donut +{ + Scene::Scene() + : m_light_pos(5.0f, 5.0f, 5.0f) { } + + glm::vec3 Scene::trace(Ray &ray) + { + float closest = std::numeric_limits<float>::infinity(); + const Object* hit_obj = nullptr; + + for(auto& obj : objs) + { + float t; + + if(obj.intersect(ray, t)) + if(t < closest) + { + closest = t; + hit_obj = &obj; + } + } + + if(hit_obj) + { + glm::vec3 hit_point = ray.m_origin + ray.m_direction * closest; + glm::vec3 normal = hit_obj->get_normal(hit_point); + glm::vec3 light_dir = glm::normalize(m_light_pos - hit_point); + + float diff = std::max(glm::dot(normal, light_dir), 0.0f); + + Ray shadow_ray(hit_point + normal * 0.001f, light_dir); + bool in_shadow = false; + + for(auto& obj : objs) + { + float t; + + if(obj.intersect(shadow_ray, t)) + { + in_shadow = true; + break; + } + } + + glm::vec3 color = hit_obj->m_material.m_color; + float ambient = 0.1f; + + if (in_shadow) + return color * ambient; + return color * (ambient + diff * 0.9f); + } + + return glm::vec3(0.0f, 0.0f, 0.1f); + } +} |
