refactor that fixed gaps in face triangles

This commit is contained in:
jb 2025-07-09 22:42:29 -07:00
parent f0bf328957
commit 86ebc14924
4 changed files with 9 additions and 22 deletions

View File

@ -95,7 +95,7 @@ void draw_line(int x0, int y0, int x1, int y1, uint32_t color) {
float current_x = x0;
float current_y = y0;
for (int i = 0; i < longest_side_length; i++) {
for (int i = 0; i <= longest_side_length; i++) {
draw_pixel(round(current_x), round(current_y), color);
current_x += x_inc;
current_y += y_inc;

View File

@ -126,7 +126,7 @@ void update(void) {
transformed_vertex = vec3_rotate_z(transformed_vertex, mesh.rotation.z);
// translate away from camera in z
transformed_vertex.z += 1;
transformed_vertex.z += 5;
// save translated vertex in the array of transformed vertices
transformed_vertices[j] = transformed_vertex;
@ -178,19 +178,6 @@ void update(void) {
triangle_t normal_triangle;
for (int j = 0; j < 3; j++) {
// project the current vertex
vec2_t projected_point = project(transformed_vertices[j]);
// scale and translate the projected points to the center of the screen
projected_point.x += (window_width / 2);
projected_point.y += (window_height / 2);
normal_triangle.points[j] = projected_point;
}
array_push(triangles_to_render, normal_triangle);
}
}

View File

@ -75,7 +75,7 @@ void load_file(void) {
FILE* fp;
char* tok;
fp = fopen("..\\assets\\beetle-alt.obj", "r");
fp = fopen("..\\assets\\cube.obj", "r");
if (fp == NULL) {
exit(EXIT_FAILURE);

View File

@ -13,8 +13,8 @@ int compare_vec2_t(const void* a, const void* b) {
return 0;
}
#define FUNC ceilf
#define FUNC2 ceilf
#define FUNC (int)ceilf
#define FUNC2 (int)floorf
uint32_t invert_rgb(uint32_t color) {
uint32_t a = color & 0xFF000000; // Alpha channel
@ -29,9 +29,9 @@ void fill_flat_bottom_triangle(const triangle_t* triangle, uint32_t color) {
float x_start = triangle->points[0].x, x_end = triangle->points[0].x;
for (int x = FUNC(triangle->points[0].y); x <= FUNC(triangle->points[2].y); x++) {
for (int x = FUNC(triangle->points[0].y); x <= FUNC2(triangle->points[2].y); x++) {
//draw_rect((int)triangle->points[0].x, i, 1, 1, 0xFFFF0000);
draw_line(FUNC2(x_start), x, FUNC2(x_end), x, color);
draw_line(FUNC(x_start), x, FUNC2(x_end), x, color);
x_start += inv_slope_1;
x_end += inv_slope_2;
}
@ -43,9 +43,9 @@ void fill_flat_top_triangle(const triangle_t* triangle, uint32_t color) {
float x_start = triangle->points[2].x, x_end = triangle->points[2].x;
for (int y = FUNC(triangle->points[2].y); y >= FUNC(triangle->points[0].y); y--) {
for (int y = FUNC2(triangle->points[2].y); y >= FUNC(triangle->points[0].y); y--) {
//draw_rect((int)triangle->points[0].x, i, 1, 1, 0xFFFF0000);
draw_line(FUNC2(x_start), y, FUNC2(x_end), y, color);
draw_line(FUNC(x_start), y, FUNC2(x_end), y, color);
x_start -= inv_slope_1;
x_end -= inv_slope_2;
}