2015-04-30 39 views
-1

我正在使用LWJGL和slick_util在Java中進行遊戲,最近我試圖實現一個健身欄,它將懸停在玩家頭頂上。問題是,它沒有正確定位。健康欄的左下角(從OpenGL開始繪製的地方)總是出現在玩家矩形的右上角,當玩家以x或y或兩者移動時,健康欄會以相同方向遠離玩家。我認爲這可能是glTranslatef的一個問題,或者是我錯過了一些愚蠢的東西。LWJGL播放器運行狀況欄未正確定位

渲染播放器的方法:

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); 
    } 
} 

回答

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()時,矩陣將被丟棄,並且堆棧返回到之前的狀態。

+0

:D像魅力一樣工作。儘管它有效,但我仍然想要理解一件事。當我從我的Game類調用渲染方法時,它貫穿每個GameObject並更新並渲染它,我將glPushMatrix和glPopMatrix放在那裏,並在它們之間渲染對特定GameObject的調用。通過在Draw方法中添加額外的Push和Pops,我是否將一個新的矩陣添加到已經存在的父矩陣中?或者我只是在主矩陣中添加一個新元素?在此先感謝 - Rex – user3690770

+0

基本上,glPushMatrix()將OpenGL矩陣的當前狀態保存到堆棧的頂部。當前矩陣不會受到影響,除非調用glLoadIdentity()會在最後一次調用glPushMatrix()時將矩陣恢復到狀態。任何轉換都應用於該矩陣。當調用glPopMatrix()時,矩陣會從堆棧頂部「彈出」,併成爲當前矩陣。該矩陣然後恢復到上次調用glPopMatrix()時的狀態。對glPopMatrix()的額外調用會再增加一個級別。 – Vincent