diff --git a/src/model/render.c b/src/model/render.c index b04d90c..6cced8c 100644 --- a/src/model/render.c +++ b/src/model/render.c @@ -75,8 +75,7 @@ internal void render_triangle(const Triangle *triangle, const Model *model, }; for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) { - vertices[i] = - run_vertex_shader(shader, &vertices[i], (Buffer *)&render->img); + vertices[i] = run_vertex_shader(shader, &vertices[i]); } if (render_type == RENDER_TYPE_WIREFRAME) { @@ -102,11 +101,17 @@ internal void fill_triangle(Render *render, ShaderID shader, const Model *model, RenderType type) { Image *img = &(render->img); Depth *depth = &(render->depth); - TriangleBBox bbox = get_triangle_bbox(img, vertices); - V3f vert0 = vertices[0]; - V3f vert1 = vertices[1]; - V3f vert2 = vertices[2]; + V3f vp_verts[TRIANGLE_VERTICES] = {0}; + for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) { + vp_verts[i] = get_viewport_vertex(&vertices[i], img); + } + + TriangleBBox bbox = get_triangle_bbox(img, vp_verts); + + V3f vert0 = vp_verts[0]; + V3f vert1 = vp_verts[1]; + V3f vert2 = vp_verts[2]; V3f point; V3f coords; @@ -140,12 +145,12 @@ internal void fill_triangle(Render *render, ShaderID shader, continue; } - px = vertices[0].x * coords.x + vertices[1].x * coords.y + - vertices[2].x * coords.z; - py = vertices[0].y * coords.x + vertices[1].y * coords.y + - vertices[2].y * coords.z; - pz = vertices[0].z * coords.x + vertices[1].z * coords.y + - vertices[2].z * coords.z; + px = vp_verts[0].x * coords.x + vp_verts[1].x * coords.y + + vp_verts[2].x * coords.z; + py = vp_verts[0].y * coords.x + vp_verts[1].y * coords.y + + vp_verts[2].y * coords.z; + pz = vp_verts[0].z * coords.x + vp_verts[1].z * coords.y + + vp_verts[2].z * coords.z; position = (V3f){px, py, pz}; normalise_v3(position); data.position = position; @@ -207,5 +212,10 @@ internal V3f get_barycentric_coords(V3f a, V3f b, V3f c, V3f p) { internal V3f get_viewport_vertex(const V3f *vertex, const Image *img) { V4f vh = {.x = vertex->x, .y = 0.0f - vertex->y, .z = vertex->z, .w = 1.0f}; vh = mat4x4_mul_vec4(viewport(vh.x, vh.y, img->width, img->height), vh); - return project_vec4(vh); + + V3f output = project_vec4(vh); + output.x = clamp(output.x, 0.0f, img->width); + output.y = clamp(output.y, 0.0f, img->height); + + return output; } diff --git a/src/shader/shader.c b/src/shader/shader.c index 0e18ffb..a4ffd28 100644 --- a/src/shader/shader.c +++ b/src/shader/shader.c @@ -28,10 +28,8 @@ ShaderID register_shader(void *shader, VertexShader *vertex, return (ShaderID){g_repository.count}; } -V3f run_vertex_shader(ShaderID shader, const V3f *vertex, - const Buffer *out_buf) { - if (IS_INVALID_SHADER(shader) || shader.id > g_repository.count || !vertex || - !out_buf) { +V3f run_vertex_shader(ShaderID shader, const V3f *vertex) { + if (IS_INVALID_SHADER(shader) || shader.id > g_repository.count || !vertex) { return (V3f){0}; } @@ -42,7 +40,7 @@ V3f run_vertex_shader(ShaderID shader, const V3f *vertex, return (V3f){0}; } - return vertex_func(shader_obj, vertex, out_buf); + return vertex_func(shader_obj, vertex); } FragmentResult run_fragment_shader(ShaderID shader, const FragmentData *data, diff --git a/src/shader/shader.h b/src/shader/shader.h index 55c6b7c..337fb57 100644 --- a/src/shader/shader.h +++ b/src/shader/shader.h @@ -29,16 +29,14 @@ struct fragment_data { #define DISCARDED_FRAGMENT ((FragmentResult){.discard = true}) #define DISCARD_FRAGMENT(RESULT) (RESULT.discard) -typedef V3f(VertexShader)(void *shader, const V3f *vertex, - const Buffer *out_buf); +typedef V3f(VertexShader)(void *shader, const V3f *vertex); typedef FragmentResult(FragmentShader)(void *shader, const FragmentData *data, const Colour *colour, const Model *model); ShaderID register_shader(void *shader, VertexShader *vertex, FragmentShader *fragment); -V3f run_vertex_shader(ShaderID shader, const V3f *vertex, - const Buffer *out_buf); +V3f run_vertex_shader(ShaderID shader, const V3f *vertex); FragmentResult run_fragment_shader(ShaderID shader, const FragmentData *data, const Colour *colour, const Model *model); diff --git a/src/shader/shaders.c b/src/shader/shaders.c index 4f8cc0f..911774d 100644 --- a/src/shader/shaders.c +++ b/src/shader/shaders.c @@ -32,8 +32,7 @@ internal V3f g_target = {0}; internal V3f g_up = {0.0f, 1.0f, 0.0f}; internal M4x4f g_cam_matrix = mat4x4_identity; -internal V3f general_shader_vertex(void *shader, const V3f *vertex, - const Buffer *out_buf); +internal V3f general_shader_vertex(void *shader, const V3f *vertex); internal FragmentResult phong_shader_fragment(void *shader, const FragmentData *data, const Colour *colour, @@ -66,22 +65,14 @@ void load_shaders(void) { albedo_shader_fragment); } -internal V3f general_shader_vertex(void *shader, const V3f *vertex, - const Buffer *out_buf) { +internal V3f general_shader_vertex(void *shader, const V3f *vertex) { Shader *shader_ptr = (Shader *)shader; V4f vh = {.x = vertex->x, .y = vertex->y, .z = vertex->z, .w = 1.0f}; vh = mat4x4_mul_vec4(shader_ptr->projection, mat4x4_mul_vec4(shader_ptr->model_view, vh)); - vh.y = 0.0 - vh.y; - vh = mat4x4_mul_vec4(viewport(vh.x, vh.y, out_buf->width, out_buf->height), - vh); - V3f output = project_vec4(vh); - output.x = clamp(output.x, 0.0f, out_buf->width); - output.y = clamp(output.y, 0.0f, out_buf->height); - - return output; + return project_vec4(vh); } internal FragmentResult phong_shader_fragment(void *shader,