Raytracer returns hit

This commit is contained in:
Ben Kyd
2019-08-06 00:33:12 +01:00
parent 2ec847bd1c
commit cf142ff5be
7 changed files with 54 additions and 10 deletions

View File

@@ -57,6 +57,7 @@ include_directories(${executable}
file(GLOB SourceFiles
${SrcDIR}/*
${SrcDIR}/acceleration/*
${SrcDIR}/core/*
${SrcDIR}/engine/*
${SrcDIR}/display/*

3
src/acceleration/kd.cpp Normal file
View File

@@ -0,0 +1,3 @@
#include "kd.hpp"

38
src/acceleration/kd.hpp Normal file
View File

@@ -0,0 +1,38 @@
#ifndef INFERNO_ACCELERATION_KD_H_
#define INFERNO_ACCELERATION_KD_H_
#include "../maths.hpp"
class Triangle;
class Ray;
class Box {
public:
Box();
Box(Triangle* object);
void ExtendTriangle(Triangle* object);
void ExtendPoint(glm::vec3 p);
int LongestAxis();
bool Hit(Ray* ray);
glm::vec3 min;
glm::vec3 max;
};
class KDTree {
public:
Box bounds;
KDTree* child0 = nullptr;
KDTree* child1 = nullptr;
std::vector<Triangle*> children;
};
KDTree* BuildKDTree(const std::vector<Triangle*>& triangles);
bool KDIntersect(KDTree* tree, Ray* ray, Triangle*& triMin, float& uMin, float& vMin, float& tMin);
#endif

View File

@@ -12,6 +12,7 @@ public:
glm::vec3 SurfaceNormal(glm::vec3 hitPoint) override;
glm::vec2 TexCoords(glm::vec3 hitPoint) override;
void Translate(glm::vec3 trans) override;
glm::vec3 Midpoint(Triangle* t);
};
#endif

View File

@@ -2,16 +2,13 @@
#include "scene.hpp"
#include "primatives/primative.hpp"
bool TraceRay(Ray ray, Scene* scene, float& t, float& iOfHit) {
iOfHit = -1;
bool TraceRay(Ray ray, Scene* scene, float& t, Primative*& hit) {
int i = 0;
float lastDistance = INFINITY;
int index = -1;
for (auto& object : scene->objects) {
float distance = INFINITY;
if (object->DoesIntersect(ray, distance)) {
iOfHit = i;
if (distance < lastDistance) {
index = i;
lastDistance = distance;
@@ -20,8 +17,11 @@ bool TraceRay(Ray ray, Scene* scene, float& t, float& iOfHit) {
i++;
}
iOfHit = index;
t = lastDistance;
return (iOfHit != -1);
if (index == -1) return false;
hit = scene->objects[index];
return true;
}

View File

@@ -4,6 +4,7 @@
#include "../maths.hpp"
class Scene;
class Primative;
class Ray {
public:
@@ -11,6 +12,6 @@ public:
glm::vec3 direction = {};
};
bool TraceRay(Ray ray, Scene* scene, float& t, float& iOfHit);
bool TraceRay(Ray ray, Scene* scene, float& t, Primative*& hit);
#endif

View File

@@ -47,14 +47,14 @@ void ProgressiveRenderer::Render() {
Ray ray = m_scene->camera->CastRay(x, y);
float t, i;
bool didhit = TraceRay(ray, m_scene, t, i);
float t;
Primative* hit = nullptr;
bool didhit = TraceRay(ray, m_scene, t, hit);
if (!didhit) {
m_interface->SetPixelSafe(x, y, 0x000000);
continue;
}
Primative* hit = m_scene->objects[i];
glm::vec3 hitPoint = ray.origin + ray.direction * t;