CS 591B Lecture 9: The OpenGL Rendering Pipeline 3D Polygon Rendering Many applications use rendering of 3D polygons with direct illumination Spring 2007 Rui Wang 3D Polygon Rendering Many applications use rendering of 3D polygons with direct illumination 3D Polygon Rendering Many applications use rendering of 3D polygons with direct illumination Quake II (Id Software) 3D Rendering Pipeline (direct illumination) Example: OpenGL This is a pipelined sequence of operations to draw a 3D primitive into a 2D image glbegin(gl_polygon); glvertex3f(0.0, 0.0, 0.0); glvertex3f(1.0, 0.0, 0.0); glvertex3f(1.0, 1.0, 1.0); glvertex3f(0.0, 1.0, 1.0); glend(); OpenGL executes steps of 3D rendering pipeline for each polygon 1
Transform into 2D screen coordinate system Transform into 2D screen coordinate system Transform into 2D screen coordinate system Clip primitives outside camera s view Clip primitives outside camera s view Draw pixels (includes texturing, hidden surface,...) 2
Rasterization s p(x,y,z) 3D Object Coordinates 3D Camera Coordinates } s -to- 2D Coordinates p (x,y ) in OpenGL OpenGL has multiple matrix stacks - transformation functions right-multiply the top of the stack Two most important stacks: GL_MODELVIEW and GL_PROJECTION Points get multiplied by the modelview matrix first, and then the projection matrix GL_MODELVIEW: Object->Camera GL_PROJECTION: Camera->Screen gl(0,0,w,h): Screen->Device OpenGL Example void SetUp() { // The viewport isn t a matrix, it s just state... gl( 0, 0, window_width, window_height ); // Set up camera->screen transformation first glmatrixmode( GL_PROJECTION ); glloadidentity(); float asp = (float)window_width/ (float)window_height; gluperspective( 60, asp, 1, 1000 ); // fov, aspect, near, far } // Set up the model->camera transformation glmatrixmode( GL_MODELVIEW ); glulookat( 3, 3, 2, // eye point 0, 0, 0, // look at point 0, 0, 1 ); // up vector glrotatef( theta, 0, 0, 1 ); // rotate the model glscalef( zoom, zoom, zoom ); // scale the model What s next? 2D Rendering Pipeline 2D Primitives Clip portions of geometric primitives residing outside the window Transform into 2D camera coordinate system Transform the clipped primitives from screen to image coordinates Clip primitives outside camera s view Draw pixels (includes texturing, hidden surface, etc.) Fill pixels representing primitives in screen coordinates 3
2D Rendering Pipeline 2D Primitives Avoid drawing parts of primitives outside window defines part of scene being viewed Must draw geometric primitives only inside window Clip portions of geometric primitives residing outside the window Transform the clipped primitives from screen to image coordinates Fill pixels representing primitives in screen coordinates Screen Coordinates Avoid drawing parts of primitives outside window defines part of scene being viewed Must draw geometric primitives only inside window Avoid drawing parts of primitives outside window Points Lines Polygons Circles etc. Point Is point (x,y) inside the clip window? Line Find the part of a line inside the clip window wy2 (x,y) inside = (x >= wx1) && (x <= wx2) && (y >= wy1) && (y <= wy2); P 7 P 8 wy1 wx1 wx2 P 9 0 Before 4
Line Find the part of a line inside the clip window Cohen Sutherland Line Use simple tests to classify easy cases first P 7 P 7 P 8 P 8 P 5 0 After P 9 Cohen Sutherland Line Classify some lines quickly by AND of bit codes representing regions of two endpoints (must be 0) Line Find the part of a line inside the clip window 1001 P 7 0001 0101 P 7 1000 P 8 0000 0100 Bit 4 P 8 1010 Bit 3 0010 P 9 0110 Bit 1 Bit 2 0 P 5 After Avoid drawing parts of primitives outside window Points Lines Polygons Circles etc. Polygon Find the part of a polygon inside the clip window? Before 5
Polygon Find the part of a polygon inside the clip window? Sutherland Hodgeman After Sutherland Hodgeman Sutherland Hodgeman Sutherland Hodgeman Sutherland Hodgeman 6
to a to a to a to a to a to a P P 7
to a to a P P P to a Summary 2D Primitives P P Clip portions of geometric primitives residing outside the window Transform the clipped primitives from screen to image coordinates Fill pixels representing primitives in screen coordinates What s Next Some Other Stuffs 3D Coordinates 3D Coordinates.Occlusion Culling.Backface Culling 3D Camera Coordinates 3D Camera Coordinates 2D Coordinates 2D Coordinates! 2D Coordinates 2D Coordinates 8