Search code examples
openglpyopenglmouse-picking

openGL picking after glTranslate / glRotated


I wrote a pick function, to pick scene objects from the opengl canvas.

glRenderMode(GL_SELECT)
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
gluPickMatrix(self.last_point_2D_.x(),viewport[3]-self.last_point_2D_.y(),10,10,viewport)
glMultMatrixf(projection)

glInitNames()
glPushName(0)

//i scene objects rendering goes here, drawn in modelview
glLoadName(i)

glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glFlush()
glPopName()

This works perfect if i do not use glTanslate or glRotated in drawing the object i.e, i can pick line when i draw,

glVertex3f(1,1,1)
glVertex3f(2,2,2)

but not when i do,

glTranslate(1,1,1)
glVertex3f(0,0,0)
glVertex3f(1,1,1) 

What am i missing?


Solution

  • You're still in projection matrix mode, when applying the translation, thus messing up the picking matrix. Switch to modelview before transforming the scene.