1

I want to use

glBlendFunc(GL_ONE, GL_ONE) 

and

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) 

at the same time. Is this possible?

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103

1 Answers1

3

You can create an offscreen Framebuffer Object with a texture attached to it. Perform the first render using glBlendFunc(GL_ONE, GL_ONE) then flip the input and output textures and perform the second render using glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA).

Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
  • Is this also possible with both textures attached the same time (color attachment 0 and 1), the shader outputting the same color to both, and using two separate blend funcs for the attachments? – leemes Oct 13 '13 at 10:46
  • @leemes the blend functions are global, they apply to everything. If you want to do separate blending, you'll have to implement it separately in your shader. – Mark Ingram Oct 14 '13 at 07:41
  • How can I implement blending in the shader? AFAIK, the shader outputs a color (vec4) which is blended by the *unprogrammable* render pipeline using the blend func set in the host program. Of course, I can have one buffer with premultiplied alpha, a second with "normal" alpha blending, by simply choosing a blend func which works for premultiplied alpha and manually premultiplying the alpha value to the second color in the shader. But there are scenarios in which this is not an option, e.g. when I only want to sum up colors in the second buffer, but the first should do "normal" alpha blending. – leemes Oct 14 '13 at 11:21
  • @leemes You would need to have multi-stage rendering, where you render to your first texture as the output, then flip it so it's the input for your second stage. You can combine both these stages by using an OpenGL extension which allows you to read from the current fragment. – Mark Ingram Oct 14 '13 at 12:59