Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Preventing vertex duplication when loading triangle indexes into index arrays?

This topic is 2825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a model loader class that reads in textfiles saved in wavefront .obj format.

From this i store the data in 4 variables.

vertices: an array of arrays size 3.
normals: an array of arrays size 3;
texCoords: an array of arrays size 2;

triangleIndexes: a struct that holds 3 arrays: each array holds the index values ffrom the above arrays to compose each triangular face.

Now i need to convert my index data to an array of GLushorts so i can pass this to openGL:

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects[INDEX_DATA]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort)*numIndexes, indexes, GL_STATIC_DRAW);

My issue is, i have to completely recreate my vertex, normals, and tCoord arrays so that the index values are aligned properly:

// Vertex data
glBindBuffer(GL_ARRAY_BUFFER, bufferObjects[VERTEX_DATA]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*numVerts*3, vertices, GL_STATIC_DRAW);
glVertexAttribPointer(GLT_ATTRIBUTE_VERTEX, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Normal data
glBindBuffer(GL_ARRAY_BUFFER, bufferObjects[NORMAL_DATA]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*numVerts*3, normals, GL_STATIC_DRAW);
glVertexAttribPointer(GLT_ATTRIBUTE_NORMAL, 3, GL_FLOAT, GL_FALSE, 0, 0);

// Texture coordinates
glBindBuffer(GL_ARRAY_BUFFER, bufferObjects[TEXTURE_DATA]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*numVerts*2, texCoords, GL_STATIC_DRAW);
glVertexAttribPointer(GLT_ATTRIBUTE_TEXTURE0, 2, GL_FLOAT, GL_FALSE, 0, 0);

This creates unneccesary duplications, how can a go about avoiding this? The only way i can think is a O(n^2) method of checking every new vertex to see if it exists already and setting the index values accordingly. But for large models, this horribly inefficient.

So it seems im stuck, either have a large loading time and exact number of vertices, or quick load time and duplications. Anyone have a method for getting around this?

Share this post

Link to post
Share on other sites
Thanks for the response, I guess ill write a converter program, something that reads .obj files, and tests every vertex again the existing array for duplicates, then print them into a new file format. Therefore no duplicates and no loading overhead!

Share this post

Link to post
Share on other sites
The only way i can think is a O(n^2) method of checking every new vertex to see if it exists already[/quote]
For this, load the vertices into a vector or list. load the texture coords into a vector or list.

When it comes to the indices, you have the vertices hold another vector or list of indices. If the vertex gets more than 1 tex coord index, then it needs to be duplicated. You also need to keep track of what face it belongs to.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!