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