From 200ecad6b39bf225904dfcd69e09c449fe14d232 Mon Sep 17 00:00:00 2001 From: benkyd Date: Fri, 20 Jan 2023 13:21:17 +0000 Subject: [PATCH] And you may ask yourself \'Well, how did i get here?\' (memory leak) --- src/hart_module.cpp | 1 - src/renderer/renderer.cpp | 77 ++++++++++++++++++++++++++++++++------- src/scene/mesh.cpp | 2 +- 3 files changed, 64 insertions(+), 16 deletions(-) diff --git a/src/hart_module.cpp b/src/hart_module.cpp index b6498f6..2df1433 100644 --- a/src/hart_module.cpp +++ b/src/hart_module.cpp @@ -65,7 +65,6 @@ void rayHitCallback(void* hhm, HitInfo* hit) void HHM::rayReturn(HitInfo* hit) { HARTModule* mod = mDirectory.getActiveModule(); - spdlog::debug("HIT!!"); Renderer->computeHit(hit); } diff --git a/src/renderer/renderer.cpp b/src/renderer/renderer.cpp index 69ee7e5..0f3381e 100644 --- a/src/renderer/renderer.cpp +++ b/src/renderer/renderer.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -99,25 +100,72 @@ void RayRenderer::draw() } mCurrentRefTable = &startRays.Reference; - mIface->startTrace(startRays.Field); + // mIface->startTrace(startRays.Field); // hault wait for the module to finish - bool frameStatus = false; - while (!frameStatus) + // bool frameStatus = false; + // while (!frameStatus) + // { + // switch(mIface->getModuleState()) + // { + // case EModuleState::Bad: + // spdlog::error("MODULE STATE BAD"); + // break; + // case EModuleState::Build: + // spdlog::error("MODULE STATE BUILD"); + // case EModuleState::Trace: + // break; + // case EModuleState::Ready: + // frameStatus = true; + // break; + // } + // } + + while(!startRays.Field.empty()) { - switch(mIface->getModuleState()) + void* verticies; void* normals; void* indicies; + int vertexCount = mCurrentScene->getRenderables()[0]->getVerticies(verticies, normals); + int indexCount = mCurrentScene->getRenderables()[0]->getIndicies(indicies); + float* mVert = (float*)verticies; float* mNorm = (float*)normals; + int mVc = vertexCount; uint32_t* mIndicies = (uint32_t*)indicies; int mIc = indexCount; + + auto* ray = startRays.Field.front(); + int bestIdx = -1; + glm::vec2 coords; + glm::vec2 bestTexcoord; + float bestDist = INFINITY; + float dist; + for (int i = 0; i < mCurrentScene->getRenderables()[0]->getIndexCount() ; i += 9) { - case EModuleState::Bad: - spdlog::error("MODULE STATE BAD"); - break; - case EModuleState::Build: - spdlog::error("MODULE STATE BUILD"); - case EModuleState::Trace: - break; - case EModuleState::Ready: - frameStatus = true; - break; + 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) + { + startRays.Field.pop_back(); + continue; + } + + hit->Distance = bestDist; + hit->UV = bestTexcoord; + + glm::ivec2 pos = (*mCurrentRefTable)[ray->Reference]; + spdlog::debug("HIT!!"); + mTarget[pos.y * mRenderTargetSize.x + pos.x] = { bestDist, bestDist, bestDist, 1.0f }; + startRays.Field.pop_back(); + // this->computeHit(hit); } spdlog::info("Sample complete"); @@ -130,6 +178,7 @@ void RayRenderer::draw() void RayRenderer::computeHit(HitInfo* info) { + spdlog::debug("HIT!!"); glm::ivec2 pos = (*mCurrentRefTable)[info->Caller->Reference]; std::lock_guard lock(this->_mTarget); float d = info->Distance; diff --git a/src/scene/mesh.cpp b/src/scene/mesh.cpp index 65d892d..c0f39ff 100644 --- a/src/scene/mesh.cpp +++ b/src/scene/mesh.cpp @@ -78,7 +78,7 @@ int Mesh::getVerticies(void*& v, void*& n) { v = (void*)&mObjLoader->getPositions()[0]; n = (void*)&mObjLoader->getNormals()[0]; - spdlog::debug("Mesh get {} {}", v, n); + // spdlog::debug("Mesh get {} {}", v, n); return mObjLoader->getVertCount(); }