14
\$\begingroup\$

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 :)

\$\endgroup\$
3
  • \$\begingroup\$ 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? \$\endgroup\$
    – Notabene
    Jun 17, 2011 at 6:40
  • \$\begingroup\$ 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). \$\endgroup\$ Jun 17, 2011 at 6:47
  • \$\begingroup\$ It would be great if you could include the picture into your question. It seems to have disappeared from its original location. \$\endgroup\$ Sep 9, 2017 at 5:51

1 Answer 1

19
\$\begingroup\$

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.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .