tinyrenderer/src/obj.c

469 lines
17 KiB
C

#include "obj.h"
#include "aliases.h"
#include "img.h"
#include "mem_arena.h"
#include "pam.h"
#include "typed_list.h"
#include "utils.h"
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRIANGLE_VERTICES 3
#define CAMERA_DISTANCE 5.0f
#define V2(T, ELEM_T, X0, Y0, X1, Y1) \
((T){(ELEM_T)X1 - (ELEM_T)X0, (ELEM_T)Y1 - (ELEM_T)Y0})
#define V3(T, ELEM_T, X0, Y0, Z0, X1, Y1, Z1) \
((T){(ELEM_T)X1 - (ELEM_T)X0, (ELEM_T)Y1 - (ELEM_T)Y0, \
(ELEM_T)Z1 - (ELEM_T)Z0})
#define dot_v2(V1, V2) ((f32)V1.x * (f32)V2.x + (f32)V1.y * (f32)V2.y)
#define dot_v3(V1, V2) \
((f32)V1.x * (f32)V2.x + (f32)V1.y * (f32)V2.y + (f32)V1.z * (f32)V2.z)
#define normalise_v3(V) \
do { \
f32 magnitude = sqrtf(dot_v3(V, V)); \
V.x /= magnitude; \
V.y /= magnitude; \
V.z /= magnitude; \
} while (0)
#define cross_product(V1, V2) \
((V3f){ \
.x = V1.y * V2.z - V1.z * V2.y, \
.y = V1.z * V2.x - V1.x * V2.z, \
.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) \
((V4f){ \
.x = MAT.row0.x * V.x + MAT.row0.y * V.y + MAT.row0.z * V.z + \
MAT.row0.w * V.w, \
.y = MAT.row1.x * V.x + MAT.row1.y * V.y + MAT.row1.z * V.z + \
MAT.row1.w * V.w, \
.z = MAT.row2.x * V.x + MAT.row2.y * V.y + MAT.row2.z * V.z + \
MAT.row2.w * V.w, \
.w = MAT.row3.x * V.x + MAT.row3.y * V.y + MAT.row3.z * V.z + \
MAT.row3.w * V.w, \
})
#define project_vec4(V) ((V3f){.x = V.x / V.w, .y = V.y / V.w, .z = V.z / V.w})
typedef struct triangle_bbox TriangleBBox;
struct triangle_bbox {
u64 x0;
u64 y0;
u64 x1;
u64 y1;
};
internal void render_triangle(const Triangle *triangle, const Model *model,
Render *render, Colour colour, RenderType type,
ProjectionType projection, M4x4f mv);
internal void fill_triangle(Render *render, V3f vertices[TRIANGLE_VERTICES],
V3f normals[TRIANGLE_VERTICES],
V2f coordinates[TRIANGLE_VERTICES], Colour colour,
Image *texture, RenderType type);
internal M4x4f lookat(V3f eye, V3f target, V3f up);
internal TriangleBBox get_triangle_bbox(const Image *img,
V3f vertices[TRIANGLE_VERTICES]);
internal V3f get_barycentric_coords(f32 d00, f32 d01, f32 d11, f32 denom,
const V2i *ab, const V2i *ac,
const V2i *ap);
internal void get_image_coordinates(f32 norm_x, f32 norm_y, const Image *img,
u64 *x, u64 *y);
internal u64 ndc_to_image_coordinate(f32 value, u64 max);
V3f g_light_dir = {0.0f, 0.0f, 1.0f};
// clang-format off
M4x4f g_cam_matrix = {
.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, -1.0f / CAMERA_DISTANCE, 1.0f},
};
// 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) {
if (!arena) {
return INVALID_MODEL;
}
FILE *fp = fopen(filename, "r");
if (!fp) {
return INVALID_MODEL;
}
Model model = (Model){
.vertices = list_create(V3f, arena),
.normals = list_create(V3f, arena),
.texture_coordinates = list_create(V2f, arena),
.triangles = list_create(Triangle, arena),
};
if (!(model.vertices) || !(model.normals) || !(model.texture_coordinates) ||
!(model.triangles)) {
return INVALID_MODEL;
}
char line[8192];
char identifier[8];
V3f vertex;
V3f normal;
V2f coord;
Triangle triangle;
f32 vx, vy, vz;
f32 nx, ny, nz;
f32 u, v;
u64 fp0, fp1, fp2;
u64 vn0, vn1, vn2;
u64 tx0, tx1, tx2;
while (fgets(line, 8191, fp) != NULL) {
sscanf(line, "%s", identifier);
if (strncmp(identifier, "v", 8) == 0) {
sscanf(line + 2, "%f %f %f", &vx, &vy, &vz);
vertex.x = vx;
vertex.y = vy;
vertex.z = vz;
list_append(V3f, arena, model.vertices, vertex);
} else if (strncmp(identifier, "vn", 8) == 0) {
sscanf(line + 2, "%f %f %f", &nx, &ny, &nz);
normal.x = nx;
normal.y = ny;
normal.z = nz;
list_append(V3f, arena, model.normals, normal);
} else if (strncmp(identifier, "vt", 8) == 0) {
sscanf(line + 2, "%f %f", &u, &v);
coord.u = u;
coord.v = v;
list_append(V2f, arena, model.texture_coordinates, coord);
} else if (strncmp(identifier, "f", 8) == 0) {
sscanf(line + 2, "%lu/%lu/%lu %lu/%lu/%lu %lu/%lu/%lu", &fp0, &tx0, &vn0,
&fp1, &tx1, &vn1, &fp2, &tx2, &vn2);
// OBJ indices start from 1
triangle.p0 = fp0 - 1;
triangle.p1 = fp1 - 1;
triangle.p2 = fp2 - 1;
triangle.n0 = vn0 - 1;
triangle.n1 = vn1 - 1;
triangle.n2 = vn2 - 1;
triangle.tx0 = tx0 - 1;
triangle.tx1 = tx1 - 1;
triangle.tx2 = tx2 - 1;
list_append(Triangle, arena, model.triangles, triangle);
}
}
if (texture) {
model.texture = load_p6_image(arena, texture);
}
return model;
}
bool init_render(Arena *arena, Render *render, u64 width, u64 height) {
render->img = (Image){.width = width, .height = height};
if (!init_buffer(arena, &(render->img))) {
return false;
}
render->depth = (Depth){.width = width, .height = height};
if (!init_buffer(arena, &(render->depth))) {
return false;
}
f32 inf = -INFINITY;
clear_buffer(&(render->depth), &inf);
return true;
}
void render_model(const Model *model, Render *render, Colour colour,
RenderType type, ColourType colour_type,
ProjectionType projection) {
Triangle triangle;
M4x4f model_view = lookat(g_eye, g_target, g_up);
for (u64 i = 0; i < model->triangles->count; ++i) {
triangle = list_get(model->triangles, i);
if (colour_type == COLOUR_TYPE_RANDOM) {
colour = (Colour){.r = rand() % UINT8_MAX,
.g = rand() % UINT8_MAX,
.b = rand() % UINT8_MAX,
.a = 255};
}
render_triangle(&triangle, model, render, colour, type, projection,
model_view);
}
}
internal void render_triangle(const Triangle *triangle, const Model *model,
Render *render, Colour colour, RenderType type,
ProjectionType projection, M4x4f mv) {
Image *img = &(render->img);
V3f vertices[TRIANGLE_VERTICES] = {
list_get(model->vertices, triangle->p0),
list_get(model->vertices, triangle->p1),
list_get(model->vertices, triangle->p2),
};
V3f normals[TRIANGLE_VERTICES] = {
list_get(model->normals, triangle->n0),
list_get(model->normals, triangle->n1),
list_get(model->normals, triangle->n2),
};
V2f coordinates[TRIANGLE_VERTICES] = {
list_get(model->texture_coordinates, triangle->tx0),
list_get(model->texture_coordinates, triangle->tx1),
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) {
V4f vertex;
for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) {
vertex = (V4f){
.x = vertices[i].x,
.y = vertices[i].y,
.z = vertices[i].z,
.w = 1.0f,
};
vertex = mat4x4_mul_vec4(g_cam_matrix, vertex);
vertices[i] = project_vec4(vertex);
}
}
if (type == RENDER_TYPE_WIREFRAME) {
V3f v0, v1;
u64 x0, y0, x1, y1;
for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) {
v0 = vertices[i];
v1 = vertices[(i + 1) % TRIANGLE_VERTICES];
get_image_coordinates(v0.x, v0.y, img, &x0, &y0);
get_image_coordinates(v1.x, v1.y, img, &x1, &y1);
draw_line(img, x0, y0, x1, y1, colour);
}
} else if (type == RENDER_TYPE_FILLED || type == RENDER_TYPE_SHADED) {
fill_triangle(render, vertices, normals, coordinates, colour,
model->texture, type);
}
}
internal void fill_triangle(Render *render, V3f vertices[TRIANGLE_VERTICES],
V3f normals[TRIANGLE_VERTICES],
V2f coordinates[TRIANGLE_VERTICES], Colour colour,
Image *texture, RenderType type) {
Image *img = &(render->img);
Depth *depth = &(render->depth);
TriangleBBox bbox = get_triangle_bbox(img, vertices);
V2u v0, v1, v2;
get_image_coordinates(vertices[0].x, vertices[0].y, img, &(v0.x), &(v0.y));
get_image_coordinates(vertices[1].x, vertices[1].y, img, &(v1.x), &(v1.y));
get_image_coordinates(vertices[2].x, vertices[2].y, img, &(v2.x), &(v2.y));
V2i ab = V2(V2i, i64, v0.x, v0.y, v1.x, v1.y);
V2i ac = V2(V2i, i64, v0.x, v0.y, v2.x, v2.y);
f32 d00 = dot_v2(ab, ab);
f32 d01 = dot_v2(ab, ac);
f32 d11 = dot_v2(ac, ac);
f32 denom = d00 * d11 - d01 * d01;
V2i ap;
V3f coords;
f32 z;
f32 zbuf;
f32 nx, ny, nz;
V3f normal;
f32 tx_u, tx_v;
u64 tx_x, tx_y;
f32 intensity = 1.0f;
for (u64 y = bbox.y0; y <= bbox.y1; ++y) {
for (u64 x = bbox.x0; x <= bbox.x1; ++x) {
ap = V2(V2i, i64, v0.x, v0.y, x, y);
coords = get_barycentric_coords(d00, d01, d11, denom, &ab, &ac, &ap);
if (coords.x < 0.0f || coords.y < 0.0f || coords.x + coords.y > 1.0f) {
continue;
}
z = 0.0f;
z += vertices[0].z * coords.x + vertices[1].z * coords.y +
vertices[2].z * coords.z;
zbuf = get_pixel(f32, &(render->depth), x, y);
if (z > zbuf) {
if (type == RENDER_TYPE_SHADED) {
nx = normals[0].x * coords.x + normals[1].x * coords.y +
normals[2].x * coords.z;
ny = normals[0].y * coords.x + normals[1].y * coords.y +
normals[2].y * coords.z;
nz = normals[0].z * coords.x + normals[1].z * coords.y +
normals[2].z * coords.z;
normal = (V3f){nx, ny, nz};
intensity = dot_v3(normal, g_light_dir);
}
if (intensity < 0.0f) {
intensity = 0.01f;
}
if (texture) {
tx_u = coordinates[0].u * coords.x + coordinates[1].u * coords.y +
coordinates[2].u * coords.z;
tx_v = coordinates[0].v * coords.x + coordinates[1].v * coords.y +
coordinates[2].v * coords.z;
tx_x = tx_u * texture->width;
tx_y = (1.0f - tx_v) * texture->height;
colour = get_pixel(Colour, texture, tx_x, tx_y);
}
colour.r *= intensity;
colour.g *= intensity;
colour.b *= intensity;
set_pixel(depth, x, y, &z);
set_pixel(img, x, y, &colour);
}
}
}
}
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,
V3f vertices[TRIANGLE_VERTICES]) {
f32 x0 = min(vertices[0].x, min(vertices[1].x, vertices[2].x));
f32 x1 = max(vertices[0].x, max(vertices[1].x, vertices[2].x));
// NOTE (Abdelrahman): Because y is flipped, we use max for the minimum and
// min for the maximum
f32 y0 = max(vertices[0].y, max(vertices[1].y, vertices[2].y));
f32 y1 = min(vertices[0].y, min(vertices[1].y, vertices[2].y));
TriangleBBox bbox = {0};
get_image_coordinates(x0, y0, img, &(bbox.x0), &(bbox.y0));
get_image_coordinates(x1, y1, img, &(bbox.x1), &(bbox.y1));
return bbox;
}
internal V3f get_barycentric_coords(f32 d00, f32 d01, f32 d11, f32 denom,
const V2i *ab, const V2i *ac,
const V2i *ap) {
if (denom == 0.0f) {
return (V3f){-INFINITY, -INFINITY, -INFINITY};
}
f32 d20 = dot_v2((*ap), (*ab));
f32 d21 = dot_v2((*ap), (*ac));
f32 v = (d11 * d20 - d01 * d21) / denom;
f32 w = (d00 * d21 - d01 * d20) / denom;
f32 u = 1.0f - v - w;
return (V3f){v, w, u};
}
internal void get_image_coordinates(f32 norm_x, f32 norm_y, const Image *img,
u64 *x, u64 *y) {
*x = ndc_to_image_coordinate(norm_x, img->width);
*y = ndc_to_image_coordinate(0.0f - norm_y, img->height);
if (*x >= img->width) {
*x = img->width - 1;
}
if (*y >= img->height) {
*y = img->height - 1;
}
}
internal u64 ndc_to_image_coordinate(f32 value, u64 max) {
f32 result = (value + 1.0f) * max * 0.5f;
return clamp((u64)result, 0, max);
}