minor fix

This commit is contained in:
Dynamitos
2025-01-27 17:09:16 +01:00
parent 5df03e280b
commit 8bcf38834e
3 changed files with 16 additions and 15 deletions
+11 -8
View File
@@ -20,15 +20,18 @@ Renderer::~Renderer() {}
void Renderer::startRender(Camera cam, RenderParameter params) void Renderer::startRender(Camera cam, RenderParameter params)
{ {
threadPool.cancel(); //threadPool.cancel();
pendingCancel = true; if (running)
if (worker.joinable()) {
running = false;
worker.join(); worker.join();
pendingCancel = false; }
sampleTimes.clear();
image.clear(); image.clear();
accumulator.clear(); accumulator.clear();
image.resize(params.width * params.height); image.resize(params.width * params.height);
accumulator.resize(params.width * params.height); accumulator.resize(params.width * params.height);
running = true;
worker = std::thread(&Renderer::render, this, cam, params); worker = std::thread(&Renderer::render, this, cam, params);
} }
@@ -43,7 +46,7 @@ void Renderer::render(Camera camera, RenderParameter params)
{ {
for (int samp = 0; samp < params.numSamples; ++samp) for (int samp = 0; samp < params.numSamples; ++samp)
{ {
if (pendingCancel) if (!running)
return; return;
auto start = std::chrono::high_resolution_clock::now(); auto start = std::chrono::high_resolution_clock::now();
Batch batch; Batch batch;
@@ -76,7 +79,7 @@ void Renderer::render(Camera camera, RenderParameter params)
0.5f) * 0.5f) *
sdim; sdim;
glm::vec3 spos = cam.origin + cx * s.x + cy * s.y, lc = cam.origin + cam.direction * 0.035f; // sample on 3d sensor plane glm::vec3 spos = cam.origin + cx * s.x + cy * s.y, lc = cam.origin + cam.direction * 0.035f; // sample on 3d sensor plane
Ray r = Ray(lc, normalize(lc - spos)); // construct ray Ray r = Ray(lc, normalize(lc - spos)); // construct ray
//-- setup lens //-- setup lens
glm::vec3 lensP = lc; glm::vec3 lensP = lc;
@@ -89,7 +92,7 @@ void Renderer::render(Camera camera, RenderParameter params)
glm::vec3 focalPoint = cam.origin + (camera.S_O + S_I) * cam.direction; glm::vec3 focalPoint = cam.origin + (camera.S_O + S_I) * cam.direction;
float t = glm::dot(focalPoint - r.origin, lensN) / glm::dot(r.direction, lensN); float t = glm::dot(focalPoint - r.origin, lensN) / glm::dot(r.direction, lensN);
glm::vec3 focus = r.origin + t * r.direction; glm::vec3 focus = r.origin + t * r.direction;
r = Ray(lensSample, normalize(focus - lensSample)); // TODO: Fix lens // r = Ray(lensSample, normalize(focus - lensSample)); // TODO: Fix lens
bvh.traceRay(r, payload, 1e-4, 1e20); bvh.traceRay(r, payload, 1e-4, 1e20);
@@ -101,7 +104,7 @@ void Renderer::render(Camera camera, RenderParameter params)
threadPool.runBatch(std::move(batch)); threadPool.runBatch(std::move(batch));
auto end = std::chrono::high_resolution_clock::now(); auto end = std::chrono::high_resolution_clock::now();
sampleTimes.push_back(std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000.0f); sampleTimes.push_back(std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000.0f);
float resolver = float(params.numSamples) / float(samp); float resolver = float(params.numSamples) / float(samp+1);
for (uint32_t i = 0; i < accumulator.size(); ++i) for (uint32_t i = 0; i < accumulator.size(); ++i)
{ {
image[i] = glm::pow(glm::max(accumulator[i] * resolver, 0.0f), glm::vec3(0.45f)); image[i] = glm::pow(glm::max(accumulator[i] * resolver, 0.0f), glm::vec3(0.45f));
+1 -1
View File
@@ -30,7 +30,7 @@ private:
virtual void render(Camera cam, RenderParameter params); virtual void render(Camera cam, RenderParameter params);
ThreadPool threadPool; ThreadPool threadPool;
std::thread worker; std::thread worker;
std::atomic_bool pendingCancel = false; std::atomic_bool running = false;
std::vector<float> sampleTimes; std::vector<float> sampleTimes;
float lastSampleTime; float lastSampleTime;
float averageSampleTime; float averageSampleTime;
+4 -6
View File
@@ -80,7 +80,7 @@ void Scene::generate()
void Scene::traceRay(Ray ray, Payload& payload, const float tmin, const float tmax) const noexcept void Scene::traceRay(Ray ray, Payload& payload, const float tmin, const float tmax) const noexcept
{ {
IntersectionInfo info = generateIntersections(hierarchy, ray, tmin, tmax); IntersectionInfo info = generateIntersections(hierarchy, ray, tmin, tmax);
if (info.hitInfo.t < std::numeric_limits<float>::max()) if (info.hitInfo.t < std::numeric_limits<float>::max())
{ {
// russian roulette ray termination // russian roulette ray termination
@@ -154,12 +154,11 @@ bool Scene::testIntersection(const PNode& currentNode, const Ray ray, const floa
return leftResults || rightResults; return leftResults || rightResults;
} }
IntersectionInfo Scene::generateIntersections(const PNode& currentNode, const Ray ray, const float tmin, IntersectionInfo Scene::generateIntersections(const PNode& currentNode, const Ray ray, const float tmin, float tmax) const noexcept
float tmax) const noexcept
{ {
if (!currentNode->aabb.intersects(ray, tmin, tmax)) if (!currentNode->aabb.intersects(ray, tmin, tmax))
{ {
// return {}; return {};
} }
if (currentNode->model.numIndices > 0) if (currentNode->model.numIndices > 0)
{ {
@@ -219,8 +218,7 @@ bool Scene::testModel(const ModelReference& reference, const Ray ray, const floa
return false; return false;
} }
IntersectionInfo Scene::intersectModel(const ModelReference& reference, const Ray ray, const float tmin, IntersectionInfo Scene::intersectModel(const ModelReference& reference, const Ray ray, const float tmin, float tmax) const noexcept
float tmax) const noexcept
{ {
IntersectionInfo intersection = {}; IntersectionInfo intersection = {};