module now traces rays but i need a ray referenec system

This commit is contained in:
benkyd
2023-01-09 17:30:40 +00:00
parent f3264b7fe4
commit 68cc749fde
6 changed files with 72 additions and 13 deletions

View File

@@ -2,6 +2,7 @@
#include <hart_graphics.hpp>
#include <tracing/ray.hpp>
#include <tracing/hit.hpp>
#include <iostream>
#include <thread>
@@ -29,7 +30,9 @@ public:
int ic) override
{
mState = EModuleState::Build;
spdlog::info("[hartcpu] Recieved {} verticies and {} indicies", vc / 3, ic / 3);
mVert = (float*)vert; mNorm = (float*)norm; mVc = vc; mIndicies = (uint32_t*)indicies; mIc = ic;
spdlog::info("[hartcpu] Recieved {} verticies ({}) and {} indicies ({})", vc / 3, vert, ic / 3, indicies);
mState = EModuleState::Ready;
}
@@ -46,13 +49,53 @@ public:
mState = EModuleState::Ready;
continue;
}
std::this_thread::sleep_for(std::chrono::microseconds(10));
mState = EModuleState::Trace;
auto* ray = mToTrace.front();
int bestIdx = -1;
glm::vec2 coords;
glm::vec2 bestTexcoord;
float bestDist = INFINITY;
float dist;
for (int i = 0; i < mIc; i += 9)
{
uint32_t ind = mIndicies[i];
// Check if the ray intersects
const glm::vec3 a = { mVert[ind + 1], mVert[ind + 2], mVert[ind + 3] };
const glm::vec3 b = { mVert[ind + 4], mVert[ind + 5], mVert[ind + 6] };
const glm::vec3 c = { mVert[ind + 7], mVert[ind + 8], mVert[ind + 9] };
if (!glm::intersectRayTriangle(ray->Origin, ray->Direction, a, b, c, coords, dist)) { continue; }
if (dist > bestDist || dist < 0.0f) { continue; }
bestIdx = i;
bestDist = dist;
bestTexcoord = coords;
}
HitInfo* hit = new HitInfo{};
// If no hit, we still need to inform the HHM
if (bestIdx < 0)
{
mToTrace.pop();
continue;
}
hit->Distance = bestDist;
hit->UV = bestTexcoord;
Hit(mCtx, hit);
mToTrace.pop();
}
}
private:
float* mVert;
float* mNorm;
int mVc;
uint32_t* mIndicies;
int mIc;
private:
std::thread mMasterWorker;
};

View File

@@ -1 +1,16 @@
#pragma once
#include <hart_graphics.hpp>
namespace inferno {
struct HitInfo
{
// indicie of hit ^ mesh idx of hit
int Identifier;
glm::vec2 UV;
glm::vec3 SurfaceNormal;
float Distance;
};
}

View File

@@ -41,10 +41,10 @@ void HHM::newScene(Scene* scene)
// as it is now, submitTris assumes it's getting the whole scene
// which would involve a lot of mesh copying (avoid!) if i were to chain them
for (auto* mesh : meshs) {
spdlog::debug("Mesh for module ready...");
void* verticies; void* normals; void* indicies;
int vertexCount = mesh->getVerticies(verticies, normals);
int indexCount = mesh->getIndicies(indicies);
spdlog::debug("Mesh for module ready... {} {}", verticies, normals);
mod->submitTris(verticies, normals, vertexCount, indicies, indexCount);
}
}
@@ -63,7 +63,7 @@ void rayHitCallback(void* hhm, HitInfo* hit)
void HHM::rayReturn(HitInfo* hit)
{
HARTModule* mod = mDirectory.getActiveModule();
spdlog::debug("HIT!!");
}
void HHM::bounce(Ray* newRay)

View File

@@ -137,11 +137,11 @@ int Inferno::run()
cornell.setMaterial(&basicMaterial);
mScene->addMesh(&cornell);
Mesh dragon;
dragon.loadOBJ("res/dragon-cornell-size.obj");
dragon.ready();
dragon.setMaterial(&basicMaterial);
mScene->addMesh(&dragon);
// Mesh dragon;
// dragon.loadOBJ("res/dragon-cornell-size.obj");
// dragon.ready();
// dragon.setMaterial(&basicMaterial);
// mScene->addMesh(&dragon);
Camera camera;
mScene->setCamera(&camera);

View File

@@ -74,14 +74,15 @@ void Mesh::ready()
spdlog::debug("Mesh for preview ready...");
}
int Mesh::getVerticies(void* v, void* n)
int Mesh::getVerticies(void*& v, void*& n)
{
v = (void*)&mObjLoader->getPositions()[0];
n = (void*)&mObjLoader->getNormals()[0];
spdlog::debug("Mesh get {} {}", v, n);
return mObjLoader->getVertCount();
}
int Mesh::getIndicies(void* i)
int Mesh::getIndicies(void*& i)
{
i = (void*)&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(void*& v, void*& n);
int getIndicies(void*& i);
int getIndexCount();