Louis Hong
Louis Hong

Reputation: 1070

Can't draw 1280x800 texture in LWJGL

My image is 1280x800, and my screen size is 1280x800. it should draw perfectly. But it right now it draws a square texture on my upper left corner of my screen that's out of scale and not large enough. I don't know why it doesn't work, I've tried writing the size of the texture larger when drawing and it seems to work sortof, but I don't know why it didn't work when drawing on a 1280x800 quad.

enter image description here

This is my OPENGL and display setup code:

    try {

        DisplayMode displayMode = null;
        DisplayMode[] modes = Display.getAvailableDisplayModes();

        for (int i = 0; i < modes.length; i++) {
            if (modes[i].getWidth() == World.SCREEN_WIDTH && modes[i].getHeight() == World.SCREEN_HEIGHT && modes[i].isFullscreenCapable()) {
                displayMode = modes[i];
            }
        }

        Display.setDisplayMode(displayMode);
        Display.setFullscreen(false);
        Display.create();

    } catch (LWJGLException e) {
        e.printStackTrace();
    }

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, World.SCREEN_WIDTH, World.SCREEN_HEIGHT, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_TEXTURE_2D);

This is my code for drawing the textures:

    background = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/menu/background.png")));
    girl_01 = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/menu/girl_01.png")));
    girl_02 = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/menu/girl_02.png")));

    glBindTexture(GL_TEXTURE_2D, background.getTextureID());
    glPushMatrix();
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex2f(0, 0); // Upper left

    glTexCoord2f(1, 0);
    glVertex2f(World.SCREEN_WIDTH, 0); // Upper right

    glTexCoord2f(1, 1);
    glVertex2f(World.SCREEN_WIDTH, World.SCREEN_HEIGHT); // Lower right

    glTexCoord2f(0, 1);
    glVertex2f(0, World.SCREEN_HEIGHT); // Lower left
    glEnd();
    glPopMatrix();

Edit : After searching for hours, I've discovered, I've arrived at a weird solution in my opinion, I made the image 2048x1024 and left the extra blank. it worked. BUT this is how it should work? So I'm suppose to make all image size powers of two leaving the extra space blank? Or is there a better way?

Upvotes: 0

Views: 221

Answers (1)

Andon M. Coleman
Andon M. Coleman

Reputation: 43339

There's really not enough information to go by here, but I suspect you have a very old implementation of OpenGL (pre-2.0).

Older versions of OpenGL are incapable of using non-power-of-two textures. You either have to rescale (stretch to fit) the image before loading or leave part of the image blank after resizing and modify your texture coordinates.

If you have a legitimate OpenGL 2.0 implementation it will support non-power-of-two textures. Back in the era of OpenGL 2.0, driver vendors were not conforming to the REQUIREMENTS very well and would often claim to implement an OpenGL version whether or not the host hardware actually met the ALL of the requirements. Non-power-of-two textures were probably the biggest violation, lots of hardware of the era could handle everything required for OpenGL 2.0 except for NPOT textures.

In any case, if you are on a platform where OpenGL 2.0 is not guaranteed, you should check for the extension GL_ARB_texture_non_power_of_two before using a npot texture.

Upvotes: 1

Related Questions