PDA

View Full Version : Question about Blending



03-12-2006, 09:51 PM
Hi, I have this code that I wrote to learn about blending in OpenGL. While I understand what the blending equation and the blend functions do, there's still one thing I don't get: what is the value of the destination's alpha? Does OpenGL save the alpha of a fragment's pixel when storing it in the framebuffer?



// render green polygon (destination)
gl::Disable(GL_BLEND);
glex::BeginQuads();
gl::Color4(0.0, 1.0, 0.0, 1.0); // is this Ad?
gl::Vertex2((GLdouble)(-size.cx), (GLdouble)(size.cy));
gl::Vertex2((GLdouble)(-size.cx), (GLdouble)(-size.cy));
gl::Vertex2((GLdouble)(size.cx), (GLdouble)(-size.cy));
gl::Vertex2((GLdouble)(size.cx), (GLdouble)(size.cy));
gl::End();

// render white polygon (source), but using blending
// subtract dest (green) from source. result
// should be purple (255, 0, 255).
gl::Enable(GL_BLEND);
gl::BlendEquation(GL_FUNC_SUBTRACT);
gl::BlendFunc(GL_ONE, GL_ONE);
glex::BeginQuads();
gl::Color4(1.0, 1.0, 1.0, 1.0); // this is As
gl::Vertex2((GLdouble)(-size.cx), (GLdouble)(size.cy));
gl::Vertex2((GLdouble)(-size.cx), (GLdouble)(-size.cy));
gl::Vertex2((GLdouble)(size.cx), (GLdouble)(-size.cy));
gl::Vertex2((GLdouble)(size.cx), (GLdouble)(size.cy));
gl::End();

// the math behind blending
// C = <1.0, 1.0, 1.0, As>*GL_ONE - <0.0, 1.0, 0.0, Ad>*GL_ONE
// C = <1.0, 0.0, 1.0, As - Ad> =
// C = <1.0, 0.0, 1.0, 0.0> (alpha is zero, shouldn't see anything???)Thanks,
Steve

ZbuffeR
03-13-2006, 02:02 AM
>> Does OpenGL save the alpha of a fragment's pixel when storing it in the framebuffer?

Yes when you request a RGBA framebuffer. It can be useful for coverage antialiasing for example.