+ | Portals

This commit is contained in:
TheRedShip
2025-01-07 12:18:37 +01:00
parent 34be8433f7
commit 3515810c8b
11 changed files with 152 additions and 34 deletions

View File

@ -11,7 +11,7 @@ struct GPUObject {
vec3 vertex1; // 12 + 4
vec3 vertex2; // 12 + 4
float radius; // 4
int mat_index; // 4
@ -62,26 +62,54 @@ struct hitInfo
#include "shaders/intersect.glsl"
#include "shaders/scatter.glsl"
Ray portalRay(Ray ray, hitInfo hit)
{
GPUObject portal_1;
GPUObject portal_2;
vec3 relative;
portal_1 = objects[hit.obj_index];
portal_2 = objects[int(portal_1.radius)]; // saving memory radius = portal_index
vec3 portal_2_normal = normalize(cross(portal_2.vertex1, portal_2.vertex2));
portal_2_normal *= sign(dot(ray.direction, portal_2_normal));
relative = portal_2.position - portal_1.position;
ray.origin = hit.position + relative + portal_2_normal * 0.01;
ray.direction = normalize(ray.direction + portal_2_normal * 0.01);
return (ray);
}
hitInfo traceRay(Ray ray)
{
hitInfo hit;
hit.t = 1e30;
hit.obj_index = -1;
for (int i = 0; i < u_objectsNum; i++)
for (int p = 0; p < 2; p++) //portals
{
GPUObject obj = objects[i];
hitInfo temp_hit;
if (intersect(ray, obj, temp_hit) && temp_hit.t > 0.0f && temp_hit.t < hit.t)
hit.t = 1e30;
hit.obj_index = -1;
for (int i = 0; i < u_objectsNum; i++)
{
hit.t = temp_hit.t;
hit.obj_index = i;
hit.position = temp_hit.position;
hit.normal = temp_hit.normal;
GPUObject obj = objects[i];
hitInfo temp_hit;
if (intersect(ray, obj, temp_hit) && temp_hit.t > 0.0f && temp_hit.t < hit.t)
{
hit.t = temp_hit.t;
hit.obj_index = i;
hit.position = temp_hit.position;
hit.normal = temp_hit.normal;
}
}
if (hit.obj_index == -1 || objects[hit.obj_index].type != 5)
break ;
ray = portalRay(ray, hit);
}
return (hit);
}
@ -105,10 +133,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

@ -153,7 +153,7 @@ bool intersect(Ray ray, GPUObject obj, out hitInfo hit)
return (intersectSphere(ray, obj, hit));
if (obj.type == 1)
return (intersectPlane(ray, obj, hit));
if (obj.type == 2)
if (obj.type == 2 || obj.type == 5)
return (intersectQuad(ray, obj, hit));
if (obj.type == 3)
return (intersectTriangle(ray, obj, hit));

View File

@ -1,3 +1,5 @@
#define M_PI 3.14159265359
float randomValue(inout uint rng_state)
{
rng_state = rng_state * 747796405u + 2891336453u;
@ -8,7 +10,7 @@ float randomValue(inout uint rng_state)
float randomValueNormalDistribution(inout uint rng_state)
{
float theta = 2.0 * 3.14159265359 * randomValue(rng_state);
float theta = 2.0 * M_PI * randomValue(rng_state);
float rho = sqrt(-2.0 * log(randomValue(rng_state)));
return (rho * cos(theta));
}
@ -27,7 +29,6 @@ vec3 randomHemisphereDirection(vec3 normal, inout uint rng_state)
return (direction * sign(dot(normal, direction)));
}
#define M_PI 3.1415926535897932384626433832795
// vec3 randomHemisphereDirection(vec3 normal, inout uint rng_state)
// {