OpenGL transparency question?

Apprentice
Posts: 16
Joined: 2010.01
Post: #1
I want an image to fade away on the screen. The game uses OpenGL. I've been looking around for some sort of transparency function but I can't seem to find it. Sad

Thanks! Smile
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #2
If you want the whole viewport to fade to black (or something) you can just draw a rectangle on top of it. Assuming that what you mean is a textured polygon in a part of a scene, you could use GL_MODULATE for glTexEnv and ramp the alpha value in your glColor.
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.01
Post: #3
backslash Wrote:If you want the whole viewport to fade to black (or something) you can just draw a rectangle on top of it. Assuming that what you mean is a textured polygon in a part of a scene, you could use GL_MODULATE for glTexEnv and ramp the alpha value in your glColor.

Thanks for the quick replay. Smile What I'm trying to do is the latter of the 2. Sorry I'm new to this OpenGL stuff, could you write an example in code?

Thank you! Grin
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #4
You're probably looking for "blending".
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.01
Post: #5
DoG Wrote:You're probably looking for "blending".

My game is just a simple 2d game. (if that helps) All I want is a small image to fade away on the screen.

Thanks!
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #6
Right, as DoG said, you need blending. glEnable(GL_BLEND). You can set your translucency different ways, but glColor4f(r, g, b, alpha) is probably what you'll start with -- "alpha" being your opacity/translucency, and r, g, and b all being 1.0f if you just want a straight fade. Then it also depends on your blend function, but that's another story we'll talk about once you get blending and glColor4f doing something, but not the way you want.
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.01
Post: #7
AnotherJake Wrote:Right, as DoG said, you need blending. glEnable(GL_BLEND). You can set your translucency different ways, but glColor4f(r, g, b, alpha) is probably what you'll start with -- "alpha" being your opacity/translucency, and r, g, and b all being 1.0f if you just want a straight fade. Then it also depends on your blend function, but that's another story we'll talk about once you get blending and glColor4f doing something, but not the way you want.

Thanks, but I still can't seem to figure this out. Wacko So I tried to go back to just triangles but it still doesn't work. Sad What's wrong with this code:
Code:
    glEnable(GL_BLEND);
    glColor4f(1.0f,1.0f,1.0f,0.5f);
    glBegin(GL_TRIANGLES);    
    glVertex3f( 0.0f, 1.0f, 0.0f);
    glVertex3f(-1.0f,-1.0f, 0.0f);
    glVertex3f( 1.0f,-1.0f, 0.0f);
    glEnd();
The following code draws a triangle with no transparency. Sad

Thanks!
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #8
What's your blend function? Try calling
Code:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
before you draw the triangle.
Quote this message in a reply
Apprentice
Posts: 16
Joined: 2010.01
Post: #9
Thank you! Smile Ok, so I've gotten it to work on the triangles but now when I try to put it in my game it will only affect the image that was drawn first (the background) but I want it to affect the last image drawn.

Thanks!
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2010.04
Post: #10
in openGL, you can have a couple different color functions, the ones you probably want are
glColor4f() or glColor4d()

they specify the r,g,b,a with a float or double respectively. What you want is the 'a' or alpha, this specifies the transparency of the object you are coloring. The alpha value ranges from 0 to 1, with 1 being opaque and 0 being completely transparent. So if you want to have something fade away, you will have away over time to change that alpha value.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL transparency green_ghost 4 3,810 Sep 6, 2008 10:52 AM
Last Post: green_ghost
  OpenGL question Leroy 2 2,000 Apr 28, 2007 04:48 PM
Last Post: BinarySpike