opengl special effects jian-liang lin 2002 blending: alpha channel alpha value can be specify by...

26
OpenGL Special Effects Jian-Liang Lin 2002

Upload: christopher-adams

Post on 19-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

OpenGL Special Effects

Jian-Liang Lin2002

Page 2: 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

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 framebufferNote: – Alpha values aren't specified in color-index

mode, so blending operations aren't performed in color-index mode.

Page 3: 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

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 );

Page 4: 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

Blending: The Source and Destination Factors -2

void glBlendFunc(GLenum sfactor, GLenum dfactor);

Page 5: 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

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

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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

Page 9: 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

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

Page 10: 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

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

Page 11: 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

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

Page 12: 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

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.

Page 13: 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

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

Page 14: 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

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.

Page 15: 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

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 );

Page 16: 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

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

Page 17: 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

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.

Page 18: 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

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

Page 19: 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

Accumulation Buffer -8

Depth Of Field– REDBOOK Sample:

• Dof.cpp

Other applications– ref. OpenGL Programming Guide,

Chapter 10

Page 20: 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

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.

Page 21: 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

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).

Page 22: 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

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.

Page 23: 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

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.

Page 24: 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

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.

Page 25: 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

Stencil Buffer -6

Some sample:– REDBOOK Sample:

• Stencil.cpp

– GLUT Advanced Sample:• Dissolve.cpp• Shadowvol.cpp

– NVidia Sample:• Nvshadow.cpp

Page 26: 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

Any Question?

?