# HG changeset patch # User Matti Hamalainen # Date 1572346116 -7200 # Node ID 62be2036f60474b481153920625f60c455c3e577 # Parent c1e8057cc4d04f4bf3291410bd1ba54733231155 Read mesh vertices/faces information from ".info" instead of having the values hardcoded. diff -r c1e8057cc4d0 -r 62be2036f604 dragon.info --- /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 diff -r c1e8057cc4d0 -r 62be2036f604 glxdragon.cpp --- 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(&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)