Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Good Morning StackOverflow, I'm having a bit of a problem right now as I can't seem to find a way to render part of a texture transparently with openGL.

Here is my setting : I have a quad, representing a wall, covered with this texture (converted to PNG for uploading purposes). Obviously, I want the wall to be opaque, except for the panes of glass. There is another plane behind the wall which is supposed to show a landscape. I want to see the landscape from behind the window. Each texture is a TGA with alpha channel.

The "landscape" is rendered first, then the wall. I thought it would be sufficient to achieve this effect but apparently it's not the case. The part of the window supposed to be transparent is black and the landscape only appears when I move past the wall.

I tried to fiddle with GLBlendFunc() after having enabled it but it doesn't seem to do the trick.

Am i forgetting an important step ?

Thank you :)

share|improve this question
    
Hello, welcome to gdse. I made little edit, we don't like placing tags into questions heading much.(especially when you same tag use in tags :)). And to your question. Are you using fixed pipeline or your own shaders? –  Notabene Jun 17 '11 at 6:40
    
Thank you for the edit. I'm not sure of the meaning of "fixed pipeline" but I do build every vertex manually and I don't use any shader. If that matters, I am not using the last version of openGL as it's for a school assignement (I think it's openGL 3). –  megamoustache Jun 17 '11 at 6:47
add comment

1 Answer

up vote 9 down vote accepted

In addition to everything you've stated (textures with alpha channels, drawing the landscape before drawing the wall+window), you also need to do two more things.

First, you need to enable OpenGL's blending functionality:

glEnable(GL_BLEND);

Second, you need to tell OpenGL how to calculate the colors of blended pixels (which OpenGL calls "fragments", incidentally). In this case, you'll be fine with something simple like this:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Once blending is turned on, OpenGL will combine pixel colors according to the function parameters you give it in glBlendFunc. Specifically, it will do "source.color * source.alpha + destination.color * (1-source.alpha)", where "source" is the texture you're currently rendering, and "destination" is the color already in the framebuffer (the landscape). So wherever your alpha channel is white, you'll get the wall color, and wherever the alpha channel is black, you'll get the landscape color. If the alpha channel is grey, then you'll get some cross-fade between the two colors.

If your window is fully transparent then there are other ways to do this, where the order in which you draw the two polygons isn't important, but that starts getting into more advanced concepts such as alpha thresholds. Probably not needed for your current situation.

share|improve this answer
    
Thank you, it works perfectly now! –  megamoustache Jun 17 '11 at 12:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.