Skip to content
Snippets Groups Projects
Commit 593f1e75 authored by Woohyung Choi's avatar Woohyung Choi
Browse files

improve stability in loadobj().

parent 13796cb3
No related branches found
No related tags found
No related merge requests found
...@@ -50,7 +50,7 @@ bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std:: ...@@ -50,7 +50,7 @@ bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std::
int charPos = 0; int charPos = 0;
while (std::getline(ifs, line)) 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)); sscanf_s(line.c_str(), "%s", op, sizeof(op));
...@@ -88,9 +88,48 @@ bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std:: ...@@ -88,9 +88,48 @@ bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std::
charPos = 0; charPos = 0;
while ((charPos = line.find(' ')) != std::string::npos) while ((charPos = line.find(' ')) != std::string::npos)
{
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); sscanf_s(line.substr(0, charPos).c_str(), "%d%*[-/]%d%*[-/]%d", &vIndex, &uvIndex, &vnIndex);
line.erase(0, charPos + 1); 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) if (vIndex >= 1)
{ {
......
...@@ -71,7 +71,7 @@ int main() ...@@ -71,7 +71,7 @@ int main()
auto defaultMaterial = new Material(lightmap, orange, transparent); auto defaultMaterial = new Material(lightmap, orange, transparent);
auto cubeMaterial = new Material(lightmap, container_diffuse, container_specular); 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")); auto teapot = make_render_object(make_mesh("teapot.obj"));
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment