diff --git a/src/img.h b/src/img.h index 998fafc..85b4f2e 100644 --- a/src/img.h +++ b/src/img.h @@ -6,7 +6,7 @@ #include #define BUF_TYPE(TYPE, NAME) \ - typedef struct { \ + typedef struct NAME { \ u64 width; \ u64 height; \ TYPE *buf; \ diff --git a/src/main.c b/src/main.c index 9788bd8..f01c2b9 100644 --- a/src/main.c +++ b/src/main.c @@ -31,7 +31,8 @@ int main(void) { return TINY_EXIT_RENDER_INIT_FAILED; } - Model model = load_obj_file(arena, "resources/head.obj"); + Model model = + load_obj_file(arena, "resources/head_new.obj", "resources/head.pnm"); if (IS_NULL_MODEL(model)) { return TINY_EXIT_MODEL_LOAD_FAILED; } diff --git a/src/obj.c b/src/obj.c index f901f1f..52b016d 100644 --- a/src/obj.c +++ b/src/obj.c @@ -2,6 +2,7 @@ #include "aliases.h" #include "img.h" #include "mem_arena.h" +#include "pam.h" #include "typed_list.h" #include "utils.h" #include @@ -72,19 +73,20 @@ struct f32_3x3 { internal void render_triangle(const Triangle *triangle, const Model *model, Render *render, Colour colour, RenderType type); internal void fill_triangle(Render *render, Vertex vertices[TRIANGLE_VERTICES], - Colour colour); + TexCoord coordinates[TRIANGLE_VERTICES], + Colour colour, f32 intensity, Image *texture); internal TriangleBBox get_triangle_bbox(const Image *img, Vertex 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(const Vertex *vertex, const Image *img, +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}; -Model load_obj_file(Arena *arena, const char *filename) { +Model load_obj_file(Arena *arena, const char *filename, const char *texture) { if (!arena) { return NULL_MODEL; } @@ -96,21 +98,25 @@ Model load_obj_file(Arena *arena, const char *filename) { Model model = (Model){ .vertices = list_create(Vertex, arena), + .texture_coordinates = list_create(TexCoord, arena), .triangles = list_create(Triangle, arena), }; - if (!(model.vertices) || !(model.triangles)) { + if (!(model.vertices) || !(model.texture_coordinates) || !(model.triangles)) { return NULL_MODEL; } char line[8192]; char identifier[8]; Vertex vertex; + TexCoord coord; Triangle triangle; f32 vx, vy, vz; + f32 u, v; u64 fp0, fp1, fp2; - u64 ign_0_1, ign_0_2; - u64 ign_1_1, ign_1_2; - u64 ign_2_1, ign_2_2; + u64 tx0, tx1, tx2; + u64 ign_0_2; + u64 ign_1_2; + u64 ign_2_2; while (fgets(line, 8191, fp) != NULL) { sscanf(line, "%s", identifier); if (strncmp(identifier, "v", 8) == 0) { @@ -119,17 +125,29 @@ Model load_obj_file(Arena *arena, const char *filename) { vertex.y = vy; vertex.z = vz; list_append(Vertex, arena, model.vertices, vertex); + } else if (strncmp(identifier, "vt", 8) == 0) { + sscanf(line + 2, "%f %f", &u, &v); + coord.u = u; + coord.v = v; + list_append(TexCoord, 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, &ign_0_1, - &ign_0_2, &fp1, &ign_1_1, &ign_1_2, &fp2, &ign_2_1, &ign_2_2); + sscanf(line + 2, "%lu/%lu/%lu %lu/%lu/%lu %lu/%lu/%lu", &fp0, &tx0, + &ign_0_2, &fp1, &tx1, &ign_1_2, &fp2, &tx2, &ign_2_2); // OBJ indices start from 1 triangle.p0 = fp0 - 1; triangle.p1 = fp1 - 1; triangle.p2 = fp2 - 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; } @@ -174,6 +192,11 @@ internal void render_triangle(const Triangle *triangle, const Model *model, list_get(model->vertices, triangle->p1), list_get(model->vertices, triangle->p2), }; + TexCoord coordinates[TRIANGLE_VERTICES] = { + list_get(model->texture_coordinates, triangle->tx0), + list_get(model->texture_coordinates, triangle->tx1), + list_get(model->texture_coordinates, triangle->tx2), + }; if (type == RENDER_TYPE_WIREFRAME) { Vertex v0, v1; @@ -182,8 +205,8 @@ internal void render_triangle(const Triangle *triangle, const Model *model, v0 = vertices[i]; v1 = vertices[(i + 1) % TRIANGLE_VERTICES]; - get_image_coordinates(&v0, img, &x0, &y0); - get_image_coordinates(&v1, img, &x1, &y1); + 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); } @@ -200,28 +223,26 @@ internal void render_triangle(const Triangle *triangle, const Model *model, normalise_v3(normal); intensity = dot_v3(normal, g_light_dir); - - colour.r *= intensity; - colour.g *= intensity; - colour.b *= intensity; } if (intensity > 0.0f) { - fill_triangle(render, vertices, colour); + fill_triangle(render, vertices, coordinates, colour, intensity, + model->texture); } } } internal void fill_triangle(Render *render, Vertex vertices[TRIANGLE_VERTICES], - Colour colour) { + TexCoord coordinates[TRIANGLE_VERTICES], + Colour colour, f32 intensity, Image *texture) { Image *img = &(render->img); Depth *depth = &(render->depth); TriangleBBox bbox = get_triangle_bbox(img, vertices); u64 v0x, v0y, v1x, v1y, v2x, v2y; - get_image_coordinates(&(vertices[0]), img, &v0x, &v0y); - get_image_coordinates(&(vertices[1]), img, &v1x, &v1y); - get_image_coordinates(&(vertices[2]), img, &v2x, &v2y); + get_image_coordinates(vertices[0].x, vertices[0].y, img, &v0x, &v0y); + get_image_coordinates(vertices[1].x, vertices[1].y, img, &v1x, &v1y); + get_image_coordinates(vertices[2].x, vertices[2].y, img, &v2x, &v2y); V2i ab = V2(V2i, i64, v0x, v0y, v1x, v1y); V2i ac = V2(V2i, i64, v0x, v0y, v2x, v2y); @@ -233,6 +254,14 @@ internal void fill_triangle(Render *render, Vertex vertices[TRIANGLE_VERTICES], V3f coords; f32 z; f32 zbuf; + f32 tx_u, tx_v; + u64 tx_x, tx_y; + + if (!texture) { + colour.r *= intensity; + colour.g *= intensity; + colour.b *= intensity; + } for (u64 y = bbox.y0; y < bbox.y1; ++y) { for (u64 x = bbox.x0; x < bbox.x1; ++x) { @@ -248,6 +277,20 @@ internal void fill_triangle(Render *render, Vertex vertices[TRIANGLE_VERTICES], zbuf = get_pixel(f32, &(render->depth), x, y); if (z > zbuf) { + 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); } @@ -264,12 +307,11 @@ internal TriangleBBox get_triangle_bbox(const Image *img, 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)); - return (TriangleBBox){ - .x0 = ndc_to_image_coordinate(x0, img->width), - .y0 = ndc_to_image_coordinate(0.0f - y0, img->height), - .x1 = ndc_to_image_coordinate(x1, img->width), - .y1 = ndc_to_image_coordinate(0.0f - y1, img->height), - }; + 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, @@ -285,10 +327,10 @@ internal V3f get_barycentric_coords(f32 d00, f32 d01, f32 d11, f32 denom, return (V3f){v, w, u}; } -internal void get_image_coordinates(const Vertex *vertex, const Image *img, +internal void get_image_coordinates(f32 norm_x, f32 norm_y, const Image *img, u64 *x, u64 *y) { - *x = ndc_to_image_coordinate(vertex->x, img->width); - *y = ndc_to_image_coordinate(0.0f - vertex->y, img->height); + *x = ndc_to_image_coordinate(norm_x, img->width); + *y = ndc_to_image_coordinate(0.0f - norm_y, img->height); } internal u64 ndc_to_image_coordinate(f32 value, u64 max) { diff --git a/src/obj.h b/src/obj.h index 65e824b..a66cb7c 100644 --- a/src/obj.h +++ b/src/obj.h @@ -16,11 +16,20 @@ struct vertex { f32 z; }; +typedef struct texcoord TexCoord; +struct texcoord { + f32 u; + f32 v; +}; + typedef struct triangle Triangle; struct triangle { u64 p0; u64 p1; u64 p2; + u64 tx0; + u64 tx1; + u64 tx2; }; typedef enum { @@ -39,12 +48,15 @@ typedef enum { } ColourType; MAKE_LIST_TYPE(Vertex); +MAKE_LIST_TYPE(TexCoord); MAKE_LIST_TYPE(Triangle); typedef struct model Model; struct model { LIST_TYPE(Vertex) * vertices; + LIST_TYPE(TexCoord) * texture_coordinates; LIST_TYPE(Triangle) * triangles; + Image *texture; }; typedef struct render Render; @@ -53,7 +65,7 @@ struct render { Depth depth; }; -Model load_obj_file(Arena *arena, const char *filename); +Model load_obj_file(Arena *arena, const char *filename, const char *texture); bool init_render(Arena *arena, Render *render, u64 width, u64 height); void render_model(const Model *model, Render *render, Colour colour, RenderType type, ColourType colour_type);