An Interactive Introduction to OpenGL Programming

Post on 16-Jan-2016

47 views 0 download

Tags:

description

An Interactive Introduction to OpenGL Programming. Ed Angel Dave Shreiner Vicki Shreiner. Welcome. Today’s Goals and Agenda Describe OpenGL and its uses Demonstrate and describe OpenGL’s capabilities and features Enable you to write an interactive, 3-D computer graphics program in OpenGL. - PowerPoint PPT Presentation

Transcript of An Interactive Introduction to OpenGL Programming

An Interactive Introduction to OpenGL Programming

An Interactive Introduction to OpenGL Programming

Ed Angel

Dave Shreiner

Vicki Shreiner

Ed Angel

Dave Shreiner

Vicki Shreiner

WelcomeWelcome

• Today’s Goals and Agenda

– Describe OpenGL and its uses

– Demonstrate and describe OpenGL’s capabilities and features

– Enable you to write an interactive, 3-D computer graphics program in OpenGL

What Is OpenGL, and What Can It Do for Me?What Is OpenGL, and What Can It Do for Me?

• OpenGL is a computer graphics rendering API

– Generate high-quality color images by rendering with geometric and image primitives

– Create interactive applications with 3D graphics

– OpenGL is

• operating system independent

• window system independent

Related APIsRelated APIs

• GLU (OpenGL Utility Library)

– part of OpenGL

– NURBS, tessellators, quadric shapes, etc.

• AGL, GLX, WGL

– glue between OpenGL and windowing systems

• GLUT (OpenGL Utility Toolkit)

– portable windowing API

– not officially part of OpenGL

OpenGL and Related APIs

GLUT

GLU

GL

GLX, AGLor WGL

X, Win32, Mac O/S

software and/or hardware

application program

OpenGL Motifwidget or similar

General Structure of an OpenGL ProgramGeneral Structure of an OpenGL Program

Configure and open a

window Initialize OpenGL’s

state Process user events

Draw an image

An OpenGL ProgramAn OpenGL Program

#include <GL/glut.h>#include "cube.h"

void main( int argc, char *argv[] ){ glutInit( &argc, argv ); glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH ); glutCreateWindow( argv[0] );

init();

glutDisplayFunc( display ); glutReshapeFunc( reshape );

glutMainLoop();}

The main part ofthe program.GLUT is used to

open the OpenGLwindow, and handleinput from the user.

An OpenGL Program(cont’d.)An OpenGL Program(cont’d.)

Set up some initial OpenGL state

Handle when theuser resizes the

window

void init( void ){ glClearColor( 0, 0, 0, 1 ); gluLookAt( 2, 2, 2, 0, 0, 0, 0, 1, 0 ); glEnable( GL_DEPTH_TEST );}

void reshape( int width, int height ){ glViewport( 0, 0, width, height ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 60, (GLfloat) width / height, 1.0, 10.0 ); glMatrixMode( GL_MODELVIEW );}

An OpenGL Program (cont’d.)An OpenGL Program (cont’d.)

void display( void ){ int i, j;

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glBegin( GL_QUADS ); for ( i = 0; i < NUM_CUBE_FACES; ++i ) { glColor3fv( faceColor[i] ); for ( j = 0; j < NUM_VERTICES_PER_FACE; ++j ) { glVertex3fv( vertex[face[i][j]] ); } } glEnd();

glFlush();}

Have OpenGLdraw a cubefrom some3D points(vertices)

OpenGL Command Formats

glVertex3fv( glVertex3fv( vv ) )

Number ofNumber ofcomponentscomponents

2 - (x,y) 2 - (x,y) 3 - (x,y,z)3 - (x,y,z)4 - (x,y,z,w)4 - (x,y,z,w)

Data TypeData Typeb - byteb - byteub - unsigned byteub - unsigned bytes - shorts - shortus - unsigned shortus - unsigned shorti - inti - intui - unsigned intui - unsigned intf - floatf - floatd - doubled - double

VectorVector

omit “v” foromit “v” forscalar formscalar form

glVertex2f( x, y )glVertex2f( x, y )

What Is Required For Your ProgramsWhat Is Required For Your Programs

• Headers Files

#include <GL/gl.h>

#include <GL/glu.h>

#include <GL/glut.h>

• Libraries

