2014-07-09 65 views
1

經過兩個多小時(是的,一個弗裏芬很長時間!)試圖調試此代碼,我似乎有一個線條渲染文本,似乎弄髒了它後面的一切(或東西)。UnicodeFont渲染似乎阻止渲染的一切嗎?

我試圖在該方法上繪製的文本工作正常,但無論出於何種原因,似乎沒有其他渲染。

我用if (true) return;行做了兩個實驗。

第一個實驗是這樣的:

​​

,第二個是這樣的:

public static void render() { 
    //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity(); 

    glDisableClientState(GL_VERTEX_ARRAY); 
    glDisableClientState(GL_NORMAL_ARRAY); 
    glBindBuffer(GL_ARRAY_BUFFER, 0); 
    glMatrixMode(GL_PROJECTION); 
    glLoadMatrix(orthographicProjectionMatrix); 
    glMatrixMode(GL_MODELVIEW); 
    glPushMatrix(); 
    glLoadIdentity(); 
    glDisable(GL_LIGHTING); 

    width = Display.getWidth()/2; 
    height = Display.getHeight()/2; 
    int ch = Display.getHeight()/10; 
    // int locationY = 100; 
    //if (true) return; 
    //if (true) return; 



     String msg = loadMessage; 

     if (msg.contains("RED")){ 
      msg = msg.replace("RED", ""); 
      int offset = font.getWidth(msg)/2; 
      if (true) return; 
      font.drawString(Display.getWidth()/2 - offset, Display.getHeight()/2, msg, Color.red); 
     }else if (msg.contains("YELLOW")){ 
      msg = msg.replace("YELLOW", ""); 
      int offset = font.getWidth(msg)/2; 
      if (true) return; 
      font.drawString(Display.getWidth()/2 - offset, Display.getHeight()/2, msg, Color.yellow); 

     }else{ 
      int offset = font.getWidth(msg)/2; 
      if (true) return; 
     font.drawString(Display.getWidth()/2 - offset, Display.getHeight()/2, msg, Color.magenta); 

     } 


    glEnableClientState(GL_VERTEX_ARRAY); 
    glEnableClientState(GL_NORMAL_ARRAY); 

    glEnable(GL_LIGHTING); 
    glPopMatrix(); 
    glMatrixMode(GL_PROJECTION); 
    glLoadMatrix(perspectiveProjectionMatrix); 
    glMatrixMode(GL_MODELVIEW); 

} 

有了第一個,沒什麼渲染(編輯:現在文本渲染,但是仍然只是藍色背後)。

隨着第二個,除了這個特定的方法渲染的文本以外的所有東西都完美地工作。

那麼,有什麼區別?我無法理解它。而我無法修復它。

試驗1的結果: enter image description here 試驗2的結果: enter image description here

其他相關的代碼:

static void setUpFonts() { 
    java.awt.Font awtFont = new java.awt.Font("Times New Roman", java.awt.Font.BOLD, 18); 
    font = new UnicodeFont(awtFont); 
    font.getEffects().add(new ColorEffect(java.awt.Color.white)); 
    font.addAsciiGlyphs(); 
    try { 
     font.loadGlyphs(); 
    } catch (SlickException e) { 
     e.printStackTrace(); 
     cleanUp(); 
    } 
} 

調用方法:

ready2D(); 
    TextDemo.setUpCamera(); 
    TextDemo.render(); 
    camSetup(); 
    ready3D(); 

調用方法的變化(建議在評論中)沒有任何區別:

ready2D(); 
    glPushMatrix(); 
    TextDemo.setUpCamera(); 
    TextDemo.render(); 
    glPopMatrix(); 
    ready3D(); 

準備推出2D:

static void ready2D() 
{ 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

    GLU.gluOrtho2D(0.0f, 1, 1, 0.0f); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glTranslatef(0.375f, 0.375f, 0.0f); 

    glDisable(GL_DEPTH_TEST); 
} 

就緒3D:

