Lect21 2009 3D-OpenGL ZBuffer
Lect21 2009 3D-OpenGL ZBuffer
Model-view matrix
Position objects relative to camera
Projection matrix
Forms the image through projection to a projection plane and helps with clipping
Projection Transformation
First tell OpenGL youre using the projection matrix
glMatrixMode(GL_PROJECTION);
Viewing volume is the frustum of a pyramid Used for perspective projection or glOrtho(-1.0, 1.0,-1.0, 1.0, 2.0, 7.0); Viewing volume is a rectangular solid for parallel projection
Modelview Transformation
Our cube as specified is not visible
It lies in front of near clipping plane
Example:
gluLookAt(2.0,2.0,2.0,0.0,0.0,0.0,0.0,0.0,1.0); camera at (2,2,2), looking at origin, z-axis is up
Modelview Transformation
Used to perform geometric translations, rotations, scalings Also implements the viewing transformation If we dont position the camera, we need to move our cube into the viewing volume
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslate(0.0f, 0.0f, -3.5f); Translates cube down z-axis by 3.5 units
OpenGL performs transformations on all vertices First modelview transformation Then projection transformation The two matrices are concatenated Resulting matrix multiplies all points in the model
Routines to facilitate setting up matrices for specific viewing orientations & projections
gluQuadricDrawStyle(mySphere,GLU_FILL);
// some other styles: GLU_POINT, GLU_LINE
gluSphere(mySphere,1.0,12,12);
// radius, # longitude lines, # latitude lines
Interaction in OpenGL
OpenGL GLUT Callback Functions
GLUTs version of event/message handling Programmer specifies function to be called by OS in response to different events Specify the function by using glut***Func(ftn)
Weve already seen glutDisplayFunc(disp_ftn) disp_ftn called when client area needs to be repainted
Like Windows response to WM_PAINT messages
All GLUT callback functions work like MFC On***() event handler functions
glutKeyboardFunc(ftn(key,x,y))
Identifies function ftn() invoked when user presses a keyboard key Character code (key) and position of mouse cursor (x,y) returned to ftn()
glutSpecialFunction(ftn(key,x,y))
For special keys such as function & arrow keys
Mouse Callbacks
glutMouseFunc(ftn(button, state, x, y))
Identifies function ftn() called when mouse events occur Button presses or releases
Position (x,y) of mouse cursor returned Also the state (GLUT_UP or GLUT_DOWN) Also which button
GLUT_LEFT_BUTTON, GLUT_RIGHT_BUTTON, or GLUT_MIDDLE_BUTTON
Mouse Motion
Move event: when mouse moves with a button pressed
glutMotionFunctionFunc(ftn(x,y))
ftn(x,y) called when theres a move event Position (x,y) of mouse cursor returned
GLUT Menus
Can create popup menus and add menu items with:
glutCreateMenu (menu-ftn(ID))
Menu-ftn(ID) is callback function called when user selects an item from the menu ID identifies which item was chosen
glutAddMenuEntry(name, ID_value)
Adds an entry with name displayed to current menu ID_value returned to menu_ftn() callback
glutAttachMenu(button)
Attaches current menu to specified mouse button When that button is pressed, menu pops up
Hierarchical Models
In many applications the parts of a model depend on each other Often the parts are arranged in a hierarchy
Represent as a tree data structure Transformations applied to parts in parent nodes are also applied to parts in child nodes Simple example: a robot arm
Base, lower arm, and upper arm Base rotates lower and upper arm also rotate Lower arm rotates upper arm also rotates
Can use to position entire object while also preserving it for drawing other objects Use in conjunction with geometrical transformations Example: Robot program
GLUT Animation
Simple method is to use an idle callback
Called whenever windows event queue is empty Could be used to update display with the next frame of the animation Identify the idle function with:
glutIdleFunc(idle_ftn())
Simple Example:
void idle_ftn() { glutPostRedisplay(); } Posts message to event queue that client area needs to be repainted Causes display callback function to be invoked Effectively displays next frame of animation
Double Buffering
Use two display buffers Front buffer is displayed by display hardware Application draws into back buffer Buffers are swapped after new frame is drawn into back buffer Implies only one access to display hardware per frame Eliminates flicker In OpenGL, implement by replacing glFlush() with glutSwapBuffers() in display callback In initialization function, must use:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
Edge table (xmin, ymin, zmin, xmax, ymax, zmax) Active edge list (AEL) with active edges intersected by current scanline sorted on xs
(See scanline polygon fill notes)
Z-Buffer ZBuf[x][y]
Will store the zv distance of point on closest polygon that projects to pixel (x,y) on screen
Initialize each element of FBuf[][] to background color Initialize each element of ZBuf[][] to infinity (largest possible value)
The Algorithm
For each polygon For each scanline y spanning the polygon Get left & right active edges from AEL Get x,z coordinates of endpoints from edge table Compute scanline/edge intersection pts (xL,zL,xR,zR ) (Use x-y & z-y interpolation) For (x=xL to xR) Compute z by z-x interpol. If (z < ZBuf[x,y]) ZBuf[x,y] = z FBuf[x,y] = polygon color
Double Interpolation
We know (from Edge Table):
lower/upper vertices of left active edge: (x0,y0,z0) and (x1,y1,z1) lower/upper vertices of right active edge: (x2,y2,z2) and (x3,y3,z3)
x-y Interpolation:
Find x coords of intersection pts (xL,xR) Left Edge:
xL-x0 y-y0 ------- = ------x1-x0 y1-y0
z-y Interpolation
Find z coordinates of intersection points of scan line (y) with left and right edges Done the same way as x-y interpolation x coordinates replaced by z coordinates Results:
zL = (z1-z0)*(y-y0)/(y1-y0) + z0 zR = (z3-z2)*(y-y2)/(y3-y2) + z2
z-x Interpolation
Find z value on polygon at pixel x on current scanline (y) Interpolate between the left and right edge intersection points:
z-zL x-xL -------- = -------zR-zL xR-xL
Z-Buffer Performance
Outer loop repeats for each polygon Complex scenes have more polygons
So complex scenes should be slower
Disadvantage of Z-Buffer
Memory requirements
Z-Buffer is at least as big as the frame buffer For best results, need floating point or doubles for z values Example 1000 X 1000 resolution screen Assume 8 bytes to store a double 8 Megabytes required for Z-Buffer But memory has become cheap Z-Buffer used very commonly now Often implemented in hardware