I’m trying to render a texture-mapped cube, as part of my self-taught (or rather SO taught) learning.
I found this example online that packs the vertex coords and texture coords into one array, like so:
Vertex Vertices[4] = { Vertex(Vector3f(-1.0f, -1.0f, 0.5773f), Vector2f(0.0f, 0.0f)),
Vertex(Vector3f(0.0f, -1.0f, -1.15475), Vector2f(0.5f, 0.0f)),
Vertex(Vector3f(1.0f, -1.0f, 0.5773f), Vector2f(1.0f, 0.0f)),
Vertex(Vector3f(0.0f, 1.0f, 0.0f), Vector2f(0.5f, 1.0f)) };
I guess it works for a pyramid-shaped object, but it doesn’t work so well for my cube. The problem is that I need to use a different texture coordinate for the same vertex which is shared with another face.
So I thought, “Oh I know! I’ll just pack the texture coordinates with the indices instead!” and I merrily created my data structure mapping the indexes to texture coordinates, but now I’ve ran into a snag: Indices need to go into the GL_ELEMENT_ARRAY_BUFFER and texture coordinates need to go into the GL_ARRAY_BUFFER.
Does this mean that there’s no way for me to pack this data into one buffer? I have to split out the index array and texture coordinate array into two separate structures?
Furthermore, I just realized that there would no longer be a 1:1 mapping between vertex positions and texture coordinates… I have no idea how I’d rewrite my vertex shader.
Or am I supposed to do it the way the tutorial does (pack the vertex positions and texture coords together) and just repeat vertices where necessary?
I thought the whole idea behind separating the indices and the vertex positions in the first place was to reduce data redundancy, but now I have to add that redundancy back in as soon as I want to use textures?
You fell for a common misconception, identifying vertices with just their position. This is not what a vertex is, though.
In reality a vertex is the full combination of all it’s attributes, i.e. position, normal, texture coordinates. So if the texture coordinates differ, you have a very different vertex, with it’s own index. So you have to duplicate the position, normal, etc. data, except that different texture coordinate.