Problems with textures (beginner question)

Started by Bjarnovikus, February 07, 2015, 13:50:27

Previous topic - Next topic

Bjarnovikus

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).

Kai

Have a look at slicks sources. Slick keeps its own OpenGL state of what the currently bound texture is.
If you manually unbind it via glBindTexture, slick wont be aware of it and makes the next call to Texture.bind a no-op.
You should use Texture.unbind.

Bjarnovikus

I'm using Slick-Util, not the full library. I see no unbind method in the Texture class. There is a release method, but calling this means that no texture gets drawn too.

Kai

Please have a look at what class I referenced in my link...

EDIT: And yes, sorry. I meant the non-virtual method unbind() in TextureImpl

Bjarnovikus

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...

It seems that something goes wrong with it. What is wrong with my player texture?

Links to my textures:
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 :P