Martijn Courteaux
Martijn Courteaux

Reputation: 68887

OpenGL: 2D Overlay is white over of 3D Scene

I'm trying to make a copy of MineCraft in Java using OpenGL (LWJGL). The problem I'm facing is that everything of my 2D overlay (aiming cross in the middle, menus, etc...) are all white. The 3D part of the game works great: every cube has a texture on each side.

But when I try to draw the overlay, as I said, every texture is white, but I can see the shape of it (because it has transparent areas). I'll add a picture of it.

enter image description here (This is supposed to be the inventory)

As you can see, the overlay is completely white. And it should look like this:

enter image description here

I'm already searching the web for hours. Can't seem to find a solution.
This drives my crazy... I already searched for instructions of how to create a 2D overlay on a 3D scene, but they don't help either. So I though, I'll give StackOverflow a try.

Hopefully someone can help me? Thanks for reading my question and for the (hopefully coming) answers!

Martijn

Here is the code:

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

Configuring the matrixes for drawing the overlay (Out of inspiration, I literally copied all the OpenGL calls for this method from BlockMania (another open-source MineCraft copy), which works great)

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

Drawing the texture itself:

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

(The texture pack I'm using is CUBISM1.00)

Upvotes: 2

Views: 2819

Answers (1)

Martijn Courteaux
Martijn Courteaux

Reputation: 68887

I found it!!

It was the fog. For one or another reason it looks like it thinks the overlay is out of sight and gives it the color of the fog. So, disabling the fog before rendering the overlay solved it.

glDisable(GL_FOG);

/* Render overlay here */

glEnable(GL_FOG);

If there are still people who read this, is this caused by matrix abuse or is this behaviour normal?

Upvotes: 3

Related Questions