• Enumerated Types

– OpenGL defines numerous types for compatibility

• GLfloat, GLint, GLenum, etc.

GLUT BasicsGLUT Basics

• Application Structure:

– Configure and open window

– Initialize OpenGL state

– Register input callback functions

– Enter event processing loop

GLUT Callback FunctionsGLUT Callback Functions

• Routine to call when something happens

– window resize or redraw

– user input

– animation

• “Register” callbacks with GLUT

glutDisplayFunc( display );

glutIdleFunc( idle );

glutKeyboardFunc( keyboard );

What can OpenGL Draw?What can OpenGL Draw?

• Geometric primitives

– points, lines and polygons

• Image Primitives

– images and bitmaps

– separate pipeline for images and geometry

• linked through texture mapping

• Rendering depends on state

– colors, materials, light sources, etc.

OpenGL Geometric Primitives

• All geometric primitives are specified by vertices

• All geometric primitives are specified by vertices

GL_QUAD_STRIPGL_QUAD_STRIP

GL_POLYGONGL_POLYGON

GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP GL_TRIANGLE_FANGL_TRIANGLE_FAN

GL_POINTSGL_POINTS

GL_LINESGL_LINES

GL_LINE_LOOPGL_LINE_LOOP

GL_LINE_STRIPGL_LINE_STRIP

GL_TRIANGLESGL_TRIANGLES

GL_QUADSGL_QUADS

Specifying Geometric Primitives

• Primitives are specified using

glBegin( glBegin( primType primType ););

glEnd();glEnd();

• primType determines how vertices are combined

glBegin( glBegin( primType primType ););for ( i = 0; i < n; ++i ) { for ( i = 0; i < n; ++i ) { glColor3f( red[i], green[i], blue[i] );glColor3f( red[i], green[i], blue[i] ); glVertex3fv( coords[i] );glVertex3fv( coords[i] );}}glEnd();glEnd();

How OpenGL Works:The Conceptual ModelHow OpenGL Works:The Conceptual Model

Configurehow OpenGLshould draw

stuff

Draw stuff

Controlling OpenGL’s DrawingControlling OpenGL’s Drawing

• Set OpenGL’s rendering state

– State controls how things are drawn

• shading – lighting

• texture maps – line styles (stipples)

• polygon patterns – transparency

The Power of Setting OpenGL StateThe Power of Setting OpenGL State

Appearance is controlled by setting OpenGL’s state.

Setting OpenGL StateSetting OpenGL State

• Three ways to set OpenGL state:

1. Set values to be used for processing vertices

• most common methods of setting state

– glColor() / glIndex()

– glNormal()

– glTexCoord()

• state must be set before calling glVertex()

Setting OpenGL State (cont’d.)Setting OpenGL State (cont’d.)

2.2. Turning on a rendering modeTurning on a rendering mode

glEnable() / glDisable()glEnable() / glDisable()

3.3. Configuring the specifics of a particular rendering Configuring the specifics of a particular rendering modemode

• Each mode has unique commands for setting its values

glMaterialfv()glMaterialfv()

OpenGL and ColorOpenGL and Color

• The OpenGL Color Model

– OpenGL uses the RGB color space• There is also a color-index mode, but we do not discuss it

• Colors are specified as floating-pointnumbers in the range [ 0.0, 1.0 ]

– for example, to set a window’s background color, you would call

glClearColor( 1.0, 0.3, 0.6, 1.0 );

Shapes TutorialShapes Tutorial

TransformationsTransformations

Camera Analogy

• 3D is just like taking a photograph (lots of photographs!)

camera

tripod model

viewingvolume

Camera Analogy and Transformations

• Projection transformations– adjust the lens of the camera

• Viewing transformations– tripod–define position and orientation of the viewing volume

in the world

• Modeling transformations– moving the model

• Viewport transformations– enlarge or reduce the physical photograph

Coordinate Systems and TransformationsCoordinate Systems and Transformations

• Steps in forming an image

1. specify geometry (world coordinates)

2. specify camera (camera coordinates)

3. project (window coordinates)

4. map to viewport (screen coordinates)

• Each step uses transformations

• Every transformation is equivalent to a change in coordinate systems (frames)

Homogeneous Coordinates

– each vertex is a column vector

– w is usually 1.0

– all operations are matrix multiplications

