Results 1 to 7 of 7

Thread: Alpha blending in 2D

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2002
    Posts
    4

    Alpha blending in 2D

    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

  2. #2
    Senior Member Regular Contributor
    Join Date
    Jun 2002
    Location
    Philadelphia, PA, US
    Posts
    108

    Re: Alpha blending in 2D

    sure, you can alpha blend in 2D. you probably want something like this:

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

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2002
    Posts
    4

    Re: Alpha blending in 2D

    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

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: Alpha blending in 2D

    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.
    Deiussum
    Software Engineer and OpenGL enthusiast

  5. #5
    Junior Member Newbie
    Join Date
    Aug 2002
    Posts
    4

    Re: Alpha blending in 2D

    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.
    Code :
            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!

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: Alpha blending in 2D

    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.
    Deiussum
    Software Engineer and OpenGL enthusiast

  7. #7
    Junior Member Newbie
    Join Date
    Aug 2002
    Posts
    4

    Re: Alpha blending in 2D

    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!

Similar Threads

  1. alpha blending
    By pommeC in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-05-2018, 05:22 AM
  2. Replies: 3
    Last Post: 06-16-2011, 12:51 PM
  3. alpha blending without modifying destination alpha
    By GordAllott in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 09-21-2009, 04:15 AM
  4. Source Alpha Blending without an Alpha Buffer
    By niallm in forum OpenGL ES
    Replies: 1
    Last Post: 06-18-2009, 02:51 AM
  5. alpha blending bug?
    By in forum OpenGL: User Software
    Replies: 1
    Last Post: 02-12-2001, 06:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean