LWJGL纹理不起作用

时间:2013-08-18 14:29:08

标签: java opengl lwjgl

我有以下代码:

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();
    }
}

和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);    
    }
}

但如果我启动脚本,我只会看到一个黑色的窗口:(。 我不知道该怎么做。 我包括的包裹如下:

-lwjgl.jar
-lwjgl_util.jar
-slick-util.jar

有什么想法吗? 谢谢:D

1 个答案:

答案 0 :(得分:0)

那是因为你错过了glTexCoord

您需要在所有glTexCoord次来电之前添加glVertexglTexCoord需要两个参数U&纹理上的V位置。

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();

编辑 - 评论请求

您需要指定GL_TEXTURE_MIN_FILTER& GL_TEXTURE_MAG_FILTER

如果您想让像素看起来像纹理,您可以执行以下操作。

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

如果你想让他们回到“blury”,你会做到以下几点。

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

请记住,对于多个纹理,您需要单独调用它们,就像这样。

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