Files
inferno-hart/src/renderer/object_tracer.cpp
2023-10-21 01:29:46 +01:00

72 lines
2.2 KiB
C++

#include "object_tracer.hpp"
#include "scene/object.hpp"
#include <cmath>
#include <maths.hpp>
#include <yolo/yolo.hpp>
namespace inferno::graphics::rays {
bool triangle_ray_collide(Ray* ray, float* t, glm::vec3 vertex0, glm::vec3 vertex1, glm::vec3 vertex2)
{
glm::vec3 edge1, edge2, h, s, q;
float a, f, u, v;
edge1 = vertex1 - vertex0;
edge2 = vertex2 - vertex0;
h = glm::cross(ray->Direction, edge2);
a = glm::dot(edge1, h);
if (a > -helpers::EPSILON && a < helpers::EPSILON)
return false; // This ray is parallel to this triangle.
f = 1.0 / a;
s = ray->Origin - vertex0;
u = f * glm::dot(s, h);
if (u < 0.0 || u > 1.0)
return false;
q = glm::cross(s, edge1);
v = f * glm::dot(ray->Direction, q);
if (v < 0.0 || u + v > 1.0)
return false;
// At this stage we can compute t to find out where the intersection point is on the line.
*t = f * glm::dot(edge2, q);
if (*t > helpers::EPSILON) // ray intersection
return true;
else // This means that there is a line intersection but not a ray intersection.
return false;
}
HitInfo* object_ray_collide(scene::SceneObject* object, Ray* ray)
{
HitInfo* info = new HitInfo;
info->Distance = INFINITY;
info->Did = false;
for (auto* mesh : scene::scene_object_get_meshs(object)) {
// extract triangles and loop over them, if there is a hit - we return
const uint32_t* ind;
const float* verts;
const float* norms;
mesh->getIndicies(&ind);
mesh->getVerticies(&verts, &norms);
float t = INFINITY;
for (int i = 0; i < mesh->getIndexCount(); i++) {
uint32_t index = ind[i];
glm::vec3 a = { verts[3 * index + 0], verts[3 * index + 1], verts[3 * index + 2] };
glm::vec3 b = { verts[3 * index + 3], verts[3 * index + 4], verts[3 * index + 5] };
glm::vec3 c = { verts[3 * index + 6], verts[3 * index + 7], verts[3 * index + 8] };
float temp_t;
if (!triangle_ray_collide(ray, &temp_t, a, b, c))
continue;
info->Did = true;
if (temp_t < t) {
t = temp_t;
info->Distance = t;
}
}
}
return info;
}
}