Contents
Can you blend two textures in a GLSL shader?
I was recently faced with the simple problem of blending two textures in a GLSL shader and found no straightforward discussions of the topic, so I’ll try to collect my thoughts here. It’s a simple problem to be sure, but initially the solution was not obvious.
What is the formula for alpha blending in OpenGL?
Wikipedia, as ever, has a full discourse on the subject of alpha blending which provided the key insight in the form of this equation. Why it took me so long, I don’t know, but it finally clicked that this formula represents the default OpenGL alpha blending function glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA).
What should the GL _ fragcolor alpha value be?
You would expect the resulting “merged layers” to give that 50% value. In fact the gl_FragColor alpha value should always simply be the sum of the two layers alpha. Getting the rgb colors right at the same time, however, I am really struggling with.
Is there a way to combine two pixel values?
There are of course many possible ways to combine two pixel values, but what I was after was a typical or conventional result, like blending layers in Photoshop with defaults. Wikipedia, as ever, has a full discourse on the subject of alpha blending which provided the key insight in the form of this equation.
How is shadow mapping done in learn OpenGL?
Shadow mapping therefore consists of two passes: first we render the depth map, and in the second pass we render the scene as normal and use the generated depth map to calculate whether fragments are in shadow. It may sound a bit complicated, but as soon as we walk through the technique step-by-step it’ll likely start to make sense.
What’s the problem with blending layers in Photoshop?
The problem can be distilled or reframed as “Given two RGBA pixel values, generate a third that is a composite of the two.” There are of course many possible ways to combine two pixel values, but what I was after was a typical or conventional result, like blending layers in Photoshop with defaults.