blending blending textures reflection fog
blendRectangles1.cpp
Fragments color and depth information destination - the pixel that is already on the screen source - the pixel that might be written to the screen
Recall with Blending OFF and Depth testing OFF How does the computer decide if source pixel replaces the destination pixel?
Recall with Blending OFF and Depth testing OFF source pixel replaces destination pixel always
Recall with Blending OFF and Depth testing ON How does the computer decide if source pixel replaces the destination pixel?
Recall with Blending OFF and Depth testing ON if source fragments's z-value is less than (closer than) destination fragments's z-value then source replaces destination. if source fragments's z-value is greater than or equal to (further than) destination fragments's z-value then source fragment is discarded.
With Blending ON and Depth testing ON or OFF if source fragment would be discarded without blending, it is still discarded. if source fragments would replace the destination fragment without blending, now they will be blended.
Blending formula terms Let X be a color (R,G,B,A) Let SX be the X color value for the source fragment and DX for the destination fragment Let SbX be the X color blending factor for the source fragment and DbX for the destination fragment.
Blending Formula DX = SbX * SX + DbX * DX
Some ways to set SbX and DbX glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); SbX = SA DbX = 1- SA
Some other ways to set SbX and DbX glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA); SbX = DA DbX = 1- DA glBlendFunc(GL_ONE, GL_ZERO); SbX = 1 DbX = 0 Lots more.
Experiment 1: blendRectangles1.cpp Left side of room: compute red rectangle pixels after blending with white background, and then pixels in the overlap after blending. Right side of room: compute blue rectangle pixels after blending with white background, and then pixels in the overlap after blending.
Experiment 1 results Red first: ( , , , ) after 1st rect. ( , , , ) after 2nd rect. Blue first: ( , , , ) after 1st rect.
Experiment 2 Change the background color to black and run. Are the colors the same?
blendRectangles2.cpp What we want: red and blue rectangles transparent and green rectangle opaque red closest, then green, then blue.
blendRectangles2.cpp Why do we get what we get? Check setup note alpha values of r,b, and g rectangles note z values of all three rectangles note gluLookAt
Experiment 3 Make blendRectangles2 do what is intended.
Experiment 4 Change the viewpoint to glLookAt(0.0, 0.0, -3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); Does it still work correctly? (Now blue should be closest)
Strategy for drawing opaque and translucent objects Turn on depth testing. Draw all the opaque items. Make the depth buffer read only : glDepthMask(GL_FALSE); Draw the transparent objects. (set the depth buffer back to read/write: glDepthMask(GL_TRUE);
Experiment 5 try it!
sphereInGlassBox.cpp run it and observe.
Review Textures In loadExternalTextures () we have glBindTexture(GL_TEXTURE_2D, texture[0]); This says texture[0] is the active texture (the state). Here we set some of its properties. In drawScene() we have At this point we are setting the state, so that texture[0] will be used to draw the following polygon(s). Start here for 21
Blending Textures Run fieldAndSkyTexturesBlended.cpp notice the blending functions used ONE and ZERO GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA variable alpha
Blending to simulate reflection run ballAndTorusReflected.cpp discuss technique glScalef(1.0, -1.0, 1.0); glFrontFace(GL_CW); //good choice? different alphas for transparent (reflective) and opaque. smooth vs flat
Fog Run fieldAndSkyFogged.cpp glEnable(GL_FOG); glFogfv(GL_FOG_COLOR,fogColor); fogColor is a pointer to a color glFogi(GL_FOG_MODE,fogmode); fogmode can be GL_LINEAR,GL_EXP, or GL_EXP2
parameters for fog for GL_LINEAR glFogf(GL_FOG_START, fogstart); glFogf(GL_FOG_END, fogend); for GL_EXP and GL_EXP2 glFogf(GL_DENSITY,fogdensity); glHint(GL_FOG, GL_NICEST);