0

How do I attach a texture to a VBO?

I had it working with a colorBuffer and now i want to implement a texture. This is my draw method:

Color.white.bind();
glBindTexture(GL_TEXTURE_2D, texture.getTextureID());

glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW);
glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);




glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(3, GL_FLOAT, 0, 0);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glDrawArrays(GL_QUADS, 0, amountOfVertices);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

That doesn't display anything at all. the texture is correctly laoded and I got it working with the immediate mode. What do I have to do to make it work with VBOs ?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Frotty
  • 75
  • 2
  • 11

1 Answers1

2

Looks like the VBO for the texture coordinates is not bound while setting the texCoordPointer. Changing the order of your commands should work. Also you are overriding the vertex with your texCoord data in your single VBO. Easiest solutions would be to have two separate VBOs for each.

glBindTexture(GL_TEXTURE_2D, texture.getTextureID());
// vertices
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);
// texCoords
glBindBuffer(GL_ARRAY_BUFFER, vboTexCoordHandle);
glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW);
glTexCoordPointer(3, GL_FLOAT, 0, 0);
// unbind VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glDrawArrays(GL_QUADS, 0, amountOfVertices);

glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

Note: usually you don't want to create new VBOs each frame by calling glBufferData more than once per VBO.

  • Ah, so basically the Texture Coordinates are just another buffer of vertices that gets buffered to the gfxcars memory? Well thank you very much, that bout did it! And I also understand it a bit more now. – Frotty May 05 '12 at 19:30
  • how do I manage more than 1 moving VBO then without calling BufferData each time? – Frotty May 06 '12 at 14:45