Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

43
Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10

Transcript of Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Page 1: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Advanced OpenGL Technique

Jian Huang, CS 594, Fall 2002

Redbook: Chapters 6.1, 9.5, 9.6, 10

Page 2: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

List of Topics

• Blending• Multi-texturing• Frame buffers

– Stencil buffer– A-buffer (accumulation buffer)

• Fragment Tests

Page 3: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Alpha: the 4th Color Component

• Measure of Opacity– simulate translucent objects

• glass, water, etc.

– composite images– antialiasing– ignored if blending is not enabled

glEnable( GL_BLEND )

Page 4: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Compositing for Semi-transparency

• Requires sorting! (BTF vs. FTB)• “over” operator - Porter & Duff 1984• : opacity

C(N)out

Cin

Cout

C,

Ci, i

C(0)in

CCC inout )1( outin iCiC 1

Page 5: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Fragment

• Fragment in OGL: after the rasterization stage (including texturing), the data are not yet pixel, but are fragments– Fragment is all the data associated with a pixel,

including coordinate, color, depth and texture coordinates.

Page 6: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Blending in OGL

• If a fragment makes it to FB, the pixel is read out and blended with the fragment’s color and then written back to FB

• The contributions of fragment and FB pixel is specified: glBlendFunc( src, dst )

Page 7: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Blending Function

• Four choices:– GL_ONE – GL_ZERO– GL_SRC_ALPHA– GL_ONE_MINUS_SRC_ALPHA

• Blending is enabled using glEnable(GL_BLEND)• Note: If your OpenGL implementation supports the

GL_ARB_imaging extension, you can modify the blending equation as well.

Page 8: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

3D Blending with Depth Buffer

• A scene of opaque and translucent objects– Enable depth buffering and depth test– Draw opaque objects– Make the depth buffer read only, with

glDepthMask(GL_FALSE)– Draw the translucent objects (sort those triangles

still, but which order, FTB or BTF?)

Page 9: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

How Many Buffers Are There?

• In OpenGL:– Color buffers: front, back, front-left, front-right, back-left,

back-right and other auxiliaries– Depth buffer– Stencil buffer– Accumulation buffer

• Exactly how many bits are there in each buffer, depends on – OGL implementation – what you choose

• Each buffer can be individually cleared

Page 10: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Selecting Color Buffer for Writing and Reading

• Drawing and reading can go into any of front, back, front-left, front-right, back-left, back-right and other auxiliaries

• glDrawBuffer: select buffer to be written or drawn into– Can have multiple writing buffer at the same time

• glReadBuffer: select buffer as the source for – glReadPixels– glCopyPixels– glCopyTexImage (copy from FB to a texture image)– glCopyTexSubImage

Page 11: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Accumulation Buffer

• Problems of compositing into color buffers– limited color resolution (e.g. 8 bits/channel)

• clamping• loss of accuracy

• Accumulation buffer acts as a “floating point” color buffer– accumulate into accumulation buffer– transfer results to frame buffer

Page 12: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Accumulation Buffer

• OGL don’t directly write into A-buffer• Typically, a series of images are rendered to

a standard color buffer and then accumulated, one at a time, into the A-buffer.

• Then, the result in A-buffer has to be copied back to a color buffer for viewing

• A-buffer may have more bits/color

Page 13: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Accessing Accumulation Buffer

• glAccum(op, value ) operations– within the accumulation buffer:

• GL_ADD, GL_MULT

– from read buffer• GL_ACCUM, GL_LOAD

– transfer back to write buffer• GL_RETURN

– glAccum( GL_ACCUM, 0.5) multiplies each value in read buffer by 0.5 and adds to accumulation buffer

– How do you average N images?!

Page 14: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Accumulation Buffer Applications

• Compositing• Full Scene Anti-aliasing• Depth of Field• Filtering• Motion Blur

Page 15: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Full Scene Antialiasing• Jittering the view (how?? )• Each time we move the viewer, the image shifts• Different aliasing artifacts in each image• Averaging images using accumulation buffer

averages out these artifacts• Like super-sampling in ray-tracing

Page 16: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Depth of Focus

• Keeping a Plane in Focus

• Jitter the viewer to keep one plane unchanged

Move the camera in a plane parallel to the focal plane.

How do you jitter the viewer ????

Page 17: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

What else?

• Can you do soft shadows?– Jitter the light sources

• What about motion blur– Jitter the moving object in the scene– glAccum(GL_MULT, decayFactor)

Page 18: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

A Fragment’s Journey Towards FB

• Many Tests (on/off with glEnable)– scissor test - an additional clipping test– alpha test - a filtering test based on alpha– stencil test - a pixel mask test– depth test - fragment occlusion test

Page 19: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Scissor Box

• Additional Clipping test– glScissor( x, y, w, h )

• any fragments outside of box are clipped• useful for updating a small section of a

viewport• affects glClear() operations

Page 20: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Alpha Test

• Reject pixels based on their alpha value• glAlphaFunc(func, value)

– GL_NEVER GL_LESS– GL_EQUAL GL_LEQUAL– GL_GREATER GL_NOTEQUAL– GL_GEUQAL GL_ALWAYS

• For instance, use as a mask for texture– How would you render a fence?

Page 21: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Stencil Test

• Used to control drawing based on values in the stencil buffer

• Fragments that failt the stencil test are not drawn– Example: create a mask in stencil buffer and draw

only objects not in mask area

Page 22: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Stencil Buffer

• Don’t render into stencil buffer– Control stencil buffer values with stencil

function– Can change stencil buffer values with each

fragment passing or failing the test

Page 23: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Controlling Stencil Buffer

• For each pixel, what do I do? Look:– glStencilFunc( func, ref, mask )

• compare value in buffer with (ref AND mask) and (stencil_pixel AND mask) using func

• func is one of standard comparison functions

– glStencilOp( fail, zfail, zpass )• Allows changes in stencil buffer based on:

• Failing stencil test

• Failing depth test

• Passing depth test

• GL_KEEP, GL_INCR, GL_REPLACE, GL_DECR, GL_INVERT, GL_ZERO

Page 24: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Creating a Mask

• Initialize MaskglInitDisplayMode( …|GLUT_STENCIL|… );

glEnable( GL_STENCIL_TEST );

glClearStencil( 0x0 );

glStencilFunc( GL_ALWAYS, 0x1, 0x1 );

glStencilOp( GL_REPLACE, GL_REPLACE,

GL_REPLACE );

Page 25: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Using Stencil Mask

• glStencilFunc( GL_EQUAL, 0x1, 0x1 )

draw objects where stencil = 1• glStencilFunc( GL_NOT_EQUAL, 0x1, 0x1 );• glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );

