aboutsummaryrefslogtreecommitdiffstats
path: root/shaders
diff options
context:
space:
mode:
Diffstat (limited to 'shaders')
-rw-r--r--shaders/compute.comp17
-rw-r--r--shaders/include/math.h8
-rw-r--r--shaders/include/ray.h11
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]
);
}