With OpenGL, is there any way to render two textures at the same position and blend them together with alpha blending so that one appears on top of the other? I am trying to make it so that my back-texture can be dynamic on the secondary texture will have a 'window' that will show the texture 'behind' it. I have done quite a bit of research and have tried several combinations of glDepthFunc, glBlendFunc, etc. and have not found any combination that works. I am guessing that this is possible, but just haven't found the trick.
its been awhile, but ill try to help some?
first you have to have GL_BLEND enabled
glEnable(GL_BLEND);
and then i usually have to follow that with a
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
But i usually use delphi so not sure how closely this will help you..
Related
I have a model with transparent quads for a beard. I can not tell what triangles belong to the beard because their color comes from the texture passed to the fragment shader. I have tried to presort the triangles back to front during export of the model, but this does not help. So I implemented MSAA and Alpha to Coverage, but this did not help either. My last attempt was to draw the model, with a depth mask off and skipping any transparent data, so the color buffer would have non-clear color values to blend with. Then I would draw the model a second time with depth testing on and drawing the alpha pieces.
Nothing I have tried so far has worked. What other techniques can I try to get the beard of the model to properly draw? I am looking for a way to handle this that doesn't use a bunch of extensions. I'd prefer techniques that can be handled with plain old OpenGL 4.
Here is an image of what I am dealing with.
This is what I got after I applied the selected answer.
What you're trying to do there is a still largely unsolved problem: Order independent transparency. MSAA is something entirely different, as is alpha coverage.
So far the best working solution is to separate the model into an opaque and a hairy part. Draw the opaque parts of your scene first, then draw everything (semi-)translucent, ordered far to near in a second pass.
The way your image looks like it seems like the beard is rendered as the first thing, which is quite the opposite of what you actually want.
Simple way:
Enable depth write (depth mask), disable alpha-blending, draw model without the beard.
Disable depth write, enable alpha-blending, draw the beard. Make sure face culling is enabled.
Hard way:
Because order-independent transparency in renderers that use z-buffer is an unsolved problem (as datenwolf said), you could try depth-peeling. I believe the paper is available within OpenGL SDK. Most likely it'll be slower than "simple way", and there'll be a limit on number of maximum overlapping transparent polygons. Also check wikipedia article on order-independent transparency.
I'm using stb_image.c for loading bytes of data to opengl.
However, I'm struggling with one issue that happens to me, when I try to load png files with alpha channel
Now the white area should be the transparent one, though it cleared only the little part of it.
Any ideas what is causing this behavior?
The photoshop histogram
You are doing the Tom Dalling OpenGL tutorial? ^^
I don't know if you are still searching for a solution to this but here I go anyways:
I actually had the same problem as I was working through that lesson but it worked for me by using glBlendFunc with sfactor GL_SRC_ALPHA and dfactor GL_ONE_MINUS_SRC_ALPHA. These are the parameters OpenGL seems to recommend in the documentation at least: glBlendFunc
Transparency is best implemented using blend function(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) with primitives sorted from farthest to nearest. Note that this transparency calculation does not require the presence of alpha bitplanes in the frame buffer.
I'm drawing 2D Polygons, actually I'm drawing lots of triangles and using GLUOrtho2D. I noticed that by zooming out I got much better frame rates. I realized I was maxing out the graphics card's fill rate, not drawing too many polygons as I had initially suspected. I think this is because I'm drawing lots of overlapping polygons and using
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Is this the blend function I should be using? How could I minimize the filling given the nature of what I'm doing? I tried enabling the GL_DEPTH_TEST and Z ordered my polygons, but since I alpha blend, this won't help. What is the most efficient way of doing this sort of thing?
Thanks
I doubt using a different blend function would help. One generally chooses the correct blend function based on desired output, not performance.
Are all the polygons you render transparent/translucent? If not, it might help to separate the rendering of those apart from the opaque polygons you have and set the proper GL states accordingly.
Also are these textured polygons? You might be able to optimize your texture handling (ex: reduce context switches, use more efficient image formats, etc).
Finally, how are you rendering these triangles? If in immediate mode or using vertex arrays, consider using VBOs.
avoid alpha blending on as many triangles as possible. It adds up and yes, it gets really expensive. There is no magic high-performance blend function.
The only solution is "use less blending and more z-buffering"
Changing blend function leads only to changes in blending coefficients, but not in equation.
Anyways, modern videocards do not use hardcoded equations. You could try to write a pixel shader that satisfies your needs.
BTW, consider using VBO's for yours "lots of triangles" if you still don't.
I am trying to map a different texture on each side of a cube using a GL_QUADS. My first problem is that I cannot even get a texture to display on the side of a GL_QUADS. I can however get a texture to display using GL_TRIANGLES but I do no understand how to draw things very well using triangles and I want to use QUADS. I also can only use GLUT for this. I need an example that works because I do not know enough about OpenGL for someone to simply explain this to me. Someone please help. Thanks!
Oops didn't realize I forgot to use glTexCoord2f. It works now.
If you post the code that you are having trouble with, perhaps I can help you. Most likely, you need to set the appropriate texture coordinates per-vertex.
I've been working on a graphics project doing Depth of Field. The method is doing several passes, each rendering the scene with different near and far clipping-parameters, such that it renders different depth ranges at each pass.
The idea is to apply a blur kernel on each individual layer by rendering to a texture and doing the blur on a rendered quad (with the texture). This is all fairly basic stuff. As is working fine. However, the part I can't get working is the combining of the layers:
The color buffer is cleared with color4(0,0,0,0) before each pass is drawn. However, the accumulation does not seem to allow the usage of glBlendFunc such that it accumulates taking the alpha channel into account (i.e. glBlendFunc(GL_SRC_ALPHA, GL_SRC_ONE_MINUS_ALPHA)).
The question is then:
- Does the glBlendFunc affect the glAccum?
- If not, how can I work around this?
Based on the documentation, this doesn't seem to be the case as it is not mentioned, however it feels this is a very useful feature.
Regards,
R
Your question doesn't explain why you need blending and accum, so this answer may not be real useful. The accum buffer does not blend. But there are two routes that might be useful:
Most modern hardware can do "Separate" blending, so you can, for example, do an additive operation on the alpha channel while doing real blending on the framebuffer.
Many modern setups will let you render to multiple draw buffers at the same time, sometimes with separate blending modes.
So you might be able to use a second framebuffer via an FBO as sort of a "fake accum buffer", using a blending mode to "accumulate". If you can find a blending mode that is close enough to the accumulate operation you want, you might be able to take advantage of the blend equation.
Take a look at these GL extensions:
http://www.opengl.org/registry/specs/ARB/draw_buffers.txt
http://www.opengl.org/registry/specs/ARB/draw_buffers_blend.txt
http://www.opengl.org/registry/specs/EXT/draw_buffers2.txt