Slick Texture Bug: Lost Textures

Started by elantzb, October 10, 2012, 05:35:56

Previous topic - Next topic

elantzb

My program seems to be forgetting my previous textures, and replacing all of them with the last texture that I loaded.

In the following I load two textures, and put them at different parts of the screen. But they both look like "grass.png"

import org.lwjgl.*;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class lwjglTest {

	Texture texture;
	Texture texture2;
	
	public static void main(String[] args)
	{
		new lwjglTest();
	}
	
	lwjglTest()
	{
		try
		{
			Display.setDisplayMode(new DisplayMode(800,600));
			Display.create();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			System.exit(0);
		}
		
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		GL11.glClearColor(0.0f, 0.5f, 0.5f, 2.0f);
		GL11.glEnable(GL11.GL_BLEND);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
		GL11.glViewport(0,0,Display.getWidth(),Display.getHeight());
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
		
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
		GL11.glMatrixMode(GL11.GL_MODELVIEW);
		
		try 
		{
			texture = TextureLoader.getTexture("PNG",ResourceLoader.getResourceAsStream("mesh.png"));
			texture2 = TextureLoader.getTexture("PNG",ResourceLoader.getResourceAsStream("grass.png"));
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		while(!Display.isCloseRequested())
		{
			GL11.glBindTexture(GL11.GL_TEXTURE,texture.getTextureID());
			
			GL11.glBegin(GL11.GL_POLYGON);
				GL11.glTexCoord2f(0,0);
				GL11.glVertex2f(10,10);
				GL11.glTexCoord2f(1,0);
				GL11.glVertex2f(10+texture.getTextureWidth(),10);
				GL11.glTexCoord2f(1,1);
				GL11.glVertex2f(10+texture.getTextureWidth(),10+texture.getTextureHeight());
				GL11.glTexCoord2f(0,1);
				GL11.glVertex2f(10,10+texture.getTextureHeight());
			GL11.glEnd();
			
			
			GL11.glBindTexture(GL11.GL_TEXTURE,texture2.getTextureID());
			
			GL11.glBegin(GL11.GL_POLYGON);
				GL11.glTexCoord2f(0,0);
				GL11.glVertex2f(100,100);
				GL11.glTexCoord2f(1,0);
				GL11.glVertex2f(100+texture2.getTextureWidth(),100);
				GL11.glTexCoord2f(1,1);
				GL11.glVertex2f(100+texture2.getTextureWidth(),100+texture2.getTextureHeight());
				GL11.glTexCoord2f(0,1);
				GL11.glVertex2f(100,100+texture2.getTextureHeight());
			GL11.glEnd();
			
			Display.update();
		}
		
		Display.destroy();
	}

}
[/size]

mattdesl

You need to specify the correct target when binding a texture: GL_TEXTURE_2D. You should also not rely on [0 - 1] texcoords as Slick pads for non-power-of-two, see here:
http://slick.javaunlimited.net/viewtopic.php?p=25911#p25911

Fool Running

Or you could replace your calls to GL11.glBindTexture() with a call to texture.bind() and texture2.bind().
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D