PDA

View Full Version : Blending



ltrain_riders
12-02-2002, 05:33 PM
If I use glColor4f( 0.5, 0.0, 0.5, 0.25 ); will I still need glBlendFunc(...); to create a transparent wall? I just want to create one section of wall in my project and not affect any others. Whats the best Constants to use inside glBlendFunc( )? This is what i'm trying to do:
glPushMatrix( );
glLoadIdentity( );
glColor4f( 0.5, 0.0, 0.5, 0.0 );
glBegin(GL_QUADS);
glNormal3f( 0.0, 1.0, 0.0 ); //top face
glVertex3f( -0.2, 0.5, -4.0 );
glVertex3f( -0.2, 0.5, -3.2 );
glVertex3f( 0, 0.5, -3.2 );
glVertex3f( 0, 0.5, -4.0 );

glNormal3f( -1.0, 0.0, 0.0 ); //left face
glVertex3f( -0.2, 0.5, -3.2 );
glVertex3f( -0.2, 0.5, -4.0 );
glVertex3f( -0.2, 0.1, -4.0 );
glVertex3f( -0.2, 0.1, -3.2 );

glNormal3f( 1.0, 0.0, 0.0 ); //right face
glVertex3f( 0.0, 0.5, -4.0 );
glVertex3f( 0.0, 0.5, -3.2 );
glVertex3f( 0.0, 0.1, -3.2 );
glVertex3f( 0.0, 0.1, -4.0 );
glEnd( );
glFlush( );
glPopMatrix( );

dorbie
12-02-2002, 06:03 PM
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

and make sure you draw the transparent wall last.

ltrain_riders
12-02-2002, 07:13 PM
do i place in glBlendFunc( ) in my initialize( ) function and follow that with glEnable(GL_BLEND); ?

dorbie
12-02-2002, 08:52 PM
That would be best. Anything else might be unwanted overhead. Just make sure you disable the blending when you don't need it.

styx
12-02-2002, 10:05 PM
it depends on your program where to place it. when you only use one blending mode, put it where it's just called once on init.

ltrain_riders
12-03-2002, 05:46 AM
Would I have to worry about GL_DEPTH_TEST, should this be disabled when I do blending ?

pbrown
12-03-2002, 07:24 AM
Originally posted by ltrain_riders:
Would I have to worry about GL_DEPTH_TEST, should this be disabled when I do blending ?

If you are drawing only one transparent wall, and you're drawing it last, there shouldn't be anything to worry about. If it fails the depth test, you can't and shouldn't see the wall (or the appropriate portions). If it passes, it will be rendered and blend appropriately.

If you are drawing multiple transparent polygons where one polygon could be on top of another, you do have to worry about DEPTH_TEST since one transparent surface could effectively hide another. To handle this case, you generally sort from back to front. All the transparent geometry should still go last. There are also other, more complicated, techniques available (e.g., NVIDIA's SDK has a bunch of "order independent transparency" demos).

If possible, enable GL_BLEND only for the transparent geometry, and turn it off when rendering opaque geometry. It would be technically OK to leave it enabled if your opaque geometry all had an alpha of 1.0, but you might lose performance by doing so (in general, blending is a read-modify-write on the color buffer).

For similar reasons, you might want to disable depth writes when rendering your transparent geometry (assuming it's the last part of your scene). This will make the Z test a read-only operation instead of a read-modify-write. This assumes you won't use the Z buffer that would otherwise be updated to hold the Z values of the nearest transparent geometry.

dorbie
12-03-2002, 11:06 AM
Draw it last and you don't need to worry about disabling depth test. Sort your transparent stuff if you have more than one transparent element. Disabling depth test might be faster so after you get it working you may want to try this depending on your scene contents.

*Aaron*
12-03-2002, 03:04 PM
Draw it last and you don't need to worry about disabling depth test. Sort your transparent stuff if you have more than one transparent element. Disabling depth test might be faster so after you get it working you may want to try this depending on your scene contents.There seems to be some confusion here about depth writing vs. depth testing. To draw multiple transparent polygons, you generally disable depth writing but not depth testing. If you disable depth testing and you have opaque objects in front of transparent objects, the transparent objects will show through. If you're only drawing one transparent polygon and it's the last thing you draw, the depth writing state is irrelevent, but if you have multiple transparent polygons, and depth testing is enabled (which it should be), a transparent polygon may be completely obscured by another transparent polygon.

Also, if you use additive blending (i.e., glBlendFunc(GL_SRC_ALPHA, GL_ONE)), you don't have to sort the transparent polygons.

ltrain_riders
12-03-2002, 03:38 PM
Thanks, it works great using GL_ONE !!

dorbie
12-03-2002, 04:23 PM
No there's no confusion but you have a point, it depends on the circumstances.

Preserving destination color will accumulate a glowing effect, it is almost never the desired effect unless you're rendering flame, flare or similar effects.


[This message has been edited by dorbie (edited 12-03-2002).]

*Aaron*
12-04-2002, 05:16 AM
Preserving destination color will accumulate a glowing effect, it is almost never the desired effect unless you're rendering flame, flare or similar effects.Good point. But if the source alpha value is low, it looks about the same. I'm sure you know how to do blending, Dorbie, but I wanted to make it clear for ltrain_riders, who's obviously a newbie. How do I know he's a newbie? He posted the same question in the Beginners' Forum an hour before posting it here Shame on you, ltrain_riders. http://www.opengl.org/discussion_boards/ubb/wink.gif

[This message has been edited by *Aaron* (edited 12-04-2002).]

ltrain_riders
12-04-2002, 01:32 PM
yes indeed aaron. the people at the beginner forum don't seem to answer my questions, unlike the fine fellas found in the advanced forum. and of course I'm a newbie, we all were at one point.

DFrey
12-04-2002, 02:43 PM
Actually, I'm sure I and Bob have answered blending questions in the beginners forum, both recently and in the past. A simple search of the forum should have given you the answer in detail.