MAE 152 Computer Graphics for Engineers and Scientists

  • Slides: 18
Download presentation
MAE 152 Computer Graphics for Engineers and Scientists Fall 03 Blending

MAE 152 Computer Graphics for Engineers and Scientists Fall 03 Blending

Outline Why do we want to blend? n What is blending? n Math behind

Outline Why do we want to blend? n What is blending? n Math behind blending n Blending in Open. GL n

Why do we want to blend? n n We use triangles to describe surfaces

Why do we want to blend? n n 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 4 th Color Component n Measure of Opacity – simulate translucent objects

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 gl. Enable( GL_BLEND )

Blend

Blend

Source and Destination n n Objects are blended together in a scene in the

Source and Destination n n 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 n RGBA blending factors – Source Sr, Sg, Sb, Sa

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), (Bs x Sb) + (Bd x Db), (As x Sa) +(Ad x Da)) n The new destination values are clamped to [0. 0 -1. 0].

Blending in OGL n n If a fragment makes it to FB, the pixel

Blending in OGL n 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 The contributions of fragment and FB pixel is specified: gl. Blend. Func( src, dst )

We would like to combine the two colors n n n n Fragment or

We would like to combine the two colors n n n n 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. Color. Final = A*Color. Source + B*Color. Destination Most APIs let you specify A and B What does A and B mean qualitatively?

Combining Colors n n n Usually we take the source alpha as a “percentage”

Combining Colors n n n Usually we take the source alpha as a “percentage” of the incoming fragment. Thus the equation becomes: Color. Final=Alpha. Source*Color. Source +(1 - Alpha. Source)*Color. Destination 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!

Order Matters with Alpha!

Order Matters with Alpha!

Setting Alpha Values n Unlit models – Use the fourth parameter of the gl.

Setting Alpha Values n Unlit models – Use the fourth parameter of the gl. Color 4 f() 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 gl. Material*() function when specifying, ambient, diffuse, specular or emissive light parameters – Example: GLfloat mat_transparent[] = { 0. 0, 0. 8, 0. 6 }; //init fn gl. Materialfv(GL_FRONT, GL_DIFFUSE, mat_transparent);

Setting the Blend Function n Blend Function controls color and alpha values between source

Setting the Blend Function n Blend Function controls color and alpha values between source and destination objects void gl. Blend. Func( 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.

Setting the Blend Function Constant Value Relevant Factor Computed Blend Factor GL_ZERO Source or

Setting the Blend Function Constant Value Relevant Factor Computed Blend Factor GL_ZERO Source or destination (0, 0, 0, 0) GL_ONE Source or destination (1, 1, 1, 1) GL_DST_COLOR Source (Rd, Gd, Bd, Ad) GL_SRC_COLOR destination (Rs, Gs, Bs, As) GL_ONE_MINUS_DST_COLOR source (1, 1, 1, 1)-(Rd, Gd, Bd, Ad) GL_ONE_MINUS_SRC_COLOR destination (1, 1, 1, 1)-(Rs, Gs, Bs, As) GL_SRC_ALPHA Source or destination (As, As, As) GL_ONE_MINUS_SRC_ALPHA Source or destination (1, 1, 1, 1)-(As, As, As) GL_DST_ALPHA Source or destination (Ad, Ad, Ad) GL_ONE_MINUS_DST_ALPHA Source or destination (1, 1, 1, 1)-(As, As, As) GL_SRC_ALPHA_SATURATE Source (f, f, f, 1): f-min(As, 1 -Ad)

Fragment n Fragment in OGL: after the rasterization stage (including texturing), the data are

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.

Demo n Overlapping Triangles n

Demo n Overlapping Triangles n

3 D Blending with Depth Buffer n A scene of opaque and translucent objects

3 D 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 gl. Depth. Mask(GL_FALSE) – Draw the translucent objects (sort those triangles still, but which order, FTB or BTF? )

Demo n 3 -D blending with depth buffer n

Demo n 3 -D blending with depth buffer n