LWJGL Player 健康栏定位不正确
LWJGL Player Health bar not positioning properly
我正在 Java 使用 LWJGL 制作游戏,最近 slick_util 我正在尝试实现一个将悬停在玩家头顶的健康栏。问题是,它没有正确定位。健康条的左下角(从 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
}
画 class:
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);
}
}
我认为你的问题出在矩阵上。当您调用 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() 时,矩阵被丢弃,堆栈返回到它之前的状态。
我正在 Java 使用 LWJGL 制作游戏,最近 slick_util 我正在尝试实现一个将悬停在玩家头顶的健康栏。问题是,它没有正确定位。健康条的左下角(从 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
}
画 class:
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);
}
}
我认为你的问题出在矩阵上。当您调用 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() 时,矩阵被丢弃,堆栈返回到它之前的状态。