2

我目前正在尝试处理我的 Java 项目,其中包括 LWJGL。到目前为止,我的“游戏”已经到了我现在在屏幕上绘制图像的地步。

我遇到的问题是绘制的图像是用黑框绘制的,而且它的大小不正确。这是它的外观图像。

这是实际的红色正方形图像的样子:

这是我用于使用 OpenGL 进行渲染的代码,我无法弄清楚我做错了什么。

public class Renderer {

//Integers used for player cordinates, Taken from player class by using Static variables
int playerX;
int playerY;

SpriteSheetLoader spriteLoader;
Texture player;

public Renderer(){

}

public void initRenderer(){
    //Initialize OpenGL
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity(); // Resets any previous projection matrices
    GL11.glOrtho(0, 800, 600, 0, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

            try {
    player = TextureLoader.getTexture("PNG",ResourceLoader.getResourceAsStream("res/PaddleTemp.png"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public void update(){
    GL11.glClear( GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT );
    playerX = Player.playerX; //Gets player x and y from the player class using static variables
    playerY = Player.playerY;

    player.bind();

    GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0, 0);  //top left
        GL11.glVertex2f(playerX, playerY);

        GL11.glTexCoord2f(1,0);    //Top right
        GL11.glVertex2f(playerX + 50, playerY);

        GL11.glTexCoord2f(1, 1); //Bottom right
        GL11.glVertex2f(playerX + 50, playerY + 150);

        GL11.glTexCoord2f(0, 1); //bottom left
        GL11.glVertex2f(playerX, playerY + 150);
    GL11.glEnd();
}
4

2 回答 2

1

抱歉,我不确定是什么问题。不幸的是,我之后似乎无法重现它。但是我认为它应该通过以下方法之一解决:

  • 不支持的图像尺寸

OpenGL relies heavily on so images, whose resolution's width and height are powers of 2 (when width/height=n*2). If the images files aren't up to that specification, LWJGL might act oddly. Also, don't worry about the image files being squished. That's dependent on the vertex input, not on the texture input.

  • Unsupported Image Extension

Try saving your image files as non-interlaced PNG files. Slick_Util, or whatever you use for loading the image files, might not fully support the images you're giving to it.

  • Correction Hints

As a last resort, you could add the following lines of code to your initialization code:

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

Hopefully one of these tips helps you.

Source: LWJGL not rendering textures correctly?

于 2013-04-20T00:19:35.133 回答
1

I have a really easy fix for you. Your main problem is that you are using an unsupported image size, but OpenGL is good when it just expands the actual size of the sprite to be correct. It also records the exact float value for the width and height of the image so that you can in fact use images that are any size you want. Just replace your current rendering code with this:

GL11.glBegin(GL11.GL_QUADS);
    GL11.glTexCoord2f(0, 0);  //top left
    GL11.glVertex2f(playerX, playerY);

    GL11.glTexCoord2f(player.getWidth(),0);    //Top right
    GL11.glVertex2f(playerX + 50, playerY);

    GL11.glTexCoord2f(player.getWidth(), player.getHeight()); //Bottom right
    GL11.glVertex2f(playerX + 50, playerY + 150);

    GL11.glTexCoord2f(0, player.getHeight()); //bottom left
    GL11.glVertex2f(playerX, playerY + 150);
GL11.glEnd();

Also, you can disregard the other answer as this one should be your solution.

于 2013-04-23T17:37:06.887 回答