aboutsummaryrefslogtreecommitdiff
path: root/src/Engine/Scene.h
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2025-08-15 03:18:29 +0200
committerhachem <im@hachem.wtf>2025-08-15 03:18:29 +0200
commit4172869e54093eee6e1595ebe0fa55c9fe98aef7 (patch)
tree2004c518688e6db529e437fe3f0120ad0fcc1dd9 /src/Engine/Scene.h
parentbc2d88c799cb3913a5defe4ea9ddea9c56814349 (diff)
[help]: Complete first geodesic 3d raytracing
Diffstat (limited to 'src/Engine/Scene.h')
-rw-r--r--src/Engine/Scene.h55
1 files changed, 52 insertions, 3 deletions
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 <vector>
#include <limits>
-#include <glm/glm.hpp>
#include "Object.h"
@@ -14,7 +13,57 @@ namespace Donut
std::vector<Object> 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<float>::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