1
0
Fork 0

Add OBJ file loading

This commit is contained in:
Austin Morlan 2018-09-04 19:50:27 -07:00
parent b024d821f5
commit c82f690943
Signed by: austin
GPG Key ID: FD6B27654AF5E348
6 changed files with 165 additions and 3 deletions

View File

@ -31,15 +31,15 @@ CC=clang++
WARNINGS_ON=-Weverything
WARNINGS_OFF=-Wno-missing-braces -Wno-gnu-anonymous-struct -Wno-old-style-cast\
-Wno-zero-as-null-pointer-constant -Wno-nested-anon-types\
-Wno-padded
-Wno-padded -Wno-exit-time-destructors -Wno-global-constructors
CFLAGS=$(D) $(O) -std=c++11 $(WARNINGS_ON) $(WARNINGS_OFF) -I$(INCLUDE_DIR)
LIBS=-lSDL2
_HEADERS = engine.h platform.h util.h
_HEADERS = engine.h loader.h geometry.h platform.h point.h util.h
HEADERS = $(patsubst %,$(INCLUDE_DIR)/%,$(_HEADERS))
_OBJS = engine.o main.o platform.o
_OBJS = engine.o loader.o main.o platform.o
OBJS = $(patsubst %,$(BUILD_DIR)/%,$(_OBJS))
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp $(HEADERS)

21
include/geometry.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef GEOMETRY_H
#include "point.h"
#include <vector>
struct Face
{
unsigned int vertIndex[3];
};
struct Mesh
{
std::vector<Point> verts;
std::vector<Face> faces;
};
#define GEOMETRY_H
#endif

11
include/loader.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef LOADER_H
#include "geometry.h"
int LoadMesh(char *filename, Mesh &mesh);
#define LOADER_H
#endif

24
include/point.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef POINT_H
// STRUCTURE
struct Point
{
inline Point(void) : x(0), y(0), z(0), w(1) {}
inline Point(float x, float y, float z) : x(x), y(y), z(z), w(1) {}
union
{
float e[4];
struct
{
float x, y, z, w;
};
};
};
#define POINT_H
#endif

View File

@ -1,7 +1,20 @@
#include "engine.h"
#include "loader.h"
// GLOBALS
static Mesh mesh;
int Engine_Init(char *filename)
{
int result = LoadMesh(filename, mesh);
if (result < 0)
{
return -1;
}
return 0;
}

93
src/loader.cpp Normal file
View File

@ -0,0 +1,93 @@
#include "loader.h"
#include <cctype>
#include <cstdio>
#include <cstring>
// STATIC PROTOTYPES
static char *GetLine(char *buffer, int maxLength, FILE *fp);
// PUBLIC FUNCTIONS
int LoadMesh(char *filename, Mesh &mesh)
{
FILE *fp;
char buffer[256];
char *token;
char garbage[5];
fp = fopen(filename, "r");
if (fp == NULL)
{
fprintf(stderr, "Error loading file: %s\n", filename);
return -1;
}
token = GetLine(buffer, sizeof(buffer), fp);
while (token != NULL)
{
if (token[0] == 'v')
{
Point v;
sscanf(
token, "%s %f %f %f",
garbage,
&v.x,
&v.y,
&v.z);
mesh.verts.push_back(v);
}
else if (token[0] == 'f')
{
Face f;
sscanf(token, "%s %d %d %d",
garbage,
&f.vertIndex[0],
&f.vertIndex[1],
&f.vertIndex[2]);
// Convert to 0-indexed
f.vertIndex[0] -= 1;
f.vertIndex[1] -= 1;
f.vertIndex[2] -= 1;
mesh.faces.push_back(f);
}
token = GetLine(buffer, sizeof(buffer), fp);
}
printf("OBJ: %s\n", filename);
printf("Verts: %lu\n", mesh.verts.size());
printf("Faces: %lu\n", mesh.faces.size());
fclose(fp);
return 0;
}
static char *GetLine(char *buffer, int maxLength, FILE *fp)
{
while(true)
{
if (!fgets(buffer, maxLength, fp))
{
return NULL;
}
// for (length = strlen(buffer), index = 0; isspace(buffer[index]); ++index);
if (buffer[0] != 'v' && buffer[0] != 'f')
{
continue;
}
return buffer;
}
}