|
@@ -19,9 +19,7 @@
|
|
#include <mesh.h>
|
|
#include <mesh.h>
|
|
|
|
|
|
#define N_CUBE_VERTICES (8)
|
|
#define N_CUBE_VERTICES (8)
|
|
-extern vec3_t cubeVertices[N_CUBE_VERTICES];
|
|
|
|
#define N_CUBE_FACES (6 * 2)
|
|
#define N_CUBE_FACES (6 * 2)
|
|
-extern face_t cubeFaces[N_CUBE_FACES];
|
|
|
|
|
|
|
|
vec3_t cubeVertices[N_CUBE_VERTICES] =
|
|
vec3_t cubeVertices[N_CUBE_VERTICES] =
|
|
{
|
|
{
|
|
@@ -57,41 +55,32 @@ face_t cubeFaces[N_CUBE_FACES] =
|
|
{ .a = 6, .b = 1, .c = 4, .a_uv = { 0, 1 }, .b_uv = { 1, 0 }, .c_uv = { 1, 1 }, .colour = 0xFFFFFFFF }
|
|
{ .a = 6, .b = 1, .c = 4, .a_uv = { 0, 1 }, .b_uv = { 1, 0 }, .c_uv = { 1, 1 }, .colour = 0xFFFFFFFF }
|
|
};
|
|
};
|
|
|
|
|
|
-mesh_t mesh =
|
|
|
|
-{
|
|
|
|
- .vertices = NULL,
|
|
|
|
- .faces = NULL,
|
|
|
|
- .rotation = { 0, 0, 0 },
|
|
|
|
- .scale = { 1, 1, 1 },
|
|
|
|
- .translation = { 0, 0, 0 },
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-void loadCubeMeshData()
|
|
|
|
|
|
+void loadCubeMeshData(mesh_t *mesh)
|
|
{
|
|
{
|
|
int i;
|
|
int i;
|
|
|
|
|
|
- meshFree();
|
|
|
|
|
|
+ meshFree(mesh);
|
|
|
|
|
|
for(i = 0; i < N_CUBE_VERTICES; i++)
|
|
for(i = 0; i < N_CUBE_VERTICES; i++)
|
|
{
|
|
{
|
|
- arrayAdd(mesh.vertices, cubeVertices[i]);
|
|
|
|
|
|
+ arrayAdd(mesh->vertices, cubeVertices[i]);
|
|
}
|
|
}
|
|
for(i = 0; i < N_CUBE_FACES; i++)
|
|
for(i = 0; i < N_CUBE_FACES; i++)
|
|
{
|
|
{
|
|
- arrayAdd(mesh.faces, cubeFaces[i]);
|
|
|
|
|
|
+ arrayAdd(mesh->faces, cubeFaces[i]);
|
|
}
|
|
}
|
|
|
|
|
|
- mesh.rotation.x = 0;
|
|
|
|
- mesh.rotation.y = 0;
|
|
|
|
- mesh.rotation.z = 0;
|
|
|
|
|
|
+ mesh->rotation.x = 0;
|
|
|
|
+ mesh->rotation.y = 0;
|
|
|
|
+ mesh->rotation.z = 0;
|
|
}
|
|
}
|
|
|
|
|
|
-void meshFree()
|
|
|
|
|
|
+void meshFree(mesh_t *mesh)
|
|
{
|
|
{
|
|
- arrayEmpty(mesh.normalVertices);
|
|
|
|
- arrayEmpty(mesh.textureVertices);
|
|
|
|
- arrayFree(mesh.faces);
|
|
|
|
- arrayFree(mesh.vertices);
|
|
|
|
|
|
+ arrayEmpty(mesh->normalVertices);
|
|
|
|
+ arrayEmpty(mesh->textureVertices);
|
|
|
|
+ arrayFree(mesh->faces);
|
|
|
|
+ arrayFree(mesh->vertices);
|
|
}
|
|
}
|
|
|
|
|
|
/***********************************************************************************************************************
|
|
/***********************************************************************************************************************
|
|
@@ -99,10 +88,10 @@ void meshFree()
|
|
**********************************************************************************************************************/
|
|
**********************************************************************************************************************/
|
|
|
|
|
|
/* Function used to load OBJ files */
|
|
/* Function used to load OBJ files */
|
|
-static int objFileLoaderParser(const char *content);
|
|
|
|
-static void objFileLoaderLineParser(char *line, uint32_t currentLine);
|
|
|
|
-static int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine);
|
|
|
|
-static void updateFaceUVValues();
|
|
|
|
|
|
+static int objFileLoaderParser(mesh_t *mesh, const char *content);
|
|
|
|
+static void objFileLoaderLineParser(mesh_t *mesh, char *line, uint32_t currentLine);
|
|
|
|
+static int objFileLoaderExecuteLine(mesh_t *mesh, int argc, char *argv[], uint32_t currentLine);
|
|
|
|
+static void updateFaceUVValues(mesh_t *mesh);
|
|
|
|
|
|
static uint32_t objFileLoaderIgnoredLines = 0;
|
|
static uint32_t objFileLoaderIgnoredLines = 0;
|
|
static uint32_t objFileLoaderLoadedVertices = 0;
|
|
static uint32_t objFileLoaderLoadedVertices = 0;
|
|
@@ -111,28 +100,28 @@ static uint32_t objFileLoaderLoadedVerticesTexture = 0;
|
|
static uint32_t objFileLoaderLoadedFaces = 0;
|
|
static uint32_t objFileLoaderLoadedFaces = 0;
|
|
static uint32_t objFileLoaderTotalTri = 0;
|
|
static uint32_t objFileLoaderTotalTri = 0;
|
|
|
|
|
|
-static void updateFaceUVValues()
|
|
|
|
|
|
+static void updateFaceUVValues(mesh_t *mesh)
|
|
{
|
|
{
|
|
uint32_t i;
|
|
uint32_t i;
|
|
- uint32_t faceCount = arrayGetSize(mesh.faces);
|
|
|
|
|
|
+ uint32_t faceCount = arrayGetSize(mesh->faces);
|
|
|
|
|
|
- if (arrayGetSize(mesh.textureVertices) > 0)
|
|
|
|
|
|
+ if (arrayGetSize(mesh->textureVertices) > 0)
|
|
{
|
|
{
|
|
for (i = 0 ; i < faceCount ; i++)
|
|
for (i = 0 ; i < faceCount ; i++)
|
|
{
|
|
{
|
|
- face_t *face = &mesh.faces[i];
|
|
|
|
-
|
|
|
|
- face->a_uv.u = mesh.textureVertices[face->at].u;
|
|
|
|
- face->a_uv.v = mesh.textureVertices[face->at].v;
|
|
|
|
- face->b_uv.u = mesh.textureVertices[face->bt].u;
|
|
|
|
- face->b_uv.v = mesh.textureVertices[face->bt].v;
|
|
|
|
- face->c_uv.u = mesh.textureVertices[face->ct].u;
|
|
|
|
- face->c_uv.v = mesh.textureVertices[face->ct].v;
|
|
|
|
|
|
+ face_t *face = &mesh->faces[i];
|
|
|
|
+
|
|
|
|
+ face->a_uv.u = mesh->textureVertices[face->at].u;
|
|
|
|
+ face->a_uv.v = mesh->textureVertices[face->at].v;
|
|
|
|
+ face->b_uv.u = mesh->textureVertices[face->bt].u;
|
|
|
|
+ face->b_uv.v = mesh->textureVertices[face->bt].v;
|
|
|
|
+ face->c_uv.u = mesh->textureVertices[face->ct].u;
|
|
|
|
+ face->c_uv.v = mesh->textureVertices[face->ct].v;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-void loadOBJFile(const char *filepath)
|
|
|
|
|
|
+void loadOBJFile(mesh_t *mesh, const char *filepath)
|
|
{
|
|
{
|
|
FILE *fp;
|
|
FILE *fp;
|
|
size_t fileSize;
|
|
size_t fileSize;
|
|
@@ -155,9 +144,14 @@ void loadOBJFile(const char *filepath)
|
|
objFileLoaderLoadedFaces = 0;
|
|
objFileLoaderLoadedFaces = 0;
|
|
objFileLoaderTotalTri = 0;
|
|
objFileLoaderTotalTri = 0;
|
|
|
|
|
|
- meshFree();
|
|
|
|
|
|
+ meshFree(mesh);
|
|
|
|
+
|
|
|
|
+ /* Set some default values */
|
|
|
|
+ mesh->rotation = vec3(0, 0, 0 );
|
|
|
|
+ mesh->scale = vec3(1, 1, 1 );
|
|
|
|
+ mesh->translation = vec3(0, 0, 0 );
|
|
|
|
|
|
- if (objFileLoaderParser(fileBuff))
|
|
|
|
|
|
+ if (objFileLoaderParser(mesh, fileBuff))
|
|
{
|
|
{
|
|
Log(TLOG_ERROR, "OBJLoader", "Errors occurred while opening the file '%s'.", filepath);
|
|
Log(TLOG_ERROR, "OBJLoader", "Errors occurred while opening the file '%s'.", filepath);
|
|
}
|
|
}
|
|
@@ -172,7 +166,7 @@ void loadOBJFile(const char *filepath)
|
|
|
|
|
|
free(fileBuff);
|
|
free(fileBuff);
|
|
|
|
|
|
- updateFaceUVValues();
|
|
|
|
|
|
+ updateFaceUVValues(mesh);
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
@@ -182,7 +176,7 @@ void loadOBJFile(const char *filepath)
|
|
|
|
|
|
#define MAX_LINE_LENGTH (512)
|
|
#define MAX_LINE_LENGTH (512)
|
|
/* Here start the fun! */
|
|
/* Here start the fun! */
|
|
-static int objFileLoaderParser(const char *content)
|
|
|
|
|
|
+static int objFileLoaderParser(mesh_t *mesh, const char *content)
|
|
{
|
|
{
|
|
/* I don't think we will handle lines of more than 512 characters... */
|
|
/* I don't think we will handle lines of more than 512 characters... */
|
|
char lineBuff[MAX_LINE_LENGTH];
|
|
char lineBuff[MAX_LINE_LENGTH];
|
|
@@ -212,7 +206,7 @@ static int objFileLoaderParser(const char *content)
|
|
memset(lineBuff, 0, MAX_LINE_LENGTH);
|
|
memset(lineBuff, 0, MAX_LINE_LENGTH);
|
|
strncpy(lineBuff, bufferPos, lineLength);
|
|
strncpy(lineBuff, bufferPos, lineLength);
|
|
|
|
|
|
- objFileLoaderLineParser(lineBuff, currentLineNum);
|
|
|
|
|
|
+ objFileLoaderLineParser(mesh, lineBuff, currentLineNum);
|
|
|
|
|
|
bufferPos += lineLength + 1;
|
|
bufferPos += lineLength + 1;
|
|
|
|
|
|
@@ -228,7 +222,7 @@ static int objFileLoaderParser(const char *content)
|
|
|
|
|
|
#define MAX_ARGS (15)
|
|
#define MAX_ARGS (15)
|
|
/* Parse the line into a couple ofr argc/argv using space as argument separator */
|
|
/* Parse the line into a couple ofr argc/argv using space as argument separator */
|
|
-static void objFileLoaderLineParser(char *line, uint32_t currentLine)
|
|
|
|
|
|
+static void objFileLoaderLineParser(mesh_t *mesh, char *line, uint32_t currentLine)
|
|
{
|
|
{
|
|
char *argv[MAX_ARGS];
|
|
char *argv[MAX_ARGS];
|
|
uint32_t argc = 0;
|
|
uint32_t argc = 0;
|
|
@@ -260,13 +254,13 @@ static void objFileLoaderLineParser(char *line, uint32_t currentLine)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- if (objFileLoaderExecuteLine(argc, argv, currentLine))
|
|
|
|
|
|
+ if (objFileLoaderExecuteLine(mesh, argc, argv, currentLine))
|
|
{
|
|
{
|
|
objFileLoaderIgnoredLines++;
|
|
objFileLoaderIgnoredLines++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-static int objFileLoaderParseFaceVertex(char *buf, uint32_t *v, uint32_t *vt, uint32_t *vn)
|
|
|
|
|
|
+static int objFileLoaderParseFaceVertex(mesh_t *mesh, char *buf, uint32_t *v, uint32_t *vt, uint32_t *vn)
|
|
{
|
|
{
|
|
uint32_t bufPos = 0;
|
|
uint32_t bufPos = 0;
|
|
uint32_t lineLength = strlen(buf);
|
|
uint32_t lineLength = strlen(buf);
|
|
@@ -309,7 +303,7 @@ static int objFileLoaderParseFaceVertex(char *buf, uint32_t *v, uint32_t *vt, u
|
|
}
|
|
}
|
|
|
|
|
|
/* Actually execute the line */
|
|
/* Actually execute the line */
|
|
-int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine)
|
|
|
|
|
|
+int objFileLoaderExecuteLine(mesh_t *mesh, int argc, char *argv[], uint32_t currentLine)
|
|
{
|
|
{
|
|
int ret = 1;
|
|
int ret = 1;
|
|
if (strncmp(argv[0], "v", 2) == 0)
|
|
if (strncmp(argv[0], "v", 2) == 0)
|
|
@@ -322,7 +316,7 @@ int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine)
|
|
else
|
|
else
|
|
{
|
|
{
|
|
vec3_t vertex = { atof(argv[1]), atof(argv[2]), atof(argv[3]) };
|
|
vec3_t vertex = { atof(argv[1]), atof(argv[2]), atof(argv[3]) };
|
|
- arrayAdd(mesh.vertices, vertex);
|
|
|
|
|
|
+ arrayAdd(mesh->vertices, vertex);
|
|
objFileLoaderLoadedVertices++;
|
|
objFileLoaderLoadedVertices++;
|
|
ret = 0;
|
|
ret = 0;
|
|
}
|
|
}
|
|
@@ -337,7 +331,7 @@ int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine)
|
|
else
|
|
else
|
|
{
|
|
{
|
|
vec3_t vertex = { atof(argv[1]), atof(argv[2]), atof(argv[3]) };
|
|
vec3_t vertex = { atof(argv[1]), atof(argv[2]), atof(argv[3]) };
|
|
- arrayAdd(mesh.normalVertices, vertex);
|
|
|
|
|
|
+ arrayAdd(mesh->normalVertices, vertex);
|
|
objFileLoaderLoadedVerticesNormal++;
|
|
objFileLoaderLoadedVerticesNormal++;
|
|
ret = 0;
|
|
ret = 0;
|
|
}
|
|
}
|
|
@@ -352,7 +346,7 @@ int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine)
|
|
else
|
|
else
|
|
{
|
|
{
|
|
tex2_t vertex = { atof(argv[1]), atof(argv[2]) };
|
|
tex2_t vertex = { atof(argv[1]), atof(argv[2]) };
|
|
- arrayAdd(mesh.textureVertices, vertex);
|
|
|
|
|
|
+ arrayAdd(mesh->textureVertices, vertex);
|
|
objFileLoaderLoadedVerticesTexture++;
|
|
objFileLoaderLoadedVerticesTexture++;
|
|
ret = 0;
|
|
ret = 0;
|
|
}
|
|
}
|
|
@@ -364,7 +358,7 @@ int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine)
|
|
uint32_t v[MAX_ARGS], vt[MAX_ARGS], vn[MAX_ARGS];
|
|
uint32_t v[MAX_ARGS], vt[MAX_ARGS], vn[MAX_ARGS];
|
|
for(i = 1; i < argc; i++)
|
|
for(i = 1; i < argc; i++)
|
|
{
|
|
{
|
|
- objFileLoaderParseFaceVertex(argv[i], &v[i], &vt[i], &vn[i]);
|
|
|
|
|
|
+ objFileLoaderParseFaceVertex(mesh, argv[i], &v[i], &vt[i], &vn[i]);
|
|
}
|
|
}
|
|
|
|
|
|
if (argc == 4)
|
|
if (argc == 4)
|
|
@@ -378,7 +372,7 @@ int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine)
|
|
.at = vt[1] - 1 , .bt = vt[2] - 1, .ct = vt[3] - 1,
|
|
.at = vt[1] - 1 , .bt = vt[2] - 1, .ct = vt[3] - 1,
|
|
.colour = MAKE_RGB(45, 170, 10)
|
|
.colour = MAKE_RGB(45, 170, 10)
|
|
};
|
|
};
|
|
- arrayAdd(mesh.faces, face);
|
|
|
|
|
|
+ arrayAdd(mesh->faces, face);
|
|
ret = 0;
|
|
ret = 0;
|
|
}
|
|
}
|
|
else if (argc > 4)
|
|
else if (argc > 4)
|
|
@@ -391,7 +385,7 @@ int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine)
|
|
.a = v[1] - 1, .b = v[i] - 1, .c = v[i + 1] - 1,
|
|
.a = v[1] - 1, .b = v[i] - 1, .c = v[i + 1] - 1,
|
|
.at = vt[1] - 1, .bt = vt[i] - 1, .ct = vt[i + 1] - 1,
|
|
.at = vt[1] - 1, .bt = vt[i] - 1, .ct = vt[i + 1] - 1,
|
|
.colour = MAKE_RGB(45, 170, 10) };
|
|
.colour = MAKE_RGB(45, 170, 10) };
|
|
- arrayAdd(mesh.faces, face);
|
|
|
|
|
|
+ arrayAdd(mesh->faces, face);
|
|
|
|
|
|
objFileLoaderTotalTri++;
|
|
objFileLoaderTotalTri++;
|
|
}
|
|
}
|