From 593f1e75a6d556bda28ed61324b5815ff9d7678d Mon Sep 17 00:00:00 2001 From: Woohyung Choi <201421062@sce212.ajou.ac.kr> Date: Tue, 10 Dec 2019 22:41:26 +0900 Subject: [PATCH] improve stability in loadobj(). --- LearnOpenGL/ResourceLoader.cpp | 45 +++++++++++++++++++++++++++++++--- LearnOpenGL/Source.cpp | 2 +- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/LearnOpenGL/ResourceLoader.cpp b/LearnOpenGL/ResourceLoader.cpp index 1d33f2a..032467c 100644 --- a/LearnOpenGL/ResourceLoader.cpp +++ b/LearnOpenGL/ResourceLoader.cpp @@ -50,7 +50,7 @@ bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std:: int charPos = 0; while (std::getline(ifs, line)) { - if (line[0] == NULL || line[0] == '\n' || line[0] == '#' || line[0] == '!' || line[0] == '$') continue; + if (line[0] == NULL || line[0] == '\n' || line[0] == '#' || line[0] == '!' || line[0] == '$' || line[0] == 'o' || line[0] == 'm' || line[0] == 'u') continue; sscanf_s(line.c_str(), "%s", op, sizeof(op)); @@ -89,8 +89,47 @@ bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std:: charPos = 0; while ((charPos = line.find(' ')) != std::string::npos) { - sscanf_s(line.substr(0, charPos).c_str(), "%d%*[-/]%d%*[-/]%d", &vIndex, &uvIndex, &vnIndex); - line.erase(0, charPos + 1); + if (line.find("//") == std::string::npos) + { + sscanf_s(line.substr(0, charPos).c_str(), "%d%*[-/]%d%*[-/]%d", &vIndex, &uvIndex, &vnIndex); + line.erase(0, charPos + 1); + } + else + { + uvIndex = 0; + + sscanf_s(line.substr(0, charPos).c_str(), "%d%*[-//]%d", &vIndex, &vnIndex); + line.erase(0, charPos + 1); + } + + if (vIndex >= 1) + { + faceVertexIndicies.push_back(vIndex - 1); + } + if (uvIndex >= 1) + { + faceVertexTexCoordIndicies.push_back(uvIndex - 1); + } + if (vnIndex >= 1) + { + faceVertexNormalIndicies.push_back(vnIndex - 1); + } + } + + if (line.size() > 0) + { + if (line.find("//") == std::string::npos) + { + sscanf_s(line.substr(0, charPos).c_str(), "%d%*[-/]%d%*[-/]%d", &vIndex, &uvIndex, &vnIndex); + line.erase(0, charPos + 1); + } + else + { + uvIndex = 0; + + sscanf_s(line.substr(0, charPos).c_str(), "%d%*[-//]%d", &vIndex, &vnIndex); + line.erase(0, charPos + 1); + } if (vIndex >= 1) { diff --git a/LearnOpenGL/Source.cpp b/LearnOpenGL/Source.cpp index 1333b7f..da4f6a2 100644 --- a/LearnOpenGL/Source.cpp +++ b/LearnOpenGL/Source.cpp @@ -71,7 +71,7 @@ int main() auto defaultMaterial = new Material(lightmap, orange, transparent); auto cubeMaterial = new Material(lightmap, container_diffuse, container_specular); - auto planeMaterial = new Material(lightmap, container_diffuse, transparent); + auto planeMaterial = new Material(lightmap, magenta, transparent); auto teapot = make_render_object(make_mesh("teapot.obj")); { -- GitLab