fix 'unexpected end' while parsing .obj

This commit is contained in:
MihailRis 2025-12-01 20:01:43 +03:00 committed by ShiftyX1
parent 7fc1c37258
commit b7751cf053

View File

@ -10,20 +10,21 @@ class ObjParser : BasicParser<char> {
std::vector<glm::vec2> uvs {{0, 0}}; std::vector<glm::vec2> uvs {{0, 0}};
std::vector<glm::vec3> normals {{0, 1, 0}}; std::vector<glm::vec3> normals {{0, 1, 0}};
// TODO: refactor
void parseFace(Mesh& mesh) { void parseFace(Mesh& mesh) {
std::vector<Vertex> vertices; std::vector<Vertex> vertices;
while (hasNext()) { while (hasNext()) {
auto c = peekInLine(); auto c = peekInLine();
if (c == '\n') { if (c == '\n') {
break; break;
} else { } else if (hasNext()) {
uint indices[3] {}; uint indices[3] {};
uint i = 0; uint i = 0;
do { do {
char next = peekInLine(); char next = peekInLine();
if (is_digit(next)) { if (is_digit(next)) {
indices[i] = parseSimpleInt(10); indices[i] = parseSimpleInt(10);
if (peekInLine() == '/') { if (hasNext() && peekInLine() == '/') {
pos++; pos++;
} }
} else if (next == '/') { } else if (next == '/') {
@ -31,13 +32,13 @@ class ObjParser : BasicParser<char> {
} else { } else {
break; break;
} }
} while (peekInLine() != '\n' && ++i < 3); } while (hasNext() && peekInLine() != '\n' && ++i < 3);
vertices.push_back(Vertex { vertices.push_back(Vertex {
coords[indices[0]], uvs[indices[1]], normals[indices[2]]}); coords[indices[0]], uvs[indices[1]], normals[indices[2]]});
} }
} }
if (peekInLine() != '\n' && hasNext()) { if (hasNext() && peekInLine() != '\n') {
skipLine(); skipLine();
} }
if (vertices.size() >= 3) { if (vertices.size() >= 3) {