+ | Portal matrices reflection

This commit is contained in:
TheRedShip
2025-01-08 12:07:26 +01:00
parent fe440958dc
commit 8e4d844f38
8 changed files with 113 additions and 68 deletions

View File

@ -28,7 +28,7 @@ else
RM := rm -rf
DIR_DUP = mkdir -p $(@D)
CC := clang++
CFLAGS := -Ofast -Wall -Wextra -Werror -g
CFLAGS := -Wall -Wextra -Werror -g
IFLAGS := -I./includes -I./includes/RT -I/usr/include
LDFLAGS := -L/usr/lib/x86_64-linux-gnu -lglfw -lGL -lGLU -lX11 -lpthread -ldl -lstdc++
FILE = $(shell ls -lR srcs/ | grep -F .c | wc -l)

29
scenes/portalcornell.rt Normal file
View File

@ -0,0 +1,29 @@
CAM 0 2.5 0
MAT 30 30 30 0.0 0.0 0.0 //white
MAT 255 255 255 0.0 1.0 1.0 //mirror
MAT 255 255 255 2.0 0.0 0.0 //sun
MAT 255 100 100 0.0 0.0 0.0 //red
MAT 100 100 255 0.0 0.0 0.0 //blue
MAT 100 255 100 0.0 0.0 0.0 //green
MAT 255 255 100 0.0 0.0 0.0 //yellow
MAT 255 255 255 0.0 1.45 0.0 DIE //glass
pl 0 0 0 0 1 0 0
cu 0 0.5 0 1 1 1 1
sp 0 1.5 0 1 1
qu -1 4 -1 2 0 0 0 0 2 2
cu 0 2.5 0 5 5 5 1
po -1 1 -1.5 0 2 0 2 0 0 0 3
po -1 1 1.5 0 2 0 2 0 0 1 4
po -1.5 1 -1 0 2 0 0 0 2 1 5
po 1.5 1 -1 0 2 0 0 0 2 0 6

23
scenes/portalrotation.rt Normal file
View File

@ -0,0 +1,23 @@
MAT 255 255 255 2.0 0.0 0.0 //light
MAT 255 255 255 0.0 0.0 0.0 //white
MAT 255 100 100 0.0 0.0 0.0 //red
MAT 100 255 100 0.0 0.0 0.0 //green
MAT 100 100 255 0.0 0.0 0.0 //blue
MAT 255 255 100 0.0 0.0 0.0 //yellow
MAT 50 50 50 0.0 0.0 0.0 //gray
qu -1 2.5 -1 2 0 0 0 0 2 0
pl 0 3 0 0 -1 0 1
pl 0 -3 0 0 1 0 6
pl 3 0 0 -1 0 0 5
pl -3 0 0 1 0 0 3
pl 0 0 -3 0 0 1 4
pl 0 0 3 0 0 -1 2
po -0.5 -0.5 -1 0 1 0 1 1 0 0 2
po -0.5 -0.5 1 0 1 0 1 0 0 1 4

View File

@ -1,26 +0,0 @@
CAM 0 2 5
MAT 30 30 30 0.0 0.0 0.0 //white
MAT 255 255 255 0.0 1.0 1.0 //mirror
MAT 255 255 255 2.0 0.0 0.0 //sun
MAT 255 100 100 0.0 0.0 0.0 //red
MAT 100 100 255 0.0 0.0 0.0 //blue
MAT 100 255 100 0.0 0.0 0.0 //green
MAT 255 255 100 0.0 0.0 0.0 //yellow
pl 0 0 0 0 1 0 0
cu 1 0.5 3 1 1 1 3
cu 1 0.5 -3 1 1 1 4
po 3 1 0 0 2 0 2 0 0 0 3
po 3 1 3 0 2 0 2 0 0 1 4
# po 6 1 0 0 2 0 2 0 0 0 3
# po 6 1 0 0 2 0 2 0 0 1 4
sp 0 30 0 30 2

View File

