简体   繁体   English

使用LWJGL难以绘制背景精灵

[英]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. 为此,我认为我将只创建一个简单的Quad并首先绘制它,以使其覆盖我的游戏背景。 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. 我正在使用LWJGL和添加的slick-util库来加载纹理。

    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. 这行代码基本上使用我使用slick-util编写的类来获取我的背景纹理。 I then bind the texture to a quad and draw it using glBegin() and glEnd() like this: 然后,我将纹理绑定到一个四边形,并使用glBegin()glEnd()绘制它,如下所示:

    // 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 它将其绘制在屏幕中间,如下所示: 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() . 将精灵放置在不同的时间间隔或弄乱glTexCoord2d()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. 如果尚未创建自己的正交投影(即使用glOrtho() IE),则顶点坐标将需要介于-1到+1之间。 Right now you're only drawing on the left half of that projection, thus giving you this result. 现在,您仅在该投影的左半部分上绘图,因此可以得到此结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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