changeset 14:62be2036f604

Read mesh vertices/faces information from "<modelfilename>.info" instead of having the values hardcoded.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 29 Oct 2019 12:48:36 +0200
parents c1e8057cc4d0
children 2d2aadfa3df3
files dragon.info glxdragon.cpp
diffstat 2 files changed, 83 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dragon.info	Tue Oct 29 12:48:36 2019 +0200
@@ -0,0 +1,6 @@
+
+
+
+v:100139
+
+f:200198
--- a/glxdragon.cpp	Mon Oct 28 19:13:27 2019 +0200
+++ b/glxdragon.cpp	Tue Oct 29 12:48:36 2019 +0200
@@ -290,8 +290,83 @@
 }
 
 
-bool dmLoadMesh(const std::string &filename, Mesh &mesh, int nvertices, int nfaces)
+bool dmLoadMesh(const std::string &filename, const std::string &infofilename, Mesh &mesh)
 {
+    std::ifstream info(infofilename.c_str());
+    int required = 0, nline = 0;
+
+    printf("INFO: Trying to read mesh meta from '%s'.\n",
+        infofilename.c_str());
+
+    if (!info.is_open())
+    {
+        printf("ERROR: Unable to open file '%s'.\n",
+            infofilename.c_str());
+        return false;
+    }
+
+    while (required != 0x03)
+    {
+        std::string tmp;
+        int value;
+        char key;
+
+        // Read one line
+        if (!std::getline(info, tmp))
+        {
+            printf("ERROR: Could not read from file '%s'.\n",
+                infofilename.c_str());
+            return false;
+        }
+
+        nline++;
+
+        // Skip empty lines and comments
+        if (tmp.empty() || tmp.substr(0, 1) == "#")
+            continue;
+
+        if (sscanf(tmp.c_str(), "%c:%d", &key, &value) != 2)
+        {
+            printf("ERROR: Syntax error in '%s' line #%d\n'%s'\n",
+                infofilename.c_str(),
+                nline,
+                tmp.c_str());
+            return false;
+        }
+
+        switch (key)
+        {
+            case 'v':
+                mesh.nvertices = value;
+                required |= 0x01;
+                break;
+
+            case 'f':
+                mesh.nfaces = value;
+                required |= 0x02;
+                break;
+
+            default:
+                printf("ERROR: Syntax error in '%s' line #%d\nUnknown key value '%c'.\n",
+                    infofilename.c_str(),
+                    nline,
+                    key);
+                break;
+        }
+    }
+
+    if (mesh.nvertices < 3 || mesh.nfaces < 1)
+    {
+        printf("ERROR: Invalid nvertices (%d) and/or nfaces (%d) in '%s'.\n",
+            mesh.nvertices, mesh.nfaces,
+            infofilename.c_str());
+        return false;
+    }
+
+    printf("INFO: %d vertices, %d faces\n", mesh.nvertices, mesh.nfaces);
+    printf("INFO: Trying to read mesh data from '%s'.\n",
+        filename.c_str());
+
     std::ifstream in(filename.c_str(), std::ios::binary);
 
     if (!in.is_open())
@@ -301,11 +376,9 @@
         return false;
     }
 
-    mesh.nvertices = nvertices;
     mesh.vertices.resize(mesh.nvertices * 6);
     in.read(reinterpret_cast<char*>(&mesh.vertices[0]), mesh.nvertices * 6 * 4);
 
-    mesh.nfaces = nfaces;
     mesh.faces.resize(mesh.nfaces * 3);
 
     for (int i = 0; i < mesh.nfaces; i++)
@@ -429,7 +502,7 @@
         goto exit;
     }
 
-    if (!dmLoadMesh(optModelPrefix + ".mesh", modelMesh, 100139, 200198))
+    if (!dmLoadMesh(optModelPrefix + ".mesh", optModelPrefix + ".info", modelMesh))
         goto exit;
 
     if (optUseShaders)