Compare commits

..

No commits in common. "1ab8c964bf5db79caa946edbee56213a1afe43d5" and "f024d51d853bcbe941c0e71e91c751d420a32a05" have entirely different histories.

6 changed files with 32 additions and 147 deletions

View File

@ -6,7 +6,7 @@
#include <stdbool.h>
#define BUF_TYPE(TYPE, NAME) \
typedef struct NAME { \
typedef struct { \
u64 width; \
u64 height; \
TYPE *buf; \

View File

@ -31,8 +31,7 @@ int main(void) {
return TINY_EXIT_RENDER_INIT_FAILED;
}
Model model =
load_obj_file(arena, "resources/head_new.obj", "resources/head.pnm");
Model model = load_obj_file(arena, "resources/head.obj");
if (IS_NULL_MODEL(model)) {
return TINY_EXIT_MODEL_LOAD_FAILED;
}

100
src/obj.c
View File

@ -2,7 +2,6 @@
#include "aliases.h"
#include "img.h"
#include "mem_arena.h"
#include "pam.h"
#include "typed_list.h"
#include "utils.h"
#include <limits.h>
@ -73,20 +72,19 @@ 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],
TexCoord coordinates[TRIANGLE_VERTICES],
Colour colour, f32 intensity, Image *texture);
Colour colour);
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(f32 norm_x, f32 norm_y, const Image *img,
internal void get_image_coordinates(const Vertex *vertex, 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, const char *texture) {
Model load_obj_file(Arena *arena, const char *filename) {
if (!arena) {
return NULL_MODEL;
}
@ -98,25 +96,21 @@ Model load_obj_file(Arena *arena, const char *filename, const char *texture) {
Model model = (Model){
.vertices = list_create(Vertex, arena),
.texture_coordinates = list_create(TexCoord, arena),
.triangles = list_create(Triangle, arena),
};
if (!(model.vertices) || !(model.texture_coordinates) || !(model.triangles)) {
if (!(model.vertices) || !(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 tx0, tx1, tx2;
u64 ign_0_2;
u64 ign_1_2;
u64 ign_2_2;
u64 ign_0_1, ign_0_2;
u64 ign_1_1, ign_1_2;
u64 ign_2_1, ign_2_2;
while (fgets(line, 8191, fp) != NULL) {
sscanf(line, "%s", identifier);
if (strncmp(identifier, "v", 8) == 0) {
@ -125,29 +119,17 @@ Model load_obj_file(Arena *arena, const char *filename, const char *texture) {
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, &tx0,
&ign_0_2, &fp1, &tx1, &ign_1_2, &fp2, &tx2, &ign_2_2);
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);
// 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;
}
@ -192,11 +174,6 @@ 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;
@ -205,8 +182,8 @@ internal void render_triangle(const Triangle *triangle, const Model *model,
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);
get_image_coordinates(&v0, img, &x0, &y0);
get_image_coordinates(&v1, img, &x1, &y1);
draw_line(img, x0, y0, x1, y1, colour);
}
@ -223,26 +200,28 @@ 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, coordinates, colour, intensity,
model->texture);
fill_triangle(render, vertices, colour);
}
}
}
internal void fill_triangle(Render *render, Vertex vertices[TRIANGLE_VERTICES],
TexCoord coordinates[TRIANGLE_VERTICES],
Colour colour, f32 intensity, Image *texture) {
Colour colour) {
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].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);
get_image_coordinates(&(vertices[0]), img, &v0x, &v0y);
get_image_coordinates(&(vertices[1]), img, &v1x, &v1y);
get_image_coordinates(&(vertices[2]), img, &v2x, &v2y);
V2i ab = V2(V2i, i64, v0x, v0y, v1x, v1y);
V2i ac = V2(V2i, i64, v0x, v0y, v2x, v2y);
@ -254,14 +233,6 @@ 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) {
@ -277,20 +248,6 @@ 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);
}
@ -307,11 +264,12 @@ 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));
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;
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),
};
}
internal V3f get_barycentric_coords(f32 d00, f32 d01, f32 d11, f32 denom,
@ -327,10 +285,10 @@ internal V3f get_barycentric_coords(f32 d00, f32 d01, f32 d11, f32 denom,
return (V3f){v, w, u};
}
internal void get_image_coordinates(f32 norm_x, f32 norm_y, const Image *img,
internal void get_image_coordinates(const Vertex *vertex, 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);
*x = ndc_to_image_coordinate(vertex->x, img->width);
*y = ndc_to_image_coordinate(0.0f - vertex->y, img->height);
}
internal u64 ndc_to_image_coordinate(f32 value, u64 max) {

View File

@ -16,20 +16,11 @@ 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 {
@ -48,15 +39,12 @@ 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;
@ -65,7 +53,7 @@ struct render {
Depth depth;
};
Model load_obj_file(Arena *arena, const char *filename, const char *texture);
Model load_obj_file(Arena *arena, const char *filename);
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);

View File

@ -1,8 +1,4 @@
#include "pam.h"
#include "img.h"
#include "mem_arena.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
@ -32,57 +28,3 @@ void write_p7_image(u64 width, u64 height, u8 *buf, const char *outfile) {
fclose(fp);
}
Image *load_p6_image(Arena *arena, const char *filename) {
Image *output = NULL;
if (!arena || !filename) {
goto RETURN_VALUE;
}
FILE *fp = fopen(filename, "rb");
if (!fp) {
goto RETURN_VALUE;
}
char type[4] = {0};
fread(type, 3, 1, fp);
if (strncmp(type, "P6\n", 4) != 0) {
goto CLOSE_FILE;
}
u64 w, h;
fscanf(fp, "%lu %lu\n", &w, &h);
u64 max;
fscanf(fp, "%lu\n", &max);
if (max > UINT8_MAX) {
goto CLOSE_FILE;
}
output = wapp_mem_arena_alloc(arena, sizeof(Image));
if (!output) {
goto CLOSE_FILE;
}
output->width = w;
output->height = h;
if (!init_buffer(arena, output)) {
goto CLOSE_FILE;
}
Colour colour = {0};
for (u64 y = 0; y < h; ++y) {
for (u64 x = 0; x < w; ++x) {
fread(&colour, 3, 1, fp);
colour.a = 255;
set_pixel(output, x, y, &colour);
}
}
CLOSE_FILE:
fclose(fp);
RETURN_VALUE:
return output;
}

View File

@ -2,9 +2,7 @@
#define PAM_H
#include "aliases.h"
#include "img.h"
void write_p7_image(u64 width, u64 height, u8 *buf, const char *outfile);
Image *load_p6_image(Arena *arena, const char *filename);
#endif // PAM_H