Files
RayTracer/src/scene/Scene.cpp
T

53 lines
1.8 KiB
C++
Raw Normal View History

#include "Scene.h"
2025-01-24 22:23:33 +01:00
#include <iostream>
#include <chrono>
2025-01-24 21:54:38 +01:00
Scene::Scene() {}
2025-01-24 21:01:01 +01:00
Scene::~Scene() {}
2025-01-24 22:23:33 +01:00
static bool firstTime = true;
2025-01-24 21:54:38 +01:00
void Scene::render(Camera cam, RenderParameter params)
2025-01-24 18:21:34 +01:00
{
2025-01-24 22:23:33 +01:00
if (!firstTime)
{
pendingCancel = true;
worker.join();
firstTime = false;
}
2025-01-24 21:54:38 +01:00
pendingCancel = false;
2025-01-24 22:23:33 +01:00
image.clear();
accumulator.clear();
image.resize(params.width * params.height);
accumulator.resize(params.width * params.height);
2025-01-24 21:20:06 +01:00
worker = std::thread(
2025-01-24 23:20:26 +01:00
[&, cam, params]()
2025-01-24 21:01:01 +01:00
{
2025-01-24 21:54:38 +01:00
for (int samp = 0; samp < params.numSamples; ++samp)
2025-01-24 21:01:01 +01:00
{
2025-01-24 21:54:38 +01:00
if (pendingCancel)
return;
Batch batch;
for (int w = 0; w < params.width; ++w)
2025-01-24 21:20:06 +01:00
{
2025-01-24 22:23:33 +01:00
batch.jobs.push_back(
[&, w]()
{
for (int h = 0; h < params.height; ++h)
2025-01-24 21:54:38 +01:00
{
2025-01-24 22:23:33 +01:00
// Ray r = Ray();
// bvh.traceRay(r);
accumulator[w + h * params.width] +=
glm::vec3(w / float(params.width * params.numSamples), h / float(params.height * params.numSamples), 0);
}
});
2025-01-24 21:54:38 +01:00
}
2025-01-24 22:34:23 +01:00
auto start = std::chrono::high_resolution_clock::now();
2025-01-24 21:54:38 +01:00
threadPool.runBatch(std::move(batch));
2025-01-24 22:34:23 +01:00
auto end = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << std::endl;
2025-01-24 22:23:33 +01:00
std::memcpy(image.data(), accumulator.data(), accumulator.size() * sizeof(glm::vec3));
2025-01-24 21:01:01 +01:00
}
2025-01-24 21:20:06 +01:00
});
2025-01-24 18:21:34 +01:00
}