[Solved, mostly] Texture binding not updating current texture

UPDATE: Actually, if I try to render the norm TextureRgba, I get the equivalent problem. It looks like once I use texRgbaLoad(), it ignores Texture.bind() and only pays attention to glBindTexture(). I can swap out the return TextureRgba.loadTexture() in texRgbaLoad() with return null, and I don’t get this problem. So it’s definitely something in my TextureRgba.loadTexture() method. It’s not the return new TextureRgba() line at the end, as commenting that out doesn’t fix it. However, if I comment out both that line and the glBindTexture() in the same method and the glTexImage2D() line, the problem does go away. But of course, then I can’t use the custom texture loading that TextureRgba.loadTexture() accomplishes. If I try to render the norm TextureRgba, it draws the door texture instead, scaled to the dimensions of norm.

I’m having a weird problem. If I load a Texture using slick’s TextureLoader.getTexture(), all is well. However, if I afterwards load a different texture using a custom loader designed to create them from BufferedImages, I can no longer draw the one I loaded from slick unless I specifically use glBindTexture(GL_TEXTURE_2D, texvar.getTextureID()). Simply using texvar.bind() no longer seems to have any effect. Instead, it just draws the one I loaded earlier with my custom loader, but scaling to the dimensions of the texvar texture.

public class GLTest {

private Texture door;
private int texW;
private int texH;
private int w = 1280;
private int h = 1024;
public GLTest() {
    //make display

    door = loadTexture("normal-example");
    TextureRgba foo = texRgbaLoad();

    //lwjgl setup, loop begins
    while(!Display.isCloseRequested()) {
    glClear(GL_COLOR_BUFFER_BIT);
    door.bind();
    glEnable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);
        renderTexture(door, 100, 200);
    glEnd();
    //rest of loop
    }

    //relevant methods
    private TextureRgba texRgbaLoad() {
        BufferedImage img; int imgw = 0, imgh = 0; int[] rgba;
        int[] colors;
        try {
            img = ImageIO.read(new File(GameBase.assetsDir + "normal-example.png"));
            imgw = img.getWidth(); imgh = img.getHeight();
            rgba = new int[imgw * imgh];
            colors = img.getRGB(0, 0, imgw, imgh, null, 0, imgw);
            for(int y = 0; y < imgh; y++) {
                for(int x = 0; x < imgw; x++) {
                    Color c = new Color(colors[y*imgw + x]);
                    rgba[y*imgw + x] = c.getRGB();
                }
            }
            return TextureRgba.loadTexture(rgba, imgw, imgh);
        } catch(IOException ex) {
            Logger.getLogger(GLTest.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    private Texture loadTexture(String key) {
        try {
            System.out.println("loading: assets/" + key + ".png");
            return TextureLoader.getTexture("PNG", new FileInputStream(new File("assets/" + key + ".png")));
        } catch(FileNotFoundException ex) {
            Logger.getLogger(GLTest.class.getName()).log(Level.SEVERE, null, ex);
        } catch(IOException ex) {
                Logger.getLogger(GLTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        return null;
    }
    
    private void renderTexture(Texture t, float x, float y) {
        int tw = t.getTextureWidth();
        int th = t.getTextureHeight();
        glTexCoord2f(0,0);
        glVertex2f(x, y);
        glTexCoord2f(1,0);
        glVertex2f(x+tw, y);
        glTexCoord2f(1, 1);
        glVertex2f(x+tw, y+th);
        glTexCoord2f(0, 1);
        glVertex2f(x, y+th);
    }
    
    public static void main(String[] args) {
        new GLTest();
    }
    
}

Note that this code never actually does anything with the custom foo texture. All it does is load it. If I put that one line, TextureRgba foo = texRgbaLoad();, before door = loadTexture(“door”); instead of after it, or if I just comment it out, everything works fine. Here is the TextureRgba.loadTexture() code, in another class, in another package:

    private TextureRgba(int id, int imgWidth, int imgHeight, int texWidth, int texHeight) {
        this.id = id;
        this.imgWidth = imgWidth;
        this.imgHeight = imgHeight;
        this.texWidth = texWidth;
        this.texHeight = texHeight;
    }
    public static TextureRgba loadTexture(int[] rgba, int sourceW, int sourceH) {
        int textureID = glGenTextures();
        
        //determine texture dimensions
        //put the pixels from the provided rgba array into a ByteBuffer named buffer and flip it
        
        //"Bind the texture handle to the context"
        glBindTexture(GL_TEXTURE_2D, textureID);
        
        //"Set the scaling filtering"
        //specifies how it is handled when minified and magnified
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        
        //"Send the buffer to OpenGL"
        //makes the texture
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texW, texH, 0,
                GL_RGBA, GL_UNSIGNED_BYTE, buffer);
       
        return new TextureRgba(textureID, sourceW, sourceH, texW, texH);
    }

Anyone got any ideas? I guess I could work around it by using the glBindTexture() instead of the Texture.bind(), or by extending the Texture class to override its bind() method to use glBindTexture()… but it’s always nice to figure out what I’m doing wrong in the first place.

Thanks in advance for any insight.