– directions (directed line segments) can be representedwith w = 0.0

w

z

y

x

v

151173

141062

13951

12840

mmmm

mmmm

mmmm

mmmm

M

3D Transformations

• A vertex is transformed by 4 x 4 matrices– all affine operations are matrix multiplications

– all matrices are stored column-major in OpenGL

– matrices are always post-multiplied

– product of matrix and vector is v

M

Specifying TransformationsSpecifying Transformations

• Programmer has two styles of specifying transformations– specify matrices (glLoadMatrix, glMultMatrixglLoadMatrix, glMultMatrix)

– specify operation (glRotate, glOrthoglRotate, glOrtho)

• Programmer does not have to remember the exact matrices– see appendix of the OpenGL Programming Guide

Programming Transformations

• Prior to rendering, view, locate, and orient:

– eye/camera position

– 3D geometry

• Manage the matrices

– including matrix stack

• Combine (composite) transformations

vertex

ModelviewMatrix

ProjectionMatrix

PerspectiveDivision

ViewportTransform

Modelview

Modelview

Projection

object eye

clip normalizeddevice

window

• other calculations here– material color– shade model (flat)– polygon rendering mode– polygon culling– clipping

TransformationPipelineTransformationPipeline

Matrix Operations

• Specify Current Matrix Stack

glMatrixMode(glMatrixMode(GL_MODELVIEWGL_MODELVIEW or or GL_PROJECTIONGL_PROJECTION))

• Other Matrix or Stack Operations

glLoadIdentity() glPushMatrix()glLoadIdentity() glPushMatrix()

glPopMatrix()glPopMatrix()

• Viewport

– usually same as window size

– viewport aspect ratio should be same as projection transformation or resulting image may be distorted

glViewport( glViewport( x, y, width, heightx, y, width, height ) )

• Specify Current Matrix Stack

glMatrixMode(glMatrixMode(GL_MODELVIEWGL_MODELVIEW or or GL_PROJECTIONGL_PROJECTION))

• Other Matrix or Stack Operations

glLoadIdentity() glPushMatrix()glLoadIdentity() glPushMatrix()

glPopMatrix()glPopMatrix()

• Viewport

– usually same as window size

– viewport aspect ratio should be same as projection transformation or resulting image may be distorted

glViewport( glViewport( x, y, width, heightx, y, width, height ) )

Projection Transformation

• Shape of viewing frustum

• Perspective projection

gluPerspective( gluPerspective( fovy, aspect, zNear, zFarfovy, aspect, zNear, zFar ) )

glFrustumglFrustum(( left,left, right,right, bottom,bottom, top,top, zNear,zNear, zFarzFar ))

• Orthographic parallel projection

glOrtho(glOrtho(left,left, right,right, bottom,bottom, top,top, zNear,zNear, zFarzFar))

gluOrtho2D( gluOrtho2D( left, right, bottom, topleft, right, bottom, top ) )

• calls glOrtho with z values near zero

Applying Projection Transformations

• Typical use (orthographic projection)glMatrixMode( GL_PROJECTION );

glLoadIdentity();

glOrtho(left, right, bottom, top, zNear, zFar);

Viewing Transformations

• Position the camera/eye in the scene– place the tripod down; aim camera

• To “fly through” a scene– change viewing transformation and

redraw scene

gluLookAt( eyex, eyey, eyez, aimx, aimy, aimz, upx, upy, upz )

– up vector determines unique orientation

– careful of degenerate positions

tripod

Projection TutorialProjection Tutorial

Modeling Transformations

• Move object

glTranslate{fd}( glTranslate{fd}( x, y, zx, y, z ) )

• Rotate object around arbitrary axis

glRotate{fd}( glRotate{fd}( angle, x, y, zangle, x, y, z ) )

– angle is in degrees

• Dilate (stretch or shrink) or mirror object

glScale{fd}( glScale{fd}( x, y, zx, y, z ) )

zyx

Transformation TutorialTransformation Tutorial

Connection: Viewing and Modeling

• Moving camera is equivalent to moving every object in the world towards a stationary camera

• Viewing transformations are equivalent to several modeling transformations

– gluLookAt() has its own command

– can make your own polar view or pilot view

Compositing Modeling Transformations

• Problem: hierarchical objects

– one position depends upon a previous position

