2011-12-17 216 views
3

我想在Java中使用OpenGL(LWJGL)複製MineCraft。我面臨的問題是,我的二維覆蓋物(瞄準中間,菜單等...)的一切都是白色的。遊戲的3D部分效果很好:每個立方體的每邊都有紋理。如我所說,當我試圖繪製覆蓋圖時,每個紋理都是白色的,但是我可以看到它的形狀(因爲它具有透明區域)。我會添加一張照片。OpenGL:2D覆蓋白色的3D場景

enter image description here (這應該是庫存)

正如你所看到的,覆蓋完全是白色。它應該是這樣的:

enter image description here

我已經在尋找小時在網上。似乎無法找到解決方案。
這讓我瘋狂......我已經在尋找如何在3D場景上創建2D疊加的說明,但它們也沒有幫助。所以我雖然,我會給StackOverflow一個嘗試。

希望有人能幫助我? 感謝您閱讀我的問題和(希望來的)答案!

的Martijn

下面是代碼:

正在初始化的OpenGL

public void initOpenGL() throws IOException 
{ 
    // init OpenGL 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrtho(0, 800, 600, 0, 1, 300); 
    glMatrixMode(GL_MODELVIEW); 

    float color = 0.9f; 

    glClearColor(color, color, color, color); 
    glEnable(GL_TEXTURE_2D); 
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 

    glShadeModel(GL_FLAT); 
    glEnable(GL_DEPTH_TEST); 
    glDepthFunc(GL_LEQUAL); 

    glEnable(GL_LINE_SMOOTH); 
    glEnable(GL_CULL_FACE); 


    glEnable(GL_FOG); 
    glFog(GL_FOG_COLOR, MineCraft.wrapDirect(color, color, color, 1.0f)); 
    glFogi(GL_FOG_MODE, GL_LINEAR); 
    glFogf(GL_FOG_START, _configuration.getViewingDistance() * 0.8f); 
    glFogf(GL_FOG_END, _configuration.getViewingDistance()); 
    glFogi(NVFogDistance.GL_FOG_DISTANCE_MODE_NV, NVFogDistance.GL_EYE_RADIAL_NV); 
    glHint(GL_FOG_HINT, GL_NICEST); 

    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
} 

配置矩陣用於繪製覆蓋(出靈感,我從字面上複製所有的OpenGL調用對於來自BlockMania(另一個開源MineCraft副本)的這種方法,其效果很好)

public void renderOverlay() 
{ 
    glMatrixMode(GL_PROJECTION); 
    glPushMatrix(); 
    glLoadIdentity(); 
    GLU.gluOrtho2D(0, conf.getWidth(), conf.getHeight(), 0); 
    glMatrixMode(GL_MODELVIEW); 
    glEnable(GL_COLOR_MATERIAL); 
    glPushMatrix(); 
    glLoadIdentity(); 

    glDisable(GL_CULL_FACE); 
    glDisable(GL_DEPTH_TEST); 
    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

    /** RENDER **/ 
    if (_activatedInventory != null) 
    { 
     _activatedInventory.renderInventory(); 
    } 

    glDisable(GL_BLEND); 
    glEnable(GL_DEPTH_TEST); 
    glEnable(GL_CULL_FACE); 

    glPopMatrix(); 
    glMatrixMode(GL_PROJECTION); 
    glPopMatrix(); 
    glMatrixMode(GL_MODELVIEW); 
} 

繪製本身的質感:

public void renderInventory() 
{ 
    Configuration conf = Game.getInstance().getConfiguration(); 

    glTranslatef(conf.getWidth()/2.0f, conf.getHeight()/2.0f, 0.0f); 

    glEnable(GL_TEXTURE_2D); 
    Texture tex = TextureStorage.getTexture("gui.inventory"); 
    tex.bind(); // newdawn.slick (same library for my whole program, so this works) 

    float hw = 170; // half width 
    float hh = 163; // half height 

    Vector2f _texPosUpLeft = new Vector2f(3, 0); 
    Vector2f _texPosDownRight = new Vector2f(_texPosUpLeft.x + hw, _texPosUpLeft.y + hh); 

    _texPosUpLeft.x /= tex.getTextureWidth(); 
    _texPosUpLeft.y /= tex.getTextureHeight(); 
    _texPosDownRight.x /= tex.getTextureWidth(); 
    _texPosDownRight.y /= tex.getTextureHeight(); 

    glColor3f(1, 1, 1); // Changes this doesn't make any effect 
    glBegin(GL_QUADS); 
    glTexCoord2f(_texPosUpLeft.x, _texPosUpLeft.y); 
    glVertex2f(-hw, -hh); 
    glTexCoord2f(_texPosDownRight.x, _texPosUpLeft.y); 
    glVertex2f(hw, -hh); 
    glTexCoord2f(_texPosDownRight.x, _texPosDownRight.y); 
    glVertex2f(hw, hh); 
    glTexCoord2f(_texPosUpLeft.x, _texPosDownRight.y); 
    glVertex2f(-hw, hh); 
    glEnd(); 
} 

(我使用的紋理包是CUBISM1.00)

回答

3

我發現它!

這是霧。出於某種原因,它看起來像是認爲疊加層不在視野之內,並且賦予它霧的顏色。因此,在渲染覆蓋層之前禁用霧化來解決它。

glDisable(GL_FOG); 

/* Render overlay here */ 

glEnable(GL_FOG); 

如果仍然有人讀這個,這是由矩陣濫用引起的還是這種行爲是正常的?

+0

你是天才一如既往。 – YumYumYum