[SOLVED] Problems with textures with LWJGL

Hi

I recently started game development in Java using LWJGL. Having a good knowledge of other programming languages, I had not really any trouble “switching” to Java. I’m currently recreating one of my older games in Java that I had written earlier in another language (but that is not really any part of the question). I currently have the following code…

package game;

import java.io.FileInputStream;
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;


public class TextureTest {
    
    private static Texture texture;
    
    public static void main(String[] args) {
        
        try {
            Display.setDisplayMode(new DisplayMode(640, 480));
            Display.create();
        } catch (LWJGLException ex) {
            ex.printStackTrace();
            System.exit(1);
        }
        
        // Init OpenGL
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 640, 480, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);
        glEnable(GL_TEXTURE_2D); // << I enabled textures
        
        texture = loadTexture("player");
        
        while (!(Display.isCloseRequested())) {
            
            glClear(GL_COLOR_BUFFER_BIT);
        
            Color.green.bind();
            glRectd(100, 100, 130, 130);

            /***/
            Color.white.bind();
            texture.bind();

            glLoadIdentity();

            glBegin(GL_QUADS);
                    glTexCoord2f(0, 0);
                    glVertex2i(400, 400);

                    glTexCoord2f(1, 0);
                    glVertex2i(450, 400);

                    glTexCoord2f(1, 1);
                    glVertex2i(450, 450);

                    glTexCoord2f(0, 1);
                    glVertex2i(400, 450);
            glEnd();

            glBindTexture(GL_TEXTURE_2D, 0);
            /***/



            Color.cyan.bind();
            glRectd(50, 50, 70, 70);
            
            Display.update();
            Display.sync(60);
        }
        
        Display.destroy();
        System.exit(0);
        
    }
    
    public static Texture loadTexture(String key) {

        try {
            return TextureLoader.getTexture("PNG", new FileInputStream("res/" + key + ".png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        return null;
    }
}

With this code I see only a cyan cube, and a white cube representing my player. The first frame (and the first frame only) I see my player texture. After that it’s a plain simple white square.

When I remove (comment) the line with glBindTexture(GL_TEXTURE_2D, 0), my player texture gets drawn, but only the player texture (no colored rectangles anymore). Without the player code (between the two /***/) I have 2 coloured squares (green and cyan).

Am I forgetting something necessary in OpenGL/LWJGL? Or did I forget an important key concept in Java?

I’m using the Slick-Util library to load my texture as a png file. And it has no problem loading it, as I see no stack trace. I’m using the latest stable build of LWJGL (legacy, so 2.9.3).

If I remember correclty then the vertexi coordinates should be normalized , they have to have a range of 0 - 1.

If I remember correctly, it’s the opposite… glTexCoord should indeed have values between 0 and 1 (so for the full texture (0,0), (1,0), (1,1) and (0,1)). But glVertex should be the position on the screen/window… At least that’s how I’ve seen it in a few (working!) examples. Also, the position is correct (but the texture is not drawn after the first frame).

I tried it with a different image file, and there are no real problems with that one. However, I see nothing wrong with my player.png file. It’s dimensions are a power of 2 (32x32), and the TextureLoader gives no error loading the image file.

But there are no problem with another file. I created my texture with GIMP (and the working one with Adobe PhotoShop), is this known to have problems? I’ll keep digging and see what works and what doesn’t, but this is quite a strange problem.

Working texture: (wood2)
http://updo.nl/file/6400f179.png

Not-working texture: (player)
http://updo.nl/file/6504bedd.png
Don’t laugh with my artistic talent, I’m a programmer :stuck_out_tongue:

Edit: Added links to texture files

Hes using an ortho camera… it has to be -1 to 1

Trying to add texture.enable() near texture.bind() can work… I’m new to OpenGL and I use pure JOGL so just ignore if it’s a nooby answer… ??? :stuck_out_tongue: :persecutioncomplex:

I see no such method, I’m using Slick-Util, not the full Slick library. Maybe that method exists there.
But I might try it later, but for now I remain with plain lwjgl+Slick Util.

Now I’m totally confused… I’ve slimmed down my code to this:

package examples;

import java.io.FileInputStream;
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;


public class TextureDemo {
    
    private Texture texture;
    
    public void start() {
        try {
            Display.setDisplayMode(new DisplayMode(640, 480));
            Display.setTitle("Texture demo");
            Display.create();
        } catch (LWJGLException ex) {
            ex.printStackTrace();
            return;
        }
        
        // Init OpenGL
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 640, 480, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);
        glEnable(GL_TEXTURE_2D);
        
        // texture = loadTexture("player");
        texture = loadTexture("wood2");
        
        while (!(Display.isCloseRequested())) {
            
            glClear(GL_COLOR_BUFFER_BIT);
            
            Color.red.bind();
            glRectd(150, 150, 170, 170);
            
            // No texture drawing here...
            
            Color.green.bind();
            glRectd(50, 50, 70, 70);
            
            Display.update();
            Display.sync(60);
        }
        
        Display.destroy();
    }
    
    public Texture loadTexture(String key) {
        try {
            return TextureLoader.getTexture("PNG", new FileInputStream("res/" + key + ".png"));
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

I only load the texture here (and keep it in a private variable to the object), but I don’t draw it. There is nothing wrong with texture wood2, but there is something wrong with the player texture.

When loading wood2, no troubles, when loading player… nothing gets drawn at all with the following code…

I’ve supplied links to the texture files previously here and are unchanged. It seems that something goes wrong with it. What is wrong with my player texture?

I took a look at the default texture binding code.

public void bind() {
if (lastBind != this) {
lastBind = this;
GL.glEnable(SGL.GL_TEXTURE_2D);
GL.glBindTexture(target, textureID);
}
}

This code only binds the texture if lastBind is null or another texture.

Instead of using glBindTexture with 0, try using [icode]TextureImpl.unbind();[/icode] This may fix your issue.

Otherwise, you can have a peek to see if you are doing something wrong in how textures are bound and unbound.

Then explain the problem I have, if I don’t even draw the texture, and only load it…

[quote]It seems that something goes wrong with it. What is wrong with my player texture?
[/quote]
Does the draw cycle not run? Does it not draw your shapes? Are errors thrown?

Also, I believe that if you don’t draw a texture you should

glDisable(GL_TEXTURE_2D);

Draw cycle runs, added a System.out.println(…) and it shows periodically.
It does not draw any shapes when loading the player texture, it does when loading the wooden (wood2) texture.
No errors are thrown at all.

I’ll experiment with glDisable(GL_TEXTURE_2D);

Enabling textures when drawing my player texture works, and disabling it afterwards too, so this is something I can use to continue development :slight_smile: But why does this problem only affect me when using one texture, but not the other? Quite strange, might experiment with different textures to see this, unless someone knows what’s going on.

Is it good practice to enable/disable this when needed, or is it really necessary to let it work? I’m quite a beginner with lwjgl and OpenGL.

Edit: I’ve just seen that this is a double post, my apologises.

When you are drawing non-textured shapes you have to disable texturing (as far as I know), and then you enable texturing when you need to display textures.

Okay, just checking to be sure :slight_smile:

It’s a good practice to enable textures and draw all the textures you can without disabling them, cause enabling and disabling continually can cause lag, but it’s only an optimization. Just remember it for the future. :wink:

Okay, noted :slight_smile: Thanks everyone for your aid :slight_smile: You made me a happy man, was looking for a solution for almost 24 hours :stuck_out_tongue: