javalwjglslick2d

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

    }
}

Solution

  • 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:

    Texture coordinates
    (source: learnopengles.com)