简体   繁体   中英

VBO with texture in LWJGL

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM