简体   繁体   中英

More problems with Slick, LWJGL textures

So I have successfully imported textures into my game, but they are upside down, distorted and also off-centre.

The code can also be found here: http://pastebin.com/nvWMZqpX

If anyone could help it would be greatly appreciated.

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

    }
}

Is your texture a power of 2 (POT) image? (for example 512x512 pixels). This could be the cause of the distortion.

Also try inverting your texcoord arguments. This would fix the upside down part. I might also noticed a wrong argument

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

Texture coordinates in openGL are handled upside down like this:

纹理坐标
(source: learnopengles.com )

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM