You forgot to enable GL_TEXTURE_2D
when you draw. Since you were disabling it after texture loading is done.
And you have to bind the texture before you start rendering quads.
Domanda
Heyho,
this problem is really annoying...
I'm trying create a window and load & render a texture with the LWJGL.
Window-Creation works perfect, texture should be loaded correct (tested and it is always the texture I wanted to render) but it will be rendered plain white.
Initing OpenGL, loading the texture and rendering it:
private void initGL(int width, int height)
{
try
{
searchDisplayMode(width, height, true);
Display.create();
Display.setVSyncEnabled(true);
} catch (LWJGLException e)
{
e.printStackTrace();
System.exit(0);
}
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0,0,width,height);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
public void loadTextures()
{
try
{
DirtID = textureManager.registerTexture("Dirt.png");
System.out.println(DirtID);
} catch (IOException e)
{
e.printStackTrace();
}
}
public void render()
{
GL11.glEnable(GL11.GL_TEXTURE_2D);
Color.white.bind();
// This was the bug! [Fixed it now]
textureManager.bindTexture(DirtID);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(0 + textureManager.getWidth(DirtID), 0);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(0 + textureManager.getWidth(DirtID), 0 + textureManager.getHeight(DirtID));
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(0, 0 + textureManager.getHeight(DirtID));
GL11.glEnd();
}
Loading-function + other stuff in TextureManager.java:
public TextureManager()
{
textures = new ArrayList<TextureIO>();
}
public int registerTexture(String file) throws IOException
{
return registerTexture(file, false);
}
public int registerTexture(String file, boolean isFlipped) throws IOException
{
for (TextureIO t : textures)
{
if (t.getName().equals(file))
{
return t.getId();
}
}
if (isFlipped)
{
System.out.println("Register flipped Texture ~* " + file);
} else
{
System.out.println("Register Texture ~* " + file);
}
TextureIO texture = new TextureIO(file, isFlipped);
texture.init();
textures.add(texture);
return texture.getId();
}
public void bindTexture(int textureId)
{
if (boundedTexture != textureId)
{
glBindTexture(GL_TEXTURE_2D, textureId);
boundedTexture = textureId;
System.out.println("The texture " + boundedTexture + " was bound");
}
}
public void unbindTexture()
{
glBindTexture(GL_TEXTURE_2D, 0);
boundedTexture = 0;
}
public TextureIO getTexture(int textureId)
{
for (TextureIO tex: textures)
{
if (tex.getId() == textureId)
{
return tex;
}
}
return null;
}
and the TextureIO.java used by the TextureManager:
public TextureIO(String ref, boolean flipped) throws IOException
{
BufferedImage image = ImageIO.read(TextureIO.class.getResourceAsStream("graphics/" + ref));
width = image.getWidth();
height = image.getHeight();
name = ref;
isFlipped = flipped;
if (flipped)
{
final AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -height);
final AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = op.filter(image, null);
}
final int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
texture = BufferUtils.createIntBuffer(pixels.length);
texture.put(pixels);
texture.rewind();
}
public String getName()
{
return name;
}
public void init()
{
final IntBuffer buffer = BufferUtils.createIntBuffer(1);
glGenTextures(buffer);
id = buffer.get(0);
glBindTexture(GL_TEXTURE_2D, id);
/*
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);*/
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL12.GL_BGRA, GL_UNSIGNED_BYTE /*GL_UNSIGNED_INT_8_8_8_8_REV*/, texture);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
public void bind()
{
glBindTexture(GL_TEXTURE_2D, id);
}
public void unbind()
{
glBindTexture(GL_TEXTURE_2D, 0);
}
and the result:
Maybe you can help me with this problem.
Soluzione
You forgot to enable GL_TEXTURE_2D
when you draw. Since you were disabling it after texture loading is done.
And you have to bind the texture before you start rendering quads.