Compare commits
No commits in common. "ab469b8f37b66aa45de870372d5b0cc2061fd7b8" and "65dcd66f76074be8f977ca5cd08ee61d5fc03e30" have entirely different histories.
ab469b8f37
...
65dcd66f76
1
compile
1
compile
@ -14,7 +14,6 @@ RAYTRACER_SRC="src/window/*.c \
|
||||
RASTERISER_SRC="src/window/*.c \
|
||||
src/vector/*.c \
|
||||
src/scene/*.c \
|
||||
src/list/*.c \
|
||||
src/rasteriser/*.c \
|
||||
src/math/*.c \
|
||||
"
|
||||
|
@ -1,60 +0,0 @@
|
||||
#ifndef TYPED_LIST_H
|
||||
#define TYPED_LIST_H
|
||||
|
||||
#include "c_cpp_aliases/aliases.h"
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BASE_LIST_CAPACITY 1024
|
||||
|
||||
#define CONCAT(A, B, C) A##B##C
|
||||
#define LIST_TYPE_NAME(T) CONCAT(list_, T, _t)
|
||||
|
||||
#define MAKE_LIST_TYPE(T) \
|
||||
typedef struct { \
|
||||
T *items; \
|
||||
u64 capacity; \
|
||||
u64 count; \
|
||||
} LIST_TYPE_NAME(T)
|
||||
|
||||
#define create_list(T) \
|
||||
(LIST_TYPE_NAME(T) *)_create_list(sizeof(T), BASE_LIST_CAPACITY)
|
||||
#define create_list_with_capacity(T, CAPACITY) \
|
||||
(LIST_TYPE_NAME(T) *)_create_list(sizeof(T), CAPACITY)
|
||||
|
||||
#define destroy_list(T, LP) \
|
||||
do { \
|
||||
munmap(LP->items, sizeof(T) * LP->capacity); \
|
||||
LP->items = NULL; \
|
||||
LP->count = 0; \
|
||||
LP->capacity = 0; \
|
||||
munmap(LP, sizeof(LIST_TYPE_NAME(T))); \
|
||||
} while (0)
|
||||
|
||||
#define append(T, LP, ITEM) \
|
||||
do { \
|
||||
if (LP->count + 1 >= LP->capacity) { \
|
||||
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 get(LP, IDX) LP->items[IDX]
|
||||
|
||||
MAKE_LIST_TYPE(void);
|
||||
|
||||
list_void_t *_create_list(u64 size, u64 count);
|
||||
void *_alloc_for_list(u64 size);
|
||||
|
||||
#endif // !TYPED_LIST_H
|
@ -1,13 +1,9 @@
|
||||
#ifndef RASTERISER_H
|
||||
#define RASTERISER_H
|
||||
|
||||
#include "c_cpp_aliases/aliases.h"
|
||||
#include "list/typed_list.h"
|
||||
#include "vector/vec.h"
|
||||
#include "window/window.h"
|
||||
|
||||
MAKE_LIST_TYPE(f32);
|
||||
|
||||
typedef struct {
|
||||
vec2i_t p0;
|
||||
vec2i_t p1;
|
||||
|
@ -1,26 +0,0 @@
|
||||
#include "list/typed_list.h"
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
list_void_t *_create_list(u64 size, u64 count) {
|
||||
list_void_t *list = (list_void_t *)_alloc_for_list(sizeof(list_void_t));
|
||||
if (!list) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list->items = (void *)_alloc_for_list(size * count);
|
||||
if (!list->items) {
|
||||
munmap(list, sizeof(list_void_t));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list->capacity = count;
|
||||
list->count = 0;
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
void *_alloc_for_list(u64 size) {
|
||||
return mmap(NULL, size, PROT_READ | PROT_WRITE,
|
||||
MAP_ANON | MAP_NORESERVE | MAP_SHARED, -1, 0);
|
||||
}
|
@ -1,76 +1,23 @@
|
||||
#include "rasteriser/rasteriser.h"
|
||||
#include "c_cpp_aliases/aliases.h"
|
||||
#include "list/typed_list.h"
|
||||
#include "vector/vec.h"
|
||||
#include "window/window.h"
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
internal list_float_t *interpolate(i32 i0, f32 d0, i32 i1, f32 d1);
|
||||
|
||||
void draw_line(window_t *wnd, line_t line, colour_t colour) {
|
||||
if (line.p1.x < line.p0.x) {
|
||||
vec_swap(vec2i_t, line.p0, line.p1);
|
||||
}
|
||||
|
||||
i32 x0 = line.p0.x;
|
||||
i32 y0 = line.p0.y;
|
||||
i32 x1 = line.p1.x;
|
||||
i32 y1 = line.p1.y;
|
||||
|
||||
list_float_t *values = NULL;
|
||||
f32 a = ((f32)y1 - y0) / ((f32)x1 - x0);
|
||||
f32 y = y0;
|
||||
|
||||
if (abs(x1 - x0) > abs(y1 - y0)) {
|
||||
if (line.p1.x < line.p0.x) {
|
||||
vec_swap(vec2i_t, line.p0, line.p1);
|
||||
}
|
||||
|
||||
values = interpolate(x0, y0, x1, y1);
|
||||
if (!values) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (i32 x = x0; x <= x1; ++x) {
|
||||
set_pixel(wnd, x, (i32)(get(values, x - x0)), colour);
|
||||
}
|
||||
} else {
|
||||
if (line.p1.y < line.p0.y) {
|
||||
vec_swap(vec2i_t, line.p0, line.p1);
|
||||
}
|
||||
|
||||
values = interpolate(y0, x0, y1, x1);
|
||||
if (!values) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (i32 y = y0; y <= y1; ++y) {
|
||||
set_pixel(wnd, (i32)(get(values, y - y0)), y, colour);
|
||||
}
|
||||
for (i32 x = x0; x <= x1; ++x) {
|
||||
set_pixel(wnd, x, (i32)y, colour);
|
||||
y += a;
|
||||
}
|
||||
|
||||
destroy_list(f32, values);
|
||||
}
|
||||
|
||||
internal list_float_t *interpolate(i32 i0, f32 d0, i32 i1, f32 d1) {
|
||||
list_float_t *values;
|
||||
if (i0 == i1) {
|
||||
values = create_list_with_capacity(f32, 20);
|
||||
if (values) {
|
||||
append(f32, values, d0);
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
values = create_list(f32);
|
||||
if (!values) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
f32 a = (d1 - d0) / ((f32)i1 - i0);
|
||||
f32 d = d0;
|
||||
|
||||
for (i32 i = i0; i <= i1; ++i) {
|
||||
append(f32, values, d);
|
||||
d += a;
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user