– robot arm or hand; sub-assemblies

• Solution: moving local coordinate system

– modeling transformations move coordinate system

– post-multiply column-major matrices

– OpenGL post-multiplies matrices

Compositing Modeling Transformations• Problem: objects move relative to absolute world origin

– my object rotates around the wrong origin

• make it spin around its center or something else

• Solution: fixed coordinate system

– modeling transformations move objects around fixed coordinate system

– pre-multiply column-major matrices

– OpenGL post-multiplies matrices

– must reverse order of operations to achieve desired effect

Additional Clipping Planes

• At least 6 more clipping planes available

• Good for cross-sections

• Modelview matrix moves clipping plane

• clipped

glEnable( GL_CLIP_PLANEi )

glClipPlane( GL_CLIP_PLANEi, GLdouble* coeff )

0 DCzByAx

Animation and Depth BufferingAnimation and Depth Buffering

DoubleBuffering

12

48

16

12

48

16FrontBuffer

BackBuffer

Display

Animation Using Double Buffering

Request a double buffered color buffer

glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );

Clear color buffer

glClear( GL_COLOR_BUFFER_BIT );

Render scene

Request swap of front and back buffers

glutSwapBuffers();

• Repeat steps 2 - 4 for animation

Request a double buffered color buffer

glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );

Clear color buffer

glClear( GL_COLOR_BUFFER_BIT );

Render scene

Request swap of front and back buffers

glutSwapBuffers();

• Repeat steps 2 - 4 for animation

Depth Buffering andHidden Surface Removal

12

48

16

12

48

16ColorBuffer

DepthBuffer

Display

Depth Buffering Using OpenGL Request a depth buffer

glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );

Enable depth bufferingglEnable( GL_DEPTH_TEST );

Clear color and depth buffersglClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

Render scene Swap color buffers

Request a depth bufferglutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );

Enable depth bufferingglEnable( GL_DEPTH_TEST );

Clear color and depth buffersglClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

Render scene Swap color buffers

LightingLighting

Lighting PrinciplesLighting Principles

• Lighting simulates how objects reflect light

– material composition of object

– light’s color and position

– global lighting parameters

• ambient light

• two sided lighting

– available in both color indexand RGBA mode

How OpenGL Simulates LightsHow OpenGL Simulates Lights

• Phong lighting model

– Computed at vertices

• Lighting contributors

– Surface material properties

– Light properties

– Lighting model properties

SurfaceNormalsSurfaceNormals

• Normals define how a surface reflects light

glNormal3f( x, y, z )

– Current normal is used to compute vertex’s color

– Use unit normals for proper lighting

• scaling affects a normal’s length

glEnable( GL_NORMALIZE )or

glEnable( GL_RESCALE_NORMAL )

Material PropertiesMaterial Properties

• Define the surface properties of a primitive

glMaterialfv( face, property, value );

– separate materials for front and back

GL_DIFFUSE Base color

GL_SPECULAR Highlight Color

GL_AMBIENT Low-light Color

GL_EMISSION Glow Color

GL_SHININESS Surface Smoothness

Light PropertiesLight Properties

glLightfv( light, property, value );

– light specifies which light

• multiple lights, starting with GL_LIGHT0

glGetIntegerv( GL_MAX_LIGHTS, &n );

– properties• colors

• position and type

• attenuation

