Blending
E N D
Presentation Transcript
MAE152Computer Graphics for Engineers and Scientists Fall 03 Blending
Outline • Why do we want to blend? • What is blending? • Math behind blending • Blending in OpenGL
Why do we want to blend? • We use triangles to describe surfaces • We always assumed opaque surfaces • How would we do transparent surfaces? • What are some types of transparent surfaces? • Windows • Saran Wrap • Plastic • Stained Glass • Water
Alpha: the 4th Color Component • Measure of Opacity • simulate translucent objects • glass, water, etc. • composite images • antialiasing • ignored if blending is not enabled glEnable( GL_BLEND )
Source and Destination • Objects are blended together in a scene in the order in which they are drawn. • An object being drawn it is the "source“. • Any object, over which a source object is drawn is a “destination”. • Blending functions, along with alpha values control how source and destination colors are mixed together.
Source and Destination Factors • RGBA blending factors • Source Sr, Sg, Sb, Sa • Destination Dr, Dg, Db, Da • “Current” RGBA components • Source Rs, Gs, Bs, As • Destination Rd, Gd, Bd, Ad • Resultant RGBA components (destination) ((Rs x Sr) + (Rd x Dr), (Gs x Sg) + (Gd x Dg), (Bs x Sb) + (Bd x Db), (As x Sa) +(Ad x Da)) • The new destination values are clamped to [0.0-1.0].
Blending in OGL • If a fragment makes it to FB, the pixel is read out and blended with the fragment’s color and then written back to FB • The contributions of fragment and FB pixel is specified: glBlendFunc( src, dst )
We would like to combine the two colors • Fragment or source - incoming color • destination - existing color • How should we combine them? • We use the alpha channel to describe the combination of the source and destination. • ColorFinal = A*ColorSource + B*ColorDestination • Most APIs let you specify A and B • What does A and B mean qualitatively?
Combining Colors • Usually we take the source alpha as a “percentage” of the incoming fragment. • Thus the equation becomes: • ColorFinal=AlphaSource*ColorSource +(1- AlphaSource)*ColorDestination • What is the “default” alpha values for no blending? • What does this mean about the order of objects? • Order DOES MATTER when you have alpha objects!
Setting Alpha Values • Unlit models • Use the fourth parameter of the glColor4f() command to set the alpha value • alpha = 0.0 makes the object completely transparent • alpha = 1.0 makes the object completely opaque • Lit models • The alpha value of an object is specified with the glMaterial*() function when specifying, ambient, diffuse, specular or emissive light parameters • Example: GLfloat mat_transparent[] = { 0.0, 0.8, 0.8, 0.6 }; //init fn glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_transparent);
Setting the Blend Function • Blend Function controls color and alpha values between source and destination objects void glBlendFunc( GLenum sfactor, GLenum dfactor) • Sfactor: source blending factor • Dfactor: destination blending factors • The value of these blending factors and their computed blending factors is tabulated.
Fragment • Fragment in OGL: after the rasterization stage (including texturing), the data are not yet pixel, but are fragments • Fragment is all the data associated with a pixel, including coordinate, color, depth and texture coordinates.
Demo • Overlapping Triangles
3D Blending with Depth Buffer • A scene of opaque and translucent objects • Enable depth buffering and depth test • Draw opaque objects • Make the depth buffer read only, with glDepthMask(GL_FALSE) • Draw the translucent objects (sort those triangles still, but which order, FTB or BTF?)
Demo • 3-D blending with depth buffer