Browse Source

Correctly load OBJ files texture infos.

Godzil 3 years ago
parent
commit
cafb99f635
3 changed files with 36 additions and 4 deletions
  1. 1 1
      source/include/mesh.h
  2. 1 0
      source/include/triangle.h
  3. 34 3
      source/mesh.c

+ 1 - 1
source/include/mesh.h

@@ -20,7 +20,7 @@ typedef struct mesh_t
 {
     vec3_t *vertices;        /**< List of vertices */
     vec3_t *normalVertices;  /**< List of normal vertices */
-    vec2_t *textureVertices; /**< List of texture vertices */
+    tex2_t *textureVertices; /**< List of texture vertices */
     face_t *faces;           /**< List of faces */
 
     vec3_t rotation;         /**< Rotation of the object */

+ 1 - 0
source/include/triangle.h

@@ -22,6 +22,7 @@
 typedef struct face_t
 {
     int a, b, c;
+    int at, bt, ct;
 
     tex2_t a_uv, b_uv, c_uv;
 

+ 34 - 3
source/mesh.c

@@ -15,6 +15,7 @@
 #include <array.h>
 #include <display.h>
 #include <triangle.h>
+#include <texture.h>
 #include <mesh.h>
 
 #define N_CUBE_VERTICES (8)
@@ -109,6 +110,26 @@ static uint32_t objFileLoaderLoadedVerticesTexture = 0;
 static uint32_t objFileLoaderLoadedFaces = 0;
 static uint32_t objFileLoaderTotalTri = 0;
 
+static void updateFaceUVValues()
+{
+    uint32_t i;
+    uint32_t faceCount = arrayGetSize(mesh.faces);
+
+    if (arrayGetSize(mesh.textureVertices) > 0)
+    {
+        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;
+        }
+    }
+}
 
 void loadOBJFile(const char *filepath)
 {
@@ -149,6 +170,8 @@ void loadOBJFile(const char *filepath)
         Log(TLOG_VERBOSE, "OBJLoader", "Number of created triangles : %lu", objFileLoaderTotalTri);
 
         free(fileBuff);
+
+        updateFaceUVValues();
     }
     else
     {
@@ -327,7 +350,7 @@ int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine)
         }
         else
         {
-            vec2_t vertex = { atof(argv[1]), atof(argv[2]) };
+            tex2_t vertex = { atof(argv[1]), atof(argv[2]) };
             arrayAdd(mesh.textureVertices, vertex);
             objFileLoaderLoadedVerticesTexture++;
             ret = 0;
@@ -348,7 +371,12 @@ int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine)
             objFileLoaderTotalTri++;
             objFileLoaderLoadedFaces++;
 
-            face_t face = { v[1], v[2], v[3], .colour = MAKE_RGB(45, 170, 10) };
+            face_t face =
+            {
+                .a = v[1], .b = v[2], .c = v[3],
+                .at = vt[1], .bt = vt[2], .ct = vt[3],
+                .colour = MAKE_RGB(45, 170, 10)
+            };
             arrayAdd(mesh.faces, face);
             ret = 0;
         }
@@ -358,7 +386,10 @@ int objFileLoaderExecuteLine(int argc, char *argv[], uint32_t currentLine)
             /* This is not a triangle, so we need to fan it */
             for(i = 2; i < (argc - 1); i++)
             {
-                face_t face = { v[1], v[i], v[i + 1], .colour = MAKE_RGB(45, 170, 10) };
+                face_t face = {
+                    .a = v[1], .b = v[i], .c = v[i + 1],
+                    .at = vt[1], .bt = vt[i], .ct = vt[i + 1],
+                    .colour = MAKE_RGB(45, 170, 10) };
                 arrayAdd(mesh.faces, face);
 
                 objFileLoaderTotalTri++;