Advanced Computer Graphics (CS & SE 233.420)
Topics Covered Picking Pipeline Viewing and Transformations
Rendering Modes OpenGL can render in one of three modes selected by glrendermode(mode) GL_RENDER Normal rendering to the frame buffer (default) GL_FEEDBACK Provides list of primitives rendered but no output to the frame buffer GL_SELECTION Each primitive in the view volume generates a hit record that is placed in a name stack which can be examined later
Picking Picking occurs when the mouse is positioned over an area of the screen to create and modify the viewing volume during selection The process of determine which object is beneath the mouse pointer is called picking Identify a user-defined object on the display In principle, it should be simple The mouse gives the position Should be able to determine to which object(s) a position corresponds Practical difficulties Pipeline architecture is feed forward, Hard to go from screen back to world Complicated by screen being 2D, world is 3D How close do we have to come to object to say we selected it?
The Name Stack The OpenGL API provides a mechanism for picking objects in a 3D scene The steps involved in detecting which objects are at the location where the mouse was clicked are: Get the window coordinates of the mouse Enter selection mode Redefine the viewing volume so that only a small area of the window around the cursor is rendered Render the scene, either using all primitives or only those relevant to the picking operation Exit selection mode and identify the objects which were rendered on that small part of the screen.
The Name Stack In order to identify the rendered objects using the OpenGL API you must name all relevant objects in your scene The OpenGL API allows you to give names to primitives, or sets of primitives (objects) glrendermode(gl_selection) When in selection mode, No objects are actually rendered in the framebuffer. Names of the objects (plus depth information) are collected in an array. For unnamed objects, only depth information is collected.
In GL Speak A hit occurs whenever a primitive is rendered in selection mode. Hit records are stored in the selection buffer. Upon exiting the selection mode OpenGL returns the selection buffer with the set of hit records. Since OpenGL provides depth information for each hit the application can then easily detect which object is closer to the user.
Using Selection Mode Initialize name buffer Enter selection mode (using mouse) Render scene with user-defined identifiers Reenter normal render mode This operation returns number of hits Examine contents of name buffer (hit records) Hit records include id and depth information
Manipulating the Name Stack OpenGL provides the following functions to manipulate the name stack glinitnames() initializes name stack Creates an empty name stack glpushname(id) Adds name to the top of the stack glpopname() Removes the name from top of the stack glloadname(id Replaces the top of the stack with name. It is the same as calling glpopname(); glpushname(name); Calls to the above functions are ignored when not in selection mode You can't place these functions inside a glbegin-glend construction
The Selection Mode Used for picking purposes The first step is to tell OpenGL where to store the hit records. This is accomplished with the following function: void glselectbuffer(glsizei size, GLuint *buffer); buffer : An array of unsigned integers. where OpenGL will store the hit records size : The size of the array Required to call this function before entering the selection mode Next you enter the selection mode with a call to void glrendermode(glenum mode); mode - use GL_SELECT to enter the rendering mode and GL_RENDER to return to normal rendering
Clearing Projection Matrix Redefine the viewing volume so that it renders only a small area around the place where the mouse was clicked. In order to do that it is necessary to set the matrix mode to GL_PROJECTION. Afterwards, the application should push the current matrix to save the normal rendering mode settings. Next initialise the matrix. The following snippet of code shows how to do this: glmatrixmode(gl_projection); glpushmatrix(); glloadidentity(); Result is a clean projection matrix
Redefine Viewing Volume First get the current viewport. This can be done querying OpenGL for the state variable GL_VIEWPORT (use the function glgetintegerv) Define the viewing volume so that rendering is done only in a small area around the cursor void glupickmatrix(gldouble x, GLdouble y, GLdouble witdth, GLdouble height, GLint viewport[4]); x,y : define the centre of the picking area width, height : The size of the picking region viewport : The current viewport Set your projection (perspective or orthogonal) just as you did for the normal rendering mode Finally get back to the modelview matrix mode, and initialise the Name Stack to start rendering
Redefine Viewing Volume The following code shows the sequence of function calls to do the above using a perspective projection glgetintegerv(gl_viewport,viewport); glupickmatrix(cursorx,viewport[3]-cursory,5,5,viewport);* gluperspective(45,ratio,0.1,1000); glmatrixmode(gl_modelview); glinitnames(); *Transforms the origin from the upper left corner, as provided by GLUT for example, into the bottom left corner Defines a 5x5 picking region
Processing the Hit Records In order to process the hit records Restore the original projection matrix. glmatrixmode(gl_projection); glpopmatrix(); glmatrixmode(gl_modelview); glflush(); Return to the normal rendering mode Num_hits = glrendermode(gl_render). This function returns the number of hit records that were created during rendering in the selection mode. Next, process the selection buffer. if (hits!= 0) processhits(hits,selectbuf);
Using Regions of the Screen Many applications use a simple rectangular arrangement of the screen Example: Maya, Photoshop Easier to look at mouse position and determine which area of screen it is in than using selection mode picking M E N U TOOLBAR
Camera Models The camera is modeled by its optical centre, C, and its retinal plane (or image plane) R. In each camera, a 3-D point, w=(x,y,z) T, in world coordinates (where the world coordinate frame is fixed arbitrarily) is projected into an image point m = (u,v) T in camera coordinates m = Pw P is the projection matrix We know perspective and orthogonal Can make your own P matrix (fish eye, wide angle, etc)
Camera Specification Six degrees of freedom Position of center of lens Orientation Lens Film size Orientation of film plane
Projections Rays (projectors) projected from the center of projection pass through each point of the models and intersect projection plane. Since everything is synthetic, the projection plane can be in front of the models, inside the models, or behind the models. 2 main types: perspective projection and parallel projection. Which type of projection is used depends on the needs of the user - whether the goal is the mathematically correct depiction of length and angles, or a realistic looking image of the object.
Parallel Projection Center of projection infinitely far from view plane Projectors will be parallel to each other Need to define the direction of projection (vector) 2 sub-types orthographic - direction of projection is normal to view plane oblique - direction of projection not normal to view plane Better for drafting / CAD applications
Perspective Projection Center of projection finitely far from view plane Projectors will not be parallel to each other Need to define the location of the center of projection (point) Classified into 1, 2, or 3-point perspective More visually realistic - has perspective foreshortening (objects further away appear smaller)
Advantages of Synthetic Camera Modeling Separation of objects, viewer, light sources Two-dimensional graphics is a special case of three-dimensional graphics Leads to simple software API Specify objects, lights, camera, attributes Let implementation determine image Leads to fast hardware implementation Via the pipeline
Following the Pipeline Transformations Much of the work in the pipeline is in converting object representations from one coordinate system to another World coordinates Camera coordinates Screen coordinates Every change of coordinates is equivalent to a matrix transformation
Clipping Just as a real camera cannot see the whole world, the virtual camera can only see part of the world space Objects that are not within this volume are said to be clipped out of the scene
Projection Must carry out the process that combines the 3D viewer with the 3D objects to produce the 2D image Perspective projections: all projectors meet at the center of projection Parallel projection: projectors are parallel, center of projection is replaced by a direction of projection
Rasterization If an object is visible in the image, the appropriate pixels in the frame buffer must be assigned colors Vertices assembled into objects Effects of lights and materials must be determined Polygons filled with interior colors/shades Must have also determine which objects are in front (hidden surface removal)
Pipeline
Viewing - Camera Analogy Set up the tripod and point the camera (view transform) Arrange the objects in the scene (model transform) Choose a lens and adjust zoom (projection transform) Choose photo print size (viewport transform)
OpenGL Transforms There are five transforms in OpenGL Vertices pass through these three. 1. Viewing Specifies location of camera 2. Modeling Moves objects around the scene 3. Projection Defines the viewing volume 4. Viewport Maps the 2D projection to the rendering window 5. Modelview Combination of viewing and modeling transforms Must be executed in a specific order.
Vertex Transformation
Modelview Matrix Modelview matrix converts object coordinates into eye coordinates Model transformations move the object, view transformations move the eyepoint, interchangeable!
Projection Matrix Selecting the lens, telephoto or wide angle Mathematically, selecting the volume to be mapped to viewport (mapping, homology, projective transform)
Perspective Division The divide by w step [ x y z z/d ] x projected = dx/z = x/(z/d) y projected = dy/z = y/(z/d)
Vertex Transformation Mapping window on projection plane to viewport Use glviewport Done in reshape function so you know what size the viewport has become
Viewing Use rotations and translations or void glulookat( GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdouble upz); Eye point Center of field of view (aim point) Up vector Default: glulookat (0.0, 0.0, 0.0, 0.0, 0.0, -100.0, 0.0, 1.0, 0.0);
glulookat(eyex,eyey,eyez, centerx,centery,centerz, upx,upy,upz) Defines a viewing matrix and multiplies it to the right of the current matrix. The desired viewpoint is specified by eyex, eyey, and eyez. The centerx, centery, and centerz arguments specify any point along the desired line of sight, but typically they're some point in the center of the scene being looked at. The upx, upy, and upz arguments indicate which direction is up (that is, the direction from the bottom to the top of the viewing volume).
References Ed Angel Lecture notes and Slides by Edward Angel 2002 (Lectures 9 and 10) Open GL web page www.opengl.org http://www.opengl.org/resources/faq/technical http://www.newcyber3d.com/asia/selfstudy/tips/selection.htm http://www.lighthouse3d.com/opengl/displaylists http://www.mfcogl.com/multiview%20animation%20mfc_opengl.h tm www.cs.colorado.edu/~janem/cs4229/lec6_ch3-4.pdf http://www.codecolony.de/docs/camera.htm http://mathcs.holycross.edu/~croyden/graphics/assignments/sol5/s olution5.html www.cs.stevens-tech.edu/~quynh/courses/cs537- fa03/projection_gl.ppt http://gamecode.tripod.com/tut/tut03.htm http://www.basic4gl.net/introduction%20to%20opengl%20part%20 2.htm http://www.falloutsoftware.com/tutorials/gl/gl0.htm