draw objects where stencil != 1

Page 26: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Dithering

• glEnable( GL_DITHER )• Dither colors for better looking results

• Used to simulate more available colors– Say, newspapers

• OGL will modify the color of a fragment with a dithering table

Page 27: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Logical Operations on Fragments

• Combine fragment and pixel color using bit-wise logical operations: glLogicOp( mode )

• Common modes– GL_CLEAR GL_SET GL_COPY,– GL_COPY_INVERTED GL_NOOP GL_INVERT– GL_AND GL_NAND GL_OR– GL_NOR GL_XOR GL_AND_INVERTED– GL_AND_REVERSE GL_EQUIV– GL_OR_INVERTED GL_OR_REVERSE

Page 28: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Texturing

• Texture functions: glTexEnv( ), specifies how texture values are combined with the color values of each fragment

• Texture environment mode: Tables 9-4,5– GL_DECAL– GL_REPLACE– GL_MODULATE– GL_BLEND

Page 29: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Multi-Texturing

• Should have at least 2 texture units if multi-texturing is supported

• Each texture unit:– Texture image– Filtering parameters– Environment application– Texture matrix stack– Automatic texture-coordinate generation (doesn’t

seem obvious, but very have very creative apps)

Page 30: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Texture Matrix Stack

• At least 2 matrices deep• 4x4 matrix• Can take texture coordinates in

homogeneous space (s, t, r, q)

Page 31: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

More extensions• [Segal 1992], need OpenGL extensions. Multi-texturing is made extremely interesting and creative.

Please check nvidia.com for white papers on these.• Not in the current redbook published in 1997.

#ifndef GL_VERSION_texture_env_combine#define GL_COMBINE 0x8570#define GL_ADD_SIGNED 0x8574#define GL_INTERPOLATE 0x8575#define GL_CONSTANT 0x8576#define GL_PRIMARY_COLOR 0x8577#define GL_PREVIOUS 0x8578#define GL_COMBINE_RGB 0x8571#define GL_COMBINE_ALPHA 0x8572#define GL_SOURCE0_RGB 0x8580#define GL_SOURCE1_RGB 0x8581#define GL_SOURCE2_RGB 0x8582#define GL_SOURCE0_ALPHA 0x8588#define GL_SOURCE1_ALPHA 0x8589#define GL_SOURCE2_ALPHA 0x858A#define GL_OPERAND0_RGB 0x8590#define GL_OPERAND1_RGB 0x8591#define GL_OPERAND2_RGB 0x8592#define GL_OPERAND0_ALPHA 0x8598#define GL_OPERAND1_ALPHA 0x8599#define GL_OPERAND2_ALPHA 0x859A#define GL_RGB_SCALE 0x8573

Page 32: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

OpenGL MultitextureExtension

• Standardized and widely available– Introduces concept of multiple texture units

• Two or more texture units

• Existing texture API extended to implicitly useactive texture unit

glActiveTextureARB(GL_TEXTUREn_ARB)

– Each unit has its own complete and independent OpenGL texture state

• Texture image, filtering modes, textureenvironment, texture matrix

Page 33: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

OpenGL Multitexture Quick Tutorial

– Configuring up a given texture unit:glActiveTextureARB(GL_TEXTURE1_ARB);glBindTexture(GL_TEXTURE_2D, texObject);glTexImage2D(GL_TEXTURE_2D, …);glTexParameterfv(GL_TEXTURE_2D, …);glTexEnvfv(GL_TEXTURE_ENV, …);glTexGenfv(GL_S, …);glMatrixMode(GL_TEXTURE);glLoadIdentity();

