Slick,LWJGL纹理有更多问题

时间:2013-12-04 01:36:50

标签: java lwjgl slick

所以我已成功将纹理导入到我的游戏中,但它们是颠倒的,扭曲的,也是偏离中心的。

代码也可以在这里找到:http://pastebin.com/nvWMZqpX

如果有人能提供帮助,我们将不胜感激。

public class Main {

    private Texture sky;

    public Main() {

        try {
            Display.setDisplayMode(new DisplayMode(640, 480));
            Display.setTitle("Spark");
            Display.create();

            sky = TextureLoader.getTexture("PNG", new FileInputStream(new File("resources/textures/sky.png"))); 

            while(!Display.isCloseRequested()) {

                setCamera();

                sky.bind();
                glEnable(GL_TEXTURE_2D);
                glBegin(GL_QUADS);
                glColor3d(1, 1, 1);

                glTexCoord2f(1, 0);
                glVertex2i(640, 0);
                glTexCoord2f(0, 0);
                glVertex2i(0, 0);
                glTexCoord2f(0, 1);
                glVertex2i(0, 480);

                glTexCoord2f(0, 1);
                glVertex2i(0, 480);
                glTexCoord2f(1, 1);
                glVertex2i(640, 640);
                glTexCoord2f(1, 0);
                glVertex2i(640, 0);

                glEnd();
                glDisable(GL_TEXTURE_2D);

                Display.update();
                Display.sync(60);
            }
            sky.release();
            Display.destroy();
        }   catch (LWJGLException e) {
            e.printStackTrace();
        }   catch(IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Main();
    }


    public static void setCamera() {

        //Clear Screen
        glClear(GL_COLOR_BUFFER_BIT);
        //Modifying the projection matrix
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 640, 0, 480, -1, 1);
        //Modify modelviewing matrix
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

    }
}

1 个答案:

答案 0 :(得分:0)

你的纹理是2(POT)图像的力量吗? (例如512x512像素)。这可能是失真的原因。

同时尝试反转您的texcoord参数。这将修复颠倒的部分。 我可能也注意到了一个错误的论点

            glTexCoord2f(1, 1);
            glVertex2i(640, 0);
            glTexCoord2f(0, 1);
            glVertex2i(0, 0);
            glTexCoord2f(0, 0);
            glVertex2i(0, 480);

            glTexCoord2f(0, 0);
            glVertex2i(0, 480);
            glTexCoord2f(1, 0);
            glVertex2i(640, 480);  // <--- this line was 640,640?
            glTexCoord2f(1, 1);
            glVertex2i(640, 0);

openGL中的纹理坐标被颠倒处理:

Texture coordinates http://www.learnopengles.com/wordpress/wp-content/uploads/2011/09/texture-coordinates.png