Compare commits
2 Commits
1c1b611bbb
...
e3c463d2f1
Author | SHA1 | Date | |
---|---|---|---|
e3c463d2f1 | |||
01ece119ed |
@ -31,9 +31,8 @@
|
|||||||
munmap(LP, sizeof(LIST_TYPE_NAME(T))); \
|
munmap(LP, sizeof(LIST_TYPE_NAME(T))); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#define list_append(T, LP, ITEM) \
|
#define _increase_list_capacity(T, LP, CAP) \
|
||||||
do { \
|
do { \
|
||||||
if (LP->count + 1 >= LP->capacity) { \
|
|
||||||
u64 new_capacity = LP->capacity * 2; \
|
u64 new_capacity = LP->capacity * 2; \
|
||||||
T *tmp = _alloc_for_list(sizeof(T) * new_capacity); \
|
T *tmp = _alloc_for_list(sizeof(T) * new_capacity); \
|
||||||
assert(tmp != NULL && "Failed to increase capacity"); \
|
assert(tmp != NULL && "Failed to increase capacity"); \
|
||||||
@ -45,13 +44,39 @@
|
|||||||
\
|
\
|
||||||
LP->capacity = new_capacity; \
|
LP->capacity = new_capacity; \
|
||||||
LP->items = tmp; \
|
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); \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
LP->items[(LP->count)++] = ITEM; \
|
LP->items[(LP->count)++] = ITEM; \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#define list_pop(LP) (LP->count -= 1)
|
||||||
|
|
||||||
#define list_get(LP, IDX) LP->items[IDX]
|
#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);
|
MAKE_LIST_TYPE(void);
|
||||||
|
|
||||||
list_void_t *_create_list(u64 size, u64 count);
|
list_void_t *_create_list(u64 size, u64 count);
|
||||||
|
@ -21,6 +21,7 @@ typedef struct {
|
|||||||
|
|
||||||
void draw_wireframe_triangle(window_t *wnd, triangle_t triangle,
|
void draw_wireframe_triangle(window_t *wnd, triangle_t triangle,
|
||||||
colour_t colour);
|
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);
|
void draw_line(window_t *wnd, line_t line, colour_t colour);
|
||||||
|
|
||||||
#endif // !RASTERISER_H
|
#endif // !RASTERISER_H
|
||||||
|
@ -40,8 +40,9 @@ int main(void) {
|
|||||||
|
|
||||||
clear_window(&window, bg);
|
clear_window(&window, bg);
|
||||||
|
|
||||||
|
draw_filled_triangle(&window, triangle, (colour_t){.colour = 0x00ff00ff});
|
||||||
draw_wireframe_triangle(&window, triangle,
|
draw_wireframe_triangle(&window, triangle,
|
||||||
(colour_t){.colour = 0xffffffff});
|
(colour_t){.colour = 0x000000ff});
|
||||||
|
|
||||||
swap_buffers(&window);
|
swap_buffers(&window);
|
||||||
}
|
}
|
||||||
|
@ -4,18 +4,60 @@
|
|||||||
#include "vector/vec.h"
|
#include "vector/vec.h"
|
||||||
#include "window/window.h"
|
#include "window/window.h"
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <math.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
|
||||||
internal list_float_t *interpolate(i32 i0, f32 d0, i32 i1, f32 d1);
|
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,
|
void draw_wireframe_triangle(window_t *wnd, triangle_t triangle,
|
||||||
colour_t colour) {
|
colour_t colour) {
|
||||||
|
order_triangle_points(&triangle);
|
||||||
|
|
||||||
draw_line(wnd, (line_t){triangle.p0, triangle.p1}, colour);
|
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.p1, triangle.p2}, colour);
|
||||||
draw_line(wnd, (line_t){triangle.p2, triangle.p0}, 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) {
|
void draw_line(window_t *wnd, line_t line, colour_t colour) {
|
||||||
list_float_t *values = NULL;
|
list_float_t *values = NULL;
|
||||||
|
|
||||||
@ -86,3 +128,15 @@ internal list_float_t *interpolate(i32 i0, f32 d0, i32 i1, f32 d1) {
|
|||||||
|
|
||||||
return values;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user