+ | 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

@ -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)