0

我有以下代码:

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

4

1 回答 1

0

那是因为你错过了glTexCoord's。

您需要在所有调用glTexCoord之前添加 's ,这需要纹理上的两个参数&位置。glVertexglTexCoordUV

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

如果您希望它们恢复“模糊”,您将执行以下操作。

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);
于 2013-08-18T14:35:48.120 回答