Load vertex normals indices

This commit is contained in:
Abdelrahman Said 2024-08-11 19:39:31 +01:00
parent e01c1397bc
commit d323c0ae14
2 changed files with 14 additions and 5 deletions

View File

@ -115,10 +115,8 @@ Model load_obj_file(Arena *arena, const char *filename, const char *texture) {
f32 nx, ny, nz; f32 nx, ny, nz;
f32 u, v; f32 u, v;
u64 fp0, fp1, fp2; u64 fp0, fp1, fp2;
u64 vn0, vn1, vn2;
u64 tx0, tx1, tx2; u64 tx0, tx1, tx2;
u64 ign_0_2;
u64 ign_1_2;
u64 ign_2_2;
while (fgets(line, 8191, fp) != NULL) { while (fgets(line, 8191, fp) != NULL) {
sscanf(line, "%s", identifier); sscanf(line, "%s", identifier);
if (strncmp(identifier, "v", 8) == 0) { if (strncmp(identifier, "v", 8) == 0) {
@ -139,12 +137,15 @@ Model load_obj_file(Arena *arena, const char *filename, const char *texture) {
coord.v = v; coord.v = v;
list_append(V2f, arena, model.texture_coordinates, coord); list_append(V2f, arena, model.texture_coordinates, coord);
} else if (strncmp(identifier, "f", 8) == 0) { } else if (strncmp(identifier, "f", 8) == 0) {
sscanf(line + 2, "%lu/%lu/%lu %lu/%lu/%lu %lu/%lu/%lu", &fp0, &tx0, sscanf(line + 2, "%lu/%lu/%lu %lu/%lu/%lu %lu/%lu/%lu", &fp0, &tx0, &vn0,
&ign_0_2, &fp1, &tx1, &ign_1_2, &fp2, &tx2, &ign_2_2); &fp1, &tx1, &vn1, &fp2, &tx2, &vn2);
// OBJ indices start from 1 // OBJ indices start from 1
triangle.p0 = fp0 - 1; triangle.p0 = fp0 - 1;
triangle.p1 = fp1 - 1; triangle.p1 = fp1 - 1;
triangle.p2 = fp2 - 1; triangle.p2 = fp2 - 1;
triangle.n0 = vn0 - 1;
triangle.n1 = vn1 - 1;
triangle.n2 = vn2 - 1;
triangle.tx0 = tx0 - 1; triangle.tx0 = tx0 - 1;
triangle.tx1 = tx1 - 1; triangle.tx1 = tx1 - 1;
triangle.tx2 = tx2 - 1; triangle.tx2 = tx2 - 1;
@ -202,6 +203,11 @@ internal void render_triangle(const Triangle *triangle, const Model *model,
list_get(model->vertices, triangle->p1), list_get(model->vertices, triangle->p1),
list_get(model->vertices, triangle->p2), 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] = { V2f coordinates[TRIANGLE_VERTICES] = {
list_get(model->texture_coordinates, triangle->tx0), list_get(model->texture_coordinates, triangle->tx0),
list_get(model->texture_coordinates, triangle->tx1), list_get(model->texture_coordinates, triangle->tx1),

View File

@ -14,6 +14,9 @@ struct triangle {
u64 p0; u64 p0;
u64 p1; u64 p1;
u64 p2; u64 p2;
u64 n0;
u64 n1;
u64 n2;
u64 tx0; u64 tx0;
u64 tx1; u64 tx1;
u64 tx2; u64 tx2;