Light Sources (cont'd.)

• Light color properties

– GL_AMBIENT

– GL_DIFFUSE

– GL_SPECULAR

• Light color properties

– GL_AMBIENT

– GL_DIFFUSE

– GL_SPECULAR

Types of LightsTypes of Lights

• OpenGL supports two types of Lights

– Local (Point) light sources

– Infinite (Directional) light sources

• Type of light controlled by w coordinate

wz

wy

wxw

zyxw

at positioned Light Local

along directed LightInfinite

0

0

Turning on the LightsTurning on the Lights

• Flip each light’s switch

glEnable( GL_LIGHTn );

• Turn on the power

glEnable( GL_LIGHTING );

Light Material TutorialLight Material Tutorial

Controlling a Light’s PositionControlling a Light’s Position

• Modelview matrix affects a light’s position

– Different effects based on when position is specified

• eye coordinates

• world coordinates

• model coordinates

– Push and pop matrices to uniquely control a light’s position

Light Position TutorialLight Position Tutorial

Tips for Better LightingTips for Better Lighting

• Recall lighting computed only at vertices

– model tessellation heavily affects lighting results

• better results but more geometry to process

• Use a single infinite light for fastest lighting

– minimal computation per vertex

Imaging and Raster PrimitivesImaging and Raster Primitives

Pixel-based primitivesPixel-based primitives

• Bitmaps

– 2D array of bit masks for pixels• update pixel color based on current color

• Images

– 2D array of pixel color information• complete color information for each pixel

• OpenGL does not understand image formats

FrameBuffer

Rasterization(including

Pixel Zoom)

Per FragmentOperations

TextureMemory

Pixel-TransferOperations

(and Pixel Map)CPU

PixelStorageModes

glReadPixels(), glCopyPixels()

glBitmap(), glDrawPixels()

glCopyTex*Image();

Pixel PipelinePixel Pipeline

• Programmable pixel storage and transfer operations

Positioning Image PrimitivesPositioning Image Primitives

glRasterPos3f( x, y, z )

– raster position transformed like geometry

– discarded if raster positionis outside of viewport

• may need to fine tuneviewport for desiredresults

Raster Position

Texture MappingPart 1Texture MappingPart 1

Texture MappingTexture Mapping

s

t

x

y

z

image

geometry screen

Texture Mapping and the OpenGL PipelineTexture Mapping and the OpenGL Pipeline

geometry pipelinevertices

pixel pipelineimage

rasterizer

• Images and geometry flow through separate pipelines that join at the rasterizer

– “complex” textures do not affect geometric complexity

Texture ExampleTexture Example

• The texture (below) is a 256 x 256 image that has been mapped to a rectangular polygon which is viewed in perspective

Applying Textures IApplying Textures I

• Three steps to applying a texture

1. specify the texture• read or generate image

• assign to texture

• enable texturing

2. assign texture coordinates to vertices

3. specify texture parameters• wrapping, filtering

Texture Objects

• Have OpenGL store your images– one image per texture object

– may be shared by several graphics contexts

• Generate texture names

glGenTextures( n, *texIds );

Texture Objects (cont'd.)Texture Objects (cont'd.)

• Create texture objects with texture data and state

glBindTexture( target, id );

• Bind textures before using

glBindTexture( target, id );

• Define a texture image from an array of texels in CPU memory

glTexImage2D( target, level, components, w, h, border, format, type, *texels );

– dimensions of image must be powers of 2

• Texel colors are processed by pixel pipeline

– pixel scales, biases and lookups can bedone

Specify TextureImageSpecify TextureImage

Converting A Texture Image

• If dimensions of image are not power of 2

gluScaleImage( format, w_in, h_in, type_in, *data_in, w_out, h_out,

type_out, *data_out );

– *_in is for source image

– *_out is for destination image

• Image interpolated and filtered during scaling

• Based on parametric texture coordinates

• glTexCoord*() specified at each vertex

s

t1, 1

0, 1

0, 0 1, 0

(s, t) = (0.2, 0.8)

(0.4, 0.2)

(0.8, 0.4)

A

B C

a

bc

Texture Space Object Space

Mapping aTextureMapping aTexture

Generating Texture Coordinates

• Automatically generate texture coords

glTexGen{ifd}[v]()

• specify a plane

– generate texture coordinates based upon distance from plane

• generation modes

– GL_OBJECT_LINEAR

– GL_EYE_LINEAR

– GL_SPHERE_MAP

0 DCzByAx

Tutorial: TextureTutorial: Texture

Texture MappingPart 2Texture MappingPart 2

Applying Textures II

– specify textures in texture objects

– set texture filter

– set texture function

– set texture wrap mode

– set optional perspective correction hint

– bind texture object

– enable texturing

– supply texture coordinates for vertex

• coordinates can also be generated

– specify textures in texture objects

– set texture filter

– set texture function

– set texture wrap mode

– set optional perspective correction hint

– bind texture object

– enable texturing

– supply texture coordinates for vertex

• coordinates can also be generated

• Filter Modes

– minification or magnification

– special mipmap minification filters

• Wrap Modes

– clamping or repeating

• Texture Functions

– how to mix primitive’s color with texture’s color

• blend, modulate or replace texels

Texture Application Methods

Filter Modes

Texture Polygon

Magnification Minification

PolygonTexture

Example:

glTexParameteri( glTexParameteri( target, type, modetarget, type, mode ); );

Mipmapped Textures

• Mipmap allows for prefiltered texture maps of decreasing resolutions

• Lessens interpolation errors for smaller textured objects

• Declare mipmap level during texture definition

glTexImage*D( glTexImage*D( GL_TEXTURE_*D, level, …GL_TEXTURE_*D, level, … ) )

• GLU mipmap builder routines

gluBuild*DMipmaps( … )gluBuild*DMipmaps( … )

• OpenGL 1.2 introduces advanced LOD controls

Wrapping Mode

• Example:

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP )

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )

