Compare commits

..

No commits in common. "e3c463d2f14915dfcf63bb872b6e9f72be0c7724" and "1c1b611bbb175ba230f13499b8c455113dd72440" have entirely different histories.

4 changed files with 12 additions and 93 deletions

View File

@ -31,52 +31,27 @@
munmap(LP, sizeof(LIST_TYPE_NAME(T))); \
} while (0)
#define _increase_list_capacity(T, LP, CAP) \
do { \
u64 new_capacity = LP->capacity * 2; \
T *tmp = _alloc_for_list(sizeof(T) * new_capacity); \
assert(tmp != NULL && "Failed to increase capacity"); \
\
memcpy(tmp, LP->items, sizeof(T) * LP->count); \
\
i32 deallocated = munmap(LP->items, sizeof(T) * LP->capacity); \
assert(deallocated == 0 && "Failed to deallocate old memory"); \
\
LP->capacity = new_capacity; \
LP->items = tmp; \
} while (0)
#define list_append(T, LP, ITEM) \
do { \
if (LP->count + 1 >= LP->capacity) { \
_increase_list_capacity(T, LP, LP->capacity * 2); \
u64 new_capacity = LP->capacity * 2; \
T *tmp = _alloc_for_list(sizeof(T) * new_capacity); \
assert(tmp != NULL && "Failed to increase capacity"); \
\
memcpy(tmp, LP->items, sizeof(T) * LP->count); \
\
i32 deallocated = munmap(LP->items, sizeof(T) * LP->capacity); \
assert(deallocated == 0 && "Failed to deallocate old memory"); \
\
LP->capacity = new_capacity; \
LP->items = tmp; \
} \
\
LP->items[(LP->count)++] = ITEM; \
} while (0)
#define list_pop(LP) (LP->count -= 1)
#define list_get(LP, IDX) LP->items[IDX]
#define list_merge(T, DST, LP1, LP2) \
do { \
u64 new_count = LP1->count + LP2->count; \
u64 capacity = \
new_count < BASE_LIST_CAPACITY ? BASE_LIST_CAPACITY : new_count * 2; \
\
DST = (LIST_TYPE_NAME(T) *)_create_list(new_count, capacity); \
assert(DST != NULL && "Failed to allocate new list"); \
\
u64 lp1_copy_size = sizeof(T) * LP1->count; \
u64 lp2_copy_size = sizeof(T) * LP2->count; \
memcpy(DST->items, LP1->items, lp1_copy_size); \
T *dst = &(DST->items[LP1->count]); \
memcpy(dst, LP2->items, lp2_copy_size); \
DST->capacity = capacity; \
DST->count = new_count; \
} while (0)
MAKE_LIST_TYPE(void);
list_void_t *_create_list(u64 size, u64 count);

View File

@ -21,7 +21,6 @@ typedef struct {
void draw_wireframe_triangle(window_t *wnd, triangle_t triangle,
colour_t colour);
void draw_filled_triangle(window_t *wnd, triangle_t triangle, colour_t colour);
void draw_line(window_t *wnd, line_t line, colour_t colour);
#endif // !RASTERISER_H

View File

@ -40,9 +40,8 @@ int main(void) {
clear_window(&window, bg);
draw_filled_triangle(&window, triangle, (colour_t){.colour = 0x00ff00ff});
draw_wireframe_triangle(&window, triangle,
(colour_t){.colour = 0x000000ff});
(colour_t){.colour = 0xffffffff});
swap_buffers(&window);
}

View File

@ -4,60 +4,18 @@
#include "vector/vec.h"
#include "window/window.h"
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
internal list_float_t *interpolate(i32 i0, f32 d0, i32 i1, f32 d1);
internal inline void order_triangle_points(triangle_t *triangle);
void draw_wireframe_triangle(window_t *wnd, triangle_t triangle,
colour_t colour) {
order_triangle_points(&triangle);
draw_line(wnd, (line_t){triangle.p0, triangle.p1}, colour);
draw_line(wnd, (line_t){triangle.p1, triangle.p2}, colour);
draw_line(wnd, (line_t){triangle.p2, triangle.p0}, colour);
}
void draw_filled_triangle(window_t *wnd, triangle_t triangle, colour_t colour) {
order_triangle_points(&triangle);
i32 x0 = triangle.p0.x;
i32 y0 = triangle.p0.y;
i32 x1 = triangle.p1.x;
i32 y1 = triangle.p1.y;
i32 x2 = triangle.p2.x;
i32 y2 = triangle.p2.y;
list_float_t *x01 = interpolate(y0, x0, y1, x1);
list_float_t *x12 = interpolate(y1, x1, y2, x2);
list_float_t *x02 = interpolate(y0, x0, y2, x2);
list_float_t *x012 = NULL;
list_pop(x01); // Last element of x01 is a duplicate of first element in x12
list_merge(f32, x012, x01, x12);
list_float_t *x_left;
list_float_t *x_right;
u64 middle = (u64)(floorf((f32)(x02->count) / 2.0f));
if (list_get(x02, middle) < list_get(x012, middle)) {
x_left = x02;
x_right = x012;
} else {
x_left = x012;
x_right = x02;
}
for (i64 y = y0; y <= y2; ++y) {
for (i64 x = list_get(x_left, y - y0); x < list_get(x_right, y - y0); ++x) {
set_pixel(wnd, x, y, colour);
}
}
}
void draw_line(window_t *wnd, line_t line, colour_t colour) {
list_float_t *values = NULL;
@ -128,15 +86,3 @@ internal list_float_t *interpolate(i32 i0, f32 d0, i32 i1, f32 d1) {
return values;
}
internal inline void order_triangle_points(triangle_t *triangle) {
if (triangle->p1.y < triangle->p0.y) {
vec_swap(vec2i_t, triangle->p0, triangle->p1);
}
if (triangle->p2.y < triangle->p0.y) {
vec_swap(vec2i_t, triangle->p0, triangle->p2);
}
if (triangle->p2.y < triangle->p1.y) {
vec_swap(vec2i_t, triangle->p1, triangle->p2);
}
}