2022-12-06 13:49:37 +01:00
|
|
|
#include "camera/camera.h"
|
|
|
|
#include "renderer/kdtreerenderer.h"
|
|
|
|
#include "scene/fastscene.h"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
Texture KDTreeRenderer::renderImage(Scene const &scene, Camera const &camera, int width, int height) {
|
2022-12-06 18:00:40 +01:00
|
|
|
Texture image(width, height);
|
|
|
|
std::cout << "KD Tree renderer will only output KD Tree visualization" << std::endl;
|
|
|
|
return image;
|
2022-12-06 13:49:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Texture KDTreeRenderer::renderKDTree(FastScene const &scene, Camera const &camera, int width, int height) {
|
|
|
|
Texture image(width, height);
|
2022-12-10 18:25:29 +01:00
|
|
|
float const aspectRatio = static_cast<float>(height) / width;
|
2022-12-06 13:49:37 +01:00
|
|
|
|
2022-12-10 18:25:29 +01:00
|
|
|
for (int x = 0; x < image.width(); ++x) {
|
|
|
|
for (int y = 0; y < image.height(); ++y) {
|
|
|
|
Ray ray = camera.createRay((static_cast<float>(x) / width * 2 - 1),
|
|
|
|
-(static_cast<float>(y) / height * 2 - 1) * aspectRatio);
|
|
|
|
|
|
|
|
image.setPixelAt(x, y, Color(float(scene.countNodeIntersections(ray)), 0.0f, 0.0f));
|
|
|
|
}
|
|
|
|
}
|
2022-12-06 13:49:37 +01:00
|
|
|
|
2022-12-10 18:25:29 +01:00
|
|
|
// map color to green -> red map
|
|
|
|
float maxVal = -INFINITY;
|
|
|
|
for (int x = 0; x < image.width(); ++x) {
|
|
|
|
for (int y = 0; y < image.height(); ++y) {
|
|
|
|
maxVal = std::max(maxVal, image.getPixelAt(x, y).r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Color r(1, 0, 0);
|
|
|
|
Color g(0, 1, 0);
|
|
|
|
for (int x = 0; x < image.width(); ++x) {
|
|
|
|
for (int y = 0; y < image.height(); ++y) {
|
|
|
|
float factor = image.getPixelAt(x, y).r / maxVal;
|
|
|
|
image.setPixelAt(x, y, (1.0f - factor) * g + factor * r);
|
|
|
|
}
|
|
|
|
}
|
2022-12-06 13:49:37 +01:00
|
|
|
|
2022-12-06 18:00:40 +01:00
|
|
|
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;
|
2022-12-06 18:12:58 +01:00
|
|
|
ints.reserve(height);
|
2022-12-06 18:00:40 +01:00
|
|
|
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));
|
2022-12-06 18:12:58 +01:00
|
|
|
max = std::max(ints[y], max);
|
2022-12-06 18:00:40 +01:00
|
|
|
}
|
|
|
|
intersections.push_back(std::move(ints));
|
|
|
|
}
|
|
|
|
for (int x = 0; x < image.width(); ++x) {
|
|
|
|
for (int y = 0; y < image.height(); ++y) {
|
2022-12-06 18:12:58 +01:00
|
|
|
image.setPixelAt(x, y, clamped(static_cast<float>(max - intersections[x][y]) / max * GREEN +
|
|
|
|
static_cast<float>(intersections[x][y]) / max * RED));
|
2022-12-06 18:00:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return image;
|
2022-12-06 13:49:37 +01:00
|
|
|
}
|