Compare commits

...

3 Commits

5 changed files with 75 additions and 9 deletions
include
rasteriser
vector
src
rasteriser
vector

@ -18,6 +18,7 @@ typedef struct {
MAKE_LIST_TYPE(f32);
MAKE_LIST_TYPE(vec2i_t);
MAKE_LIST_TYPE(vec3f_t);
MAKE_LIST_TYPE(vec4f_t);
MAKE_LIST_TYPE(scene_triangle_t);
typedef struct {
@ -51,11 +52,21 @@ typedef struct {
transform_t transform;
} instance_t;
typedef struct {
vec3f_t normal;
f32 distance;
} clipping_plane_t;
typedef struct {
u64 instance_count;
instance_t *instances;
camera_t *camera;
const mat3x4f_t *proj_matrix;
clipping_plane_t near;
clipping_plane_t left;
clipping_plane_t right;
clipping_plane_t bottom;
clipping_plane_t top;
} rasteriser_scene_t;
void render_scene(window_t *wnd, Arena *arena, const rasteriser_scene_t *scene);

@ -128,6 +128,7 @@ mat4x4f_t get_scaling_matrix(vec3f_t scale);
mat3x3f_t get_rotation_mat3x3f(vec3f_t rotation);
vec3f_t mul_mat3x4f_by_vec4f(mat3x4f_t mat, vec4f_t vec);
mat3x4f_t mul_mat3x4f_by_mat4x4f(mat3x4f_t mat1, mat4x4f_t mat2);
vec4f_t mul_mat4x4f_by_vec4f(mat4x4f_t mat, vec4f_t vec);
mat4x4f_t mul_mat4x4f(mat4x4f_t mat1, mat4x4f_t mat2);
#endif // !VEC_H

@ -8,6 +8,7 @@
#include "window/window.h"
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_keycode.h>
#include <math.h>
#include <stdlib.h>
int main(void) {
@ -97,11 +98,40 @@ int main(void) {
(vec4f_t){0.0f, 0.0f, 1.0f, 0.0f},
};
f32 sqrt_2 = sqrtf(2.0f);
f32 one_div_sqrt_2 = 1.0f / sqrt_2;
f32 neg_one_div_sqrt_2 = -1.0f / sqrt_2;
rasteriser_scene_t scene = {
.instance_count = ARR_LEN(instances),
.instances = instances,
.camera = &camera,
.proj_matrix = &proj_matrix,
.near =
(clipping_plane_t){
.normal = (vec3f_t){0.0f, 0.0f, 1.0f},
.distance = viewport.z,
},
.left =
(clipping_plane_t){
.normal = (vec3f_t){one_div_sqrt_2, 0.0f, one_div_sqrt_2},
.distance = 0.0f,
},
.right =
(clipping_plane_t){
.normal = (vec3f_t){neg_one_div_sqrt_2, 0.0f, one_div_sqrt_2},
.distance = 0.0f,
},
.bottom =
(clipping_plane_t){
.normal = (vec3f_t){0.0f, one_div_sqrt_2, one_div_sqrt_2},
.distance = 0.0f,
},
.top =
(clipping_plane_t){
.normal = (vec3f_t){0.0f, neg_one_div_sqrt_2, one_div_sqrt_2},
.distance = 0.0f,
},
};
bool running = true;
@ -141,7 +171,7 @@ int main(void) {
clear_window(&window, bg);
render_scene(&window, arena, &scene);
render_scene(&window, frame_arena, &scene);
swap_buffers(&window);

@ -31,9 +31,9 @@ void render_scene(window_t *wnd, Arena *arena,
void render_instance(window_t *wnd, Arena *arena, mat4x4f_t camera_matrix,
mat3x4f_t projection_matrix, const instance_t *instance) {
list_vec2i_t *projected = list_create_with_capacity(
vec2i_t, arena, instance->model->vertices->count * sizeof(vec3f_t));
if (!projected) {
list_vec4f_t *transformed = list_create_with_capacity(
vec4f_t, arena, instance->model->vertices->count * sizeof(vec4f_t));
if (!transformed) {
return;
}
@ -47,14 +47,25 @@ void render_instance(window_t *wnd, Arena *arena, mat4x4f_t camera_matrix,
mat4x4f_t xf_cam_mat = mul_mat4x4f(camera_matrix, trs_mat);
mat3x4f_t xf_proj_mat = mul_mat3x4f_by_mat4x4f(projection_matrix, xf_cam_mat);
vec3f_t vertex = {0};
vec2i_t point = {0};
vec4f_t xf_vertex = {0};
for (u64 i = 0; i < instance->model->vertices->count; ++i) {
vertex = list_get(instance->model->vertices, i);
vertex = mul_mat3x4f_by_vec4f(
xf_proj_mat, (vec4f_t){vertex.x, vertex.y, vertex.z, 1.0f});
xf_vertex = mul_mat4x4f_by_vec4f(
xf_cam_mat, (vec4f_t){vertex.x, vertex.y, vertex.z, 1.0f});
list_append(vec4f_t, arena, transformed, xf_vertex);
}
list_vec2i_t *projected = list_create_with_capacity(
vec2i_t, arena, instance->model->vertices->count * sizeof(vec2i_t));
if (!projected) {
return;
}
vec2i_t point = {0};
for (u64 i = 0; i < transformed->count; ++i) {
xf_vertex = list_get(transformed, i);
vertex = mul_mat3x4f_by_vec4f(projection_matrix, xf_vertex);
point = (vec2i_t){(i32)(vertex.x / vertex.z), (i32)(vertex.y / vertex.z)};
list_append(vec2i_t, arena, projected, point);
}

@ -342,6 +342,19 @@ mat3x4f_t mul_mat3x4f_by_mat4x4f(mat3x4f_t mat1, mat4x4f_t mat2) {
};
}
vec4f_t mul_mat4x4f_by_vec4f(mat4x4f_t mat, vec4f_t vec) {
f32 x = mat.row0.x * vec.x + mat.row0.y * vec.y + mat.row0.z * vec.z +
mat.row0.w * vec.w;
f32 y = mat.row1.x * vec.x + mat.row1.y * vec.y + mat.row1.z * vec.z +
mat.row1.w * vec.w;
f32 z = mat.row2.x * vec.x + mat.row2.y * vec.y + mat.row2.z * vec.z +
mat.row2.w * vec.w;
f32 w = mat.row3.x * vec.x + mat.row3.y * vec.y + mat.row3.z * vec.z +
mat.row3.w * vec.w;
return (vec4f_t){x, y, z, w};
}
mat4x4f_t mul_mat4x4f(mat4x4f_t mat1, mat4x4f_t mat2) {
f32 row0_col0 = mat1.row0.x * mat2.row0.x + mat1.row0.y * mat2.row1.x +
mat1.row0.z * mat2.row2.x + mat1.row0.w * mat2.row3.x;