Simple Texturing with LWJGL and VBOs does not work
I wrote a little 3D program with VBO, but failed with the textures.
Loading them is fine but they just are not displayed. So I wrinte a tiny
texture renderer for VBO textures and also took what was in this topic:
VBO with texture in LWJGL. But there is still just nothing rendered.
PLease help me just to make this code show a Texture ;)
public class VBOTextureDemo {
private static Texture texture;
public static void main(String[] args) {
try {
Display.setDisplayMode(new DisplayMode(500, 500));
Display.setTitle("Texture");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(1, -1, 1, -1, 1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
try {
texture = TextureLoader.getTexture("PNG",
ResourceLoader.getResourceAsStream("res/images/grass.png"));
} catch (IOException e) {
e.printStackTrace();
}
final int amountOfVertices = 6;
final int vertexSize = 3;
final int texSize = 2;
FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices
* vertexSize);
vertexData.put(new float[] { -0.5f, -0.5f, 0, 0.5f, -0.5f, 0, 0.5f,
0.5f, 0, -0.5f, -0.5f, 0, 0.5f, 0.5f, 0, -0.5f, 0.5f, 0f });
vertexData.flip();
FloatBuffer textureData = BufferUtils
.createFloatBuffer(amountOfVertices * texSize);
textureData.put(new float[] { 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1 });
textureData.flip();
int vboVertexHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
int vboTexCoordHandle = texture.getTextureID();
glBindBuffer(GL_ARRAY_BUFFER, vboTexCoordHandle);
glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glClearColor(0.5f, 0.1f, 0f, 1f);
while (!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, vboTexCoordHandle);
glTexCoordPointer(2, GL_FLOAT, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, amountOfVertices);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
Display.update();
Display.sync(60);
}
glDeleteBuffers(vboVertexHandle);
glDeleteBuffers(vboTexCoordHandle);
Display.destroy();
System.exit(0);
}
}
No comments:
Post a Comment