texture

s

t

GL_CLAMPwrapping

GL_REPEATwrapping

Texture Functions

• Controls how texture is appliedglTexEnv{fi}[v]( GL_TEXTURE_ENV, prop, param )

• GL_TEXTURE_ENV_MODE modes

– GL_MODULATE

– GL_BLEND

– GL_REPLACE

• Set blend color with GL_TEXTURE_ENV_COLOR

Advanced OpenGL TopicsAdvanced OpenGL Topics

Immediate Mode versus Display Listed Rendering

• Immediate Mode Graphics

– Primitives are sent to pipeline and display right away

– No memory of graphical entities

• Display Listed Graphics

– Primitives placed in display lists

– Display lists kept on graphics server

– Can be redisplayed with different state

– Can be shared among OpenGL graphics contexts

Immediate Mode versus Display Lists

Immediate Mode

Display Listed

DisplayList

PolynomialEvaluator

Per VertexOperations &

PrimitiveAssembly

RasterizationPer Fragment

Operations

TextureMemory

CPU

PixelOperations

FrameBuffer

Display Lists

• Creating a display listGLuint id;void init( void ){ id = glGenLists( 1 ); glNewList( id, GL_COMPILE ); /* other OpenGL routines */ glEndList();}

• Call a created listvoid display( void ){ glCallList( id );}

Display Lists

• Not all OpenGL routines can be stored in display lists

• State changes persist, even after a display list is finished

• Display lists can call other display lists

• Display lists are not editable, but you can fake it

– make a list (A) which calls other lists (B, C, and D)

– delete and replace B, C, and D, as needed

Display Lists and HierarchyDisplay Lists and Hierarchy

• Consider model of a car

– Create display list for chassis

– Create display list for wheelglNewList( CAR, GL_COMPILE );

glCallList( CHASSIS );glTranslatef( … );glCallList( WHEEL );glTranslatef( … );glCallList( WHEEL );

…glEndList();

Advanced Primitives

• Vertex Arrays

• Bernstein Polynomial Evaluators

– basis for GLU NURBS

• NURBS (Non-Uniform Rational B-Splines)

• GLU Quadric Objects

– sphere

– cylinder (or cone)

– disk (circle)

• Vertex Arrays

• Bernstein Polynomial Evaluators

– basis for GLU NURBS

• NURBS (Non-Uniform Rational B-Splines)

• GLU Quadric Objects

– sphere

– cylinder (or cone)

– disk (circle)

Alpha: the 4th Color ComponentAlpha: 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 )

BlendingBlending

• Combine fragments with pixel values that are already in the framebuffer

glBlendFunc( src, dst )

FramebufferFramebufferPixelPixel((dstdst))

BlendingEquation

BlendingEquation

FragmentFragment((srcsrc))

BlendedBlendedPixelPixel

pfr CdstCsrcC

FogFog

glFog{if}( property, value )

• Depth Cueing

– Specify a range for a linear fog ramp• GL_FOG_LINEAR

• Environmental effects

– Simulate more realistic fog• GL_FOG_EXP

• GL_FOG_EXP2

Multi-pass RenderingMulti-pass Rendering

• Blending allows results from multiple drawing passes to be combined together

– enables more complex rendering algorithms

Example of bump-mappingdone with a multi-pass

OpenGL algorithm

AntialiasingAntialiasing

• Removing the Jaggies

glEnable( mode )• GL_POINT_SMOOTH

• GL_LINE_SMOOTH

• GL_POLYGON_SMOOTH

– alpha value computed by computingsub-pixel coverage

