I'm trying to draw an image on top of a fully opaque background, using additive blending or multiplicative blending depending on a toggle.
Both work, but the alpha of the image gets ignored when doing multiplicative blending, while in additive it's working.
For additive I have:
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
For multiplicative I have:
glBlendFunc(GL_DST_COLOR, GL_ZERO);
Additive works as expected, multiplicative ignores the alpha.
I am aware I can swap shaders to achieve what I want, but if I could do it through glBlendFunc it would be much better.
Related
What is the standard builtin blend function used to implement a highlighter in OpenGL? Using glBlendEquation(GL_MIN); produces the desired effect but does not allow for opacity-based blending as well. I am modeling transparency using glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); Is it possible to account for opacity using min blending? If not, is it best to use a custom shader with a frame buffer object?
For reference, here is an example of the desired result (not rendered using OpenGL):
I ended up rendering highlight-able elements to floating point texture and blending using glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);. This intermediate frame buffer can then be blended with the default framebuffer 0 using the same blend function. Subtractive blending can be emulated by multiplying the rgb components of the final value in the fragment shader by -1. Floating point textures support negative values and are typically write-able as frame buffer color attachments. See Blend negative value into framebuffer 0 opengl for more information.
I searched a while, but couldn't find a answer to my problem.
I am trying to add a transparent object in front of an image containing white. For this I use additive transparency
//enable trancparency
glEnable(GL_BLEND);
// disable backface culling
glDisable(GL_CULL_FACE);
// disable depth buffer writing
glDepthMask(GL_FALSE);
// additive blending
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
it looks like this know.
White background still white
I tried to add
glClampColor(GL_CLAMP_READ_COLOR, GL_FALSE);
Is there a way to say opengl that when he adds red to white it should take the red one and not white?
Sorry my bad english, but I hope someone understands my problem and can help me!
Thanks a lot!!!
Is there a way to say opengl that when he adds red to white it should take the red one and not white?
Of course not; that's impossible. Well, for non-HDR rendering.
Your (non-HDR) colors are clamped to the range [0, 1]. And white is defined a 1.0 for all three channels. The result of your addition will still be clamped to [0, 1]. So 1 + something will still be 1. And 1 + 0 will still be 1.
You cannot make a color channel whiter-than-white.
You probably want linear interpolation blending, not additive:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Additive blending can only be used sparingly in non-HDR rendering systems.
Is there a blending equation that can be made using the OpenGL glBlendFunc that would allow for a transparent color (RGBA) to be rendered behind an additive overlay.
Rendering Ontop:
This effect can be achieved using glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Rendering Underneath:
Is there an equation for this blending effect?
There are no glBlendFunc options for directly drawing overlay. Info can be found here:
http://benmcdowell.com/implementing-photoshop-blend-modes-in-opengl/
However, your effect doesn't seem to be overlay, seems to be either screen
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_COLOR);
or additive
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
I'd suggest you try all combinations, there aren't that many possible. If you need crazier effects however, you'll need to code shaders.
P.S. I lied. You don't HAVE to code shaders to do crazy effects like overlay, but you have to draw so many times it becomes unusable in real time. I should have said it's better to use shaders.
I am attempting to put together a simple program that just displays two textured quads overlapping each other, and the textures have alpha channels.
I have successfully been able to get the quads themselves to display, and the textures look correct except for the transparency.
On this topic, the GL_BLEND flag does not seem to do anything at all, whether it is enabled or not. Is this particular flag inapplicable when shaders are being used?
I know for a fact that the alpha is being rendered in correctly, since if I set out_color = texture.aaaa, I get a nice pattern of blacks/whites/grays that match the original texture.
So, if GL_BLEND doesn't work, what are the usual methods for getting alpha blending working?
All that glEnable(GL_BLEND) does is turn blending on and off. It does not say what the blending will do; that is controlled by the various blending functions. If you want a typical alpha transparency blend, you do this:
glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
If you use pre-multiplied alpha (and you probably should), you would do:
glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
Is it possible in opengl to setup blending to achieve additive color overlays?
Red + green = yellow, cyan + magenta = white, etc.. (see diagram)
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
should do it.
Have a look at the full description of glBlendFunc
EDIT: Old tutorial link seems to be dead (403 Forbidden). Wayback'd.
Simple additive blending is achieved with glBlendFunc (GL_ONE, GL_ONE). You need to be aware of the fact that OpenGL's color value range is limited to [0,1], and values greater than 1 will be clamped to 1, so adding bright colors may not produce physically correctly blended colors. If you want to achieve that, you will have to add and scale the colors in your own software rather than having OpenGL handle it, or write a shader program that does that while rendering.