Implement camera movement

This commit is contained in:
Abdelrahman Said 2024-08-12 00:24:12 +01:00
parent 4143fd7b05
commit 2f126f95ff

103
src/obj.c
View File

@ -28,13 +28,55 @@
V.x /= magnitude; \ V.x /= magnitude; \
V.y /= magnitude; \ V.y /= magnitude; \
V.z /= magnitude; \ V.z /= magnitude; \
} while (0); } while (0)
#define cross_product(V1, V2) \ #define cross_product(V1, V2) \
((V3f){ \ ((V3f){ \
.x = V1.y * V2.z - V1.z * V2.y, \ .x = V1.y * V2.z - V1.z * V2.y, \
.y = V1.z * V2.x - V1.x * V2.z, \ .y = V1.z * V2.x - V1.x * V2.z, \
.z = V1.x * V2.y - V1.y * V2.x, \ .z = V1.x * V2.y - V1.y * V2.x, \
}) })
#define mat4x4_identity \
((M4x4f){ \
.row0 = {1.0f, 0.0f, 0.0f, 0.0f}, \
.row1 = {0.0f, 1.0f, 0.0f, 0.0f}, \
.row2 = {0.0f, 0.0f, 1.0f, 0.0f}, \
.row3 = {0.0f, 0.0f, 0.0f, 1.0f}, \
})
#define mat4x4_mul(MAT1, MAT2) \
((M4x4f){ \
.row0.x = MAT1.row0.x * MAT2.row0.x + MAT1.row0.y * MAT2.row1.x + \
MAT1.row0.z * MAT2.row2.x + MAT1.row0.w * MAT2.row3.x, \
.row0.y = MAT1.row0.x * MAT2.row0.y + MAT1.row0.y * MAT2.row1.y + \
MAT1.row0.z * MAT2.row2.y + MAT1.row0.w * MAT2.row3.y, \
.row0.z = MAT1.row0.x * MAT2.row0.z + MAT1.row0.y * MAT2.row1.z + \
MAT1.row0.z * MAT2.row2.z + MAT1.row0.w * MAT2.row3.z, \
.row0.w = MAT1.row0.x * MAT2.row0.w + MAT1.row0.y * MAT2.row1.w + \
MAT1.row0.z * MAT2.row2.w + MAT1.row0.w * MAT2.row3.w, \
.row1.x = MAT1.row1.x * MAT2.row0.x + MAT1.row1.y * MAT2.row1.x + \
MAT1.row1.z * MAT2.row2.x + MAT1.row1.w * MAT2.row3.x, \
.row1.y = MAT1.row1.x * MAT2.row0.y + MAT1.row1.y * MAT2.row1.y + \
MAT1.row1.z * MAT2.row2.y + MAT1.row1.w * MAT2.row3.y, \
.row1.z = MAT1.row1.x * MAT2.row0.z + MAT1.row1.y * MAT2.row1.z + \
MAT1.row1.z * MAT2.row2.z + MAT1.row1.w * MAT2.row3.z, \
.row1.w = MAT1.row1.x * MAT2.row0.w + MAT1.row1.y * MAT2.row1.w + \
MAT1.row1.z * MAT2.row2.w + MAT1.row1.w * MAT2.row3.w, \
.row2.x = MAT1.row2.x * MAT2.row0.x + MAT1.row2.y * MAT2.row1.x + \
MAT1.row2.z * MAT2.row2.x + MAT1.row2.w * MAT2.row3.x, \
.row2.y = MAT1.row2.x * MAT2.row0.y + MAT1.row2.y * MAT2.row1.y + \
MAT1.row2.z * MAT2.row2.y + MAT1.row2.w * MAT2.row3.y, \
.row2.z = MAT1.row2.x * MAT2.row0.z + MAT1.row2.y * MAT2.row1.z + \
MAT1.row2.z * MAT2.row2.z + MAT1.row2.w * MAT2.row3.z, \
.row2.w = MAT1.row2.x * MAT2.row0.w + MAT1.row2.y * MAT2.row1.w + \
MAT1.row2.z * MAT2.row2.w + MAT1.row2.w * MAT2.row3.w, \
.row3.x = MAT1.row3.x * MAT2.row0.x + MAT1.row3.y * MAT2.row1.x + \
MAT1.row3.z * MAT2.row2.x + MAT1.row3.w * MAT2.row3.x, \
.row3.y = MAT1.row3.x * MAT2.row0.y + MAT1.row3.y * MAT2.row1.y + \
MAT1.row3.z * MAT2.row2.y + MAT1.row3.w * MAT2.row3.y, \
.row3.z = MAT1.row3.x * MAT2.row0.z + MAT1.row3.y * MAT2.row1.z + \
MAT1.row3.z * MAT2.row2.z + MAT1.row3.w * MAT2.row3.z, \
.row3.w = MAT1.row3.x * MAT2.row0.w + MAT1.row3.y * MAT2.row1.w + \
MAT1.row3.z * MAT2.row2.w + MAT1.row3.w * MAT2.row3.w, \
})
#define mat4x4_mul_vec4(MAT, V) \ #define mat4x4_mul_vec4(MAT, V) \
((V4f){ \ ((V4f){ \
.x = MAT.row0.x * V.x + MAT.row0.y * V.y + MAT.row0.z * V.z + \ .x = MAT.row0.x * V.x + MAT.row0.y * V.y + MAT.row0.z * V.z + \
@ -58,13 +100,12 @@ struct triangle_bbox {
internal void render_triangle(const Triangle *triangle, const Model *model, internal void render_triangle(const Triangle *triangle, const Model *model,
Render *render, Colour colour, RenderType type, Render *render, Colour colour, RenderType type,
ProjectionType projection); ProjectionType projection, M4x4f mv);
internal void fill_triangle(Render *render, V3f vertices[TRIANGLE_VERTICES], internal void fill_triangle(Render *render, V3f vertices[TRIANGLE_VERTICES],
V3f normals[TRIANGLE_VERTICES], V3f normals[TRIANGLE_VERTICES],
V2f coordinates[TRIANGLE_VERTICES], Colour colour, V2f coordinates[TRIANGLE_VERTICES], Colour colour,
Image *texture, RenderType type); Image *texture, RenderType type);
internal void reorder_points(V2u vertices[TRIANGLE_VERTICES], internal M4x4f lookat(V3f eye, V3f target, V3f up);
V2f coordinates[TRIANGLE_VERTICES]);
internal TriangleBBox get_triangle_bbox(const Image *img, internal TriangleBBox get_triangle_bbox(const Image *img,
V3f vertices[TRIANGLE_VERTICES]); V3f vertices[TRIANGLE_VERTICES]);
internal V3f get_barycentric_coords(f32 d00, f32 d01, f32 d11, f32 denom, internal V3f get_barycentric_coords(f32 d00, f32 d01, f32 d11, f32 denom,
@ -85,6 +126,10 @@ M4x4f g_cam_matrix = {
}; };
// clang-format on // clang-format on
V3f g_eye = {0.2f, 0.1f, 0.7f};
V3f g_target = {0};
V3f g_up = {0.0f, 1.0f, 0.0f};
Model load_obj_file(Arena *arena, const char *filename, const char *texture) { Model load_obj_file(Arena *arena, const char *filename, const char *texture) {
if (!arena) { if (!arena) {
return INVALID_MODEL; return INVALID_MODEL;
@ -182,6 +227,7 @@ void render_model(const Model *model, Render *render, Colour colour,
RenderType type, ColourType colour_type, RenderType type, ColourType colour_type,
ProjectionType projection) { ProjectionType projection) {
Triangle triangle; Triangle triangle;
M4x4f model_view = lookat(g_eye, g_target, g_up);
for (u64 i = 0; i < model->triangles->count; ++i) { for (u64 i = 0; i < model->triangles->count; ++i) {
triangle = list_get(model->triangles, i); triangle = list_get(model->triangles, i);
@ -191,13 +237,14 @@ void render_model(const Model *model, Render *render, Colour colour,
.b = rand() % UINT8_MAX, .b = rand() % UINT8_MAX,
.a = 255}; .a = 255};
} }
render_triangle(&triangle, model, render, colour, type, projection); render_triangle(&triangle, model, render, colour, type, projection,
model_view);
} }
} }
internal void render_triangle(const Triangle *triangle, const Model *model, internal void render_triangle(const Triangle *triangle, const Model *model,
Render *render, Colour colour, RenderType type, Render *render, Colour colour, RenderType type,
ProjectionType projection) { ProjectionType projection, M4x4f mv) {
Image *img = &(render->img); Image *img = &(render->img);
V3f vertices[TRIANGLE_VERTICES] = { V3f vertices[TRIANGLE_VERTICES] = {
list_get(model->vertices, triangle->p0), list_get(model->vertices, triangle->p0),
@ -215,8 +262,21 @@ internal void render_triangle(const Triangle *triangle, const Model *model,
list_get(model->texture_coordinates, triangle->tx2), list_get(model->texture_coordinates, triangle->tx2),
}; };
// Camera
for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) {
V4f vertex;
vertex = (V4f){
.x = vertices[i].x,
.y = vertices[i].y,
.z = vertices[i].z,
.w = 1.0f,
};
vertex = mat4x4_mul_vec4(mv, vertex);
vertices[i] = project_vec4(vertex);
}
// Basic perspective projection
if (projection == PROJECTION_TYPE_PERSPECTIVE) { if (projection == PROJECTION_TYPE_PERSPECTIVE) {
// Basic perspective projection
V4f vertex; V4f vertex;
for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) { for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) {
vertex = (V4f){ vertex = (V4f){
@ -304,7 +364,7 @@ internal void fill_triangle(Render *render, V3f vertices[TRIANGLE_VERTICES],
} }
if (intensity < 0.0f) { if (intensity < 0.0f) {
continue; intensity = 0.01f;
} }
if (texture) { if (texture) {
@ -329,6 +389,33 @@ internal void fill_triangle(Render *render, V3f vertices[TRIANGLE_VERTICES],
} }
} }
internal M4x4f lookat(V3f eye, V3f target, V3f up) {
V3f z = V3(V3f, f32, target.x, target.y, target.z, eye.x, eye.y, eye.z);
normalise_v3(z);
V3f x = cross_product(up, z);
normalise_v3(x);
V3f y = cross_product(z, x);
normalise_v3(y);
M4x4f rotation = mat4x4_identity;
rotation.row0.x = x.x;
rotation.row0.y = x.y;
rotation.row0.z = x.z;
rotation.row1.x = y.x;
rotation.row1.y = y.y;
rotation.row1.z = y.z;
rotation.row2.x = z.x;
rotation.row2.y = z.y;
rotation.row2.z = z.z;
M4x4f translation = mat4x4_identity;
translation.row0.w = -(eye.x);
translation.row1.w = -(eye.y);
translation.row2.w = -(eye.z);
return mat4x4_mul(rotation, translation);
}
internal TriangleBBox get_triangle_bbox(const Image *img, internal TriangleBBox get_triangle_bbox(const Image *img,
V3f vertices[TRIANGLE_VERTICES]) { V3f vertices[TRIANGLE_VERTICES]) {
f32 x0 = min(vertices[0].x, min(vertices[1].x, vertices[2].x)); f32 x0 = min(vertices[0].x, min(vertices[1].x, vertices[2].x));