2011-12-17 55 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