~ | Better portals

This commit is contained in:
TheRedShip
2025-01-07 17:02:19 +01:00
parent 3515810c8b
commit 9df8b78d6d
7 changed files with 46 additions and 8 deletions

View File

@ -16,7 +16,10 @@
# define WIDTH 1920
# define HEIGHT 1080
#define GLM_ENABLE_EXPERIMENTAL
# include "glm/glm.hpp"
# include "glm/gtx/string_cast.hpp"
# include "glm/gtc/matrix_transform.hpp"
# include "glm/gtc/type_ptr.hpp"

View File

@ -48,7 +48,7 @@ class Camera
float _maxSpeed = 8.0f;
float _acceleration_rate = 40.0f;
float _deceleration_rate = 20.0f;
float _deceleration_rate = 10000.0f;
float _sensitivity = 0.2f;
};

View File

@ -17,6 +17,8 @@
struct GPUObject
{
glm::mat4 transform;
alignas(16) glm::vec3 position;
alignas(16) glm::vec3 normal; // plane triangle

View File

@ -46,6 +46,18 @@ class Portal : public Object
_edge1 = glm::vec3(x1, y1, z1);
_edge2 = glm::vec3(x2, y2, z2);
glm::vec3 up = glm::normalize(_edge1);
glm::vec3 right = glm::normalize(_edge2);
glm::vec3 forward = glm::normalize(glm::cross(right, up));
up = normalize(glm::cross(forward, right));
_transform = glm::mat3(right, up, forward);
_normal = forward;
std::cout << "Portal Transform Matrix:" << std::endl;
std::cout << glm::to_string(_transform) << std::endl;
_linked_portal = linked_portal;
_mat_index = mat_index;
@ -57,12 +69,20 @@ class Portal : public Object
glm::vec3 getEdge1() const { return (_edge1); }
glm::vec3 getEdge2() const { return (_edge2); }
glm::vec3 getNormal() const { return (_normal); }
glm::mat3 getTransform() const { return (_transform); }
int getLinkedPortalIndex() const { return (_linked_portal); }
Type getType() const override { return Type::PORTAL; }
private:
glm::vec3 _edge1;
glm::vec3 _edge2;
glm::vec3 _normal;
glm::mat3 _transform;
int _linked_portal;
};

View File

@ -12,7 +12,11 @@ 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 0 0 2 0 0 0 2 0 4 0
po -3 0 0 2 0 0 0 2 0 3 0
qu 2.9 0.9 -0.001 0 2.2 0 2.2 0 0 3
po 3 1 0 0 2 0 2 0 0 6 0
qu -3.1 0.9 -0.001 0 2.2 0 2.2 0 0 4
po -3 1 0 0 2 0 2 0 0 4 0
sp 0 30 0 30 2

View File

@ -5,6 +5,8 @@ layout(binding = 0, rgba32f) uniform image2D output_image;
layout(binding = 1, rgba32f) uniform image2D accumulation_image;
struct GPUObject {
mat4 transform;
vec3 position; // 12 + 4
vec3 normal; // 12 + 4
@ -12,6 +14,7 @@ struct GPUObject {
vec3 vertex1; // 12 + 4
vec3 vertex2; // 12 + 4
float radius; // 4
int mat_index; // 4
@ -71,13 +74,17 @@ Ray portalRay(Ray ray, hitInfo hit)
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));
vec3 portal_2_normal = portal_2.normal * sign(dot(ray.direction, portal_2.normal));
relative = portal_2.position - portal_1.position;
relative = hit.position - portal_1.position;
ray.origin = hit.position + relative + portal_2_normal * 0.01;
ray.direction = normalize(ray.direction + portal_2_normal * 0.01);
ray.origin = portal_2.position + mat3(portal_2.transform) * relative;
ray.origin += portal_2_normal * 0.01;
if (dot(ray.direction, portal_2_normal) < 0.0)
ray.direction = reflect(ray.direction, portal_2_normal);
ray.direction = normalize(mat3(portal_2.transform) * ray.direction);
return (ray);
}

View File

@ -111,6 +111,8 @@ void Scene::updateGPUData()
auto portal = static_cast<const Portal *>(obj);
gpu_obj.vertex1 = portal->getEdge1();
gpu_obj.vertex2 = portal->getEdge2();
gpu_obj.normal = portal->getNormal();
gpu_obj.transform = glm::mat4(portal->getTransform());
gpu_obj.radius = portal->getLinkedPortalIndex();
}