– available in both RGBA and colormap modes

Accumulation BufferAccumulation Buffer

• Problems of compositing into color buffers– limited color resolution

• clamping

• loss of accuracy

– Accumulation buffer acts as a “floating point” color buffer

• accumulate into accumulation buffer

• transfer results to frame buffer

Accumulation Buffer ApplicationsAccumulation Buffer Applications

• Compositing

• Full Scene Antialiasing

• Depth of Field

• Filtering

• Motion Blur

Stencil BufferStencil Buffer

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

– Fragments that fail the stencil test are not drawn

– Example: create a mask in stencil buffer and draw only objects not in mask area

Fog TutorialFog Tutorial

Getting to the FramebufferGetting to the Framebuffer

BlendingBlendingDepthTest

DepthTest DitheringDithering Logical

Operations

LogicalOperations

ScissorTest

ScissorTest

StencilTest

StencilTest

AlphaTest

AlphaTest

Fra

gmen

t

Fra

meb

uffe

r

Alpha TestAlpha Test

• Reject pixels based on their alpha value

glAlphaFunc( func, value )

glEnable( GL_ALPHA_TEST )

– use alpha as a mask in textures

GPUs and GLSLGPUs and GLSL

• Over the past few years, graphical processing units (GPUs) have become more powerful and now are programmable

• Support first through OpenGL extensions and OpenGL Shading Language (GLSL)

• Incorporated in OpenGL 2.0

OpenGL Pipeline RevisitedOpenGL Pipeline Revisited

GeometryProcessingGeometryProcessing RasterizationRasterization

FragmentProcessingFragmentProcessing

FrameBufferFrameBuffer

CPUCPUTextureMemoryTextureMemory

texelstexels

verticesvertices verticesvertices fragmentsfragments

pixelspixels

Geometry ProcessingGeometry Processing

• Coordinate Transformations

• Primitive Assembly

• Clipping

• Per-vertex lighting

• Programmable through a vertex program or vertex shader

RasterizerRasterizer

• Outputs fragments (“potential pixels”) that are interior to primitives

• Interpolates colors, depth, texture coordinates, and other per vertex variables to obtain values for each fragment

• Not programmable

Fragment ProcessingFragment Processing

• Assigns color to each fragment and updates frame buffer

• Handles hidden surface removal, texture mapping, and blending

• Programmable through fragment program or fragment shader

GLSLGLSL

• OpenGL Shading Language

• C like language for writing both vertex and fragment shaders

• Adds matrix and vector data types + overloading of operators

• OpenGL state variables available to shaders

• Variables can be passed among shaders and applications

Vertex Shader ApplicationsVertex Shader Applications

• General Vertex Lighting

• Off loading CPU

• Dynamic Meshes

• Morphing

• Additional Application Specific Vertex Attributes

Vertex Shader Execution Vertex Shader Execution

Fragment Shader ApplicationsFragment Shader Applications

• Per Fragment Lighting

• Bump Maps

• Environment Maps

• Texture Manipulation

• Shadow Maps

Fragment Shader ExecutionFragment Shader Execution

Linking with ApplicationLinking with Application

• In OpenGL application, we must:

– Read shaders

– Compile shaders

– Define variables

– Link everything together

• OpenGL 2.0 contains a set of functions for each of these steps

Summary / Q & ASummary / Q & A

Ed Angel

Dave Shreiner

Vicki Shreiner

On-Line ResourcesOn-Line Resources

– http://www.opengl.org

• start here; up to date specification and lots of sample code

– news:comp.graphics.api.opengl

– http://www.sgi.com/software/opengl

– http://www.mesa3d.org/

• Brian Paul’s Mesa 3D

– http://www.cs.utah.edu/~narobins/opengl.html

• very special thanks to Nate Robins for the OpenGL Tutors

• source code for tutors available here!

Books

• OpenGL Programming Guide, 4th Edition

• OpenGL Reference Manual, 4th Edition

• Interactive Computer Graphics: A top-down approach with OpenGL, 4rd Edition

• OpenGL Programming for the X Window System

– includes many GLUT examples

Thanks for ComingThanks for Coming

• Questions and Answers

Ed Angel angel@cs.unm.edu

Dave Shreiner shreiner@sgi.com

Vicki Shreiner vshreiner@sgi.com