Transcript Document
Chapter 6
Blending, Antialiasing, Fog, and Polygon
Offset
Guihua Yang
Jan 14, 2004
1
What we are going to learn in this lecture
Blending: concepts and the example “alpha.c”
Antialiasing: concepts and the example “aargb.c”
Fog: concepts and the example “fog.c”
Polygon: concepts and the example “polyoff.c”
2
Blending
Blending: combines color values from a source
and a destination. The final effect is that parts of
your scene appear translucent.
Source: color values of the incoming fragment
Destination: color values of the corresponding
currently stored pixel
3
Blending-cont
Alpha(opacity):
alpha--, transparent or translucent surfaces
alpha++, opaque surfaces
glColor*(R,G,B,A)
4
Blending-cont
The Source and Destination Factors:
blended RGBA values are given by
(RsSr+RdDr, GsSg+GdDg, BsSb+BdDb, AsSa+AdDa)
Each component of this quadruplet is eventually
clamped to [0,1].
5
Blending Example: alpha.c(a)
………………..
static int leftFirst = GL_TRUE;
/* Initialize alpha blending function. */
static void init(void)
{glEnable (GL_BLEND); //to have blending take effect; use glDisable()
to disable blending.
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// void glBlendFunc(GLenum sfactor, GLenum dfactor);
glShadeModel (GL_FLAT); //select flat or smooth shading
glClearColor (0.0, 0.0, 0.0, 0.0); // specify clear values for the color
buffers: black
6
alpha.c(b)
static void drawLeftTriangle(void)
{
/* draw yellow triangle on LHS of screen */
glBegin (GL_TRIANGLES);
glColor4f(1.0, 1.0, 0.0, 0.75); //to set the color to yellow, alpha to 0.75
glVertex3f(0.1, 0.9, 0.0); //3 points of the triangle …..
glVertex3f(0.1, 0.1, 0.0);
glVertex3f(0.7, 0.5, 0.0);
glEnd();
}
7
alpha.c(c)
static void drawRightTriangle(void)
{
/* draw cyan triangle on RHS of screen */
glBegin (GL_TRIANGLES);
glColor4f(0.0, 1.0, 1.0, 0.75); //to set the color to cyan, alpha to 0.75
glVertex3f(0.9, 0.9, 0.0); //3 points of the triangle …..
glVertex3f(0.3, 0.5, 0.0);
glVertex3f(0.9, 0.1, 0.0);
glEnd();
}
8
alpha.c(d)
void keyboard(unsigned char key, int x, int y)
{ switch (key) {
case `t':
case `T':
leftFirst = !leftFirst;
glutPostRedisplay(); //marks the current or specified window as
needing to be redisplayed.
break;
case 27: /* Escape key */ // 27 is ascii code for escape key.
exit(0);
break;
default:
break; } }
9
alpha.c(e)
void display(void)
{ glClear(GL_COLOR_BUFFER_BIT); // clear buffers within the
viewport . GL_COLOR_BUFFER_BIT : Indicates the buffers
currently enabled for color writing.
if (leftFirst) {
drawLeftTriangle();
drawRightTriangle();
}
else {
drawRightTriangle();
drawLeftTriangle();
}
glFlush(); //force execution of GL commands in finite time
}
10
alpha.c(f)
void reshape(int w, int h)
{ glViewport(0, 0, (GLsizei) w, (GLsizei) h); //set the viewport, lower left
corner(0,0), viewport’s width and height.
glMatrixMode(GL_PROJECTION); //specify which matrix is the current
matrix. GL_PROJECTION: Applies subsequent matrix operations
to the projection matrix stack.
glLoadIdentity(); // replace the current matrix with the identity matrix
if (w <= h)
gluOrtho2D (0.0, 1.0, 0.0, 1.0*(GLfloat)h/(GLfloat)w);
else
gluOrtho2D (0.0, 1.0*(GLfloat)w/(GLfloat)h, 0.0, 1.0);
}
11
alpha.c(g)
int main(int argc, char** argv)
{ // 5 routines perform tasks necessary to initialize a window.
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (200, 200); // plus glutInitWindwoPosition(…).
glutCreateWindow (argv[0]);
// until glutMainLoop() is called, the window is not yet displayed.
init();
glutReshapeFunc (reshape);
glutKeyboardFunc (keyboard);
glutDisplayFunc (display);
glutMainLoop();
return 0;
}
12
Antialiasing
aliasing causes images to have a jagged staircase look to
their edges while anti-aliasing smoothes out these
jagged edges.
13
Antialiasing-cont
anti-aliasing is more than just making something slightly
fuzzy so that you can't see the jagged edges: it's a way of
fooling the eye into seeing straight lines and smooth
curves where there are none.
14
Antialiasing in RGBA Mode
2 color modes: RGBA mode, Color index mode
Blending is enabled
The blending factors are: GL_SRC_ALPHA (source) and
GL_ONE_MINUS_SRC_ALPHA (destination).
15
Antialiasing Example: aargb.c(a)
static float rotAngle = 0.;
/* Initialize antialiasing for RGBA mode, including alpha blending, hint, and line width.
Print out implementation specific info on line width granularity and width.*/
void init(void)
{ GLfloat values[2];
glGetFloatv (GL_LINE_WIDTH_GRANULARITY, values); //returns one value, the
width difference between adjacent supported widths for antialiased lines.
printf ("GL_LINE_WIDTH_GRANULARITY value is %3.1f\n", values[0]);
glGetFloatv (GL_LINE_WIDTH_RANGE, values); //returns two values: the
smallest and largest supported widths for antialiased lines
printf ("GL_LINE_WIDTH_RANGE values are %3.1f %3.1f\n", values[0], values[1]);
glEnable (GL_LINE_SMOOTH); //indicates that antialiasing of lines is enabled
glEnable (GL_BLEND); // enable blending
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
glLineWidth (1.5); //specify the width of rasterized lines
glClearColor(0.0, 0.0, 0.0, 0.0); }
16
aargb.c(b)
/* Draw 2 diagonal lines to form an X */
void display(void)
{ glClear(GL_COLOR_BUFFER_BIT);
//green color
glPushMatrix(); //pushes the current matrix stack down by one, duplicating the
current matrix. That is, after a glPushMatrix call, the matrix on the top of
the stack is identical to the one below it.
glRotatef(-rotAngle, 0.0, 0.0, 0.1); //multiply the current matrix by a rotation matrix
glColor3f (0.0, 1.0, 0.0);
glBegin (GL_LINES);
glVertex2f (-0.5, 0.5);
glVertex2f (0.5, -0.5);
glEnd ();
//pop the current matrix stack
glColor3f (0.0, 0.0, 1.0); //blue color
glPopMatrix();
glPushMatrix();
glRotatef(rotAngle, 0.0, 0.0, 0.1);
glBegin (GL_LINES);
glVertex2f (0.5, 0.5);
glVertex2f (-0.5, -0.5);
glEnd ();
glPopMatrix();
glFlush(); }
17
aargb.c(c)
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case `r':
case `R':
rotAngle += 20.;
if (rotAngle >= 360.) rotAngle = 0.;
glutPostRedisplay();
break;
case 27: /* Escape Key */
exit(0);
break;
default:
break;
}
}
18
Fog
Fog: a general term that describes similar forms of
atmospheric effects; it can be used to simulate haze, mist,
smoke, or pollution.
Make an entire image appear more natural by adding fog,
which makes objects fade into the distance.
When fog is enabled, objects that are farther from the
viewpoint begin to fade into the fog color.
19
Fog Example
20
Fog equations
Fog blends a fog color with an incoming fragment's color using a fog
blending factor. This factor, f, is computed with one of these three
equations and then clamped to the range [0,1].
The equation for GL_LINEAR fog is:
The equation for GL_EXP fog is:
The equation for GL_EXP2 fog is:
Z: the distance in eye coordinates from the origin to the
fragment being fogged
21
Fog example: fog.c(a)
static GLint fogMode;
static void init(void)
{
GLfloat position[] = { 0.5, 0.5, 3.0, 0.0 };
glEnable(GL_DEPTH_TEST); //do depth comparisons and update the depth buffer.
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHTING); // use the current lighting parameters to compute the vertex
color or index.
glEnable(GL_LIGHT0); // include light i in the evaluation of the lighting equation. It is
always the case that GL_LIGHTi = GL_LIGHT0 + i.
{
GLfloat mat[3] = {0.1745, 0.01175, 0.01175};
glMaterialfv (GL_FRONT, GL_AMBIENT, mat); //specify material parameters for the
lighting model
mat[0] = 0.61424; mat[1] = 0.04136; mat[2] = 0.04136;
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat);
mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959;
glMaterialfv (GL_FRONT, GL_SPECULAR, mat);
glMaterialf (GL_FRONT, GL_SHININESS, 0.6*128.0);
}
22
fog.c(b)
glEnable(GL_FOG);
{
GLfloat fogColor[4] = {0.5, 0.5, 0.5, 1.0};
fogMode = GL_EXP;
glFogi (GL_FOG_MODE, fogMode); //specify fog parameters. The equation for
GL_EXP fog is
f is blending factor
glFogfv (GL_FOG_COLOR, fogColor);
glFogf (GL_FOG_DENSITY, 0.35); //the fog density used in both exponential fog
equations. Only nonnegative densities are accepted. The default fog density is 1.0.
glHint (GL_FOG_HINT, GL_DONT_CARE);
glFogf (GL_FOG_START, 1.0); //the near distance used in the linear fog equation.
glFogf (GL_FOG_END, 5.0); //the far distance used in the linear fog equation.
}
glClearColor(0.5, 0.5, 0.5, 1.0); /* fog color */
}
23
fog.c( c)
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case `f':
case `F':
if (fogMode == GL_EXP) {
fogMode = GL_EXP2;
printf ("Fog mode is GL_EXP2\n"); }
else if (fogMode == GL_EXP2) {
fogMode = GL_LINEAR;
printf ("Fog mode is GL_LINEAR\n"); }
else if (fogMode == GL_LINEAR) {
fogMode = GL_EXP;
printf ("Fog mode is GL_EXP\n"); }
glFogi (GL_FOG_MODE, fogMode);
glutPostRedisplay();
break;
…………………
}
}
24
Polygon Offset
Raster(from merriam-webster dictionary):a scan pattern (as
of the electron beam in a cathode-ray tube) in which an area
is scanned from side to side in lines from top to bottom; also
: a pattern of closely spaced rows of dots that form the image
on a cathode-ray tube (as of a television or computer
display)
Rasterize: The process of converting a vector image into a
bitmap image. This is the rasterize dialog box you will see
when importing a vector-based image into Photoshop.
25
Polygon Offset-cont.
Rasterization: Rasterization is the process by which
a primitive is converted to a two-dimensional
image. Each point of this image contains such
information as color and depth.
http://www.opengl.org/documentation/specs/versi
on1.1/glspec1.1/node41.html
26
Polygon Offset-cont.
GL_LINE: Boundary edges of the polygon are drawn as
line segments.
GL_FILL: The interior of the polygon is filled.
Polygon offset: adds an appropriate offset to force
coincident z values apart to cleanly separate a polygon
edge from its highlighting line.
27
Polygon offset example:polyoff.c
Polygon Offset to Eliminate Visual Artifacts: polyoff.c
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0, 1.0);
glCallList (list);
glDisable(GL_POLYGON_OFFSET_FILL);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glColor3f (1.0, 1.0, 1.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glCallList (list);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
28
References
Red book, OpenGL Programming Guide (Addison-Wesley
Publishing Company)
Blue book, The OpenGL "Bluebook" HTML format
http://www.parallab.uib.no/SGI_bookshelves/SGI_Developer/books
/OpenGL_RM/sgi_html/bk02.html
http://www.sgi.com/software/opengl/glandx/intro/subsection3_2_2.h
tml
http://users.easystreet.com/jkirwan/dda.html
http://www.opengl.org/documentation/specs/version1.1/glspec1.1/n
ode41.html
29