Modules/RayTracer/private/RayTracer.cpp
agent 8dac345afe Math: stateless triangle intersection and source-based topology cache
- TrigCache::castRay returns std::optional<halnf> for the hit distance instead
  of writing a static hit position through static scratch variables, so ray
  casts are reentrant.
- TopologyCache::update takes its source Topology instead of holding a pointer
  that dangled whenever a Buffer moved the objects it referred to.
2026-10-29 12:51:39 +03:00

186 lines
5.3 KiB
C++

#include "MathCommon.hpp"
#include "ConnectionCommon.hpp"
#include "Module.hpp"
#include "Ray.hpp"
#include "RayTracer.hpp"
#include "TypeInfo.hpp"
#include <cstdio>
using namespace tp;
void RayTracer::cycle(const RayHit& castData, LightData& out, uhalni depth) {
if (depth) {
depth--;
Vec3F normal = castData.trig->getNormal();
normal.normalize();
const auto delta1 = castData.trig->mEdgeP1P2.unitV();
const auto delta2 = normal.cross(delta1);
for (auto idx : IterRange(mSettings.spray)) {
LightData lightData;
auto d1 = ((halnf) randomFloat() - 0.5f) * 2;
auto d2 = ((halnf) randomFloat() - 0.5f) * 2;
auto sprayNormal = (normal + delta1 * d1 + delta2 * d2).normalize();
const auto bounce = mScene->castRay({ sprayNormal, castData.position }, mScene->mCamera.getFar());
if (bounce) {
cycle(*bounce, lightData, depth);
out.intensity += lightData.intensity * 0.2;
}
}
}
// cast for light
for (auto light : mScene->mLights) {
auto dir = light->pos - castData.position;
auto length = (halnf) dir.length();
Ray lightRay = { dir.unitV(), castData.position };
if (lightRay.dir.dot(castData.trig->mNormal) < 0) {
continue;
}
if (mScene->castRay(lightRay, length)) {
continue;
}
out.intensity += light->intensity / (length * length);
}
}
void RayTracer::render(const Scene& scene, OutputBuffers& out, const RenderSettings& settings) {
out.color.reserve({ settings.size.x, settings.size.y });
out.normals.reserve({ settings.size.x, settings.size.y });
out.depth.reserve({ settings.size.x, settings.size.y });
out.albedo.reserve({ settings.size.x, settings.size.y });
mScene = &scene;
mSettings = settings;
auto pos = mScene->mCamera.getPos();
auto camera = mScene->mCamera;
const auto planeLeftTop = camera.project({ -1, -1 });
const auto planeRightTop = camera.project({ 1, -1 });
const auto planeRightBottom = camera.project({ 1, 1 });
const auto up = (planeRightBottom - planeRightTop);
const auto right = planeRightTop - planeLeftTop;
Ray ray = { { 0, 0, 0 }, pos };
Vec3F iterPoint = { 0, 0, 0 };
Vec3F deltaX = right / halnf(mSettings.size.x);
Vec3F deltaY = up / halnf(mSettings.size.y);
ualni maxIterations = mSettings.size.x * mSettings.size.y;
ualni currIter = 0;
halnf maxDepth = 0;
halnf minDepth = mScene->mCamera.getFar() * mSettings.multisampling;
auto accumulateColor = [](RGBA& col, const RGBA& in) {
col.r += in.r;
col.g += in.g;
col.b += in.b;
col.a = 1;
};
auto divideColor = [](RGBA& col, const halnf num) {
col.r /= num;
col.g /= num;
col.b /= num;
};
// clear colors
for (ualni i = 0; i < mSettings.size.x; i++) {
for (ualni j = 0; j < mSettings.size.y; j++) {
out.color.set({ i, j }, 0.f);
out.normals.set({ i, j }, 0.f);
out.depth.set({ i, j }, 0.f);
out.albedo.set({ i, j }, 0.f);
}
}
for (ualni i = 0; i < mSettings.size.x; i++) {
for (ualni j = 0; j < mSettings.size.y; j++) {
for (auto sample = 0; sample < mSettings.multisampling; sample++) {
auto randX = randomFloat();
auto randY = randomFloat();
iterPoint = planeLeftTop + ((deltaX * (halnf) (i + randX)) + (deltaY * (halnf) (j + randY)));
ray.dir = (iterPoint - pos).unitV();
const auto castData = mScene->castRay(ray, mScene->mCamera.getFar());
halni albedoColor = abs(hash((alni) (castData ? castData->object->id() : 0)));
halnf albedoColorR = float((albedoColor & 0x00000011) % 155) + 100;
halnf albedoColorG = float((albedoColor & 0x00001100) % 155) + 100;
halnf albedoColorB = float((albedoColor & 0x00110000) % 155) + 100;
out.albedo.set({ i, j }, { albedoColorR, albedoColorG, albedoColorB, 1.f });
if (castData) {
LightData lightData;
cycle(*castData, lightData, mSettings.depth);
const auto normal = castData->trig->getNormal();
const auto depth = castData->distance;
lightData.intensity = clamp(lightData.intensity, 0.f, 1.f);
RGBA col = { lightData.intensity, lightData.intensity, lightData.intensity, 1.f };
accumulateColor(out.color.get({ i, j }), col);
accumulateColor(out.normals.get({ i, j }), { normal.x * 0.5f + 0.5f, normal.y * 0.5f + 0.5f, normal.z * 0.5f + 0.5f, 1.f });
accumulateColor(out.depth.get({ i, j }), { depth, depth, depth, 1.f });
} else {
// out.color.set({ i, j }, 0.f);
// out.normals.set({ i, j }, 0.f);
// out.depth.set({ i, j }, 0.f);
}
// auto tmp = buff.get({i, j});
// printf(" %f, %f, %f, %f, ", tmp.r, tmp.g, tmp.b, tmp.a);
}
mProgress.percentage = (halnf) currIter / (halnf) maxIterations;
currIter++;
}
}
for (auto i = 0; i < mSettings.size.x * mSettings.size.y; i++) {
divideColor(out.color.getBuff()[i], (halnf) mSettings.multisampling);
divideColor(out.normals.getBuff()[i], (halnf) mSettings.multisampling);
divideColor(out.depth.getBuff()[i], (halnf) mSettings.multisampling);
}
for (auto i = 0; i < mSettings.size.x * mSettings.size.y; i++) {
if (!out.depth.getBuff()[i].a) {
continue;
}
const auto depth = out.depth.getBuff()[i].r;
if (maxDepth < depth) {
maxDepth = depth;
}
if (minDepth > depth) {
minDepth = depth;
}
}
for (auto i = 0; i < mSettings.size.x * mSettings.size.y; i++) {
auto& col = out.depth.getBuff()[i];
if (col.a == 1.f) {
col.r = (col.r - minDepth) / (maxDepth - minDepth);
col.g = col.r;
col.b = col.r;
}
}
}