503 508














Open GL Super Bible:Visual Effects:nBlending and Fog















To access the contents, click the chapter and section titles.


Open GL Super Bible


(Publisher: Macmillan Computer Publishing)

Author(s): Waite group Press

ISBN: 1571690735

Publication Date: 08/01/96

 




Previous
Table of Contents
Next




Chapter 16Visual Effects: Blending and Fog

What youłll learn in this chapter:


How to
Functions Youłll Use



Display transparent or translucent lines and polygons
glBlendFunc

Add weather haze and fog effects
glFog



This chapter introduces the color blending and fog functions provided by OpenGL, both of which can be used to add that last bit of realism you need.

The color blending functions support effects such as transparency that can be used to simulate windows, drink glasses, and other transparent objects. The fog functions add a variable amount of color to the polygons you draw, producing a scene that looks “hazy" or just downright dreary!
Something to remember when using these special effects is that they donłt look good on an 8-bit display. Make sure your programs contain the option of disabling these effects when running on 8-bit displays.
Blending
Blending in OpenGL provides pixel-level control of RGBA color storage in the color buffer. Blending operations cannot be used in color index mode and are disabled in color index windows.

To enable blending in RGBA windows, you must first call glEnable(GL_BLEND). After this, you call glBlendFunc with two arguments: the source and the destination colorsł blending functions (see Tables 16-1 and 16-2). By default, these arguments are GL_ONE and GL_ZERO, respectively, which is equivalent to glDisable(GL_BLEND).
Table 16-1 Blending Functions for Source Color



Function
Blend Factor



GL_ZERO
Source color = 0,0,0,0.

GL_ONE
Uses <?> Source color.

GL_DST_COLOR
Source color is multiplied by the destination pixel color.

GL_ONE_MINUS_DST_COLOR
Source color is multiplied by (1,1,1,1
destination color).

GL_SRC_ALPHA
Source color multiplied by source alpha.

GL_ONE_MINUS_SRC_ALPHA
Source color multiplied by (1
source alpha).

GL_DST_ALPHA
Source color multiplied by destination alpha; not supported by Microsoft OpenGL.

GL_ONE_MINUS_DST_ALPHA
Source color multiplied by (1
destination alpha); not supported by Microsoft OpenGL.

GL_SRC_ALPHA_SATURATE
Source color multiplied by the minimum of the source and (1
destination) alphas; not supported by Microsoft OpenGL.





Table 16-2 Blending Functions for Destination Color



Function
Blend Factor



GL_ZERO
Destination color = 0,0,0,0.

GL_ONE
Use <?> Destination color.

GL_SRC_COLOR
Destination color is multiplied by the source pixel color.

GL_ONE_MINUS_SRC_COLOR
Destination color is multiplied by (1,1,1,1
source color).

GL_SRC_ALPHA
Destination color multiplied by source alpha.

GL_ONE_MINUS_SRC_ALPHA
Destination color multiplied by (1
source alpha).

GL_DST_ALPHA
Destination color multiplied by destination alpha; not supported by Microsoft OpenGL.

GL_ONE_MINUS_DST_ALPHA
Destination color multiplied by (1
destination alpha); not supported by Microsoft OpenGL.

GL_SRC_ALPHA_SATURATE
Destination color multiplied by the minimum of the source and (1
destination) alphas; not supported by Microsoft OpenGL.



Using Blending for Transparency
Transparency is perhaps the most typical use of blending, often used for windows, bottles, and other 3D objects that you can see through. Transparency can also be used to combine multiple images, or for “soft" brushes in a paint program.

Following are the blending functions for all of these applications:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

This combination takes the source color and scales it based on the alpha component, and then adds the destination pixel color scaled by 1 minus the alpha value. Stated more simply, this blending function takes a fraction of the current drawing color and overlays it on the pixel on the screen. The alpha component of the color can be from 0 (completely transparent) to 1 (completely opaque), as follows:


Rd = Rs * As + Rd * (1 - As)
Gd = Gs * As + Gd * (1 - As)
Bd = Bs * As + Bd * (1 - As)

Because only the source alpha component is used, you do not need a graphics board that supports alpha color planes in the color buffer. This is important because the standard Microsoft OpenGL implementation does not support alpha color planes.
Something to remember with alpha-blended transparency is that the normal depth-buffer test can interfere with the effect youłre trying to achieve. To make sure that your transparent polygons and lines are drawn properly, always draw them from back to front.
Listing 16-1 shows the code that was used to draw the transparent teapot in Figure 16-1. In the draw_scene function, we draw the two teapots from back to front to ensure that the rear teapot can be seen through the front one. Youłll notice some artifacts remain visible in the front teapot where the surface polygons intersect. You canłt eliminate these completely, but you can reduce them by sorting the polygons by depth first and enabling back-face culling with glEnable(GL_CULL_FACE).

Figure 16-1  Transparent teapot using blending
The first thing draw_scene does is set the blending function to do transparency based on the drawing (source) colorłs alpha component:


glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Next, the opaque teapot is drawn with blending disabled so that we can always see the teapot through the transparent one:


glDisable(GL_BLEND);
glColor3f(1.0, 1.0, 0.0);
auxSolidTeapot(1.0);

Finally, blending is enabled and the transparent teapot is drawn with an alpha (transparency) value of 0.25:


glEnable(GL_BLEND);
glColor4f(1.0, 1.0, 1.0, 0.25);
auxSolidTeapot(1.0);





Previous
Table of Contents
Next














 


Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.












Wyszukiwarka

Podobne podstrony:
Peugeot 508 napełnianie zbiornika dodatku(1)
2014 12 23 Dec nr 508 MON 1 BPZ 17 Wlkp BZ odznaki
503 Wpusty żeliwne
508 (3)
fringe 503 hdtv lol
503 Jak sporządzić dokumentację zasad rachunkowości cz II
503 505
04 (508)
508 510
House MD 503 Adverse Events
505 508
503 (2)
503 (2)
503 505
ReadMe (503)

więcej podobnych podstron