Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Preventing vertex duplication when loading triangle indexes into index arrays?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 pondwater   Members   -  Reputation: 191

Like
0Likes
Like

Posted 20 January 2012 - 03:23 PM

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?

Sponsor:

#2 V-man   Members   -  Reputation: 805

Like
1Likes
Like

Posted 20 January 2012 - 07:59 PM

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);

#3 pondwater   Members   -  Reputation: 191

Like
0Likes
Like

Posted 21 January 2012 - 12:19 PM

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!

#4 dpadam450   Members   -  Reputation: 1187

Like
0Likes
Like

Posted 21 January 2012 - 12:41 PM

The only way i can think is a O(n^2) method of checking every new vertex to see if it exists already

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS