From 928b1b0503d2ecab09cc275e9775593a41e417cb Mon Sep 17 00:00:00 2001 From: ladevieq Date: Thu, 7 Jul 2022 23:01:36 +0200 Subject: Interpolation function & no more triangle structure in shader --- shaders/compute.comp | 17 +++++------------ shaders/include/math.h | 8 ++++++++ shaders/include/ray.h | 11 +++-------- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/shaders/compute.comp b/shaders/compute.comp index 6ca193e..0f85da6 100644 --- a/shaders/compute.comp +++ b/shaders/compute.comp @@ -36,16 +36,10 @@ struct sphere { float[3] padding; }; -struct vertex { - vec3 position; - vec3 normal; - vec2 uv; -}; - struct triangle { - vertex v1; - vertex v2; - vertex v3; + vec3 positions[3]; + vec3 normals[3]; + vec2 uvs[3]; material mat; }; @@ -141,11 +135,10 @@ vec3 ray_color(ray r, uint seed) { - // TODO: Interpolation function & indices decoding function triangle tri = get_triangle(info.primitive_id); - vec3 shading_normal = normalize(tri.v1.normal * info.barycentrics.x + tri.v2.normal * info.barycentrics.y + tri.v3.normal * info.barycentrics.z); - vec2 uv = tri.v1.uv * info.barycentrics.x + tri.v2.uv * info.barycentrics.y + tri.v3.uv * info.barycentrics.z; + vec2 uv = interpolate_attribute(tri.uvs, info.barycentrics); + vec3 shading_normal = normalize(interpolate_attribute(tri.normals, info.barycentrics)); vec3 diffuse_color = srgb_to_linear(texture( sampler2D( diff --git a/shaders/include/math.h b/shaders/include/math.h index cc4e2f4..c95b065 100644 --- a/shaders/include/math.h +++ b/shaders/include/math.h @@ -43,3 +43,11 @@ vec3 rotate_point(vec4 q, vec3 v) { const vec3 q_axis = q.xyz; return 2.0f * dot(q_axis, v) * q_axis + (q.w * q.w - dot(q_axis, q_axis)) * v + 2.0f * q.w * cross(q_axis, v); } + +vec3 interpolate_attribute(vec3 values[3], vec3 weights) { + return values[0] * weights.x + values[1] * weights.y + values[2] * weights.z; +} + +vec2 interpolate_attribute(vec2 values[3], vec3 weights) { + return values[0] * weights.x + values[1] * weights.y + values[2] * weights.z; +} diff --git a/shaders/include/ray.h b/shaders/include/ray.h index 3732c42..221202f 100644 --- a/shaders/include/ray.h +++ b/shaders/include/ray.h @@ -55,15 +55,10 @@ vec2[3] get_triangle_uvs(uvec3 indices) { triangle get_triangle(uint triangle_id) { uvec4 indices = decode_triangle_indices(triangle_id); - vec3 positions[3] = get_triangle_positions(indices.xyz); - vec3 normals[3] = get_triangle_normals(indices.xyz); - vec2 uvs[3] = get_triangle_uvs(indices.xyz); - - vertex v1 = vertex(positions[0], normals[0], uvs[0]); - vertex v2 = vertex(positions[1], normals[1], uvs[1]); - vertex v3 = vertex(positions[2], normals[2], uvs[2]); return triangle( - v1, v2, v3, + get_triangle_positions(indices.xyz), + get_triangle_normals(indices.xyz), + get_triangle_uvs(indices.xyz), bufs.materials_arr.materials[indices.w] ); } -- cgit v1.3