Evening’s Goals

25

description

Evening’s Goals. Discuss animation Introduce hidden surface removal (HSR) methods Analyze user interaction discuss what GLUT provides. Animation. Interactive graphics needs things to move Very simple double buffered window motion variable to keep track of movement way to swap buffers. - PowerPoint PPT Presentation

Transcript of Evening’s Goals

Page 1: Evening’s Goals
Page 2: Evening’s Goals

2COEN 290 - Computer Graphics I

Evening’s Goals

Discuss animation Introduce hidden surface removal (HSR)

methods Analyze user interaction

• discuss what GLUT provides

Page 3: Evening’s Goals

3COEN 290 - Computer Graphics I

Animation

Interactive graphics needs things to move Very simple

• double buffered window• motion variable to keep track of movement• way to swap buffers

Page 4: Evening’s Goals

4COEN 290 - Computer Graphics I

Double Buffered Windows

Divide the color buffer into two buffers• front - display contents on screen• back - update contents for next frame

– frame is the term for a picture in a sequence Double buffering doesn’t come for free

• either requires– a deep framebuffer– loss of color resolution

Page 5: Evening’s Goals

5COEN 290 - Computer Graphics I

Creating a Double Buffered Window

UseglutInitDisplayMode( mode )

mode is a bit-wise or of maskGLUT_RGB | GLUT_DOUBLE

Must call before creating windowglutInitDisplayMode( mode );glutCreateWindow( windowName );

Page 6: Evening’s Goals

6COEN 290 - Computer Graphics I

Motion Variables

Need a variable to keep track of changes between frames• new eye position• time for computing an object’s position from its

velocity• rotation angles• translation values

Usually a global or static variable

Page 7: Evening’s Goals

7COEN 290 - Computer Graphics I

One Last Thing

Must swap front and back buffersglutSwapBuffers();

Last call in your rendering routine

Page 8: Evening’s Goals

8COEN 290 - Computer Graphics I

The Problem ...

Things are not always what they seem

Might really be

Page 9: Evening’s Goals

9COEN 290 - Computer Graphics I

Hidden Surface Removal Algorithms

Determine which primitives are “in front of” others• usually depends on the eye’s position

Methods• painter’s algorithm• backface culling• depth buffering

Page 10: Evening’s Goals

10

COEN 290 - Computer Graphics I

Painter’s Algorithm

Technique inspired by how painters paint• layer foreground objects over top of

background objects How do we do this?

• sort graphics primitives in eye space based on their distance from the eye

• use after transforming into eye spacez

Page 11: Evening’s Goals

11

COEN 290 - Computer Graphics I

Painter’s Algorithm ( cont. )

Very limited algorithm• no intersecting primitives

– must tessellate all intersecting primitives• sorting is slow• must re-sort after eye moves

Nothing in OpenGL to help

Page 12: Evening’s Goals

12

COEN 290 - Computer Graphics I

Backface Culling

Eliminate polygons based on vertex winding If all polygons are ordered consistently,

remove all ordered the same• by convention, choose clockwise

ordered polygons to be back facing

Facedness determined inwindow space by thepolygon’s area 1

5

2

3

4

Page 13: Evening’s Goals

13

COEN 290 - Computer Graphics I

Determining a Polygon’s Area

In window coordinates, compute

where

1

0112

1n

iiiii yxyxarea

nii mod)1(1

Page 14: Evening’s Goals

14

COEN 290 - Computer Graphics I

The Down Side …

Backface culling is only of limited use• works best with closed convex shapes

– spheres, cylinders, cubes, etc. Really used for increasing rendering

performance• reduces the number of pixels you need to fill

for a frame

Page 15: Evening’s Goals

15

COEN 290 - Computer Graphics I

Depth Buffering

Test every pixel to see whose in front Requires an additional buffer the size of the

window to store depth values• depth is really distance from eye

wzyx

ProjectionTransformation

1wzwywx

ModelingTransformations

Page 16: Evening’s Goals

16

COEN 290 - Computer Graphics I

Depth Buffering Algorithm

foreach ( pixel in primitive )if ( depth(x,y).z > pixel.z ) {color(x,y).r = pixel.r;color(x,y).g = pixel.g;color(x,y).b = pixel.b;depth(x,y).z = pixel.z;} else {// Discard pixel}

Page 17: Evening’s Goals

17

COEN 290 - Computer Graphics I

Depth Buffering and OpenGL

Recall frame buffer configuration is a function of the window system.• need to request depth buffer for window

glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );

Page 18: Evening’s Goals

18

COEN 290 - Computer Graphics I

Depth Buffering and OpenGL ( cont. )

Turn on depth testing

glEnable( GL_DEPTH_TEST ); Initialize all buffers to their initial values

glClear( GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT );

Page 19: Evening’s Goals

19

COEN 290 - Computer Graphics I

Interactive Computer Graphics

Need to interact with the user Implies we need to process input

• window resize• keyboard• mouse

Function of the window system• not part of OpenGL• part of GLUT

Page 20: Evening’s Goals

20

COEN 290 - Computer Graphics I

GLUT’s Input Model

Use callback functions to process use input• you write a function to do something• tell GLUT which function to call and when• glutMainLoop() calls callbacks for you

We’ve already seen one exampleglutDisplayFunc( render );• use glutPostRedisplay() to force

GLUT to repaint the window

Page 21: Evening’s Goals

21

COEN 290 - Computer Graphics I

Handling Window Resizes

glutReshapeFunc( resize );void resize( int width, int height ){ GLdouble aspect = (GLdouble) width / height;

// Reset Projection Transform glViewport( 0, 0, width, height ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( fovy, aspect, near, far );

// Update Viewing Transform}

Page 22: Evening’s Goals

22

COEN 290 - Computer Graphics I

Handling Keyboard Input

glutKeyboardFunc( key );void key( unsigned char key, int x, int y ){ switch( key ) { case ‘q’: case ‘Q’: case 033: exit( EXIT_SUCCESS ); break;

case ‘t’: xTrans += xIncrement; break; } glutPostRedisplay();

}

Page 23: Evening’s Goals

23

COEN 290 - Computer Graphics I

Handling Mouse Input

glutMouseFunc( mouse );void mouse( int button, int state, int x, int y ){ switch( button ) { case GLUT_LEFT_BUTTON: if ( state == GLUT_DOWN ) { rotateMode = True; xStart = x; yStart = y; glutMotionFunc( motion ); } else { rotateMode = False; glutMotionFunc( NULL ); } break; }}

Page 24: Evening’s Goals

24

COEN 290 - Computer Graphics I

Handling Mouse Input ( cont. )

glutMotionFunc( motion );void motion( int x, int y ){ // Compute values to be used later azim = x - xStart; elev = y - yStart; glutPostRedisplay();}

Page 25: Evening’s Goals

25

COEN 290 - Computer Graphics I

glutDisplayFunc( render );void render( void ){ glClear( GL_COLOR_BUFFER_BIT ); glPushMatrix(); polarview( dist, azim, inc, twist ); renderCube(); glPushMatrix(); glTranslatef( 0.0, 0.0, height ); glutSolidTeapot(); glPopMatrix(); glPopMatrix();

glFlush(); glutSwapBuffers();}

Controlling Rendering