OpenGL alpha transparency
-
Until now i used the blend function glBlendFunc(GL_SRC_ALPHA,GL_ONE) to use transparency. But the problem with that is that although it blends black with the background to make it transparent it also blends all colours. That is, it adds the colour with the background colour so black becomes transparent but red blended with green(?) will give white. The point is that i don't want to blend all colours but simply make a vertex/poly transparent if i set the colour to glColor4f(red, green, blue, alpha) so that the image will have the colour (red,green,blue) but also transparent, instead of blending red, green and blue if you know what i mean. Here are the params i set on init (if it helps):
glShadeModel(GL\_SMOOTH); glClearDepth(1.0f); glEnable(GL\_DEPTH\_TEST); glBlendFunc(GL\_SRC\_ALPHA,GL\_ONE); glDepthFunc(GL\_LEQUAL); glEnable(GL\_COLOR\_MATERIAL); glHint(GL\_PERSPECTIVE\_CORRECTION\_HINT, GL\_FASTEST);
Can anyone help me please? ----------------------------------
Customer in computer shop: "Can you copy the Internet onto this disk for me?"
-
Until now i used the blend function glBlendFunc(GL_SRC_ALPHA,GL_ONE) to use transparency. But the problem with that is that although it blends black with the background to make it transparent it also blends all colours. That is, it adds the colour with the background colour so black becomes transparent but red blended with green(?) will give white. The point is that i don't want to blend all colours but simply make a vertex/poly transparent if i set the colour to glColor4f(red, green, blue, alpha) so that the image will have the colour (red,green,blue) but also transparent, instead of blending red, green and blue if you know what i mean. Here are the params i set on init (if it helps):
glShadeModel(GL\_SMOOTH); glClearDepth(1.0f); glEnable(GL\_DEPTH\_TEST); glBlendFunc(GL\_SRC\_ALPHA,GL\_ONE); glDepthFunc(GL\_LEQUAL); glEnable(GL\_COLOR\_MATERIAL); glHint(GL\_PERSPECTIVE\_CORRECTION\_HINT, GL\_FASTEST);
Can anyone help me please? ----------------------------------
Customer in computer shop: "Can you copy the Internet onto this disk for me?"
VizCoder wrote:
to use transparency.
the question is actually more complex than you think. OpenGL doesn't know how to do transparency. "Alpha blending" applies a mathematical function to the source and destination values of color from raster buffer to new data, to achieve a "similar" appearance. You can do better than this and get close to true transparency, but you must get smart on ordering of your scene and combination of blends. You've already realized that color add goes high to white which may not be the look you want. There are other blending functions, combining them with ordered triangles, you can get the look you want through design more than automation. True OpenGL transparency takes a bit of thought and work. http://www.opengl.org/resources/faq/technical/transparency.htm[^] http://www.derivativeinc.com/Tools/Touch000/Manual/Tips/011_AlphaBlending/AlphaBlending.asp[^] http://www.sjbaker.org/steve/omniv/alpha_sorting.html[^]
_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
-
VizCoder wrote:
to use transparency.
the question is actually more complex than you think. OpenGL doesn't know how to do transparency. "Alpha blending" applies a mathematical function to the source and destination values of color from raster buffer to new data, to achieve a "similar" appearance. You can do better than this and get close to true transparency, but you must get smart on ordering of your scene and combination of blends. You've already realized that color add goes high to white which may not be the look you want. There are other blending functions, combining them with ordered triangles, you can get the look you want through design more than automation. True OpenGL transparency takes a bit of thought and work. http://www.opengl.org/resources/faq/technical/transparency.htm[^] http://www.derivativeinc.com/Tools/Touch000/Manual/Tips/011_AlphaBlending/AlphaBlending.asp[^] http://www.sjbaker.org/steve/omniv/alpha_sorting.html[^]
_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
I know about sorting the vertices and all that. That's not my problem. Why can't OpenGL simply blend the colour with the alpha value so that an alpha of 1.0 will mean (r,g,b) stays as (r,g,b) but alpha of 0.5 will blend 1/2 the red of the source with 1/2 the red of the dest (& same for green & blue). I keep reading that glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) does that trick but it doesn't work for me. Black came out as black even when alpha was at 0. Or am i just doing something wrong?
-------------------------------- Customer in computer shop: "Can you copy the Internet onto this disk for me?"
-
I know about sorting the vertices and all that. That's not my problem. Why can't OpenGL simply blend the colour with the alpha value so that an alpha of 1.0 will mean (r,g,b) stays as (r,g,b) but alpha of 0.5 will blend 1/2 the red of the source with 1/2 the red of the dest (& same for green & blue). I keep reading that glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) does that trick but it doesn't work for me. Black came out as black even when alpha was at 0. Or am i just doing something wrong?
-------------------------------- Customer in computer shop: "Can you copy the Internet onto this disk for me?"
The problem is that blending really doesn't know all that goes into the accumulation of color in the raster image. This is where some of the other layers come in. You can utilize a overlay or underlay to improve blending by providing a forced layer of "this is different than that". have you tried the blend program from the Red book on opengl? http://cs-sdl.sourceforge.net/index.php/Red_Book_Chapter_07[^] http://www.opengl.org/resources/code/samples/redbook/[^] The issue is complicated because of performance consideration. True blending would mean that all objects are blended independently on their own layer such that the blend of background and foreground and even midground can all be weighted appropriately. But it doesn't work that way since all we have is a raster "destination" plane and a source object. Once the blend operation is finished the net result is a new destination plane (the raster plane) and all you can do is change the calculation and whether or not you write to the z-buffer which prevents something else from blending.
_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
-
The problem is that blending really doesn't know all that goes into the accumulation of color in the raster image. This is where some of the other layers come in. You can utilize a overlay or underlay to improve blending by providing a forced layer of "this is different than that". have you tried the blend program from the Red book on opengl? http://cs-sdl.sourceforge.net/index.php/Red_Book_Chapter_07[^] http://www.opengl.org/resources/code/samples/redbook/[^] The issue is complicated because of performance consideration. True blending would mean that all objects are blended independently on their own layer such that the blend of background and foreground and even midground can all be weighted appropriately. But it doesn't work that way since all we have is a raster "destination" plane and a source object. Once the blend operation is finished the net result is a new destination plane (the raster plane) and all you can do is change the calculation and whether or not you write to the z-buffer which prevents something else from blending.
_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
I really don't know anything about OpenGL. I just want to create transparent areas (among other things) so could you please just tell me how to make an area transparent. It must be possible since many games do it (like car windows). See this picture: http://img187.imageshack.us/my.php?image=alphale2.png[^] which shows what i am getting when i use the blend functions. The edges of the wave (which is from Windows Media Player) should fade to transparent. The edges are (0,0,0,0) and the inside is (r,g,b,1). If you can't help me then i guess the second image (GL_SRC_ALPHA, GL_ONE) look good enough. thanks
-------------------------------- Customer in computer shop: "Can you copy the Internet onto this disk for me?"
-
I really don't know anything about OpenGL. I just want to create transparent areas (among other things) so could you please just tell me how to make an area transparent. It must be possible since many games do it (like car windows). See this picture: http://img187.imageshack.us/my.php?image=alphale2.png[^] which shows what i am getting when i use the blend functions. The edges of the wave (which is from Windows Media Player) should fade to transparent. The edges are (0,0,0,0) and the inside is (r,g,b,1). If you can't help me then i guess the second image (GL_SRC_ALPHA, GL_ONE) look good enough. thanks
-------------------------------- Customer in computer shop: "Can you copy the Internet onto this disk for me?"
VizCoder wrote:
which shows what i am getting when i use the blend functions.
I am not positive what the issue is, have you set your driver renderings to quality? are you blending these via vertex coloring or via alpha textures? I understand you don't want to learn all the hardware dependant modes of alpha blending, and only want to do transparency, but the problem is how to do transparency correctly. When you see see light through a transparent window you see a mixture of different High Dynamic events. You may not realize it, and you may not care, but when you have to model this on a computer, you really need to care. Yes people do transparency, I do it regularly via Z-sorting. Worse, I do it with plane canopies. Why is that worse? because half the canopy is behind the cockpit. The cockpit exists between two transparent volumes. So now I must sort such that without loosing bits in a blending environment, one half of the canopy is merged with the background earth image, then the cockpit is drawn, and then the other half of hte canopy is merged with the after-rending result of the previous operations.
_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
-
VizCoder wrote:
which shows what i am getting when i use the blend functions.
I am not positive what the issue is, have you set your driver renderings to quality? are you blending these via vertex coloring or via alpha textures? I understand you don't want to learn all the hardware dependant modes of alpha blending, and only want to do transparency, but the problem is how to do transparency correctly. When you see see light through a transparent window you see a mixture of different High Dynamic events. You may not realize it, and you may not care, but when you have to model this on a computer, you really need to care. Yes people do transparency, I do it regularly via Z-sorting. Worse, I do it with plane canopies. Why is that worse? because half the canopy is behind the cockpit. The cockpit exists between two transparent volumes. So now I must sort such that without loosing bits in a blending environment, one half of the canopy is merged with the background earth image, then the cockpit is drawn, and then the other half of hte canopy is merged with the after-rending result of the previous operations.
_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
El Corazon wrote:
are you blending these via vertex coloring or via alpha textures?
I am blending via vertex colouring. And you're right, i don't really care about all of the High Dynamic events. I do a Computer Science degree at uni but graphics is not one of my courses. I am just doing this as a hobby because i like graphics. So i really shouldn't waste my time learning about OpenGL lighting effects (i might read up on it on the holidays). I guess i will just use GL_SRC_ALPHA, GL_ONE for now, as long as i don't blend many colours at once and end up with a white screen! :) thanks for your help anyway
-------------------------------- Customer in computer shop: "Can you copy the Internet onto this disk for me?"