Introduction to OpenGL Programming

Download Report

Transcript Introduction to OpenGL Programming

OpenGL Special Effects
Jian-Liang Lin
2002
Blending: Alpha Channel
Alpha value can be specify by glColor*
When blending is enabled, the alpha value is
often used to combine the color value of the
fragment being processed with that of the
pixel already stored in the framebuffer
Note:
– Alpha values aren't specified in color-index mode,
so blending operations aren't performed in colorindex mode.
Blending: The Source and
Destination Factors -1
let the source and destination blending
factors be (Sr, Sg, Sb, Sa) and (Dr, Dg, Db, Da),
respectively, and the RGBA values of the
source and destination be indicated with a
subscript of s or d. Then the final, 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].
– You can use glBlendFunc() to specify these two factors
– glEnable( GL_BLEND );
Blending: The Source and
Destination Factors -2
void glBlendFunc(GLenum sfactor, GLenum dfactor);
Blending: Example
glBlendFunc( GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA );
– ( RS, GS, BS )*AS + ( RD, GD, BD )*( 1- AS )
Billboarding
REDBOOK Sample:
– Alpha.cpp
GLUT Advanced97 Sample:
– alphablend.cpp, alphablendnosort.cpp
Antialiasing -1
Antialiasing point
– glEnable( GL_POINT_SMOOTH );
Antialiasing line
– glEnable( GL_LINE_SMOOTH );
Antialiasing polygon
– glEnable( GL_POLYGON_SMOOTH );
– Ref. OpenGL Programming Guide, Chapter 6
Antialiasing -2
Using glHint(…);
– void glHint(GLenum target, GLenum hint);
– Controls certain aspects of OpenGL behavior
– hint can be GL_FASTEST / GL_NICEST /
GL_DONT_CARE
Antialiasing -3
And you must enable alpha-blending
– glEnable (GL_BLEND);
– glBlendFunc (GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA);
REDBOOK Sample with my
modification:
– aargb2.cpp
Fog: Introduction
Fog
– Makes objects fade into the distance
– A general term that describes similar forms
of atmospheric effects
– It can be used to simulate haze, mist,
smoke, or pollution
Fog: Using Fog
glFog{I,f}[v]( GLenum pname, TYPE param );
– If pname is GL_FOG_MODE, param can be
GL_EXP(default), GL_EXP2, GL_LINEAR
– pname also can be GL_FOG_DENSITY,
GL_FOG_START, GL_FOG_END,
GL_FOG_COLOR, GL_FOG_INDEX
Fog: Example
Ex:
glEnable(GL_FOG);
GLfloat fogColor[4] = {0.5, 0.5, 0.5, 1.0};
glFogi(GL_FOG_MODE, GL_EXP);
glFogfv(GL_FOG_COLOR, fogColor);
glFogf(GL_FOG_DENSITY, 0.35);
glClearColor(0.5, 0.5, 0.5, 1.0);
REDBOOK Sample:
– Fog.cpp
Accumulation Buffer -1
The accumulation buffer can be used
for such things as:
–
–
–
–
scene antialiasing,
motion blur,
simulating photographic depth of field,
calculating the soft shadows that result
from multiple light sources.
Accumulation Buffer -2
OpenGL graphics operations don't write
directly into the accumulation buffer.
Typically, a series of images is generated in
one of the standard color buffers, and these
are accumulated, one at a time, into the
accumulation buffer.
When the accumulation is finished, the result
is copied back into a color buffer for viewing
Accumulation Buffer -3
void glAccum(GLenum op, GLfloat value);
– Op can be GL_ACCUM, GL_LOAD, GL_RETURN,
GL_ADD, GL_MULT
– GL_ACCUM reads each pixel from the buffer
currently selected for reading with glReadBuffer(),
multiplies the R, G, B, and alpha values by value,
and adds the result to the accumulation buffer
– GL_LOAD does the same thing, except that the
values replace those in the accumulation buffer
rather than being added to them.
Accumulation Buffer -4
– GL_RETURN takes values from the accumulation
buffer, multiplies them by value, and places the
result in the color buffer(s) enabled for writing.
– GL_ADD and GL_MULT simply add or multiply
the value of each pixel in the accumulation buffer
by value and then return it to the accumulation
buffer. For GL_MULT, value is clamped to be in
the range [-1.0,1.0]. For GL_ADD, no clamping
occurs.
glClearAccum( … );
glClear( GL_ACCUM_BUFFER_BIT );
Accumulation Buffer -5
Scene antialiasing
– To perform scene antialiasing,
– first clear the accumulation buffer and enable the
front buffer for reading and writing.
– Then loop several times (say, n) through code that
jitters and draws the image (jittering is moving
the image to a slightly different position),
accumulating the data with
• glAccum(GL_ACCUM, 1.0/n);
– and finally calling
• glAccum(GL_RETURN, 1.0);
– GLUT Advanced97 sample:
• Accumaa.cpp
Accumulation Buffer -6
Motion Blur
– Suppose your scene has some stationary and some
moving objects in it
– Set up the accumulation buffer in the same way,
but instead of spatially jittering the images, jitter
them temporally.
– The entire scene can be made successively dimmer
by calling
• glAccum (GL_MULT, decayFactor);
– as the scene is drawn into the accumulation buffer,
where decayFactor is a number from 0.0 to 1.0.
– Smaller numbers for decayFactor cause the object to
appear to be moving faster.
Accumulation Buffer -7
– You can transfer the completed scene with the
object's current position and "vapor trail" of
previous positions from the accumulation buffer
to the standard color buffer with
• glAccum (GL_RETURN, 1.0);
– GLUT Advanced Sample:
• Motionblur.cpp
Accumulation Buffer -8
Depth Of Field
– REDBOOK Sample:
• Dof.cpp
Other applications
– ref. OpenGL Programming Guide,
Chapter 10
Stencil Buffer -1
One use for the stencil buffer is to restrict
drawing to certain portions of the screen, just
as a cardboard stencil can be used with a can
of spray paint to make fairly precise painted
images.
The stencil buffer prevents anything that
wouldn't be visible through the windshield
from being drawn. Thus, if your application
is a driving simulation, you can draw all the
instruments and other items inside the
automobile once, and as the car moves, only
the outside scene need be updated.
Stencil Buffer -2
void glStencilFunc(GLenum func, GLint ref,
GLuint mask);
– func
• The test function. The following eight tokens are valid:
• GL_NEVER
– Always fails.
• GL_LESS
– Passes if ( ref & mask) < ( stencil & mask).
• GL_LEQUAL
– Passes if ( ref & mask) ≤ ( stencil & mask).
• GL_GREATER
– Passes if ( ref & mask) > ( stencil & mask).
• GL_GEQUAL
– Passes if ( ref & mask) ≥ ( stencil & mask).
Stencil Buffer -3
• GL_EQUAL
– Passes if ( ref & mask) = ( stencil & mask).
• GL_NOTEQUAL
– Passes if ( ref & mask) ¹ ( stencil & mask).
• GL_ALWAYS
– Always passes.
– ref
• The reference value for the stencil test. The ref parameter
is clamped to the range [0,2n – 1], where n is the number
of bitplanes in the stencil buffer.
– mask
• A mask that is ANDed with both the reference value and
the stored stencil value when the test is done.
Stencil Buffer -4
void glStencilOp(GLenum fail, GLenum zfail,
GLenum zpass);
– fail
• The action to take when the stencil test fails. The
following six symbolic constants are accepted:
• GL_KEEP
– Keeps the current value.
• GL_ZERO
– Sets the stencil buffer value to zero.
• GL_REPLACE
– Sets the stencil buffer value to ref, as specified by
glStencilFunc.
• GL_INCR
– Increments the current stencil buffer value. Clamps to the
maximum representable unsigned value.
Stencil Buffer -5
• GL_DECR
– Decrements the current stencil buffer value. Clamps to
zero.
• GL_INVERT
– Bitwise inverts the current stencil buffer value.
– zfail
• Stencil action when the stencil test passes, but the depth
test fails. Accepts the same symbolic constants as fail.
– zpass
• Stencil action when both the stencil test and the depth
test pass, or when the stencil test passes and either there
is no depth buffer or depth testing is not enabled.
Accepts the same symbolic constants as fail.
Stencil Buffer -6
Some sample:
– REDBOOK Sample:
• Stencil.cpp
– GLUT Advanced Sample:
• Dissolve.cpp
• Shadowvol.cpp
– NVidia Sample:
• Nvshadow.cpp
Any Question?