LWJGL - color4f alpha not working

I've got a display list in which I want to draw a transparent quad (snake element). I've already enabled GL_BLEND and use glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) as rendering. But as I enable GL_BLEND I don't see my quad anymore, even when the alpha is set to 1.

My render method:

        for(SnakeElement e : elements)
            e.render();

        c.render();


        if(!walls) {
            glColor3f(1, 1, 1);
            glBegin(GL_QUADS);
                glVertex2f(0, elements.get(0).getPos().getY() + 7);
                glVertex2f(2, elements.get(0).getPos().getY() + 7);
                glVertex2f(2, elements.get(0).getPos().getY() + 12);
                glVertex2f(0, elements.get(0).getPos().getY() + 12);

                glVertex2f(Display.getWidth(), elements.get(0).getPos().getY() + 7);
                glVertex2f(Display.getWidth() - 2, elements.get(0).getPos().getY() + 7);
                glVertex2f(Display.getWidth() - 2, elements.get(0).getPos().getY() + 12);
                glVertex2f(Display.getWidth(), elements.get(0).getPos().getY() + 12);

                glVertex2f(elements.get(0).getPos().getX() + 7, 0);
                glVertex2f(elements.get(0).getPos().getX() + 12, 0);
                glVertex2f(elements.get(0).getPos().getX() + 12, 2);
                glVertex2f(elements.get(0).getPos().getX() + 7, 2);

                glVertex2f(elements.get(0).getPos().getX() + 7, Display.getHeight());
                glVertex2f(elements.get(0).getPos().getX() + 12, Display.getHeight());
                glVertex2f(elements.get(0).getPos().getX() + 12, Display.getHeight() - 2);
                glVertex2f(elements.get(0).getPos().getX() + 7, Display.getHeight() - 2);
            glEnd();
        }

        // render hud
        glEnable(GL_BLEND);
        TextureImpl.bindNone();
        Font.getFont().drawString(10, Display.getHeight() - 40, lang.words.get(10) + ": " + score);
        glDisable(GL_BLEND);

        if(menuState == MenuState.BACK)
            back.render();
        else if(menuState == MenuState.QUIT2)
            quit2.render();

And the SnakeElement render method:

    glEnable(GL_BLEND);
    glColor4f(1, 1, 1, 0.5f);
    glBegin(GL_QUADS);
        glVertex2f(leftTop.getX(), leftTop.getY());
        glVertex2f(leftBottom.getX(), leftBottom.getY());
        glVertex2f(rightBottom.getX(), rightBottom.getY());
        glVertex2f(rightTop.getX(), rightTop.getY());
    glEnd();
    glDisable(GL_BLEND);

And here is the problem. I don't see this quad after enabling GL_BLEND. When I uncomment the glEnable(GL_BLEND) it works, but obviously not with an alpha channel.

My init (if needed):

    glViewport(0, 0, Display.getWidth(), Display.getHeight());
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);

    glDisable(GL_DEPTH_TEST);
    glClearColor(0, 0, 0, 0);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Thanks for your help!

Answers


It may be something to do with your glClearColor command.

You set the alpha to 0 so effectively don't even clear the screen

Try changing it to

glClearColor(0, 0, 0, 1);

Need Your Help

How to maximize non-analytic function over space of variables

python search optimization

I have a very simple question that I thought there might be a solution online but I couldn't find one yet.

MIPS Floating Point Multiplication

assembly floating-point mips floating-point-precision

I am new to programming in MIPS and am having trouble understanding how I can multiply two floats after reading them in from the user. How do I get the argument into a single precision floating point