static void ready3D() 
{ 
    glViewport(0, 0, Display.getWidth(),Display.getHeight()); 
    glMatrixMode(GL_PROJECTION); 

    glLoadIdentity(); 
    GLU.gluPerspective(45, (float) Display.getWidth()/Display.getHeight(), 0.1f, 5000.0f); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    glDepthFunc(GL_LEQUAL); 
    glEnable(GL_DEPTH_TEST); 
} 

Ready2D和Ready3D已知的工作,因爲我使用它們進一步下跌的代碼來實現的健康吧以上截圖中的一個沒有問題。

如果需要更多信息,請告訴我。我希望我已經足夠了。

+0

什麼是TerrainDemo.reg? – immibis

+0

@immibis - 已編輯。 – Joehot200

+2

那麼'if(true)return;如果(TerrainDemo.reg)總是返回,但是if(TerrainDemo.reg){if(true)return;'如果TerrainDemo.reg'爲false將不會返回。問題出現在'if'語句之後,當函數返回時會跳過。 – immibis

回答

1

以下呼叫:

glLoadIdentity(); 

    glDisableClientState(GL_VERTEX_ARRAY); 
    glDisableClientState(GL_NORMAL_ARRAY); 
    glBindBuffer(GL_ARRAY_BUFFER, 0); 
    glMatrixMode(GL_PROJECTION); 
    glLoadMatrix(orthographicProjectionMatrix); 
    glMatrixMode(GL_MODELVIEW); 
    glPushMatrix(); 
    glLoadIdentity(); 
    glDisable(GL_LIGHTING); 

似乎是影響我的3D繪圖矩陣。

爲了解決這個問題,我使用了以前從未用過的東西,glPushAttrib()glPopAttrib()

下面的代碼解決了該問題:

glPushAttrib(GL_ENABLE_BIT); 
    glLoadIdentity(); 

    glDisableClientState(GL_VERTEX_ARRAY); 
    glDisableClientState(GL_NORMAL_ARRAY); 
    glBindBuffer(GL_ARRAY_BUFFER, 0); 
    glMatrixMode(GL_PROJECTION); 
    glLoadMatrix(orthographicProjectionMatrix); 
    glMatrixMode(GL_MODELVIEW); 
    glPushMatrix(); 
    glLoadIdentity(); 
    glDisable(GL_LIGHTING); 

    width = Display.getWidth()/2; 
    height = Display.getHeight()/2; 
    int ch = Display.getHeight()/10; 
    // int locationY = 100; 
    //if (true) return; 
    //if (true) return; 

    glDisable(GL_TEXTURE_2D); 

     String msg = loadMessage; 

     if (false){ //If statements are here, but are irrelevant to fixing the issue 
     }else{ 
      int offset = font.getWidth(msg)/2; 
     font.drawString(Display.getWidth()/2 - offset, Display.getHeight()/2, msg, Color.magenta); 
     glPopMatrix(); 
     glPopAttrib(); 
     System.out.println("Error: " + glGetError()); 
     glGetError(); 
     if (true) return; 
     } 

謝謝大家誰幫我調試我的問題!你的幫助是無價的。

+0

哦,太棒了,我只能在發佈後10小時內自我接受我的回答。偉大的社區,但在這種情況下,系統本身非常糟糕。 – Joehot200

1

我們有

if (TerrainDemo.reg){ 
    if (true) return; 

static boolean reg = false; 

由於TerrainDemo.reg是假的,語句if (true) return不執行(除非你改變TerrainDemo.reg的價值,很明顯)。

因爲你的代碼,如果render()幾乎立即返回的方法,那麼就必須在該方法的OpenGL調用,防止現場的其餘部分從正常呈現,而不是一個邏輯錯誤的錯誤工作。使用glGetError()來識別錯誤。

+0

return;在我的IDE中引發錯誤。如果(真)返回;纔不是。 我會研究你的GLGetError事情。 – Joehot200

+0

您的IDE很奇怪。關於glGetError,包含GLUT並使用gluErrorString():'gluErrorString(glGetError())' –

+0

他的IDE只檢測不可達代碼。這並不奇怪。 – Dawnkeeper