Projections and Picking Wed 24 Sep 2003 project 1 solution demo recap: projections 2 projections 3 picking
News Project 1 solution executable available Readings reminder idea of what’s expected no need to copy look and feel exactly Readings reminder Chapter 5 Week 4, Wed 24 Sep 03 © Tamara Munzner
Projections recap
Transforming View Volumes orthographic view volume x z VCS y x=left y=top x=right z=-far z=-near y=bottom perspective view volume NDCS y (1,1,1) z (-1,-1,-1) x Week 4, Wed 24 Sep 03 © Tamara Munzner
Basic Perspective Projection y P(x,y,z) similar triangles P(x’,y’,d) z z=d also but nonuniform foreshortening not affine Week 4, Wed 24 Sep 03 © Tamara Munzner
Basic Perspective Projection can express as homogenous 4x4 matrix! Week 4, Wed 24 Sep 03 © Tamara Munzner
Projective Transformations determining the matrix representation need to observe 5 points in general position, e.g. [left,0,0,1]T[-1,0,0,1]T [0,top,0,1]T[0,1,0,1]T [0,0,-f,1]T[0,0,1,1]T [0,0,-n,1]T[0,0,-1,1]T [left*f/n,top*f/n,-f,1]T[-1,1,1,1]T solve resulting equation system to obtain matrix Week 4, Wed 24 Sep 03 © Tamara Munzner
OpenGL Orthographic Matrix scale, translate, reflect for new coord sys Week 4, Wed 24 Sep 03 © Tamara Munzner
OpenGL Perspective Matrix shear, scale, reflect for new coord sys Week 4, Wed 24 Sep 03 © Tamara Munzner
Projection Normalization viewing clipping normalized device VCS CCS NDCS projection transformation perspective division alter w / w distort such that orthographic projection of distorted objects is desired persp projection separate division from standard matrix multiplies division: normalization Week 4, Wed 24 Sep 03 © Tamara Munzner
Projections 3
Demos Brown Projections Demos http://www.cs.brown.edu/exploratories/freeSoftware/catalogs/viewing_techniques.html Nate Robbins tutorial (take 2): http://www.xmission.com/~nate/tutors.html Week 4, Wed 24 Sep 03 © Tamara Munzner
Perspective Example view volume left = -1, right = 1 bot = -1, top = 1 near = 1, far = 4 Week 4, Wed 24 Sep 03 © Tamara Munzner
Perspective Example view volume left = -1, right = 1 tracks in VCS: bot = -1, top = 1 near = 1, far = 4 tracks in VCS: left x=-1, y=-1 right x=1, y=-1 x=-1 x=1 1 ymax-1 z=-4 real midpoint z=-1 -1 -1 1 xmax-1 x -1 NDCS (z not shown) DCS (z not shown) z VCS top view Week 4, Wed 24 Sep 03 © Tamara Munzner
Perspective Example / w Week 4, Wed 24 Sep 03 © Tamara Munzner
Viewport Transformation generate pixel coordinates map x, y from range –1…1 (normalized device coordinates) to pixel coordinates on the display involves 2D scaling and translation y display x viewport Week 4, Wed 24 Sep 03 © Tamara Munzner
Viewport Transformation (1,1) (w,h) DCS b NDCS a y (-1,-1) x (0,0) OpenGL glViewport(x,y,a,b); default: glViewport(0,0,w,h); Week 4, Wed 24 Sep 03 © Tamara Munzner
Projective Rendering Pipeline glVertex3f(x,y,z) object world viewing alter w OCS WCS VCS glFrustum(...) projection transformation modeling transformation viewing transformation clipping glTranslatef(x,y,z) glRotatef(th,x,y,z) .... gluLookAt(...) / w CCS perspective division normalized device OCS - object coordinate system WCS - world coordinate system VCS - viewing coordinate system CCS - clipping coordinate system NDCS - normalized device coordinate system DCS - device coordinate system glutInitWindowSize(w,h) glViewport(x,y,a,b) NDCS viewport transformation device DCS Week 4, Wed 24 Sep 03 © Tamara Munzner
Picking
Interactive Object Selection move cursor over object, click how to decide what is below? ambiguity many 3D world objects map to same 2D point four common approaches manual ray intersection bounding extents backbuffer color coding selection region with hit list Picking and 3D Selection Pick: Select an object by positioning mouse over it and clicking. Question: How do we decide what was picked? We could do the work ourselves: Map selection point to a ray; Intersect with all objects in scene. Let OpenGL/graphics hardware do the work. Idea: Draw entire scene, and ``pick'' anything drawn near the cursor. Only ``draw'' in a small viewport near the cursor. Just do clipping, no shading or rasterization. Need a method of identifying ``hits''. OpenGL uses a name stack managed by glInitNames(), glLoadName(), glPushName(), and glPopName(). ``Names'' are unsigned integers When hit occurs, copy entire contents of stack to output buffer. Process: Set up pick buffer Initialize name stack May need pushName(-1); Save old projective transformation and set up a new one Draw your scene with appropriate use of name stack Restore projective transformation Query for number of hits and process them A hit occurs if a draw occurs in the (small) viewport All information (except 'hits') is stored in buffer given with glSelectBuffer Example: Two objects Might pick none Might pick either one Might pick both glSelectBuffer(size, buffer); /* initialize */ glRenderMode(GL_SELECT); glInitNames(); glPushName(-1); glGetIntegerv(GL_VIEWPORT,viewport); /* set up pick view */ glMatrixMode(GL_PROJECTION); glPushMatrix(); glIdentity(); gluPickMatrix(x,y,w,h,viewport); glMatrixMode(GL_MODELVIEW); ViewMatrix(); glLoadName(1); Draw1(); glLoadName(2); Draw2(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); hits = glRenderMode(GL_RENDER); Hits stored consequetively in array In general, if h is the number of hits, the following is returned. hits = h. h hit records, each with four parts: The number of items q on the name stack at the time of the hit (1 int). The minimum z value amoung the primitives hit (1 int). The maximum z value amoung the primitives hit (1 int). The contents of the hit stack, deepest element first (q ints). At most one hit between each call glRenderMode or a change to name stack In our example, you get back the following: If you click on Item 1 only: hits = 1, buffer = 1, min(z1), max(z1), 1. If you click on Item 2 only: hits = 1, buffer = 1, min(z2), max(z2), 2. If you click over both Item 1 and Item 2: hits = 2, buffer = 1, min(z1), max(z1), 1, 1, min(z2), max(z2), 2. More complex example: /* initialization stuff goes here */ glPushName(1); Draw1(); /* stack: 1 */ glPushName(1); Draw1_1(); /* stack: 1 1 */ glPushName(1); Draw1_1_1(); /* stack: 1 1 1 */ glPopName(); glPushName(2); Draw1_1_2(); /* stack: 1 1 2 */ glPopName(); glPopName(); glPushName(2); /* stack: 1 2 */ Draw1_2(); glPopName(); glPopName(); glPushName(2); Draw2(); /* stack: 2 */ glPopName(); /* wrap-up stuff here */ What you get back: If you click on Item 1: hits = 1, buffer = 1, min(z1), max(z1), 1. If you click on Items 1:1:1 and 1:2: hits = 2, buffer = 3, min(z111), max(z111), 1, 1, 1, 2, min(z12), max(z12), 1, 2. If you click on Items 1:1:2, 1:2, and 2: hits = 3, buffer = 3, min(z112), max(z112), 1, 1, 2, 2, min(z12), max(z12), 1, 2, 1, min(z2), max(z2), 2. Important Details: Make sure that projection matrix is saved with a glPushMatrix() and restored with a glPopMatrix(). glRenderMode(GL_RENDER) returns negative if buffer not big enough. When a hit occurs, a flag is set. Entry to name stack only made at next gl*Name(s) or glRenderMode call. So, each draw block can only generate at most one hit. Readings: GL Programming Manual, Chapter 12 -------------------- The user provides each object of interest with a “name” (in fact, the name is an integer). The user defines a small region of the screen. Typically, this is a rectangle that includes the mouse position and a few pixels either side. OpenGL then renders the scene in selection mode. Whenever a named object is displayed inside the selected region, a “hit record”, containing its name and some other information is added to the “selection list”. The user examines the hit records to determine the object — or objects — selected. Week 4, Wed 24 Sep 03 © Tamara Munzner
Manual Ray Intersection do all computation at application level map selection point to a ray intersect ray with all objects in scene. advantages no library dependence y VCS x Week 4, Wed 24 Sep 03 © Tamara Munzner
Manual Ray Intersection do all computation at application level map selection point to a ray intersect ray with all objects in scene. advantages no library dependence disadvantages difficult to program slow: work to do depends on total number and complexity of objects in scene Week 4, Wed 24 Sep 03 © Tamara Munzner
Bounding Extents keep track of axis-aligned bounding rectangles advantages conceptually simple easy to keep track of boxes in world space Week 4, Wed 24 Sep 03 © Tamara Munzner
Bounding Extents disadvantages extensions low precision must keep track of object-rectangle relationship extensions do more sophisticated bound bookkeeping Week 4, Wed 24 Sep 03 © Tamara Munzner
Backbuffer Color Coding use backbuffer for picking create image as computational entity never displayed to user redraw all objects in backbuffer turn off shading calculations set unique color for each pickable object store in table read back pixel at cursor location check against table Week 4, Wed 24 Sep 03 © Tamara Munzner
Backbuffer Color Coding advantages conceptually simple variable precision disadvantages number of color bits must be adequate introduce 2x redraw delay Week 4, Wed 24 Sep 03 © Tamara Munzner
Backbuffer Example for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) { glPushMatrix(); switch (i*2+j) { case 0: glColor3ub(255,0,0);break; case 1: glColor3ub(0,255,0);break; case 2: glColor3ub(0,0,255);break; case 3: glColor3ub(250,0,250);break; } glTranslatef(i*3.0,0,-j * 3.0) glCallList(snowman_display_list); glPopMatrix(); } glColor3f(1.0f, 1.0f, 1.0f); for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) { glPushMatrix(); glTranslatef(i*3.0,0,-j * 3.0); glColor3f(1.0f, 1.0f, 1.0f); glCallList(snowman_display_list); glPopMatrix(); } http://www.lighthouse3d.com/opengl/picking/ Week 4, Wed 24 Sep 03 © Tamara Munzner
Select/Hit use small region around cursor for viewport assign per-object integer keys (names) redraw in special mode store hit list of objects in region examine hit list OpenGL support Week 4, Wed 24 Sep 03 © Tamara Munzner
Viewport small rectangle around cursor why rectangle instead of point? change coord sys so fills viewport why rectangle instead of point? people aren’t great at positioning mouse Fitts’s Law: time to acquire a target is function of the distance to and size of the target allow several pixels of slop Week 4, Wed 24 Sep 03 © Tamara Munzner
Viewport tricky to compute simple utility command invert viewport matrix, set up new orthogonal projection simple utility command gluPickMatrix(x,y,w,h,viewport) x,y: cursor point w,h: sensitivity/slop (in pixels) push old setup first, so can pop it later Week 4, Wed 24 Sep 03 © Tamara Munzner
Render Modes glRenderMode(mode) GL_RENDER: normal color buffer default GL_SELECT: selection mode for picking (GL_FEEDBACK: report objects drawn) Week 4, Wed 24 Sep 03 © Tamara Munzner
Name Stack “names” are just integers glInitNames() flat list glLoadName(name) or hierarchy supported by stack glPushName(name), glPopName can have multiple names per object stack is at least 64 levels deep Week 4, Wed 24 Sep 03 © Tamara Munzner
Hierarchical Names Example for(int i = 0; i < 2; i++) { glPushName(i); for(int j = 0; j < 2; j++) { glPushMatrix(); glPushName(j); glTranslatef(i*10.0,0,j * 10.0); glPushName(HEAD); glCallList(snowManHeadDL); glLoadName(BODY); glCallList(snowManBodyDL); glPopName(); glPopName(); glPopMatrix(); } glPopName(); } http://www.lighthouse3d.com/opengl/picking/ Week 4, Wed 24 Sep 03 © Tamara Munzner
Hit List glSelectBuffer(buffersize, *buffer) where to store hit list data on hit, copy entire contents of name stack to output buffer. hit record number of names on stack minimum and minimum depth of object vertices depth lies in the z-buffer range [0,1] multiplied by 2^32 -1 then rounded to nearest int Week 4, Wed 24 Sep 03 © Tamara Munzner
Separate Pick Function? use same function to draw and pick simpler to code name stack commands ignored in render mode customize functions for each potentially more efficient can avoid drawing unpickable objects Week 4, Wed 24 Sep 03 © Tamara Munzner
Select/Hit advantages disadvantages faster flexible precision OpenGL support means hardware accel only do clipping work, no shading or rasterization flexible precision size of region controllable flexible architecture custom code possible, e.g. guaranteed frame rate disadvantages more complex Week 4, Wed 24 Sep 03 © Tamara Munzner