From 558d1205a741f6908a4ffc616c495b6697392aad Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Sat, 1 Feb 2025 16:33:40 +0100 Subject: [PATCH] + | Triangle optimization --- imgui.ini | 4 ++-- scenes/sponza.rt | 7 +++++-- scenes/test.rt | 4 ++-- shaders/intersect.glsl | 19 ++++++++----------- shaders/trace.glsl | 32 ++++++++++++++++++-------------- 5 files changed, 35 insertions(+), 31 deletions(-) diff --git a/imgui.ini b/imgui.ini index 6319185..633cc90 100644 --- a/imgui.ini +++ b/imgui.ini @@ -29,6 +29,6 @@ Pos=1556,610 Size=284,382 [Window][Settings] -Pos=1595,22 -Size=259,975 +Pos=1568,11 +Size=336,975 diff --git a/scenes/sponza.rt b/scenes/sponza.rt index 87b4e93..57953f1 100644 --- a/scenes/sponza.rt +++ b/scenes/sponza.rt @@ -1,4 +1,4 @@ -CAM 18.9143 1.24843 -5.48951 8.60003 -200.801 0 2 45 5 +CAM 18.9143 1.24843 -5.48951 8.5 -200 0.04 14.829 45 4 MAT 255 255 255 0.0 1.0 0.5 // dragon 0 MAT 255 220 50 0.0 1.0 0.5 // dragon 1 @@ -9,6 +9,9 @@ sp 0 50 0 25 2 OBJ scenes/obj/sponza.obj 0 0 0 0.025 -OBJ scenes/obj/Dragon_80K.obj 7 2.5 0 9 0 140 0 0 +# OBJ scenes/obj/Dragon_80K.obj 7 2.5 0 9 0 140 0 0 OBJ scenes/obj/Dragon_80K.obj 9 1.35 -3.5 5 0 40 0 1 +OBJ scenes/obj/Dragon_800K.obj 10 3.35 -3.5 45 0 230 0 0 +# OBJ scenes/obj/Dragon_80K.obj 9 1.35 -3.5 5 0 40 0 1 + diff --git a/scenes/test.rt b/scenes/test.rt index 75f70ca..0f4cd70 100644 --- a/scenes/test.rt +++ b/scenes/test.rt @@ -1,6 +1,6 @@ -CAM 0 0 -5 +CAM -0.181956 1.3733 2.83437 -16 -87.8001 0 1 90 5 MAT 255 255 255 10.0 1.0 0.0 //white # sp 0 0 0 1 0 -OBJ scenes/obj/room.obj 0 0 0 1 +OBJ scenes/obj/jinx.obj 0 0 0 1 diff --git a/shaders/intersect.glsl b/shaders/intersect.glsl index 6dea610..b62ce3d 100644 --- a/shaders/intersect.glsl +++ b/shaders/intersect.glsl @@ -81,20 +81,17 @@ bool intersectTriangle(Ray ray, GPUTriangle obj, out hitInfo hit) vec3 tvec = ray.origin - obj.position; float invDet = 1.0 / det; - float u = dot(tvec, pvec) * invDet; + hit.u = dot(tvec, pvec) * invDet; vec3 qvec = cross(tvec, vertex1); - float v = dot(ray.direction, qvec) * invDet; - float t = dot(vertex2, qvec) * invDet; + hit.v = dot(ray.direction, qvec) * invDet; + hit.t = dot(vertex2, qvec) * invDet; - bool valid = u >= 0.0 && u <= 1.0 && - v >= 0.0 && (u + v) <= 1.0 && - t > 0.0; + bool valid = hit.u >= 0.0 && hit.u <= 1.0 && + hit.v >= 0.0 && (hit.u + hit.v) <= 1.0 && + hit.t > 0.0; - hit.u = u; - hit.v = v; - hit.t = t; - hit.position = ray.origin + ray.direction * t; - hit.normal = obj.normal * sign(-dot(ray.direction, obj.normal)); + // hit.position = ray.origin + ray.direction * t; + // hit.normal = obj.normal * sign(-dot(ray.direction, obj.normal)); // hit.normal = vec3(u, v, 1 - (u + v)); //texture mapping return (valid); diff --git a/shaders/trace.glsl b/shaders/trace.glsl index 76950d6..fe12af8 100644 --- a/shaders/trace.glsl +++ b/shaders/trace.glsl @@ -43,7 +43,6 @@ hitInfo traceScene(Ray ray) if (intersect(ray, obj, temp_hit) && temp_hit.t < hit.t) { hit.t = temp_hit.t; - hit.last_t = temp_hit.last_t; hit.obj_index = i; hit.obj_type = obj.type; hit.mat_index = obj.mat_index; @@ -84,11 +83,10 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data) hit.u = temp_hit.u; hit.v = temp_hit.v; hit.t = temp_hit.t; - hit.last_t = temp_hit.last_t; hit.obj_index = bvh_data.triangle_start_index + node.index + i; - hit.mat_index = obj.mat_index; - hit.position = temp_hit.position; - hit.normal = temp_hit.normal; + // hit.mat_index = obj.mat_index; + // hit.position = temp_hit.position; + // hit.normal = temp_hit.normal; } } } @@ -143,22 +141,28 @@ hitInfo traverseBVHs(Ray ray) hitInfo temp_hit = traceBVH(transformedRay, BvhData[i]); - temp_hit.t = temp_hit.t / bvh_data.scale; - - if (temp_hit.t < hit.t) + + float transformed_t = temp_hit.t / bvh_data.scale; + if (transformed_t < hit.t) { + GPUTriangle triangle = triangles[temp_hit.obj_index]; + hit.u = temp_hit.u; hit.v = temp_hit.v; - hit.t = temp_hit.t; - hit.last_t = temp_hit.last_t / bvh_data.scale; + hit.t = transformed_t; hit.obj_index = temp_hit.obj_index; - hit.mat_index = temp_hit.mat_index; - hit.obj_type = 3; - hit.position = inverseTransformMatrix * temp_hit.position + bvh_data.offset; - hit.normal = normalize(inverseTransformMatrix * temp_hit.normal); + hit.mat_index = triangle.mat_index; + + vec3 position = transformedRay.origin + transformedRay.direction * temp_hit.t; + hit.position = inverseTransformMatrix * position + bvh_data.offset; + + vec3 based_normal = triangle.normal; // * sign(-dot(transformedRay.direction, triangle.normal)); + hit.normal = normalize(inverseTransformMatrix * based_normal); } } + hit.obj_type = 3; + return (hit); }