Rendering Trouble

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.

Maybe you should print out the error instead of silently ignoring it?


catch(Exception e)
{
         
}

Are you certain that the texture is actually being loaded? The exception handler silently ignores an exceptions thrown.

Edit: @theagent, beat me to it :wink:

[quote=“theagentd,post:2,topic:40807”]
I have added it in and no error is shown.

[quote=“StrideColossus,post:3,topic:40807”]
The output line shows this:

Texture loaded: org.newdawn.slick.opengl.TextureImpl@1ded246d

Call glViewport() directly before glOrtho()

[quote=“HeroesGraveDev,post:5,topic:40807”]
I have tried that and still nothing is displayed.

GL11.glOrtho(0, 640, 0, 480, 1, -1);

should be

GL11.glOrtho(0, 640, 480, 0, 1, -1);

[quote=“theagentd,post:7,topic:40807”]
Well that was because I wanted the bottom left to be the origin. Though it doesn’t seem to work both ways.

OK, i got it!

You mixed up tileset.getWidth() and tileset.getImageWidth(). First one gets you value from 0 to 1, second one in pixels…
Correct code:

      GL11.glBegin(GL11.GL_QUADS);
         GL11.glTexCoord2f(0, 0);
         GL11.glVertex2f(0, 0);
         GL11.glTexCoord2f(1, 0);
         GL11.glVertex2f(tileset.getImageWidth(), 0);
         GL11.glTexCoord2f(1, 1);
         GL11.glVertex2f(tileset.getImageWidth(), tileset.getImageHeight());
         GL11.glTexCoord2f(0, 1);
         GL11.glVertex2f(0, tileset.getImageHeight());
      GL11.glEnd();

IMHO you should write your own texture loader rather than relying on the old and deprecated SlickUtil; it’s really the “first step” to understanding how to use LWJGL and OpenGL.

[quote=“Sparky83,post:9,topic:40807”]
This has fixed the problem, thanks for your help.

[quote=“davedes,post:10,topic:40807”]
Yeah, I am now going to learn more about the proper use of OpenGL. I just wanted to get something to work first.