Files
inferno-cpu/src/engine/renderengine.cpp
2019-08-31 15:30:05 +01:00

135 lines
3.8 KiB
C++

#include "renderengine.hpp"
#include "../pixel.hpp"
#include "../util/threadpool.hpp"
#include "../definitions/primatives/primative.hpp"
#include "../definitions/camera.hpp"
#include "../definitions/scene.hpp"
#include "../definitions/ray.hpp"
#include "../display/displayinterface.hpp"
#include "../display/framebuffer.hpp"
#include "../display/tonemapfb.hpp"
#include "../engine/renderengine.hpp"
#include "../engine/progressiverenderer.hpp"
static glm::vec3 Black{ 0.0f, 0.0f, 0.0f };
static glm::vec3 White{ 1.0f, 1.0f, 1.0f };
static glm::vec3 Red { 1.0f, 0.2f, 0.2f };
static glm::vec3 Green{ 0.2f, 1.0f, 0.2f };
static glm::vec3 Blue { 0.2f, 0.2f, 1.0f };
void workerThread(RenderThreadPool* threadpool, ProgressiveRenderer* renderer, int idd, int yStart, int yRange) {
while (!renderer->Ready && !threadpool->Ready) {
std::chrono::milliseconds dura(10);
std::this_thread::sleep_for(dura);
}
while (renderer->Ready && threadpool->Ready) {
for (int y = yStart; y < yStart + yRange; y++)
for (int x = 0; x < renderer->m_scene->w; x++) {
Ray ray = renderer->m_scene->camera->CastRay(x, y);
int depth = 0;
glm::vec3 col = renderer->m_engine->GetColour(ray, depth);
if (renderer->m_engine->Mode == MODE_RENDER_NORMALS) {
threadpool->MappedThreadFrameBuffer->SetPixelSafe(x, y, col);
} else if (renderer->m_engine->Mode == MODE_RENDER_PATHLENGTH) {
col.r = depth; col.g = depth / 3.0f; col.b = depth / 3.0f;
threadpool->MappedThreadFrameBuffer->AddPixelSafeDepth(x, y, col);
} else {
threadpool->MappedThreadFrameBuffer->AddPixelSafe(x, y, col);
}
}
threadpool->ThreadStatus[idd] = true;
while (threadpool->ThreadStatus[idd]) {
std::chrono::nanoseconds dura(1);
std::this_thread::sleep_for(dura);
}
}
}
RenderEngine::RenderEngine() {
}
void RenderEngine::SetScene(Scene* scene) {
m_scene = scene;
}
glm::vec3 RenderEngine::GetColour(Ray ray, int& depth) {
if (depth > 5) return { 0.0f, 0.0f, 0.0f };
float t; Primative* hit = nullptr;
bool didhit = TraceRayScene(ray, m_scene, t, hit);
if (!didhit) return m_scene->SampleSky(ray);
glm::vec3 hitPoint = ray.origin + ray.direction * t;
glm::vec3 normal = hit->SurfaceNormal(hitPoint);
if (Mode == MODE_RENDER_NORMALS) { return GetNormalColour(normal); }
glm::vec3 colour = hit->material->Colour;
if (hit->material->Emissive) return (colour * hit->material->Emittance);
//if (hit->type == TYPE_PLANE) {
// glm::vec2 uv = hit->TexCoords(hitPoint);
// float angle = fastDegreetoRadian(.0f);
// float s = uv.x * cos(angle) - uv.y * sin(angle);
// float t = uv.y * cos(angle) + uv.x * sin(angle);
// float S = 0.05f; float T = 0.05f;
// float pattern = (modulo(s * S) < 0.5f) ^ (modulo(t * T) < 0.5f);
// colour.r = pattern; colour.g = pattern; colour.b = pattern;
//}
glm::vec3 direction = hit->material->Bounce(ray.direction, normal);
Ray newRay{ hitPoint, direction };
// Prevent acne
if (glm::dot(newRay.direction, normal) < 0.0f) {
newRay.origin = ray.origin + ray.direction * t - normal * EPSILON;
}
else {
newRay.origin = ray.origin + ray.direction * t + normal * EPSILON;
}
depth++;
return GetColour(newRay, depth) * colour;
}
void RenderEngine::PostProcess(glm::vec3* src, glm::vec3* dst, int w, int h) {
if (Mode == MODE_RENDER_NORMALS) {
SPP = 0;
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {
dst[y * w + x] = src[y * w + x];
}
return;
}
if (Mode == MODE_RENDER_PATHLENGTH) {
SPP = 0;
SPPDepth++;
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {
dst[y * w + x] = src[y * w + x] / (float)SPPDepth;
}
return;
}
SPP++;
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {
dst[y * w + x] = src[y * w + x] / (float)SPP;
}
}
glm::vec3 RenderEngine::GetNormalColour(glm::vec3 normal) {
return ((normal + 1.0f) * 127.5f) / 255.0f;
}