I have the following code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.lwjgl.LWJGLException;
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;
public class GameManager {
static int DEFAULT_DISPLAY_WIDTH = 855;
static int DEFAULT_DISPLAY_HEIGHT = 485;
public void startGame()
{
startGame("Unnamed Game");
}
public void startGame(String gameTitle)
{
new EventGameStart().run();
try {
Display.setTitle(gameTitle);
Display.setDisplayMode(new DisplayMode(DEFAULT_DISPLAY_WIDTH,
DEFAULT_DISPLAY_HEIGHT));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
new GameLoader().load();
Texture texture = null;
try {
texture = TextureLoader.getTexture("PNG", new FileInputStream("resources/ball.png"), true);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (!Display.isCloseRequested()) {
while (Keyboard.next()) {
if (Keyboard.getEventKey() == Keyboard.KEY_F11
&& Keyboard.getEventKeyState()) {
new EventFullscreen().run();
}
if (Keyboard.getEventKey() == Keyboard.KEY_F12
&& Keyboard.getEventKeyState()) {
new EventScreenshot().run();
}
}
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(0, 0);
GL11.glVertex2f(0 + 200, 0);
GL11.glVertex2f(0 + 200, 0 + 200);
GL11.glVertex2f(0, 0 + 200);
GL11.glEnd();
Display.update();
}
new EventGameClose().run();
Display.destroy();
}
}
and the GameLoader():
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
/** Loads the game... */
public class GameLoader extends GameManager {
/** Called by GameManager */
public void load() {
setUpOpenGL();
}
/** Loading the OpenGL Settings */
private void setUpOpenGL()
{
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getWidth(), 0, Display.getHeight(), 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
}
but if I start the script, i only see a black window :(. I don't know what i should do. The packages, I have included are the following:
-lwjgl.jar
-lwjgl_util.jar
-slick-util.jar
Any idea? Thanks :D
That is because you're missing the glTexCoord
's.
You need to add glTexCoord
's before all glVertex
calls, the glTexCoord
takes two parameters the U
& V
location on the texture.
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0f, 0f);
GL11.glVertex2f(0, 0);
GL11.glTexCoord2f(1f, 0f);
GL11.glVertex2f(0 + 200, 0);
GL11.glTexCoord2f(1f, 1f);
GL11.glVertex2f(0 + 200, 0 + 200);
GL11.glTexCoord2f(0f, 1f);
GL11.glVertex2f(0, 0 + 200);
GL11.glEnd();
You need to specify the GL_TEXTURE_MIN_FILTER
& GL_TEXTURE_MAG_FILTER
.
If you want to keep the pixel look to the texture, you would do the following.
texture.bind(); // First you bind your texture
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
and if you want them back to being "blury", you would do the following.
texture.bind(); // First you bind your texture
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
Remember that for multiple textures you need to call them individually, like this.
texture1.bind();
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
texture2.bind();
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.