2

I am testing alpha in glColor4f, but the result of using glColor4f(1.0, 1.0, 0, 0); is same as using glColor4f(1.0, 1.0, 0, 1);.

enter image description here

I've tried changing glClearColor(0.0, 0.0, 0.0, 0.0); to glClearColor(0.0, 0.0, 0.0, 1.0);,it changes nothing.

Here's my code:

#include <GL/glut.h>

void init();
void display();

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(0, 0);
    glutInitWindowSize(300, 300);

    glutCreateWindow("OpenGL 3D View");

    init();
    glutDisplayFunc(display);

    glutMainLoop();
    return 0;
}

void init()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glOrtho(-5, 5, -5, 5, 5, 15);
    glMatrixMode(GL_MODELVIEW);
    gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor4f(1.0, 1.0, 0, 0.01);
    glutWireTeapot(3);

    glFlush();
}

I am wondering how did the transparency works. Thank you~

Po-Jen Lai
  • 411
  • 1
  • 8
  • 18

2 Answers2

1

I suppose the result of setting alpha to zero in glClearColor() you expected to be the windows with transparent background?No,it won't happen.The windows transparency is controlled by OS .See this answer.Then why glClearColor() has alpha channel clear option you would ask?That's indeed useful and sometimes critical feature when rendering between multiple offscreen frame buffers.Sometimes,when you blit (or blend)a content of one FBO into another you must clear target FBO attachment alpha as well to zero to prevent edge bleeding (actual for transparent graphics) and other nasty artifacts.But yeah,changing that value on default FBO as you do would have no effect.Hope it is clear now.

Also,you must not forget that when using such FBO texture attachments later in the pipeline, which have alpha information in them, you must use hardware alpha blending(expressed by OGL API blending functions).Otherwise the alpha info will be lost in the final result.

Community
  • 1
  • 1
Michael IV
  • 11,016
  • 12
  • 92
  • 223
1

So the first thing I see is that you aren't initializing your display to use blending. this is done with the:

glEnable(GL_BLEND);

By Default, GL blending is disabled. See the description here: http://lmb.informatik.uni-freiburg.de/people/reisert/opengl/doc/glBlendFunc.html

The next thing would be to setup your blend mode. The above link shows the routine for setting up your blend function.

Also if you look at the glutInitDisplayMode routine description here: https://www.opengl.org/resources/libraries/glut/spec3/node12.html

it shows that "Note that GLUT_RGBA selects the RGBA color model, but it does not request any bits of alpha (sometimes called an alpha buffer or destination alpha) be allocated. To request alpha, specify GLUT_ALPHA. The same applies to GLUT_LUMINANCE."

trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
  • I think he was asking about glClearColor() alpha component impact which has no effect on window background transparency by default. – Michael IV Mar 09 '14 at 10:53