– Setting texture coordinates for a vertex:glMultiTexCoord4f(GL_TEXTURE0_ARB, s0, t0, r0 ,q0);glMultiTexCoord2f(GL_TEXTURE1_ARB, s1, t1);glMultiTexCoord3f(GL_TEXTURE2_ARB, s2, t2, r2);glVertex3f(x, y, z);

Implicitlyupdatestate ofactivetextureunit

Sets active texture unit

Page 34: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

OpenGL Multitexture Texture Environments

• Chain of Texture Environment Stages

GL_MODULATE

GL_DECAL

GL_BLEND

glMultiTexCoord2f( GL_TEXTURE0_ARB, …)

glMultiTexCoord2f( GL_TEXTURE1_ARB, …)

glMultiTexCoord2f( GL_TEXTURE2_ARB, …)

Pre-texturing colorglColor3f(r,g,b)

#0

#1

#2Post-texturingcolor

Lookup& filter

Lookup& filter

Lookup& filter

Page 35: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Extending the OpenGLTexture Environment

• ARB_multitexture is just the start– Standard OpenGL has just GL_REPLACE,

GL_MODULATE, GL_DECAL, and GL_BLEND

– ENV_texture_env_add introduces GL_ADD

– ENV_texture_env_combine• Powerful “(AB + C) scale + bias”

• A, B, C variables use various color inputs

– Future texture environments even more powerful

Page 36: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Examples of More PowerfulTexture Environments

• NVIDIA GPU OpenGL extensions– TNT and later GPUs support

NV_texture_env_combine4• A * B + C * D computation

• multiple texture inputs available in a single stage

• enables single-pass emboss bump mapping

– GeForce and later GPUs support NV_register_combiners

• dot products, signed math, much more

• register processing model for non-sequential data flows

Page 37: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Cube Map Texturing

• Direct Use of Cube Maps– Latest hardware supports this

• DirectX 7 support

• OpenGL extension support

– Hardware directly performs cube map accesses• Requires hardware support for good performance

– Cube map construction much easier

Page 38: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Using Cube Mapsin OpenGL

• Very easy to use ARB extension– New texture target:

GL_TEXTURE_CUBE_MAP_ARB• Used for manipulating cube map as a whole

– Six cube map “face” targets• Used for manipulating particular cube map faces• GL_TEXTURE_CUBE_MAP_dir_axis _ARB• dir is { POSITIVE, NEGATIVE }• axis is { X, Y, Z }• Use 2D texture image API (glTexImage2D, etc)

Page 39: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Cube Map Support

• Official OpenGL standard now– OpenGL cube map support was introduced by NVIDIA

• GeForce drivers have EXT_texture_cube_map extension

– OpenGL Architectural Review Board (ARB) recently accepted the extension as an ARB standard

• renamed ARB_texture_cube_map

• but same identical functionality

• semantics & enumerant values same as the EXT version

– Other vendors such as ATI planning cube map support

Page 40: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

OpenGL Cube MapSetup Usage

Binding to a cube map textureglEnable(GL_TEXTURE_CUBE_MAP_ARB);glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, cmap);

Loading cube map facesglTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,

level, format, width, height, border, format, type, positiveXpixels);glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,

level, format, width, height, border, format, type, negativeXpixels);glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,

level, format, width, height, border, format, type, negativeYpixels);

•. . . similarly load other three faces . . .

Page 41: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

OpenGL Cube Map Rendering Usage

• Enabling a cube map textureglEnable(GL_TEXTURE_CUBE_MAP_ARB);

• Explicit cube map coordinatesglTexCoord3f(vx, vy, vz); // (vx,vy,vz) is unnormalized direction vector

• Generated cube map coordinates (recommended)glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);

glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);glEnable(GL_TEXTURE_GEN_S);glEnable(GL_TEXTURE_GEN_T);glEnable(GL_TEXTURE_GEN_R);glEnable(GL_NORMALIZE);

Page 42: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Advantages of Cube Maps

• Compared to Other Approaches– View-independent

• Unlike sphere mapping

– Uses a single texture unit• Unlike dual-paraboloid mapping

– Entire texture resolution is sampled• Unlike sphere or dual-paraboloid approaches

– Improved environment sampling

Page 43: Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Getting Back to Object Space• No magic here. Just undo the transformation.

Gldouble objX, objY, objZ;

// get the modelview, project, and viewport

glGetDoublev( GL_MODELVIEW_MATRIX, mv_mat );

glGetDoublev( GL_PROJECTION_MATRIX, proj_mat );

glGetIntegerv( GL_VIEWPORT, viewport );

// get the current depth buffer

g_depth_view = new GLfloat[winWidth * winHeight];

glReadPixels( 0, 0, winWidth, winHeight, GL_DEPTH_COMPONENT, GL_FLOAT, depth_view );

for( y = 0; y < winHeight; y ++ ) // go through every pixel in frame buffer

for( x = 0; x < winWidth; x ++ ) {

if ( *depth_view < 1.00 )

gluUnProject(x, y, *depth_view, mv_mat, proj_mat, viewport, &objX, &objY, &objZ);

depth_view ++;

}