Preventing vertex duplication when loading triangle indexes into index arrays?

Started by
2 comments, last by dpadam450 12 years, 3 months ago
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]);
glEnableVertexAttribArray(GLT_ATTRIBUTE_VERTEX);
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]);
glEnableVertexAttribArray(GLT_ATTRIBUTE_NORMAL);
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]);
glEnableVertexAttribArray(GLT_ATTRIBUTE_TEXTURE0);
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?
Advertisement
There is no way around it
http://www.opengl.org/wiki/FAQ#Multi_indexed_rendering
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
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!
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.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

This topic is closed to new replies.

Advertisement