Move viewport matrix multiplication out of vertex shader

This commit is contained in:
2024-08-26 22:30:05 +01:00
parent 319bad9659
commit 97622d0bf7
4 changed files with 31 additions and 34 deletions

View File

@@ -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;
}