Sign in to follow this  
sfelixjr

OpenGL picking opengl + python not working

Recommended Posts

Hi guys, I'm trying to implement selection in opengl through pyopengl, but it's not working I don't know why, but there is no glSelectWithCallback method in my python (2.6) I believe It became deprecated. Anyway, I googled it and I found this selection code:
    def glSelectWithCallback(x, y, callback, xsize = 5, ysize = 5, buffer_size = 512):
        '''glSelectWithCallback(int x, int y, Callable callback, int xsize=5, int ysize=5)
      x,y -- x and y window coordinates for the center of the pick box
      rendercallback -- callback (callable Python object) taking 0 arguments
        which performs pick-mode rendering
      xsize,ysize -- x and y dimensions of the pick box (default = 5x5)
    The function returns a tuple (possibly empty) of:
      ( (minimumzdepth, maximumzdepth, (name, name, name),...)
        minimumzdepth, maximumzdepth -- depths in integer format
          If you want the more traditional 0.0 to 1.0 numbers, divide
          by (2**32)-1
          If you want the physical depth, multiply that by the frustrum depth and
            add your near clipping plane.
        name -- the names (integers) used in calls to glPushName( int )'''
        # this import needs to be late binding or Python gets stuck in an infinite import loop   
        from OpenGL.GLU import gluPickMatrix
        viewport = glGetIntegerv(GL_VIEWPORT)
        glSelectBuffer(buffer_size)
        glRenderMode(GL_SELECT)
        glInitNames()
        glMatrixMode(GL_PROJECTION)
        previousviewmatrix = glGetDoublev(GL_PROJECTION_MATRIX)
        glLoadIdentity()
        gluPickMatrix(x, viewport[3] - y, xsize, ysize, viewport)
        glMultMatrixd(previousviewmatrix)
        callback()
        glFlush()
        glMatrixMode(GL_PROJECTION)
        glLoadMatrixd(previousviewmatrix)
        return glRenderMode(GL_RENDER)
However, in order to succeed, I'm registering the names in the stack before drawing it, look:
        def paintObjects(self) :
            glRotatef    (self.rotx, 1,0,0);
            glRotatef    (self.roty, 0,1,0);
            for i in range(0,len(self.objects)):
                glPushName(i)
                if (self.wire):
                    self.objects[i].paintWire();
                else:
                    self.objects[i].paint()
                glPopName()

I have a list of objects, every time I paint them I push / draw / pop. However, when I try to get the hits list, It comes empty. I'm calling it this way:
    def mouseReleaseEvent(self,ev):
            self.select(ev.pos().x(), ev.pos().y())

    def select(self,x,y):
            result = glSelectWithCallback(x,y, self.paintObjects)
            print str(result)

I don't what is wrong with this... I read almost all available tutorials from google search (ok. Not possible)... Does anyone know what's going on here? Please, help me, I'm trying to do it since my last 5 days, thanks a lot [Edited by - sfelixjr on May 4, 2009 5:14:38 PM]

Share this post


Link to post
Share on other sites
If the push / draw / pop operations are performed while in the modelview matrix mode, try inserting glMatrixMode(GL_MODELVIEW) before the callback() and before the return statements in the glSelectWithCallback routine. This will ensure the glRotatef() statements in your paintObjects routine are applied to the modelview matrix and not the projection matrix.

[Edited by - gedesch on May 9, 2009 12:51:47 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this