简体   繁体   English

VBO与LWJGL中的纹理

[英]VBO with texture in LWJGL

How do I attach a texture to a VBO? 如何将纹理附加到VBO?

I had it working with a colorBuffer and now i want to implement a texture. 我使用了colorBuffer,现在我想实现一个纹理。 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 ? 我需要做些什么来使其与VBO一起使用?

Looks like the VBO for the texture coordinates is not bound while setting the texCoordPointer. 在设置texCoordPointer时,看起来没有绑定纹理坐标的VBO。 Changing the order of your commands should work. 更改命令的顺序应该有效。 Also you are overriding the vertex with your texCoord data in your single VBO. 您还可以在单​​个VBO中使用texCoord数据覆盖顶点。 Easiest solutions would be to have two separate VBOs for each. 最简单的解决方案是为每个提供两个独立的VBO。

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. 注意:通常您不希望每个VBO通过多次调用glBufferData来创建每个帧的新VBO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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