From 4172869e54093eee6e1595ebe0fa55c9fe98aef7 Mon Sep 17 00:00:00 2001 From: hachem Date: Fri, 15 Aug 2025 03:18:29 +0200 Subject: [help]: Complete first geodesic 3d raytracing --- src/Engine/Scene.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) (limited to 'src/Engine/Scene.h') diff --git a/src/Engine/Scene.h b/src/Engine/Scene.h index fa80302..f85fc13 100644 --- a/src/Engine/Scene.h +++ b/src/Engine/Scene.h @@ -2,7 +2,6 @@ #include #include -#include #include "Object.h" @@ -14,7 +13,57 @@ namespace Donut std::vector objs; glm::vec3 m_LightPos; - Scene(); - glm::vec3 Trace(Ray &ray); + Scene() + : m_LightPos(5.0f, 5.0f, 5.0f) { } + + glm::vec3 Trace(Ray &ray) + { + float closest = std::numeric_limits::infinity(); + const Object* hitObj = nullptr; + + for(auto& obj : objs) + { + float t; + + if(obj.Intersect(ray, t)) + if(t < closest) + { + closest = t; + hitObj = &obj; + } + } + + if(hitObj) + { + glm::vec3 hitPoint = ray.m_Origin + ray.m_Direction * closest; + glm::vec3 normal = hitObj->GetNormal(hitPoint); + glm::vec3 lightDir = glm::normalize(m_LightPos - hitPoint); + + float diff = std::max(glm::dot(normal, lightDir), 0.0f); + + Ray shadowRay(hitPoint + normal * 0.001f, lightDir); + bool inShadow = false; + + for(auto& obj : objs) + { + float t; + + if(obj.Intersect(shadowRay, t)) + { + inShadow = true; + break; + } + } + + glm::vec3 color = hitObj->m_Material.m_Color; + float ambient = 0.1f; + + if (inShadow) + return color * ambient; + return color * (ambient + diff * 0.9f); + } + + return glm::vec3(0.0f, 0.0f, 0.1f); + } }; }; \ No newline at end of file -- cgit v1.3