问题
I'm trying to use the VBO with a Element Array Buffer for my triangle, like this :
glBindBuffer(GL_ARRAY_BUFFER, g_Buffer[0]);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glNormalPointer(GL_FLOAT, 0, BUFFER_OFFSET(Model->GetNbVertex()*3*sizeof(float)));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_Buffer[1]);
glDrawElements(GL_TRIANGLES, Model->GetNbTriangle()*3, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
But I wanna use some Texture Coord, but my TextCoord isn't willingly "indexed"
A triangle have 3 Text coords. I have N vertexs, and M Triangles, so i have 3M Text Coord, and not 3N TextCoord. So i can't use glTexCoordPointer because he's expecting that a vertex have a only one text coord, and it's not my case.
I wanna keep the indexation of my vertex to don't explode my GPU memory.
There is a way to use Triangle Indexation for some element like vertex and not for other, like my text coord ? (I'm using GLSL)
回答1:
Let me clarify the following for you
A triangle have 3 Text coords. I have N vertexs, and M Triangles, so i have 3M Text Coord, and not 3N TextCoord.
A triangle has 3 vertices. A vertex is a vector consisting of several attributes; it's a common misconception that a vertex was just the position! A vertex is in fact the whole combination of all the attributes it consists of. And if two vertices differ in only one of their attributes, they're different vertices.
In your case the attributes are
- position
- texture coordinate
So when you say you have M triangles, then this implies that you have 3N vertices – except for the corner case that some of the triangles share vertices, which means that they share all their attributes.
Or in other words: You always have exactly as many texture coordinates as you have vertices, because the texture coordinate is part of the vertex.
Update
Some mesh formats separate the vertex attributes into independtly indexed lists of attributes, like position, normal, texture coordinate. Usually those lists are then indexed from faces. If you want to draw the contents of such a mesh data structure you first have to expand it into a single indexed list of vertices. The usual way in doing so is iterating over all faces of the mesh and use the tuple of indices as a key into a list of new substitution indices. Then for each key encountered the attributes are collected from the various attribute lists into a single vertex indexed by the substitution index.
回答2:
So i can't use glTexCoordPointer because he's expecting that a vertex have a only one text coord...
...per texture unit. Activate two more TUs and call glTexCoordPointer()
for each:
glVertexPointer( ... );
glNormalPointer( ... );
glActiveTexture( GL_TEXTURE0 + 0 );
glClientActiveTexture( GL_TEXTURE0 + 0 );
glBindTexture( ..., texture1 );
glTexCoordPointer( ..., FIRST_OFFSET );
glActiveTexture( GL_TEXTURE0 + 1 );
glClientActiveTexture( GL_TEXTURE0 + 1 );
glBindTexture( ..., texture2 );
glTexCoordPointer( ..., SECOND_OFFSET );
glActiveTexture( GL_TEXTURE0 + 2 );
glClientActiveTexture( GL_TEXTURE0 + 2 );
glBindTexture( ..., texture3 );
glTexCoordPointer( ..., THIRD_OFFSET );
glDrawElements( ... );
EDIT: Though for maximum flexibility and forward compatibility just use generic vertex attributes for both the vertex position and the three texture coordinates.
来源:https://stackoverflow.com/questions/13981217/vbo-indexation-without-indexation