开发者

Combining vertex arrays with textures in OpenGL

I'm trying to use vertex arrays to draw a reasonably large mesh, containing a large number of vertices. The textures have been determined from these and it's easy enough to draw in immediate mode along the lines of:

glBegin(GL_TRIANGLES) {
  for ( int faceIdx = 0; faceIdx < nFaces; ++faceIdx )
     glVertex3fv(&vertexArray[vIdx]);
     glTexCoord2fv(&texCoord[vIdx++]);
     glVertex3fv(&vertexArray[vIdx]);
     glTexCoord2fv(&texCoord[vIdx++]);      
     glVertex3fv(&vertexArray[vIdx]);
     glTexCoord2fv(&texCoord[vIdx++]);  
  }   
} 
glEnd();

However for readability, speed and the rest I want to use vertex arrays (with a view to moving to VBOs). Is there a way of getting around putting a single vertex into the array multiple times?

As I understand it at the moment it's necessary to specify each vertex of the mesh as many times as it appears in a face of the mesh, as each vertex identifi开发者_Go百科es to multiple texture coordinates (the textures are captured from a real-world image of the object the mesh approximates), i.e. my vertex/tex coord array reads as if I'd filled it in immediate mode.

Is there any way to use vertex arrays, whilst specifying the texture coordinates, without using redundant (by which I mean repeated) vertices?


A single vertex is comprised of all attributes that make up this single vertex. So two vertices that share the same position but have different texture coordinates are conceptually different vertices. So no, there is no simple way around repeating vertex positions for different texCoords.

But usually such vertex duplications are only neccessary in some rare regions (like sharp edges, because of different normals, or like in your case a texture seam). So do all your faces' corners really have different texCoords? Maybe you can pre-process your data a bit and find neighbouring faces that share positions and texCoords and can therefore share the vertex. I would be suprised if that wouldn't be the case for many many vertices and you end up with only a small bunch of duplicated vertices.


If I understand your question correct there is indeed a way of getting rid of having to put multiple times the same vertex into the vertexbuffer. You can use an indexbuffer and use indices to specify the same vertex. This can speed up the rendering significantly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