From ab71bbc438c36fef5238a1686e22774d5a2bd271 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Fri, 13 Sep 2024 23:55:47 +0100 Subject: [PATCH] Add testing depth shader --- src/shader/depth_shader.c | 33 +++++++++++++++++++++++++++++++++ src/shader/depth_shader.h | 18 ++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/shader/depth_shader.c create mode 100644 src/shader/depth_shader.h diff --git a/src/shader/depth_shader.c b/src/shader/depth_shader.c new file mode 100644 index 0000000..c2a8b48 --- /dev/null +++ b/src/shader/depth_shader.c @@ -0,0 +1,33 @@ +#include "depth_shader.h" +#include "constants.h" +#include "img.h" +#include "shader.h" +#include "utils.h" + +VertexData depth_shader_vertex(void *shader, const VertexData *vert, u8 index, + const Model *model) { + DepthShader *shdr = (DepthShader *)shader; + + V4f vh = V3_to_V4(vert->position); + vh.y = 0.0 - vh.y; + vh = mat4x4_mul_vec4(shdr->final, vh); + shdr->vertices[index].position = project_vec4(vh); + + return shdr->vertices[index]; +} + +FragmentResult depth_shader_fragment(void *shader, const V3f *barycentric, + const Colour *colour, const Model *model) { + DepthShader *shdr = (DepthShader *)shader; + + // clang-format off + M3x3f pos_mat = {.rows = {shdr->vertices[0].position, shdr->vertices[1].position, shdr->vertices[2].position}}; + pos_mat = mat3x3_transpose(pos_mat); + // clang-format on + + V3f position = mat3x3_mul_vec3(pos_mat, (*barycentric)); + f32 channel = clamp(position.z + DEPTH_MAX, 0.0f, DEPTH_MAX); + Colour output = {.r = (u8)(channel), .g = (u8)(channel), .b = (u8)(channel), .a = 255}; + + return (FragmentResult){.colour = output}; +} diff --git a/src/shader/depth_shader.h b/src/shader/depth_shader.h new file mode 100644 index 0000000..5edbd25 --- /dev/null +++ b/src/shader/depth_shader.h @@ -0,0 +1,18 @@ +#ifndef DEPTH_SHADER_H +#define DEPTH_SHADER_H + +#include "constants.h" +#include "shader.h" +#include "vec.h" + +typedef struct depth_shader DepthShader; +struct depth_shader { +#include "shader_base.inc" +}; + +VertexData depth_shader_vertex(void *shader, const VertexData *vert, u8 index, + const Model *model); +FragmentResult depth_shader_fragment(void *shader, const V3f *barycentric, + const Colour *colour, const Model *model); + +#endif // !DEPTH_SHADER_H