Apply transformation first then project the points
This commit is contained in:
parent
9d929c22ba
commit
26fc513d4a
@ -18,6 +18,7 @@ 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 {
|
||||||
|
@ -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_vec2i_t *projected = list_create_with_capacity(
|
list_vec4f_t *transformed = list_create_with_capacity(
|
||||||
vec2i_t, arena, instance->model->vertices->count * sizeof(vec3f_t));
|
vec4f_t, arena, instance->model->vertices->count * sizeof(vec4f_t));
|
||||||
if (!projected) {
|
if (!transformed) {
|
||||||
return;
|
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);
|
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};
|
||||||
vec2i_t point = {0};
|
vec4f_t xf_vertex = {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);
|
||||||
vertex = mul_mat3x4f_by_vec4f(
|
xf_vertex = mul_mat4x4f_by_vec4f(
|
||||||
xf_proj_mat, (vec4f_t){vertex.x, vertex.y, vertex.z, 1.0f});
|
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)};
|
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);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user