Hello, I am trying to learn the basics of LWJGL. I can’t seem to get a texture to render in 2D. Could someone take a look at my code and tell me what I am doing wrong?
package com.game;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class Game
{
Texture tileset;
public static void main(String[] args)
{
Game game = new Game();
game.initialise();
game.run();
}
public void initialise()
{
try
{
Display.setDisplayMode(new DisplayMode(640, 480));
Display.setTitle("Game");
Display.create();
}
catch(Exception e)
{
e.printStackTrace();
}
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0, 0, 640, 480);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 640, 0, 480, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
try
{
tileset = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("/res/tilesets/001-Grassland01.png"));
System.out.println("Texture loaded. " + tileset);
}
catch(Exception e)
{
}
}
public void run()
{
while(!Display.isCloseRequested())
{
this.render();
Display.update();
Display.sync(60);
}
Display.destroy();
}
public void render()
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
Color.white.bind();
tileset.bind();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(tileset.getWidth(), 0);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(tileset.getWidth(), tileset.getHeight());
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(0, tileset.getHeight());
GL11.glEnd();
}
}
The display is shown and the texture is loaded but nothing is rendered. Here is a screenshot of the display.
http://s1.postimage.org/ln2g3mv7z/Game.png
I know I may be using bad techniques, but I would just like to get it working first.