简体   繁体   中英

Difficulty drawing a background sprite using LWJGL

I'm trying to render a background image for a new game I'm creating. To do this, I thought I'd just create a simple Quad and draw it first so that it stretched over the background of my game. The problem is that the quad doesn't draw to it's correct size and draws at the complete wrong place on the screen. I am using LWJGL and an added slick-util library for loading textures.

    background = TextureHandler.getTexture("background", "png");

This is the line of code which basically gets my background texture using a class that I wrote using slick-util. I then bind the texture to a quad and draw it using glBegin() and glEnd() like this:

    // Draw the background.
    background.bind();
    glBegin(GL_QUADS);
    {
        glTexCoord2d(0.0, 0.0);
        glVertex2d(0, 0);

        glTexCoord2d(1.0, 0.0);
        glVertex2d(Game.WIDTH, 0);

        glTexCoord2d(1.0, 1.0);
        glVertex2d(Game.WIDTH, Game.HEIGHT);

        glTexCoord2d(0.0, 1.0);
        glVertex2d(0, Game.HEIGHT);
    }
    glEnd();

You'd expect this block to draw the quad so that it covered the entire screen, but it actually doesn't do this. It draws it in the middle of the screen, like so: http://imgur.com/Xw9Xs9Z

The large, multicolored sprite that takes up the larger portion of the screen is my background, but it isn't taking up the full space like I want it to.

A few things I've tried:

  • Checking, double-checking, and triple-checking to make sure that the sprite's size and the window's size are identical

  • Resizing the sprite so that it is both larger and smaller than my target size. Nothing seems to change when I do this.

  • Positioning the sprite at different intervals or messing with the parameters of the glTexCoord2d() and glVertex2d() . This is just messy, and looks unnatural.

Why won't this background sprite draw to it's correct size?

If you have not created your own orthogonal projection (IE using glOrtho() ), then your vertex coordinates will need to range from -1 to +1. Right now you're only drawing on the left half of that projection, thus giving you this result.

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