actual raytracing (almost)

This commit is contained in:
Benjamin Kyd
2023-10-21 00:17:35 +01:00
parent 0cbcd608b2
commit 7c0b6286ff
4 changed files with 51 additions and 9 deletions

View File

@@ -1,18 +1,59 @@
#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 = nullptr;
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 += 3) {
uint32_t index = ind[i];
glm::vec3 a = { };
glm::vec3 b = { };
glm::vec3 c = { };
}
}
return info;
}
}

View File

@@ -113,7 +113,7 @@ void rayr_draw(RayRenderer* renderer)
for (int x = 0; x < renderer->Viewport.x; x++) {
for (int y = 0; y < renderer->Viewport.y; y++) {
rays::Ray* ray = startRays.Field[x * renderer->Viewport.y + y];
renderer->RenderData[y * renderer->Viewport.x + x] = { ray->Direction.x, ray->Direction.y, ray->Direction.z, 1.0f };
renderer->RenderData[y * renderer->Viewport.x + x] = { ray->Direction.x + 0.5, ray->Direction.y + 0.5, ray->Direction.z + 0.5, 1.0f };
// we want to iterate over every object in the scene and then ask that object for an intersection
for (auto& obj : scene::scene_get_renderables(renderer->Scene)) {

View File

@@ -74,16 +74,17 @@ void Mesh::ready()
yolo::debug("Mesh for preview ready...");
}
int Mesh::getVerticies(void** v, void** n)
int Mesh::getVerticies(const float** v, const float** n)
{
*v = (void*)&mObjLoader->getPositions()[0];
*n = (void*)&mObjLoader->getNormals()[0];
*v = &mObjLoader->getPositions()[0];
*n = &mObjLoader->getNormals()[0];
return mObjLoader->getVertCount();
}
int Mesh::getIndicies(void** i)
int Mesh::getIndicies(const uint32_t** i)
{
*i = (void*)&mObjLoader->getFaces()[0];
*i = &mObjLoader->getFaces()[0];
yolo::debug("{}", &mObjLoader->getFaces()[0]);
return mObjLoader->getIndexCount();
}

View File

@@ -26,8 +26,8 @@ public:
void loadOBJ(std::filesystem::path file);
void ready();
int getVerticies(void** v, void** n);
int getIndicies(void** i);
int getVerticies(const float** v, const float** n);
int getIndicies(const uint32_t** i);
int getIndexCount();