LWJGL播放器健康栏未正确定位

时间:2015-04-30 10:34:46

标签: java opengl position lwjgl

我最近使用LWJGL和slick_util在Java中制作游戏我试图实现一个将悬停在玩家头上的健康栏。问题是,它没有正确定位。健康栏的左下角(OpenGL开始绘制的地方)总是出现在玩家矩形的右上角,当玩家在x或y或两者中移动时,健康栏会移动远离玩家的方向相同。我认为这可能是glTranslatef的一个问题,或者也许是我错过的傻事。

玩家的渲染方法:

protected void render() {
        Draw.rect(x, y, 32, 32, tex); //Player texture drawn
        Draw.rect(x, y + 33, 32, 15, 1, 0, 0); //Health bar drawn. x is the same as player's, but y is +33 because I want it to hover on top
    }

画班:

package rpgmain;
import static org.lwjgl.opengl.GL11.*;
import org.newdawn.slick.opengl.*;
/**
 *
 * @author Samsung
 */
public class Draw {

    public static void rect(float x, float y, float width, float height, Texture tex) {
        glEnable(GL_TEXTURE_2D);
        glTranslatef(x, y, 0);        
        glColor4f(1f, 1f, 1f, 1f);
        tex.bind();
        glBegin(GL_QUADS); //Specifies to the program where the drawing code begins. just to keep stuff neat. GL_QUADS specifies the type of shape you're going to be drawing.
        {
            //PNG format for images
            glTexCoord2f(0,1); glVertex2f(0, 0);  //Specify the vertices. 0, 0 is on BOTTOM LEFT CORNER OF SCREEN.
            glTexCoord2f(0,0); glVertex2f(0, height); //2f specifies the number of args we're taking(2) and the type (float)
            glTexCoord2f(1,0); glVertex2f(width, height); 
            glTexCoord2f(1,1); glVertex2f(width, 0);
        }
        glEnd();
        glDisable(GL_TEXTURE_2D);
    }

    public static void rect(float x, float y, float width, float height, float r, float g, float b) {
        glDisable(GL_TEXTURE_2D);
        glTranslatef(x, y, 0);
        glColor3f(r, g, b); 

        glBegin(GL_QUADS); //Specifies to the program where the drawing code begins. just to keep stuff neat. GL_QUADS specifies the type of shape you're going to be drawing.
        {                 
            glVertex2f(0, 0);  //Specify the vertices. 0, 0 is on BOTTOM LEFT CORNER OF SCREEN.
            glVertex2f(0, height); //2f specifies the number of args we're taking(2) and the type (float)
            glVertex2f(width, height);
            glVertex2f(width, 0); 
        }
        glEnd();
        glEnable(GL_TEXTURE_2D);
    }
}

1 个答案:

答案 0 :(得分:0)

我认为你的问题与矩阵有关。当您调用glTranslatef时,您正在转换OpenGL ModelView矩阵。但是,由于OpenGL是基于状态的计算机,因此将保留此转换以用于进一步的绘图事件。第二个矩形将被翻译两次。你想要做的是使用OpenGL矩阵堆栈。我将在这里重写一种绘制方法:

public static void rect(float x, float y, float width, float height, Texture tex) {
    glEnable(GL_TEXTURE_2D);
    glPushMatrix();
    glTranslatef(x, y, 0);        
    glColor4f(1f, 1f, 1f, 1f);
    tex.bind();
    glBegin(GL_QUADS);
    {
        glTexCoord2f(0,1); glVertex2f(0, 0);
        glTexCoord2f(0,0); glVertex2f(0, height);
        glTexCoord2f(1,0); glVertex2f(width, height); 
        glTexCoord2f(1,1); glVertex2f(width, 0);
    }
    glEnd();
    glPopMatrix();
    glDisable(GL_TEXTURE_2D);
}

glPushMatrix()行将在堆栈顶部添加一个新矩阵。此时的所有转换都将应用于新添加的矩阵。然后,当你调用glPopMatrix()时,矩阵被丢弃,堆栈返回到它之前的状态。