2

I tried setting the background color to a transparent one using the functions - glClearColor() and glClear(). But, the alpha values passed to glClearColor() simply doesn't change anything.

Here is the code I tried running:

 #include<GL/glut.h>

    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0.0,(float)glutGet(GLUT_WINDOW_WIDTH),0.0,(float)glutGet(GLUT_WINDOW_HEIGHT));

        glBegin(GL_LINES);
            glVertex2i(200,200);
            glVertex2i(300,305);
        glEnd();

        glFlush();
    }

    int main(int argc, char *argv[const])
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA);
        glutInitWindowSize(1100,620);
        glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH)-1100)/2,(glutGet(GLUT_SCREEN_HEIGHT)-620)/2);
        glutCreateWindow("GLUT Programming");
        glClearColor(0.0f,0.0f,0.0f,0.5f);   // I have tried experimenting with this part, but, nothing happens
        glutDisplayFunc(display);
        glutMainLoop();
    }

I am using freeglut and freeglut-devel on my machine running Fedora 26, if it helps.

EDIT :

Result I am getting :

The background color is black and fully opaque

Result I am trying to obtain :

The background is transparent and the window underneath is visible

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Enqueue
  • 63
  • 1
  • 10
  • 1
    I have never seen it working. There is a good chance your glut implementation does not dot it, this tends not to be a common use case. You may want to look into this: https://gist.github.com/je-so/903479/834dfd78705b16ec5f7bbd10925980ace4049e17 That does it directly through xlib. – rioki Feb 08 '18 at 00:30
  • 1
    What are you seeing? What do you expect to see? (This is the case where posting images is appropriate) – n. m. could be an AI Feb 08 '18 at 06:19
  • 1
    Which result are you expecting? A background with an alpha value != 1 will only make a difference when you have blending enabled and use a blend function that requires a destination alpha value. Or are you trying to make the window transparent? – BDL Feb 08 '18 at 08:54
  • @rioki The code provided in the link does work. But, is it not possible to do that using solely OpenGL? Also, just out of curiosity, how can the window be made completely transparent, including the title bar? (There is an implementation for windows, but, for linux? ) – Enqueue Feb 10 '18 at 09:54
  • @n.m. Included pictures. – Enqueue Feb 10 '18 at 09:57
  • @BDL Even with blending enabled, the opacity of the background doesn't change. – Enqueue Feb 10 '18 at 09:58
  • Having a transparent window is not the same as clearing with a transparent color. Actually, OpenGL can't make a window transparent. There is a good answer that explains the problem in this [question](https://stackoverflow.com/questions/4052940/how-to-make-an-opengl-rendering-context-with-transparent-background). The question is about windows, but somewhere down is an answer showing a solution for x11. – BDL Feb 10 '18 at 10:11
  • What does the alpha value(fourth parameter) in glClearColor() do, then? – Enqueue Feb 10 '18 at 10:33
  • The most probable reason is that you need to work with a 32-bit visual, and the default visual chosen by GLUT is 24 bit. Freeglut doesn't seemto support selection of visual. Look at [this program](https://gist.github.com/je-so/903479/834dfd78705b16ec5f7bbd10925980ace4049e17) that uses Xlib to do the X11 stuff and creates a transparent window. – n. m. could be an AI Feb 10 '18 at 23:00
  • @Enqueue The alpha component may not do much for a window, but do also clear custom frame buffers and they may have relevant alpha channels. A RBGA texture may be attached to the buffer, which is then used in further rendering passes. – rioki Feb 15 '18 at 20:03

1 Answers1

1

If you want to enable Blending, then you have to enable blending (glEnable( GL_BLEND )) and you have to set the blend function (glBlendFunc).
Further you have to set the alpha channel of the color, which you use to draw the geometry (glColor4f)

Change your code somehow like this:

glClearColor( 0.5f, 0.5f, 0.5f, 1.0f );              // background color
glClear(GL_COLOR_BUFFER_BIT);                        // clear background with background color

glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); // = color * alpha + background * (1-alpha)
glColor4f( 1.0f, 0.0f, 0.0f, 0.1f );                 // color of the line, alpha channel 0.1 (very "transparent")
glLineWidth( 5.0 );

glBegin(GL_LINES);
.....
BDL
  • 21,052
  • 22
  • 49
  • 55
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • The modified code renders the lines transparent. I want to do the same to the background. Changing the alpha value in the first line of your code doesn't make any difference. I want to use it to render a transparent background. – Enqueue Feb 10 '18 at 10:00
  • @Enqueue Ok, I understnad. But that won't work like that. See the comments to your question. – Rabbid76 Feb 10 '18 at 10:35