I am starting to use LWJGL/OpenGL in Java to create a 2D game, however I am having difficulty rendering a simple 32x32 quad (square) that is textured. I have created a texture loading system and have followed the proper instructions to texture, but the texture will not display. My code is below:
devBlock64.bind();
glEnable(GL_TEXTURE_2D);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(32, 0);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(32, 32);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(0, 32);
GL11.glEnd();
glDisable(GL_TEXTURE_2D);
The above is code for each time "render" is called. "devBlock64" is simply a Texture object that has loaded a 64x64 texture (but in this case it's 32x32, because I saved it as the wrong size)
Furthermore, this is the options and functions I have called after loading a texture and generating its texture id:
this.bind();
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
The image loads properly but it won't be displayed. this.bind();
calls my function that simply does this: glBindTexture(GL_TEXTURE_2D, id):
And, my game loop if anyone is curious, everything else was done according to OpenGL's tutorials for setting up a window:
public void loop() {
// Binds GLFW with OpenGL
GL.createCapabilities();
glOrtho(0f, 800, 600, 0f, 0f, 1f);
glClearColor(1.0f, 1f, 1f, 1f);
//glLoadIdentity();
world.loadTextures();
while(!hasWindowRequestedClose()) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
world.onUpdate();
world.render();
glfwSwapBuffers(handle);
glfwPollEvents();
}
destroy();
}