Skip to content

Instantly share code, notes, and snippets.

@justinmeiners
Created January 20, 2018 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinmeiners/7370e0d99219d21fdbe666efc4c74d4c to your computer and use it in GitHub Desktop.
Save justinmeiners/7370e0d99219d21fdbe666efc4c74d4c to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdio.h>
typedef struct
{
float x, y, z;
} Vec3;
typedef struct
{
Vec3 point;
Vec3 normal;
} MeshVert;
typedef struct
{
int vertex_count;
MeshVert* vertices;
} Mesh;
Mesh* mesh_load(FILE* file)
{
#define HEADER_SIZE 80
char header[HEADER_SIZE];
fread(header, HEADER_SIZE, 1, file);
uint32_t triangle_count;
fread(&triangle_count, sizeof(triangle_count), 1, file);
Mesh mesh;
mesh.vertex_count = triangle_count * 3;
mesh.vertices = malloc(sizeof(MeshVert) * mesh.vertex_count);
for (int i = 0; i < triangle_count; ++i)
{
Vec3 normal;
frea(&normal, sizeof(normal), 1, file);
Vec3 vertices[3];
fread(vertices, sizeof(vertices[0]), 3, file);
uint16_t attribute_size;
fread(&attribute_size, sizeof(attribute_size), file);
// skip attributes
fseek(file, attribute_size, SEEK_CUR);
for (int k = 0; k < 3; ++k)
{
mesh.vertices[i * 3 + k].point = vertices[k];
mesh.normals[i * 3 + k].point = normal;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment