Merge issue cleanup

This commit is contained in:
arvid schröder 2022-12-12 14:56:14 +01:00
parent 21d82ec5e2
commit 63a0ec8f28
2 changed files with 49 additions and 71 deletions

View file

@ -38,25 +38,5 @@ Texture KDTreeRenderer::renderKDTree(FastScene const &scene, Camera const &camer
image.setPixelAt(x, y, (1.0f - factor) * g + factor * r); image.setPixelAt(x, y, (1.0f - factor) * g + factor * r);
} }
} }
return image;
int max = 0;
float const aspectRatio = static_cast<float>(height) / static_cast<float>(width);
for (int x = 0; x < image.width(); ++x) {
std::vector<int> ints;
ints.reserve(height);
for (int y = 0; y < image.height(); ++y) {
Ray ray = camera.createRay((static_cast<float>(x) / image.width() * 2.0f - 1),
-(static_cast<float>(y) / image.height() * 2 - 1) * aspectRatio);
ints.push_back(scene.countNodeIntersections(ray));
max = std::max(ints[y], max);
}
intersections.push_back(std::move(ints));
}
for (int x = 0; x < image.width(); ++x) {
for (int y = 0; y < image.height(); ++y) {
image.setPixelAt(x, y, clamped(static_cast<float>(max - intersections[x][y]) / max * GREEN +
static_cast<float>(intersections[x][y]) / max * RED));
}
}
return image;
} }

View file

@ -5,76 +5,74 @@
#include <thread> #include <thread>
#include <chrono> #include <chrono>
#include <iomanip> #include <iomanip>
#include <thread>
void SimpleRenderer::renderThread(const Scene *scene, Camera const *camera, Texture *image, int width, int widthStep, int widthOffset, int height, int heightStep, int heightOffset, std::atomic<int> *k, int const stepSize) { void SimpleRenderer::renderThread(const Scene *scene, Camera const *camera, Texture *image, int width, int widthStep, int widthOffset, int height, int heightStep, int heightOffset, std::atomic<int> *k, int const stepSize) {
float const aspectRatio = static_cast<float>(height) / width; float const aspectRatio = static_cast<float>(height) / width;
for (int y = heightOffset; y < height; y += heightStep) { for (int y = heightOffset; y < height; y += heightStep) {
for (int x = widthOffset; x < width; x += widthStep) { for (int x = widthOffset; x < width; x += widthStep) {
Ray ray = camera->createRay((static_cast<float>(x) / width * 2 - 1), -(static_cast<float>(y) / height * 2 - 1) * aspectRatio); Ray ray = camera->createRay((static_cast<float>(x) / width * 2 - 1), -(static_cast<float>(y) / height * 2 - 1) * aspectRatio);
image->setPixelAt(x, y, clamped(scene->traceRay(ray))); image->setPixelAt(x, y, clamped(scene->traceRay(ray)));
// Super hacky progress bar! // Super hacky progress bar!
if (++*k % stepSize == 0) { if (++*k % stepSize == 0) {
std::cout << "=" << std::flush; std::cout << "=" << std::flush;
} }
}
} }
}
} }
Texture SimpleRenderer::renderImage(Scene const &scene, Camera const &camera, int width, int height) { Texture SimpleRenderer::renderImage(Scene const &scene, Camera const &camera, int width, int height) {
Texture image(width, height); Texture image(width, height);
// Setup timer // Setup timer
std::chrono::steady_clock::time_point start, stop; std::chrono::steady_clock::time_point start, stop;
// Reset Ray counting // Reset Ray counting
Ray::resetRayCount(); Ray::resetRayCount();
// Super-hacky progress bar! // Super-hacky progress bar!
std::cout << "(SimpleRenderer): Begin rendering..." << std::endl; std::cout << "(SimpleRenderer): Begin rendering..." << std::endl;
std::cout << "| 0%"; std::cout << "| 0%";
int const barSizeGoal = 100; int const barSize = 50;
int const stepSize = width / barSizeGoal; int const stepSize = (width * height) / barSize;
for (int i = 0; i < width / stepSize - 3 - 5; ++i) for (int i = 0; i < barSize - 3 - 5; ++i)
std::cout << " "; std::cout << " ";
std::cout << "100% |" << std::endl << "|"; std::cout << "100% |" << std::endl << "|";
std::atomic<int> k(0); std::atomic<int> k(0);
// Start timer /* Start timer */ start = std::chrono::steady_clock::now();
start = std::chrono::steady_clock::now();
// Spawn a thread for every logical processor -1, calling the renderThread function // Spawn a thread for every logical processor -1, calling the renderThread function
int const nThreads = std::thread::hardware_concurrency(); int const nThreads = std::thread::hardware_concurrency();
std::vector<std::thread> threads; std::vector<std::thread> threads;
for (int t = 0; t < nThreads - 1; ++t) { for (int t = 0; t < nThreads - 1; ++t) {
threads.emplace_back(renderThread, &scene, &camera, &image, width, nThreads, t, height, 1, 0, &k, stepSize); threads.emplace_back(renderThread, &scene, &camera, &image, width, nThreads, t, height, 1, 0, &k, stepSize);
} }
// Call the renderThread function yourself // Call the renderThread function yourself
renderThread(&scene, &camera, &image, width, nThreads, nThreads - 1, height, 1, 0, &k, stepSize); renderThread(&scene, &camera, &image, width, nThreads, nThreads - 1, height, 1, 0, &k, stepSize);
// Rejoin the threads // Rejoin the threads
for (int t = 0; t < nThreads - 1; ++t) { for (int t = 0; t < nThreads - 1; ++t) {
threads[t].join(); threads[t].join();
} }
// Stop timer // Stop timer
stop = std::chrono::steady_clock::now(); stop = std::chrono::steady_clock::now();
std::cout << "| Done!" << std::endl; std::cout << "| Done!" << std::endl;
// Calculate the Time taken in seconds // Calculate the Time taken in seconds
double seconds = std::chrono::duration_cast<std::chrono::duration<double>>(stop - start).count(); double seconds = std::chrono::duration_cast<std::chrono::duration<double>>(stop - start).count();
std::cout << "Time: " << seconds << "s" << std::endl; std::cout << "Time: " << seconds << "s" << std::endl;
// Get the number of seconds per ray // Get the number of seconds per ray
int rays = Ray::getRayCount(); int rays = Ray::getRayCount();
std::cout << "Paths: " << rays << std::endl; std::cout << "Paths: " << rays << std::endl;
std::cout << "Paths per second: " << std::fixed << std::setprecision(0) << rays / seconds << std::endl; std::cout << "Paths per second: " << std::fixed << std::setprecision(0) << rays / seconds << std::endl;
return image; return image;
} }