Download presentation
Presentation is loading. Please wait.
Published byVeronica Page Modified over 9 years ago
1
Blending MAE152 Computer Graphics for Engineers and Scientists Fall 03
2
Outline n Why do we want to blend? n What is blending? n Math behind blending n Blending in OpenGL
3
Why do we want to blend? n We use triangles to describe surfaces n We always assumed opaque surfaces n How would we do transparent surfaces? n What are some types of transparent surfaces? –Windows –Saran Wrap –Plastic –Stained Glass –Water
4
Alpha: the 4 th Color Component n Measure of Opacity –simulate translucent objects n glass, water, etc. –composite images –antialiasing –ignored if blending is not enabled glEnable( GL_BLEND ) glEnable( GL_BLEND )
5
Blend
6
Source and Destination n Objects are blended together in a scene in the order in which they are drawn. n An object being drawn it is the "source“. n Any object, over which a source object is drawn is a “destination”. n Any object, over which a source object is drawn is a “destination”. n Blending functions, along with alpha values control how source and destination colors are mixed together.
7
Source and Destination Factors n RGBA blending factors –Source Sr, Sg, Sb, Sa –Destination Dr, Dg, Db, Da n “Current” RGBA components –Source Rs, Gs, Bs, As –Destination Rd, Gd, Bd, Ad n Resultant RGBA components (destination) ((Rs x Sr) + (Rd x Dr), (Gs x Sg) + (Gd x Dg), (Gs x Sg) + (Gd x Dg), (Bs x Sb) + (Bd x Db), (Bs x Sb) + (Bd x Db), (As x Sa) +(Ad x Da)) (As x Sa) +(Ad x Da)) n The new destination values are clamped to [0.0-1.0].
8
Blending in OGL n 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 n The contributions of fragment and FB pixel is specified: glBlendFunc( src, dst )
9
We would like to combine the two colors n Fragment or source - incoming color n destination - existing color n How should we combine them? n We use the alpha channel to describe the combination of the source and destination. n Color Final = A*Color Source + B*Color Destination n Most APIs let you specify A and B n What does A and B mean qualitatively?
10
Combining Colors n Usually we take the source alpha as a “percentage” of the incoming fragment. n Thus the equation becomes: n Color Final =Alpha Source *Color Source +(1- Alpha Source )*Color Destination n What is the “default” alpha values for no blending? n What does this mean about the order of objects? n Order DOES MATTER when you have alpha objects!
11
Order Matters with Alpha!
12
Setting Alpha Values n 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 n 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);
13
Setting the Blend Function n 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 n The value of these blending factors and their computed blending factors is tabulated.
14
Setting the Blend Function Constant Value Relevant Factor Computed Blend Factor GL_ZERO Source or destination (0,0,0,0) GL_ONE (1,1,1,1) GL_DST_COLORSource(Rd,Gd,Bd,Ad) GL_SRC_COLORdestination(Rs,Gs,Bs,As) GL_ONE_MINUS_DST_COLORsource(1,1,1,1)-(Rd,Gd,Bd,Ad) GL_ONE_MINUS_SRC_COLORdestination(1,1,1,1)-(Rs,Gs,Bs,As) GL_SRC_ALPHA (As,As,As,As) GL_ONE_MINUS_SRC_ALPHA (1,1,1,1)-(As,As,As,As) GL_DST_ALPHA (Ad,Ad,Ad,Ad) GL_ONE_MINUS_DST_ALPHA (1,1,1,1)-(As,As,As,As) GL_SRC_ALPHA_SATURATESource (f,f,f,1): f-min(As,1-Ad)
15
OpenGL n glColor4f(1,0,0,0.5); n You must have a framebuffer with alpha: –glutInitDisplayMode(GLUT_RGBA); n Assign colors at the vertices to be a certain alpha. n Other suggestions for combining colors: n Let’s write out the equation n A = 1, B = 1 n A = 1-Alpha Source, B = Alpha Source n A = Alpha Source, B = 1 n glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); n glBlendFunc(GL_ONE, GL_ZERO);
16
Fragment n 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.
17
n Demo n Overlapping Triangles
18
Blending Function n Four choices: –GL_ONE –GL_ZERO –GL_SRC_ALPHA –GL_ONE_MINUS_SRC_ALPHA n Blending is enabled using glEnable(GL_BLEND) n Note: If your OpenGL implementation supports the GL_ARB_imaging extension, you can modify the blending equation as well.
19
3D Blending with Depth Buffer n 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?)
20
n Demo n 3-D blending with depth buffer
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.