Loading OBJ model data using regexes?

81 Views Asked by At

I'm trying to write a class for loading a *.obj model, but I ran into a problem.

I'm fetching vertex data and vertex indexes from a *.obj file. For this I use regular expressions.

With prefix v:

Regex: v ([-+]?([0]\.[0-9]*|[1-9]*[0-9]*\.[0-9]*)) ([-+]? ([0]\.[0-9]*|[1-9]*[0-9]*\.[0-9]*)) ([-+]?([0]\.[0- 9]*|[1-9]*[0-9]*\.[0-9]*))

With prefix f:

Regex: f ([0-9]|[1-9][0-9]+)\/.*\/.* ([0-9]|[1-9][0-9]+)\ /.*\/.* ([0-9]|[1-9][0-9]+)\/.*\/.*

All values are added to vectors:

std::vector<glm::fvec3> vertices_postion;
std::vector<GLint> vertices_postion_indicies;

Next, the data is sent to OpenGL:

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices_postion.size() * sizeof(glm::fvec3), &vertices_postion[0], GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, vertices_postion_indicies.size() * sizeof(GLint), &vertices_postion_indicies[0], GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

In terms of rendering:

glUseProgram(shader->getProgram());
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, vertices_postion_indicies.size(), GL_UNSIGNED_INT, (void*)0);

The cube model was made in Autodesk Inventor and then exported to .OBJ format.

However, the rendering does not look correct, I think there are problems with the vertex rendering indexes.

Vertices:

v1: -5 v2: 0  v3: -5
v1: -5 v2: 0  v3: 5
v1: -5 v2: 10 v3: 5
v1: -5 v2: 10 v3: -5
v1: 5  v2: 0  v3: -5
v1: 5  v2: 10 v3: -5
v1: 5  v2: 0  v3: 5
v1: 5  v2: 10 v3: 5

Vertex indices:

1,2,4,4,2,3,5,1,6,6,1,4,7,5,8,8,5,6,2,7,3,3,7,8,2,1,7,7,1,5,8,6,3,3,6,4

Picture:

Incorrect cube

0

There are 0 best solutions below