glClearColor(0, 0, 0, 0) is transparent, not black
Solution 1
GLUT does not request an alpha buffer by default, and I suspect what you're seeing might be a fail-safe, ad hoc implementation of window transparency. Try adding:
glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA);
near the other GLUT init calls. Keep this function in mind, as you'll need to modify the call again if/when you want depth or stencil buffers, or double buffering.
Solution 2
Use glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB) in display mode
Niklas R
Updated on October 06, 2020Comments
-
Niklas R over 3 years
I'm trying to use OpenGL my first time. I was looking at some code online and then tried to write my own, but I always just got an empty (transparent) window. (I used GLUT to open the window).
I thought I did something wrong, so I copyied the code from here to my C source-code, and my window is still transparent. Also, the alpha parameter for glClearColor() does not seem to have any effect. Instead, the alpha-value seems to be determined by the red, green and blue parameters.
glClearColor(0, 0, 0, 0)
glClearColor(0, 0, 0, 1)
glClearColor(1, 0, 0, 0)
glClearColor(1, 0, 0, 1)
glClearColor(1, 1, 0, 0)
glClearColor(1, 1, 0, 1)
glClearColor(1, 0, 0, 0)
glClearColor(1, 0, 0, 1)
The alpha parameter doesn't change the result.
I use Ubuntu 12.04 LTS, libgl1-mesa-dev.
Is this a bug or do I do something wrong?
-
Niklas R over 11 yearsThanks, that already had an effect. For example,
glClearColor(1, 1, 0, 1)
is now fully opaque, butglClearColor(1, 1, 0, 0)
is not, just like in the picture above. Shouldn't it be absolutely transparent when alpha=0.0? -
aib over 11 yearsYes, any
(r, g, b, 0)
should be fully transparent. Did you try other values such as .8 and .2 for alpha? I suspect they'll round down, but it's still interesting to see what happens. Also, are you absolutely sure your display mode, window manager or whatever backend supports alpha? -
Niklas R over 11 yearsWell, as
(1, 1, 0, 0)
is a little transparent and(1, 1, 0, 1)
is fully opaque yellow, setting alpha to a value in[0;1]
seems to interpolate between these. Yes I am, I can make ma Terminal transparent or half-transparent for example. I use the default window-manager that came with LTS 12.04 (uses Unity UI). -
aib over 11 yearsSo
(0, 0, 0, a)
works, and you have a window whose transparency (but not background color) you can adjust? By the way, what are those big rectangles in the background in your pictures? (The ones whose lower-right corner touch the "a" of an "and".) -
Niklas R over 11 yearsHm, I admit the pictures are a more confusing than I first thought. It's the terminal-window in the background which originally has a violett background-color. Hm no it doesn't work well, as
a=0
is not fully transparent. :) Well, I'm fine without transparency, but I wonder why(1, 1, 0, 0)
looks like(1, 1, 0, 0.66)
. Or is this just by design? -
aib over 11 yearsHmm, I think what the underlying system is doing is something along the lines of
glBlendFunc(GL_SRC_ALPHA, GL_ONE)
where "classic" transparency is(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
going from back to front. (In other words, "newer" pixels should obscure "older" pixels instead of just adding to them.) This has nothing to do with your code possibly callingglBlendFunc
- it's merely an observation. -
aib over 11 yearsI still think it must be a limitation of the underlying "window manager" (or whoever is responsible for doing the blending.) You mentioned a transparent terminal, but it may simply be setting the window's opacity - as in, a single alpha for the whole window. In OpenGL, you have an actual alpha buffer and can draw anything you want on it. Individual pixels can be fully transparent, fully opaque or somewhere in between. Have you seen such a case on your system?
-
aib over 11 yearsIf this is the case you should still be able to do OpenGL blending (e.g. flower pot seen through blue-tinted glass). You should still be able to have a wholly semi-transparent window, granted you can get a handle (pun intended) on the GLUT-created main window (e.g. the terminal can be seen through your scene, though equally so through the wall as through the glass and the pot).
-
Gaurava Agarwal over 7 yearsPlease explain your answer to make your views clear to readers.