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?