On 8/9/2019 at 11:20 AM, VanillaSnake21 said:
But doesn't that means I still need 36 vertices to represent every possible "tuple" of attributes? Or am I just misunderstanding what you mean when you say "de-index"? The way I understand it now is that I have to generate a unique vertex from the index table of the obj file, or do you mean something different? By the way this is the index table given to me by the library...
If you de-index them (get rid of the indices from the file and create unique tuples of values), then yes, you end up with 36 vertices...
But, in your array, item[0] and item[3] are identical. Within those 36 vertices, there's actually only 24 unique ones (4 for each face of the cube).
Instead of de-indexing (turning the OBJ indices into a vertex attribute structre) and then reindexing them (finding the unique vertex structures), you can do this in one step with something like:
//The index data from your OBJ file:
struct ObjIndices { int vertex_index, normal_index, texcoord_index; };
//we're going to find the unique set of ObjIndices and map them to a NEW singular index value
map<ObjIndices, int> reindexer;
vector<int> newIndexBuffer; // this will be the new index buffer data that we send to the GPU
//little routine checks if this ObjIndices struct has been seen before, and if not, assigns it a new index value
int GetNewIndex(objIndices)
if not reindexer.contains(objIndices) then
reindexer[objIndices] = reindexer.size()
return reindexer[objIndices]
// for each vert in your OBJ file, add it to your new index buffer
for each indices as o
newIndexBuffer.push_back( GetNewIndex(o) )
//After this, newIndexBuffer should contain 36 entries (6 faces * 2 triangles * 3 corners per tri)
// and reindexer should contain just 24 entries (6 faces * 4 unique corners)
//now you can read the unique values from the 'reindexer' map, and use them to build your vertex buffer that will be sent to the GPU
struct Vertex { float3 vertex; float3 normal; float2 texcoord; }
vector<Vertex> newVertexBuffer
newVertexBuffer.resize( reindexer.size() )
for each reindexer as objIndices, index
newVertexBuffer[index] = Vertex{
obj.vertex_values[objIndices.vertex_index],
obj.normal_values[objIndices.normal_index],
obj.texcoord_values[objIndices.texcoord_index],
}