CS418 OpenGL GLUT Programming Tutorial IV MP2 Bonus Object Picking Presented by Wei Wen Feng 2 20 2008 MP2 MP2 is due next Mon Try to get key parts done first Terrain Rendering with Texture Camera Control Multiple Objects rendering Bonus Object selection manipulation 30 of Bonus Credits More complicated than required parts Today s Agenda MP2 Bonus OpenGL Picking Manipulate the motion of objects Q A MP2 Bonus Main Objective Let user choose one of the object control its motion User interaction will affect how the selected object moves Requirements Object Selection Animate an object Orientation Manipulation MP2 Bonus Object Selection Animate Object Let user choose which object to be animated Should be performed by mouse click Object should move along a velocity vector Change the magnitude of velocity Orientation Manipulation Change direction of velocity vector Choose the UI yourself Object Selection Picking an object on the screen based on mouse click Problem How to relate 2D Mouse position to 3D objects Click x y Which Object Selection First Idea Color coding Disable Lighting Draw objects in different colors Check pixel color to identify the object Apply glReadPixels for each mouse click to obtain pixel values Pixel Green Object Selection OpenGL Picking Similar idea Define a screen area for picking Set each object with a name ID Draw Objects with its Name OpenGL identify Name of object you are clicking Object Selection Picking Matrix Define a screen drawing area for picking Set projection matrix with smaller screen area gluPickMatrix x y w h viewport x y window coordinates w h picking region Viewport screen size Screen Coord x 0 0 y y Picking area x 0 0 Window Coord Object Selection Name Stack OpenGL maintains a stack of Names Each name is a number ID When an object is drawn all Names in current stack are used as IDs Name Stack Name 1 4 2 1 Name 1 2 4 Object Selection Hit Records Need to set up a GLuint buffer to have results returned to you glSelectBuffer size buffer Depth 0 2 32 Multiple records might be stored in selection buffer Choose the record with smallest depth Closest to you Names Name 1 Names 2 Min Depth 10 30 Max Depth 300 60 Name1 Name2 10 1 2 Records1 Records2 Object Selection OpenGL Picking example Init Selection glSelectBuffer BUFSIZE selectBuf glRenderMode GL SELECT glInitNames Drawing glPushName 1 drawObj1 glPopName glPushName 2 drawObj2 glPopName Define Pick Region glGetIntegerv GL VIEWPORT vp glMatrixMode GL PROJECTION glPushMatrix backup projection glLoadIdentity gluPickMatrix mouseX vp 3 mouseY 5 5 vp gluPerspective After Picking glMatrixMode GL PROJECTION glPopMatrix restore projection Hits glRenderMode GL RENDER process Hit records from selectBuf Object Manipulation Once we select the object we can animate that specific object Object translational animation Move object along a pre defined direction Update its position periodically and redraw Change velocity based on UI Move along a direction Object Manipulation Animation Example Init m T Vec3 0 0 0 m V Vec3 0 0 1 m Speed 1 0 Timer m T m V m Speed Change Speed m Speed or Rendering glPushMatrix glTranslate m T glBegin glEnd glPopMatrix Object Manipulation Move object along a fixed direction is not enough Rotate the object to change its moving direction Problem What kind of UI to use Keyboard Mouse How to rotate its moving direction Object Manipulation Choice of UI Keyboard Key requirements Must be able to perform a rotation about arbitrary axis Rotation about only one fixed axis won t get full credit Press Key Tilt Change the angle axis of rotation based on key press Not very intuitive to perform arbitrary rotation Analogy to flight simulator 3rd Person view control Keep track a total accumulated rotations Mouse Make use of mouse movement to define a rotation Sounds familiar Arcball interface R Rkey R Object Manipulation How to re orient object Maintain a model rotation matrix Change its values based on user input Object also needs to be rotated accordingly Apply the rotation for both Velocity vector Object model transformation Object Manipulation Re orientation Example Init m Rot Identity m InitV m V Vec3 0 0 1 UI Input Determine fAngle vAxis Mat4 newR fAngle vAxis m Rot newR m Rot Update Orientation m V m Rot m InitV m T m V m Speed Rendering glPushMatrix glTranslate m T glMultMatrix m Rot glBegin glEnd glPopMatrix Q A Summary OpenGL Picking sample code http www lighthouse3d com opengl picking Object Manipulation Update object s translation periodically Make use of DemoArcball class to re orient object
View Full Document
Unlocking...