hartcpu: we are tracing rays correctly!

This commit is contained in:
Benjamin Kyd
2023-02-17 16:05:06 +00:00
parent 6f91c44b77
commit 9135fe1469
3 changed files with 24 additions and 13 deletions

View File

@@ -60,7 +60,6 @@ public:
if (!interrupt)
{
mIsRunning = false;
mMasterWorker.join();
return;
}
// TODO: Find a way to force the thread to hault
@@ -91,16 +90,23 @@ public:
glm::vec2 bestTexcoord;
float bestDist = INFINITY;
float dist;
for (int i = 0; i < mIc; i += 9)
for (int i = 0; i < mIc; i += 3)
{
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] };
uint32_t ind1 = mIndicies[i];
uint32_t ind2 = mIndicies[i + 1];
uint32_t ind3 = mIndicies[i + 2];
const glm::vec3 a = { mVert[ind1 * 3], mVert[ind1 * 3 + 1], mVert[ind1 * 3 + 2] };
const glm::vec3 b = { mVert[ind2 * 3], mVert[ind2 * 3 + 1], mVert[ind2 * 3 + 2] };
const glm::vec3 c = { mVert[ind3 * 3], mVert[ind3 * 3 + 1], mVert[ind3 * 3 + 2] };
// Perform intersection test...
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;
@@ -121,7 +127,6 @@ public:
Hit(mCtx, &hit);
mToTrace.pop();
spdlog::debug("[hartcpu] One (1) sample completed");
}
}

View File

@@ -49,12 +49,16 @@ ReferencedRayField RaySource::getInitialRays(bool MSAA)
for (int x = 0; x < mReferenceCamera->getRayViewport().x; x++)
for (int y = 0; y < mReferenceCamera->getRayViewport().y; y++)
{
float Px = (2.0f * ((x + 0.5f) / mReferenceCamera->getRayViewport().x) - 1.0f) * scale * aspect;
float Py = (1.0f - 2.0f * ((y + 0.5f) / mReferenceCamera->getRayViewport().y) * scale);
float Px = (2.0f * ((x + 0.5f) / mReferenceCamera->getRayViewport().x) - 1.0f) * scale * aspect;
float Py = (2.0f * ((y + 0.5f) / mReferenceCamera->getRayViewport().y) - 1.0f) * scale;
Ray* ray = new Ray{};
glm::vec4 dir4 = glm::vec4(Px, Py, -1.0f, 1.0f) * cameraToWorld;
glm::vec3 dir3 = glm::vec3(dir4) / dir4.w;
ray->Direction = glm::normalize(dir3);
ray->Origin = origin;
ray->Direction = glm::normalize((glm::vec4(Px, Py, -1.0f, 1.0f) * cameraToWorld));
ray->Reference = i;
reference[i] = {x, y};

View File

@@ -91,6 +91,8 @@ void RayRenderer::prepare()
void RayRenderer::draw()
{
mCurrentScene->newFrame();
// TODO: Rays should definately be bump allocated if possible, this is KBs of
// ray data and nothing else being reallocated every frame for no reason
ReferencedRayField startRays = mRaySource->getInitialRays(true);
for (int x = 0; x < mRenderTargetSize.x; x++)
@@ -120,6 +122,6 @@ void RayRenderer::computeHit(HitInfo* info)
return;
}
glm::ivec2 pos = (*mCurrentRefTable)[info->Caller->Reference];
float d = info->Distance;
float d = info->Distance;
mTarget[pos.y * mRenderTargetSize.x + pos.x] = { d, d, d, 1.0f };
}