PDA

View Full Version : Alpha blending in 2D



lunarboy1
08-03-2002, 06:55 PM
Is it possible to alpha blend on a 2d plane?
What would my blend func have to be?
any other help would be greatly appreciated

thanks
lunarboy

SThomas
08-03-2002, 07:25 PM
sure, you can alpha blend in 2D. you probably want something like this:



glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

lunarboy1
08-04-2002, 01:05 PM
hmm... it still doesn't seem to work, but thanks anyways. Would these have anything to do with it?

in int main()
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);

in render func:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

thanks

Deiussum
08-04-2002, 04:55 PM
Well... if all your polygons are at the same depth, you will either have to disable the depth test, or make your depth function something like GL_LEQUAL.

Alpha blended polys still write to the depth buffer, so if you are drawing 2 overlapping polys at the same depth the first one will draw, then the second will fail the depth test if you leave depth testing on with the default depth func of GL_LESS.

lunarboy1
08-04-2002, 05:25 PM
does that just mean i remove GLUT_DEPTH from the glutInitDisplayMode()?

i tried that and it doesn't work.

i put GLUT_DEPTH back in and added glDepthFunc( GL_LEQUAL ); but that didn't help.

i tried glDisable( GL_DEPTH_TEST ); and that didn't work either.

would some of this code cause the problem? i go it from a tutorial on how to set the display to 2d and I'm not sure what most of it does so if anybody would like to explain that would be cool. BUT, Could it be causing the problem where all the polys are opaque? This code is right at the beginning of my idle func.



glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, winw, 0, winh);
glScalef( 1, 1, 1);
glTranslatef( 0, 0, 0);
glMatrixMode(GL_MODELVIEW);


Thanks for all help given so far and thanks in advance for other who might take a shot at my problem!

Deiussum
08-04-2002, 06:10 PM
Depth testing is disabled by default so if you never glEnable(GL_DEPTH_TEST) then you should be ok. The GLUT_DEPTH part of gluInitDisplay mode just tells glut to create a window that has a depth buffer available for use.

The code you show shouldn't have anything to do with blending, but there are a couple of things that are a bit odd about it.

1. Are you doing the 2d stuff as an overlay of a 3d scene? That's usually the only reason to use glPushMatrix in the projection matrix.

2. Using glScalef and glTranslatef on the projection matrix isn't usually good as it can mess up lighting and fog calculations.

Your blending should be working so long as you used glEnable(GL_BLEND) and specified the blend function, AND make sure you are supplying alpha values.

Just out of curiosity, when you say it isn't working, what exactly do you mean? What are you seeing that makes you think that it isn't working? Maybe it is working, but you just are getting different results than you expected.

lunarboy1
08-04-2002, 07:32 PM
thanks for the wealth of info!

When i say it isn't working i mean:

I have a simple 2d game of pong(its my learning of opengl project) and i was to be able to hit 'p' for pause and have action stop and a semi-transparent screen come up. The having everthing stop works with some if statements i added, but the screen that shows up( it happens to be red just for leaning how this alpha stuff works) it doesn't let some of the other polys show through eventhough its color is:

glColor4f( 1.0, 0.0, 0.0, 0.5 );

It still shows up opaque.

The whole time i do not change my idle function.

| sidenote |
so if the code allows 2d to be displayed on top of 3d then could i delete some lines i don't need so i dont learn wrong ways of programing?

Thanks for all help!