Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 identifies 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?

like image 847
northerncodemonkey Avatar asked Sep 02 '11 15:09

northerncodemonkey


People also ask

Can Textures be used in a vertex shader?

So it can be used in vertex shader.No matter with whether the texture has mipmap. Basically yes.

How do you show textures in OpenGL?

in display() function : GLuint texture; texture = LoadTexture("bubble. png"); glBindTexture(GL_TEXTURE_2D, texture);

What is OpenGL texture mapping?

1.0 Introduction. Texture mapping is a process of stretching a texture onto a 3D object; the applied texture will follow the 3D object as it transforms. Texture mapping is fun and beneficial but it can be challenging for beginners. Texture can also be distorted for a visual effect.

What are texture coordinates used for in OpenGL?

Texture coordinates specify the point in the texture image that will correspond to the vertex you are specifying them for. Think of a rectangular rubber sheet with your texture image printed on it, where the length of each side is normalized to the range 0-1.


1 Answers

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.

like image 105
Christian Rau Avatar answered Sep 30 '22 09:09

Christian Rau