Desperado17
Desperado17

Reputation: 1015

Conditional Blending with OpenGL ES

I want to implement a fragment/blending operation using OpenGL ES 3.1 that fulfills the following requirements:

Can this be done via the usual blending functions, alpha tricks etc.?

Upvotes: 2

Views: 262

Answers (2)

Columbo
Columbo

Reputation: 6766

I think you could just use standard premultiplied alpha blending:

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); 

If you want to replace, then you output an alpha value of 1 from your fragment shader. If you want to do additive then you output an alpha value of 0 from your fragment shader.

That assumes you're only really interested in the RGB values that end up in your framebuffer.

Upvotes: 2

solidpixel
solidpixel

Reputation: 12159

If you know this at vertex shading time I presume that whole triangles are either one blend or another. This is an ideal thing for stencil testing provided that you don't have too much geometry.

  • Clear stencil to zero.
  • Draw shape with color writes disabled, and stencil being set to one for regions which match one of the two blend rules.
  • Set blend rule one.
  • Draw shape with stencil testing enabled and passing when stencil == 0.
  • Set blend rule two.
  • Draw shape with stencil testing enabled and passing when stencil == 1.

How much this costs depends on your geometry, as you need to pass it in to rendering multiple times, but the stencil testing part of this is normally close to "free".

Upvotes: 2

Related Questions