Reformat and reorganise before writing shadow code
This commit is contained in:
@@ -8,8 +8,6 @@
|
||||
#include "vec.h"
|
||||
#include <math.h>
|
||||
|
||||
internal M4x4f g_viewport;
|
||||
|
||||
typedef struct triangle_bbox TriangleBBox;
|
||||
struct triangle_bbox {
|
||||
u64 x0;
|
||||
@@ -43,8 +41,6 @@ bool init_render(Arena *arena, Render *render, u64 width, u64 height) {
|
||||
f32 inf = -INFINITY;
|
||||
clear_buffer(&(render->depth), &inf);
|
||||
|
||||
g_viewport = viewport(0, 0, width, height);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -63,13 +59,11 @@ internal void render_triangle(const Triangle *triangle, const Model *model,
|
||||
Image *img = &(render->img);
|
||||
VertexData vertices[TRIANGLE_VERTICES];
|
||||
for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) {
|
||||
// clang-format off
|
||||
vertices[i].position = list_get(model->vertices, triangle->positions[i]);
|
||||
vertices[i].normal = list_get(model->normals, triangle->normals[i]);
|
||||
vertices[i].uv = list_get(model->texture_coordinates, triangle->coordinates[i]);
|
||||
// clang-format on
|
||||
vertices[i].normal = list_get(model->normals, triangle->normals[i]);
|
||||
vertices[i].uv = list_get(model->texture_coordinates, triangle->coordinates[i]);
|
||||
|
||||
vertices[i] = run_vertex_shader(shader, &vertices[i], i, model);
|
||||
vertices[i] = run_vertex_shader(shader, &vertices[i], i, model);
|
||||
}
|
||||
|
||||
if (render_type == RENDER_TYPE_WIREFRAME) {
|
||||
@@ -81,8 +75,7 @@ internal void render_triangle(const Triangle *triangle, const Model *model,
|
||||
//
|
||||
// draw_line(img, (u64)v0.x, (u64)v0.y, (u64)v1.x, (u64)v1.y, colour);
|
||||
// }
|
||||
} else if (render_type == RENDER_TYPE_FILLED ||
|
||||
render_type == RENDER_TYPE_SHADED) {
|
||||
} else if (render_type == RENDER_TYPE_FILLED || render_type == RENDER_TYPE_SHADED) {
|
||||
fill_triangle(render, shader, vertices, colour, model, render_type);
|
||||
}
|
||||
}
|
||||
@@ -112,14 +105,13 @@ internal void fill_triangle(Render *render, ShaderID shader,
|
||||
for (u64 y = bbox.y0; y <= bbox.y1; ++y) {
|
||||
for (u64 x = bbox.x0; x <= bbox.x1; ++x) {
|
||||
point = (V2f){x, y};
|
||||
coords =
|
||||
get_barycentric_coords(vp_verts[0], vp_verts[1], vp_verts[2], point);
|
||||
coords = get_barycentric_coords(vp_verts[0], vp_verts[1], vp_verts[2], point);
|
||||
if (coords.x < 0.0f || coords.y < 0.0f || coords.z < 0.0f) {
|
||||
continue;
|
||||
}
|
||||
|
||||
z = 0.0f;
|
||||
z += vertices[0].position.z * coords.x +
|
||||
z = 0.0f;
|
||||
z += vertices[0].position.z * coords.x +
|
||||
vertices[1].position.z * coords.y +
|
||||
vertices[2].position.z * coords.z;
|
||||
zbuf = get_pixel(f32, &(render->depth), x, y);
|
||||
@@ -167,10 +159,7 @@ internal V3f get_barycentric_coords(V2f a, V2f b, V2f c, V2f p) {
|
||||
}
|
||||
|
||||
internal V2f 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(g_viewport, vh);
|
||||
|
||||
V3f output = project_vec4(vh);
|
||||
V3f output = *vertex;
|
||||
output.x = clamp(output.x, 0.0f, img->width);
|
||||
output.y = clamp(output.y, 0.0f, img->height);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user