Compare commits

..

No commits in common. "26fc513d4a4c0bdefafea3c34e9b117b44e0dd0d" and "7823e92861bbf11d3bcfb54ea2ee55c0f4ee81fa" have entirely different histories.

5 changed files with 9 additions and 75 deletions

View File

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

View File

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

View File

@ -8,7 +8,6 @@
#include "window/window.h" #include "window/window.h"
#include <SDL2/SDL_events.h> #include <SDL2/SDL_events.h>
#include <SDL2/SDL_keycode.h> #include <SDL2/SDL_keycode.h>
#include <math.h>
#include <stdlib.h> #include <stdlib.h>
int main(void) { int main(void) {
@ -98,40 +97,11 @@ int main(void) {
(vec4f_t){0.0f, 0.0f, 1.0f, 0.0f}, (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 = { rasteriser_scene_t scene = {
.instance_count = ARR_LEN(instances), .instance_count = ARR_LEN(instances),
.instances = instances, .instances = instances,
.camera = &camera, .camera = &camera,
.proj_matrix = &proj_matrix, .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; bool running = true;
@ -171,7 +141,7 @@ int main(void) {
clear_window(&window, bg); clear_window(&window, bg);
render_scene(&window, frame_arena, &scene); render_scene(&window, arena, &scene);
swap_buffers(&window); swap_buffers(&window);

View File

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

View File

@ -342,19 +342,6 @@ 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) { 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 + 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; mat1.row0.z * mat2.row2.x + mat1.row0.w * mat2.row3.x;