@ -78,6 +78,12 @@ Ray portalRay(Ray ray, hitInfo hit)
mat3 rotation = mat3(portal_2.transform) * transpose(mat3(portal_1.transform));
if (dot(portal_1.normal, portal_2.normal) > 0.0)
{
mat3 reflection = mat3(1.0) - 2.0 * outerProduct(portal_2.normal, portal_2.normal);
rotation *= reflection;
}
ray.origin = portal_2.position + rotation * relative;
ray.direction = normalize(rotation * ray.direction);
@ -90,7 +96,7 @@ hitInfo traceRay(Ray ray)
{
hitInfo hit;
for (int p = 0; p < 10; p++) //portals
for (int p = 0; p < 5; p++) //portals
{
hit.t = 1e30;
hit.obj_index = -1;
@ -137,10 +143,10 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
GPUMaterial mat = materials[obj.mat_index];
// RR
float p = max(color.r, max(color.g, color.b));
if (randomValue(rng_state) > p && i > 1)
break;
color /= p;
// float p = max(color.r, max(color.g, color.b));
// if (randomValue(rng_state) > p && i > 1)
// break;
// color /= p;
//
color *= mat.color;

View File

@ -112,41 +112,46 @@ bool intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit)
return (true);
}
bool intersectCube(Ray ray, GPUObject obj, out hitInfo hit)
{
vec3 obj_size = obj.vertex1;
vec3 boxMin = obj.position - obj_size * 0.5;
vec3 boxMax = obj.position + obj_size * 0.5;
vec3 t1 = (boxMin - ray.origin) / ray.direction;
vec3 t2 = (boxMax - ray.origin) / ray.direction;
vec3 tNear = min(t1, t2);
vec3 tFar = max(t1, t2);
float tMin = max(max(tNear.x, tNear.y), tNear.z);
float tMax = min(min(tFar.x, tFar.y), tFar.z);
if (tMax < tMin || tMax < 0.0)
return (false);
hit.t = tMin > 0.0 ? tMin : tMax; // Use the closer valid intersection point
hit.position = ray.origin + hit.t * ray.direction; // Calculate hit position
vec3 hitPointLocal = hit.position - obj.position;
vec3 halfSize = obj_size * 0.5;
if (abs(hitPointLocal.x) > halfSize.x - 1e-4)
hit.normal = vec3(sign(hitPointLocal.x), 0.0, 0.0);
else if (abs(hitPointLocal.y) > halfSize.y - 1e-4)
hit.normal = vec3(0.0, sign(hitPointLocal.y), 0.0);
else if (abs(hitPointLocal.z) > halfSize.z - 1e-4)
hit.normal = vec3(0.0, 0.0, sign(hitPointLocal.z));
return (true);
vec3 rayOriginLocal = ray.origin - obj.position;
vec3 invDir = 1.0 / ray.direction;
vec3 t1 = (-halfSize - rayOriginLocal) * invDir;
vec3 t2 = (halfSize - rayOriginLocal) * invDir;
vec3 tMinVec = min(t1, t2);
vec3 tMaxVec = max(t1, t2);
float tMin = max(tMinVec.x, max(tMinVec.y, tMinVec.z));
float tMax = min(tMaxVec.x, min(tMaxVec.y, tMaxVec.z));
bool hit_success = (tMax >= tMin) && (tMax > 0.0);
if (!hit_success) return false;
hit.t = tMin > 0.0 ? tMin : tMax;
vec3 hitPointLocal = rayOriginLocal + hit.t * ray.direction;
hit.position = hitPointLocal + obj.position;
vec3 distances = abs(hitPointLocal) - halfSize;
const float epsilon = 1e-4;
vec3 signs = sign(hitPointLocal);
vec3 masks = step(abs(distances), vec3(epsilon));
hit.normal = normalize(masks * signs);
bool inside = all(lessThan(abs(rayOriginLocal), halfSize + vec3(epsilon)));
hit.normal *= (inside ? -1.0 : 1.0);
return true;
}
bool intersect(Ray ray, GPUObject obj, out hitInfo hit)
{
if (obj.type == 0)

View File

@ -70,7 +70,7 @@ void Scene::updateGPUData()
_gpu_objects.clear();
_gpu_materials.clear();
for (int i = 0; i < _objects.size(); i++)
for (unsigned int i = 0; i < _objects.size(); i++)
{
Object *obj = _objects[i];
@ -115,13 +115,18 @@ void Scene::updateGPUData()
gpu_obj.vertex2 = portal->getEdge2();
gpu_obj.normal = portal->getNormal();
gpu_obj.transform = glm::mat4(portal->getTransform());
gpu_obj.radius = i + 2;
if (_objects[i - 2]->getType() == Object::Type::PORTAL)
gpu_obj.radius = i - 2;
Portal *linked = static_cast<Portal *>(_objects[i - 2]);
Quad *quad = portal->createSupportQuad();
_objects.push_back(quad);
if (portal->getLinkedPortalIndex() == -1)
{
if (linked->getType() == Object::Type::PORTAL && linked->getLinkedPortalIndex() == static_cast<int>(i))
portal->setLinkedPortalIndex(i - 2);
else
portal->setLinkedPortalIndex(i + 2);
}
gpu_obj.radius = portal->getLinkedPortalIndex();
}
_gpu_objects.push_back(gpu_obj);

View File

@ -111,6 +111,9 @@ bool SceneParser::parseLine(const std::string &line)
Object *obj = it->second(ss);
(void) _scene->getMaterial(obj->getMaterialIndex()); //verify material
if (obj->getType() == Object::Type::PORTAL)
_scene->addObject(static_cast<Portal *>(obj)->createSupportQuad());
_scene->addObject(obj);
}