Transparency problems with blending and alpha
hi there i got quite a problem here
I got a bit of code that uses blend and alpha value to do transparency. The problem is that things that shouldnt be transparent ARE and things that should be BEHIND objects are draw BEFORE them. I suspect that has somthing to do with the depth buffer but i cant see whats wrong. Heres are samples of my code :
//initialize...
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
glShadeModel(GL_SMOOTH);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
//lighning code here...i tested it quite alot and it works now
//draw code...
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
//draw a sphere without light
glDisable(GL_LIGHTING);
//drawing glu sphere here
glEnable(GL_LIGHTING);
//draw a rectangle supposed to be behind things becauz of the -0.1 in the translatef
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glTranslatef(bonuses.x, bonuses.y, -0.1f);
//draw rectangle here
//draw a big rectangle behind of all things that is supposed to be opaque and that is TRANSPARENT ?!?!?!
glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
//draw big rectangle here
//Beginning drawing transparent stuff
//
glEnable(GL_BLEND);
//read only mode for the depth buffer
glDepthMask(GL_FALSE);
//draw blocks BEFORE all the scene… oneBlock.alpha is sometimes 1.0, somtimes 0.5
glColor4f(1.0f, 1.0f, 1.0f, oneBlock.alpha);
//draw blocks here
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
glFlush();
SwapBuffers(g_HDC);
I really cant see the problem… the big rectangle is semi-transparent and appears to be BEFORE almost all the other objects. The rectangle though appears with full color before the big rectangle.
To make it short, the big rectangle should be behind all and opaque.
The rectangle should be just before that
The blocks should be cefore the rectangle and somtimes transparent.
Thats about all…tell me if you see somthing its been a week and i cant find the solution :-(
Kiroke
</i>
Kirokewww.geocities.com/kiroke2
Remember to disable GL_BLEND when you dont need it, and translucent surfaces should be drawn in back-to-front order after all the opaque surfaces are drawn.
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
Since your blending function is (GL_SRC_ALPHA, GL_ONE), you _have_ to disable blending (using glDisable(GL_BLEND)) before draw any *supposed* opaque objects.
I just tried that and it still wont work... :-(
Kirokewww.geocities.com/kiroke2
Doesn''t it even correct the translucency problem ?
btw, "before" is an english term for time events. You can say "everyday I go to the shower _before_ I go to the WC". Sorry for the english lesson, but the "before" term has no meaning to me when talking about triangles :b
"behind" means the triangles are further from the eye.
If you want some triangles to be closer from the eye, the right term is "in front of".
Back on topic :
you won''t have your second geometry (the one supposed to be translucent) in front of the first geometry (the one supposed to be opaque) if you don''t translate the matrix back. Because setting the translation to (bonuses.x, bonuses.y, -0.1f) will not set the translation back to the origin unless you call glTranslatef(-bonuses.x, -bonuses.y, +0.1f);
btw, "before" is an english term for time events. You can say "everyday I go to the shower _before_ I go to the WC". Sorry for the english lesson, but the "before" term has no meaning to me when talking about triangles :b
"behind" means the triangles are further from the eye.
If you want some triangles to be closer from the eye, the right term is "in front of".
Back on topic :
you won''t have your second geometry (the one supposed to be translucent) in front of the first geometry (the one supposed to be opaque) if you don''t translate the matrix back. Because setting the translation to (bonuses.x, bonuses.y, -0.1f) will not set the translation back to the origin unless you call glTranslatef(-bonuses.x, -bonuses.y, +0.1f);
Yeah i use glPushMatrix(); and glPopMatrix; but i just didnt put it there...and thx for the english tip :-)
I still got that damn problem though...
I still got that damn problem though...
Kirokewww.geocities.com/kiroke2
huh, excuse me for posting the question again : what problem ?
the object that you would like to be opaque is translucent again ?
the object that you would like to be behind is still not behind ?
both ?
something else... ?
the object that you would like to be opaque is translucent again ?
the object that you would like to be behind is still not behind ?
both ?
something else... ?
nothing has changed since the beginning becauz i had already the glPushMatrix and glPopMatrix.
I still got things that should be behind and that are not... like the big rectangle.
The objects that are supposed to be opaque, like the big rectangle, is transparent.
The only change ive made is to disable blending right after the definition of the blending i use in the initialisation.
Thx alot for your help... i really cant see the cause of that error.
Kiroke
ps:dont hesitate to comment my english... I''m a french guy and I wanna learn that langage better
I still got things that should be behind and that are not... like the big rectangle.
The objects that are supposed to be opaque, like the big rectangle, is transparent.
The only change ive made is to disable blending right after the definition of the blending i use in the initialisation.
Thx alot for your help... i really cant see the cause of that error.
Kiroke
ps:dont hesitate to comment my english... I''m a french guy and I wanna learn that langage better
Kirokewww.geocities.com/kiroke2
tsss. si tu as du mal à parler Anglais, on peut parler Français tu sais 
you don''t have to disable blending just after the initialization. It is useless if it is called only at one moment in the program.
most OpenGL applications run like that :
init();
while (!end)
{
process_one_frame();
}
in that case, blending will be deactivated after the call to init(), but when process_one_frame() is called, blending is activated and is _never_ deactivated again.
So, since 2nd frame the blending is always enabled.... unless you disable it
I recommend disabling blending just after transparent stuff :
//Beginning drawing transparent stuff
//
glEnable(GL_BLEND);
//read only mode for the depth buffer
glDepthMask(GL_FALSE);
//draw blocks BEFORE all the scene... oneBlock.alpha is sometimes 1.0, somtimes 0.5
glColor4f(1.0f, 1.0f, 1.0f, oneBlock.alpha);
//draw blocks here
glDepthMask(GL_TRUE);
//disable blending after drawing blended polygons
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glFlush();
SwapBuffers(g_HDC);

you don''t have to disable blending just after the initialization. It is useless if it is called only at one moment in the program.
most OpenGL applications run like that :
init();
while (!end)
{
process_one_frame();
}
in that case, blending will be deactivated after the call to init(), but when process_one_frame() is called, blending is activated and is _never_ deactivated again.
So, since 2nd frame the blending is always enabled.... unless you disable it

I recommend disabling blending just after transparent stuff :
//Beginning drawing transparent stuff
//
glEnable(GL_BLEND);
//read only mode for the depth buffer
glDepthMask(GL_FALSE);
//draw blocks BEFORE all the scene... oneBlock.alpha is sometimes 1.0, somtimes 0.5
glColor4f(1.0f, 1.0f, 1.0f, oneBlock.alpha);
//draw blocks here
glDepthMask(GL_TRUE);
//disable blending after drawing blended polygons
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glFlush();
SwapBuffers(g_HDC);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement