-1

I have an issue regarding LWJGL and Slick

If I import a texture using Slick while having alpha blending enabled, the display will stop rendering!

Here is the code. Upon start it will simply render a purple square. Once the button T is pressed it will simply import the texture. Although i never actually bind the texture, the screen still turns black!

Here is my code:

import static org.lwjgl.opengl.GL11.GL_BLEND;

import java.io.IOException;

import org.lwjgl.input.Keyboard;
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 Game {

public static void main(String[] args) throws Exception {
    Display.setDisplayMode(new DisplayMode(800, 600));
    Display.create();
    Game game = new Game();
    while (!Display.isCloseRequested()) {
        game.update();
    }
    game.close();
}

public Game() {
    initGL();

}

Texture boxTexture;

public void update() {
    clearGL();

    // Use the texture!
    GL11.glColor3f(0.4f, 0.2f, 0.9f);
    GL11.glBegin(GL11.GL_QUADS);

    GL11.glVertex2f(100, 100);

    GL11.glVertex2f(140, 100);

    GL11.glVertex2f(140, 140);

    GL11.glVertex2f(100, 140);
    GL11.glEnd();

    if(Keyboard.isKeyDown(Keyboard.KEY_T))
    {
        try {
            boxTexture = TextureLoader.getTexture("png",
                    ResourceLoader.getResourceAsStream("res/ps.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    Display.update();
}

public void close() {
    Display.destroy();
    System.exit(0);
}

public void initGL() {
    // Enable Alpha Blending
     GL11.glEnable(GL_BLEND);
     GL11.glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE_MINUS_SRC_ALPHA); 

    // Opengl init
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, 500, 0, 500, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

    // Enable textures
    GL11.glEnable(GL11.GL_TEXTURE_2D);
}

public void clearGL() {
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    GL11.glLoadIdentity();
}
}
LW001
  • 2,452
  • 6
  • 27
  • 36
  • If you never bind a texture, then you should not enable `GL_TEXTURE_2D` that is going to produce undefined results. If you're seeing a black screen that's probably because the color `(0.0, 0.0, 0.0, 1.0)` is being assigned to everything. – Andon M. Coleman Dec 19 '14 at 22:44

1 Answers1

0

TextureLoader.getTexture() binds the texture. If you look at the Slick source code, you will find the following line in the getTexture() method of the InternalTextureLoader class:

GL.glBindTexture(target, textureID);

It pretty much has to bind the texture, since it needs to call functions like glTexImage2D() to load the texture.

So you will actually use texturing after calling this function, since you now have a texture bound, and you enabled texturing in your own code:

GL11.glEnable(GL11.GL_TEXTURE_2D);

To see the texture image, you'll need to perform additional steps, like specify texture coordinates.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133