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 :)
Answer
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.
No comments:
Post a Comment