Exemple #1
0
    def refresh_position(self):
        """Recalc where in modelspace quad needs to be to fill screen."""

        screensize = pygame.display.get_surface().get_size()
        bottomleft = oglu.gluUnProject(screensize[0] / 2 - \
                                       self._winsize[0] / 2 + \
                                       self._position[0],
                                       screensize[1] / 2 - \
                                       int(round(self._winsize[1] / 2.0)) + \
                                       self._position[1], 0)
        bottomright = oglu.gluUnProject(screensize[0] / 2 + \
                                        int(round(self._winsize[0] / 2.0)) + \
                                        self._position[0],
                                        screensize[1] / 2 - \
                                        int(round(self._winsize[1] / 2.0)) + \
                                        self._position[1], 0)
        topleft = oglu.gluUnProject(screensize[0] / 2 - \
                                    self._winsize[0] / 2 + \
                                    self._position[0],
                                    screensize[1] / 2 + \
                                    self._winsize[1] / 2 + \
                                    self._position[1], 0)
        topright = oglu.gluUnProject(screensize[0] / 2 + \
                                     int(round(self._winsize[0] / 2.0)) + \
                                     self._position[0],
                                     screensize[1] / 2 + \
                                     self._winsize[1] / 2 + \
                                     self._position[1], 0)

        self.dims = topleft, topright, bottomright, bottomleft
        width = topright[0] - topleft[0]
        height = topright[1] - bottomright[1]
        self._qdims = topleft[0], topleft[1], width, height
Exemple #2
0
    def refreshPosition(self, possibly_changed):
        """Recalc where in modelspace quad needs to be to fill screen."""
        ogl.glMatrixMode(ogl.GL_MODELVIEW)
        ogl.glLoadIdentity()
        ogl.glMatrixMode(ogl.GL_PROJECTION)
        ogl.glLoadIdentity()

        if not possibly_changed:
            return

        depth = self._depth
        (top, bottom, left, right) = self.tblr

        bottomleft = oglu.gluUnProject(left, bottom, depth)
        if bottomleft == self.prev_bl:
            # ~ print "not changed"
            return

        # ~ print "changed"
        # ~ print bottomleft[0] - self.prev_bl[0]
        self.prev_bl = bottomleft
        bottomright = oglu.gluUnProject(right, bottom, depth)
        topleft = oglu.gluUnProject(left, top, depth)
        topright = oglu.gluUnProject(right, top, depth)

        self.dims = topleft, topright, bottomright, bottomleft
        width = topright[0] - topleft[0]
        height = topright[1] - bottomright[1]
        self._qdims = topleft[0], topleft[1], width, height
Exemple #3
0
    def refreshPosition(self, possibly_changed):
        """Recalc where in modelspace quad needs to be to fill screen."""
        ogl.glMatrixMode(ogl.GL_MODELVIEW)
        ogl.glLoadIdentity()
        ogl.glMatrixMode(ogl.GL_PROJECTION)
        ogl.glLoadIdentity()

        if not possibly_changed:
            return

        depth = self._depth
        (top, bottom, left, right) = self.tblr

        bottomleft = oglu.gluUnProject(left, bottom, depth)
        if bottomleft == self.prev_bl:
            #~ print "not changed"
            return

        #~ print "changed"
        #~ print bottomleft[0] - self.prev_bl[0]
        self.prev_bl = bottomleft
        bottomright = oglu.gluUnProject(right, bottom, depth)
        topleft = oglu.gluUnProject(left, top, depth)
        topright = oglu.gluUnProject(right, top, depth)

        self.dims = topleft, topright, bottomright, bottomleft
        width = topright[0] - topleft[0]
        height = topright[1] - bottomright[1]
        self._qdims = topleft[0], topleft[1], width, height
Exemple #4
0
def screen_to_world(app, screen_x, screen_y):
    """
    Return 3D (float) world space coordinates for given 2D (int) screen space
    coordinates.
    """
    # thanks http://www.bfilipek.com/2012/06/select-mouse-opengl.html
    # get world space ray from view space mouse loc
    screen_y = app.window_height - screen_y
    z1, z2 = 0, 0.99999
    pjm = np.matrix(app.camera.projection_matrix, dtype=np.float64)
    vm = np.matrix(app.camera.view_matrix, dtype=np.float64)
    start_x, start_y, start_z = GLU.gluUnProject(screen_x, screen_y, z1, vm,
                                                 pjm)
    end_x, end_y, end_z = GLU.gluUnProject(screen_x, screen_y, z2, vm, pjm)
    dir_x, dir_y, dir_z = end_x - start_x, end_y - start_y, end_z - start_z
    # define Z of plane to test against
    # TODO: what Z is appropriate for game mode picking? test multiple planes?
    art = app.ui.active_art
    plane_z = art.layers_z[
        art.active_layer] if art and not app.game_mode else 0
    x, y, z = ray_plane_intersection(
        0,
        0,
        plane_z,  # plane loc
        0,
        0,
        1,  # plane dir
        end_x,
        end_y,
        end_z,  # ray origin
        dir_x,
        dir_y,
        dir_z)  # ray dir
    return x, y, z
Exemple #5
0
    def refresh_position(self):
        """Recalc where in modelspace quad needs to be to fill screen."""

        screensize = pygame.display.get_surface().get_size()
        bottomleft = oglu.gluUnProject(screensize[0] // 2 - \
                                       self._winsize[0] // 2 + \
                                       self._position[0],
                                       screensize[1] // 2 - \
                                       int(round(self._winsize[1] / 2.0)) + \
                                       self._position[1], 0)
        bottomright = oglu.gluUnProject(screensize[0] // 2 + \
                                        int(round(self._winsize[0] / 2.0)) + \
                                        self._position[0],
                                        screensize[1] // 2 - \
                                        int(round(self._winsize[1] / 2.0)) + \
                                        self._position[1], 0)
        topleft = oglu.gluUnProject(screensize[0] // 2 - \
                                    self._winsize[0] // 2 + \
                                    self._position[0],
                                    screensize[1] // 2 + \
                                    self._winsize[1] // 2 + \
                                    self._position[1], 0)
        topright = oglu.gluUnProject(screensize[0] // 2 + \
                                     int(round(self._winsize[0] / 2.0)) + \
                                     self._position[0],
                                     screensize[1] // 2 + \
                                     self._winsize[1] // 2 + \
                                     self._position[1], 0)

        self.dims = topleft, topright, bottomright, bottomleft
        width = topright[0] - topleft[0]
        height = topright[1] - bottomright[1]
        self._qdims = topleft[0], topleft[1], width, height
Exemple #6
0
 def refreshPosition(self):
     """Recalc where in modelspace quad needs to be to fill screen."""
     depth = self._depth
     bottomleft = oglu.gluUnProject(0, 0, depth)
     bottomright = oglu.gluUnProject(self._winSize[0], 0, depth)
     topleft = oglu.gluUnProject(0, self._winSize[1], depth)
     topright = oglu.gluUnProject(self._winSize[0], self._winSize[1], depth)
     self.dims = topleft, topright, bottomright, bottomleft
     width = topright[0] - topleft[0]
     height = topright[1] - bottomright[1]
     self._qdims = topleft[0], topleft[1], width, height
Exemple #7
0
 def refreshPosition(self):
     """Recalc where in modelspace quad needs to be to fill screen."""
     depth = self._depth
     bottomleft = oglu.gluUnProject(0, 0, depth)
     bottomright = oglu.gluUnProject(self._winSize[0], 0, depth)
     topleft = oglu.gluUnProject(0, self._winSize[1], depth)
     topright = oglu.gluUnProject(self._winSize[0], self._winSize[1], depth)
     self.dims = topleft, topright, bottomright, bottomleft 
     width = topright[0] - topleft[0]
     height = topright[1] - bottomright[1]
     self._qdims = topleft[0], topleft[1], width, height
Exemple #8
0
 def display(self):
     """Display texture. """
     if self._dirty:
         depth = self._depth
         topleft = oglu.gluUnProject(0,self._winSize[1],depth)
         assert topleft, topleft
         if topleft[0:2] != self._qdims[0:2]:
             bottomleft = oglu.gluUnProject(0,0,depth)
             bottomright = oglu.gluUnProject(self._winSize[0],0,depth)
             topright = oglu.gluUnProject(self._winSize[0],self._winSize[1],depth)
             self.dims = topleft, topright, bottomright, bottomleft 
             width = topright[0] - topleft[0]
             height = topright[1] - bottomright[1]
             self._qdims = topleft[0], topleft[1], width, height
     LaminaScreenSurface.display(self)
Exemple #9
0
 def display(self):
     """Display texture. """
     if self._dirty:
         depth = oglu.gluProject(*self._point)[2]
         if depth != self._depth:
             bottomleft = oglu.gluUnProject(0,0,depth)
             bottomright = oglu.gluUnProject(self._winSize[0],0,depth)
             topleft = oglu.gluUnProject(0,self._winSize[1],depth)
             topright = oglu.gluUnProject(self._winSize[0],self._winSize[1],depth)
             self.dims = topleft, topright, bottomright, bottomleft 
             width = topright[0] - topleft[0]
             height = topright[1] - bottomright[1]
             self._qdims = topleft[0], topleft[1], width, height
             self._depth = depth
     LaminaScreenSurface.display(self)
Exemple #10
0
 def display(self):
     """Display texture. """
     if self._dirty:
         depth = oglu.gluProject(*self._point)[2]
         if depth != self._depth:
             bottomleft = oglu.gluUnProject(0, 0, depth)
             bottomright = oglu.gluUnProject(self._winSize[0], 0, depth)
             topleft = oglu.gluUnProject(0, self._winSize[1], depth)
             topright = oglu.gluUnProject(self._winSize[0], self._winSize[1], depth)
             self.dims = topleft, topright, bottomright, bottomleft
             width = topright[0] - topleft[0]
             height = topright[1] - bottomright[1]
             self._qdims = topleft[0], topleft[1], width, height
             self._depth = depth
     LaminaScreenSurface.display(self)
Exemple #11
0
 def display(self):
     """Display texture. """
     if self._dirty:
         depth = self._depth
         topleft = oglu.gluUnProject(0, self._winSize[1], depth)
         assert topleft, topleft
         if topleft[0:2] != self._qdims[0:2]:
             bottomleft = oglu.gluUnProject(0, 0, depth)
             bottomright = oglu.gluUnProject(self._winSize[0], 0, depth)
             topright = oglu.gluUnProject(self._winSize[0], self._winSize[1], depth)
             self.dims = topleft, topright, bottomright, bottomleft
             width = topright[0] - topleft[0]
             height = topright[1] - bottomright[1]
             self._qdims = topleft[0], topleft[1], width, height
     LaminaScreenSurface.display(self)
Exemple #12
0
 def mouseDoubleClickEvent(self, event):
     if isinstance(self.skin_surf, surface.Surface):
         self.lastPos = QtCore.QPoint(event.pos())
         x = float(self.lastPos.x())
         y = float(self.view[3] - self.lastPos.y())
         Near = GLU.gluUnProject(x, y, 0., self.model_matrix,
                                 self.projection_matrix, self.view)
         Far = GLU.gluUnProject(x, y, 1., self.model_matrix,
                                self.projection_matrix, self.view)
         self.intersect_point, self.intersect_normal = self.skin_surf.interceptRay(
             Near, Far)
         if self.intersect_point is not None:
             self.indicator = self.drawIndicator(self.intersect_point,
                                                 self.intersect_normal)
             self.update()
         self.windowClicked.emit(1)
Exemple #13
0
    def mouseDown(self, x, y):
        self.oldMouseX = x
        self.oldMouseY = y

        # Create the correct matrices for unprojection and set them up i matrices
        self.setUpCubeTransRot()
        modelView = gl.glGetDoublev(gl.GL_MODELVIEW_MATRIX)
        projectionView = gl.glGetDoublev(gl.GL_PROJECTION_MATRIX)
        viewport = gl.glGetIntegerv(gl.GL_VIEWPORT)

        # Invert y win-coord to match GL-coord and find 3d coords of the click
        y = viewport[3] - y
        z = gl.glReadPixels(x, y, 1, 1, gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT)
        pos3dx, pos3dy, pos3dz = glu.gluUnProject(x, y, z, modelView,
                                                  projectionView, viewport)

        # Bias allows us to click close to the boxs' edges
        bias = 0.01
        nSize = float(self.boxSize) / 2 + bias
        selectedBox = -1
        self.moveDir = ""

        # Test for which box has been clicked, if none of the clickables
        # we'll just rotate the entire cube around its own axes.
        for i in self.clickable:
            if (pos3dx >= self.boxes[i].pos[0] - nSize) and \
                (pos3dx <= self.boxes[i].pos[0] + nSize) and \
                (pos3dy >= self.boxes[i].pos[1] - nSize) and \
                (pos3dy <= self.boxes[i].pos[1] + nSize) and \
                (pos3dz >= self.boxes[i].pos[2] - nSize) and \
                (pos3dz <= self.boxes[i].pos[2] + nSize):
                selectedBox = i
                break

        self.selectedBox = selectedBox
Exemple #14
0
    def mouseDown(self, x, y):
        self.oldMouseX = x
        self.oldMouseY = y

        # Create the correct matrices for unprojection and set them up i matrices
        self.setUpCubeTransRot()
        modelView = gl.glGetDoublev(gl.GL_MODELVIEW_MATRIX)
        projectionView = gl.glGetDoublev(gl.GL_PROJECTION_MATRIX)
        viewport = gl.glGetIntegerv(gl.GL_VIEWPORT)

        # Invert y win-coord to match GL-coord and find 3d coords of the click
        y = viewport[3] - y
        z = gl.glReadPixels(x, y, 1, 1, gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT)
        pos3dx, pos3dy, pos3dz = glu.gluUnProject(x, y, z, modelView, projectionView, viewport)

        # Bias allows us to click close to the boxs' edges
        bias = 0.01
        nSize = float(self.boxSize) / 2 + bias
        selectedBox = -1
        self.moveDir = ""

        # Test for which box has been clicked, if none of the clickables
        # we'll just rotate the entire cube around its own axes.
        for i in self.clickable:
            if (pos3dx >= self.boxes[i].pos[0] - nSize) and \
                (pos3dx <= self.boxes[i].pos[0] + nSize) and \
                (pos3dy >= self.boxes[i].pos[1] - nSize) and \
                (pos3dy <= self.boxes[i].pos[1] + nSize) and \
                (pos3dz >= self.boxes[i].pos[2] - nSize) and \
                (pos3dz <= self.boxes[i].pos[2] + nSize):
                selectedBox = i
                break

        self.selectedBox = selectedBox
Exemple #15
0
def get_pick_ray(x: float, y: float, modelview,
                 projection) -> Tuple[List[float], List[float]]:
    """
    :param x: rightward screen coordinate
    :param y: downward screen coordinate
    :param modelview: modelview matrix
    :param projection: projection matrix
    :return: two points of the pick ray from the closest and furthest frustum
    """
    viewport = GL.glGetIntegerv(GL.GL_VIEWPORT)
    real_y = viewport[3] - y  # adjust for down-facing y positions

    # Unproject screen coords into world coordsdd
    p_front = GLU.gluUnProject(x, real_y, 0, modelview, projection, viewport)
    p_back = GLU.gluUnProject(x, real_y, 1, modelview, projection, viewport)
    return p_front, p_back
Exemple #16
0
    def mouseMoveEvent(self, event):
        if event.buttons() & Qt.LeftButton:
            x, y, z = glu.gluUnProject(event.x(), event.y(), 0)

            radius = self.radius()

            def normalize(P):
                return P / numpy.sqrt(numpy.sum(P**2))

            def toSphere(P, r=radius):
                #Project P to the centered sphere
                P = P - self.center
                P = normalize(P)
                return self.center + (P * radius)

            diff = numpy.array([x, y, z]) - numpy.array(self._lastMousePos)
            pos = diff + self._last_camera

            camera = numpy.array(self._last_camera)
            center = numpy.array(self.center)
            dist = numpy.sqrt(numpy.sum(
                (camera - center)**2))  #distance from the center
            camera = (pos - center) / numpy.sqrt(numpy.sum((pos - center)**2))
            camera = camera * dist
            camera = center + camera
            self.camera[:] = camera

            self._lastMousePos = x, y, z
            self._last_camera = numpy.array(self.camera)
            self.updateGL()
Exemple #17
0
    def get_world_coords(self,
                         x: int,
                         y: int,
                         z: float = None,
                         correction: bool = False
                         ) -> Tuple[float, float, float]:
        x *= self.DEVICE_PIXEL_RATIO  # For fixing mac retina bug
        y *= self.DEVICE_PIXEL_RATIO

        # Stored projection matrices are taken from loop
        viewport = GL.glGetIntegerv(GL.GL_VIEWPORT)
        real_y = viewport[3] - y  # adjust for down-facing y positions

        if z is None:
            buffer_size = 21
            center = buffer_size // 2 + 1
            depths = GL.glReadPixels(
                x - center + 1,
                real_y - center + 1,
                buffer_size,
                buffer_size,
                GL.GL_DEPTH_COMPONENT,
                GL.GL_FLOAT,
            )
            z = depths[center][center]  # Read selected pixel from depth buffer

            if z == 1:
                z = depth_smoothing(depths, center)
            elif correction:
                z = depth_min(depths, center)

        mod_x, mod_y, mod_z = GLU.gluUnProject(x, real_y, z, self.modelview,
                                               self.projection, viewport)
        return mod_x, mod_y, mod_z
Exemple #18
0
 def pickPoint(self, wx, wy, wz=None):
     wy = self._height - wy
     if wz == None:
         wz = glReadPixels(wx, wy, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT)
     try:
         return na.array(GLU.gluUnProject(wx, wy, wz))
     except ValueError:
         return None
Exemple #19
0
 def pixelUnproject(self, winx, winy, winz):
     center = glu.gluUnProject(winx,
                               winy,
                               winz,
                               model=QMatrix4x4().data(),
                               proj=self.cam.projectView.data(),
                               view=(0, 0, self.width(), self.height()))
     return QVector3D(*center)
Exemple #20
0
 def cursorPosition(self, eventPos):
     """cursor coordinates on z=0 plane for the given screen pixel coord"""
     w, h = self.scene.surf.get_size()
     sx, sy = eventPos
     # something's messed up with my projections. there shouldn't
     # be any estimated 5.3 multiplier in here
     wx, wy, _ = GLU.gluUnProject(w / 2 + (sx - w / 2) * 5.3,
                                  h / 2 - (sy - h / 2) * 5.3, 0)
     return [wx, wy, 0]
    def paintGL(self):
        GL.glClearColor(1, 1, 1, 1.0)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glLoadIdentity()

        GL.glTranslatef(0, 0, -self._zoom)

        GL.glColor4f(1, 1, 1, 1.0)
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex3f(20, -20, 0)
        GL.glVertex3f(20, 20, 0)
        GL.glVertex3f(-20, 20, 0)
        GL.glVertex3f(-20, -20, 0)
        GL.glEnd()

        modelview = GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX)
        projection = GL.glGetDoublev(GL.GL_PROJECTION_MATRIX)
        viewport = GL.glGetIntegerv(GL.GL_VIEWPORT)
        winX = float(self._mouseX)
        winY = float(viewport[3] - self._mouseY)
        winZ = GL.glReadPixels(winX, winY, 1, 1, GL.GL_DEPTH_COMPONENT,
                               GL.GL_FLOAT)
        self._glX, self._glY, self._glZ = GLU.gluUnProject(
            winX, winY, winZ[0][0], modelview, projection, viewport)

        ####CLEAR EVERYTHING
        GL.glClearColor(1, 1, 1, 1.0)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glLoadIdentity()

        GL.glTranslatef(0, 0, -self._zoom)

        GL.glEnable(GL.GL_LIGHTING)
        GL.glEnable(GL.GL_LIGHT0)
        GL.glEnable(GL.GL_COLOR_MATERIAL)

        GL.glTranslatef(self._x - self._moveGlX, self._y - self._moveGlY, 0.0)

        for node in self._nodes.values():
            node.viewerDraw()
            GL.glColor4f(0.0, 0.0, 0.0, 1.0)
            self.renderText(node._viewer_x + node._viewer_radius + 0.005,
                            node._viewer_y - 0.001, 0.0, node.name)

        for module in self._nodes.values():
            for name, param in module.formControls.items():
                connectedModule = param.value
                if isinstance(param, ModuleConnection) and isinstance(
                        connectedModule, OTModuleWorkFlowItem):
                    connectedModule.viewerDrawConnectionBetween(module)

        GL.glDisable(GL.GL_LIGHTING)
        GL.glDisable(GL.GL_LIGHT0)
        GL.glDisable(GL.GL_COLOR_MATERIAL)
Exemple #22
0
 def cursorPosition(self, eventPos):
     """cursor coordinates on z=0 plane for the given screen pixel coord"""
     w, h = self.scene.surf.get_size()
     sx, sy = eventPos
     # something's messed up with my projections. there shouldn't
     # be any estimated 5.3 multiplier in here
     wx, wy, _ = GLU.gluUnProject(w / 2 + (sx - w / 2) * 5.3,
                                  h / 2 - (sy - h / 2) * 5.3,
                                  0)
     return [wx, wy, 0]
    def _getPickingRay(self, x, y):
        '''
        Process the ray for the current
        mouse position
        '''
        viewport = glGetIntegerv(GL_VIEWPORT)
        model_mat = np.array(glGetDoublev(GL_MODELVIEW_MATRIX))
        proj_mat = np.array(glGetDoublev(GL_PROJECTION_MATRIX))

        # win_coord   = (x*2, viewport[3] - y*2)
        win_coord = (x, viewport[3] - y)
        near_point = np.array(
            GLU.gluUnProject(win_coord[0], win_coord[1], 0.0, model_mat,
                             proj_mat, viewport))
        far_point = np.array(
            GLU.gluUnProject(win_coord[0], win_coord[1], 1.0, model_mat,
                             proj_mat, viewport))

        return far_point - near_point
Exemple #24
0
 def mouseDoubleClickEvent(self, event):
     if isinstance(self.skin_surf, surface.Surface):
         r = self.devicePixelRatio()
         #print "Ratio:", r
         self.lastPos = QtCore.QPoint(event.pos())
         view = GL.glGetIntegerv(GL.GL_VIEWPORT)
         #print "View port:", view
         #print self.lastPos
         Near = GLU.gluUnProject(float(r * self.lastPos.x()),
                                 float(view[3] - r * self.lastPos.y()), 0.)
         Far = GLU.gluUnProject(float(r * self.lastPos.x()),
                                float(view[3] - r * self.lastPos.y()), 1.)
         self.intersect_point, self.intersect_normal = self.skin_surf.interceptRay(
             Near, Far)
         if self.intersect_point is not None:
             self.indicator = self.drawIndicator(self.intersect_point,
                                                 self.intersect_normal)
             self.updateGL()
         self.windowClicked.emit(1)
Exemple #25
0
    def tkHandlePick(self, event):
        """Handle a pick on the scene."""

        if hasattr(self, 'pick'):
            # here we need to use glu.UnProject

            # Tk and X have their origin top left,
            # while Opengl has its origin bottom left.
            # So we need to subtract y from the window height to get
            # the proper pick position for Opengl

            realy = self.winfo_height() - event.y

            p1 = GLU.gluUnProject(event.x, realy, 0.)
            p2 = GLU.gluUnProject(event.x, realy, 1.)

            if self.pick(self, p1, p2):
                """If the pick method returns true we redraw the scene."""

                self.tkRedraw()
Exemple #26
0
    def draw_axes_markings(self):
        gl.glLineWidth(2.5)
        self.qglColor(QtCore.Qt.black)
        left, bottom, z_ = glu.gluUnProject(0, 0, 0)
        right, top, z_ = glu.gluUnProject(self.width, self.height, 0)
        gl.glBegin(gl.GL_LINES)
        # Bottom
        gl.glVertex2f(left, bottom)
        gl.glVertex2f(right, bottom)
        # Right
        gl.glVertex2f(right, bottom)
        gl.glVertex2f(right, top)
        # Top
        gl.glVertex2f(left, top)
        gl.glVertex2f(right, top)
        # Left
        gl.glVertex2f(left, bottom)
        gl.glVertex2f(left, top)
        gl.glEnd()

        self.draw_axes_ticks(left, right, bottom, top, self.x_ticks, self.y_ticks)
Exemple #27
0
 def get_point_in_3D(self, x, y):
     model = gl.glGetDoublev(gl.GL_MODELVIEW_MATRIX)
     proj = gl.glGetDoublev(gl.GL_PROJECTION_MATRIX)
     view = gl.glGetIntegerv(gl.GL_VIEWPORT)
     winY = int(float(view[3]) - float(y))
     z = gl.glReadPixels(x, winY, 1, 1, gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT)
     point = glu.gluUnProject(x, y, z, model, proj, view)
     al = math.pi * self.rotX / 180
     bet = 0  # math.pi * self.rotY / 180
     gam = math.pi * self.rotZ / 180
     point = self.rotate_un_vector(np.array(point), al, bet, gam)
     point = self.rotate_vector(np.array(point), al, bet, gam)
     return point
def unProject(x, y, win):
    "Map the window coordinates (x,y) to object coordinates." ""
    win.makeCurrent()
    y = win.h - y
    model = GL.glGetFloatv(GL_MODELVIEW_MATRIX)
    proj = GL.glGetFloatv(GL_PROJECTION_MATRIX)
    view = GL.glGetIntegerv(GL_VIEWPORT)
    # print "Modelview matrix:",model
    # print "Projection matrix:",proj
    # print "Viewport:",view
    objx, objy, objz = GLU.gluUnProject(x, y, 0.0, model, proj, view)
    # print "Coordinates: ",x,y," map to ",objx,objy
    return (objx, objy)
Exemple #29
0
 def unProject(self, x, y, z, locked=False):
     "Map the window coordinates (x,y,z) to object coordinates." ""
     locked = False
     if locked:
         model, proj, view = self.projection_matrices
     else:
         self.makeCurrent()
         self.camera.loadProjection()
         model = GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX)
         proj = GL.glGetDoublev(GL.GL_PROJECTION_MATRIX)
         view = GL.glGetIntegerv(GL.GL_VIEWPORT)
     objx, objy, objz = GLU.gluUnProject(x, y, z, model, proj, view)
     return (objx, objy, objz)
     return self.camera.unProject(x, y, z)
Exemple #30
0
 def Screen2World(self,x=None,y=None):
   """
   @note:http://nehe.gamedev.net/article/using_gluunproject/16013/
   """
   if x==None or y==None:
     x=self.x
     y=self.y
   viewport=gl.glGetIntegerv(gl.GL_VIEWPORT)
   modelview=gl.glGetDoublev(gl.GL_MODELVIEW_MATRIX)
   projection=gl.glGetDoublev(gl.GL_PROJECTION_MATRIX) #matrice pour clip coordinates
   y = viewport[3] - y
   z=gl.glReadPixels(x,y, 1, 1,gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT)
   pos=glu.gluUnProject(x,y,z, modelview, projection, viewport)
   return pos
Exemple #31
0
    def pix2canvas(self, pt):
        """Takes a 2-tuple of (x, y) in window coordinates and gives
        the (cx, cy, cz) coordinates on the canvas.
        """
        x, y = pt
        mm = gl.glGetDoublev(gl.GL_MODELVIEW_MATRIX)
        pm = gl.glGetDoublev(gl.GL_PROJECTION_MATRIX)
        vp = gl.glGetIntegerv(gl.GL_VIEWPORT)

        win_x, win_y = float(x), float(vp[3] - y)
        win_z = gl.glReadPixels(int(x), int(win_y), 1, 1,
                                gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT)
        pos = glu.gluUnProject(win_x, win_y, win_z, mm, pm, vp)
        return pos
Exemple #32
0
 def unProject(self,x,y,z,locked=False):
     "Map the window coordinates (x,y,z) to object coordinates."""
     locked=False
     if locked:
         model,proj,view = self.projection_matrices
     else:
         self.makeCurrent()
         self.camera.loadProjection()
         model = GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX)
         proj = GL.glGetDoublev(GL.GL_PROJECTION_MATRIX)
         view = GL.glGetIntegerv(GL.GL_VIEWPORT)
     objx, objy, objz = GLU.gluUnProject(x,y,z,model,proj,view)
     return (objx,objy,objz)
     return self.camera.unProject(x,y,z)
Exemple #33
0
  def CastRayThroughWindowCoordinate(self, x, y):
    self.SetupCamera()
    modelview = GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX)
    projection = GL.glGetDoublev(GL.GL_PROJECTION_MATRIX)
    viewport = GL.glGetIntegerv(GL.GL_VIEWPORT)
    y = viewport[3] - y
    near = numpy.asarray(GLU.gluUnProject(
        x,
        y,
        0.0,
        modelview,
        projection,
        viewport
        ))
    far = numpy.asarray(GLU.gluUnProject(
        x,
        y,
        1.0,
        modelview,
        projection,
        viewport
        ))

    return numpy.array((near, far - near))
Exemple #34
0
    def paintGL(self):
        GL.glClearColor(0.5, 0.5, 0.5, 1.0)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glLoadIdentity()

        translateX = (len(self._texture) * self._width) / 2

        if len(self._texture) > 0:
            GL.glTranslatef(-translateX, -self._height / 2, -self._zoom)

            GL.glDisable(GL.GL_TEXTURE_2D)
            GL.glColor4f(0.5, 0.5, 0.5, 1.0)
            GL.glBegin(GL.GL_QUADS)
            GL.glVertex3f(20, -20, -.001)
            GL.glVertex3f(20, 20, -.001)
            GL.glVertex3f(-20, 20, -.001)
            GL.glVertex3f(-20, -20, -.001)
            GL.glEnd()

            GL.glColor4f(1, 1, 1, 1.0)

            GL.glEnable(GL.GL_TEXTURE_2D)

            for texture_index in range(0, len(self._texture)):
                if texture_index > 0: GL.glTranslatef(self._width, 0, 0)

                GL.glBindTexture(GL.GL_TEXTURE_2D,
                                 self._texture[texture_index])

                if self._mouseRightDown:
                    self.drawVideo(self._width, self._height,
                                   self._x - (self._lastGlX - self._glX),
                                   self._y - (self._lastGlY - self._glY), 0.0)
                else:
                    self.drawVideo(self._width, self._height, self._x, self._y,
                                   0.0)

            if self._mouseDown:
                modelview = GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX)
                projection = GL.glGetDoublev(GL.GL_PROJECTION_MATRIX)
                viewport = GL.glGetIntegerv(GL.GL_VIEWPORT)
                winX = float(self._mouseX)
                winY = float(viewport[3] - self._mouseY)
                winZ = GL.glReadPixels(winX, winY, 1, 1, GL.GL_DEPTH_COMPONENT,
                                       GL.GL_FLOAT)
                self._glX, self._glY, self._glZ = GLU.gluUnProject(
                    winX, winY, winZ[0][0], modelview, projection, viewport)
Exemple #35
0
    def draw_axes_ticks(self, left, right, bottom, top, num_ticks_x, num_ticks_y):
        tick_width = 5
        x_, y_, z_ = glu.gluUnProject(tick_width, tick_width, 0)
        dx = (right - left) / (num_ticks_x - 1)
        dy = (top - bottom) / (num_ticks_y - 1)

        gl.glLineWidth(1)
        gl.glBegin(gl.GL_LINES)
        # Draw number ticks in x direction
        for x in range(num_ticks_x):
            gl.glVertex3f(x * dx + left, bottom, 0)
            gl.glVertex3f(x * dx + left, y_, 0)
        for y in range(num_ticks_y):
            gl.glVertex3f(left, y * dy + bottom, 0)
            gl.glVertex3f(x_, y * dy + bottom, 0)
        gl.glEnd()
Exemple #36
0
    def __winPosTo3DPos(self, x, y):
        point = [None, None, None]                              # result point
        modelview  = GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX)    # get the modelview info
        projection = GL.glGetDoublev(GL.GL_PROJECTION_MATRIX)   # get the projection matrix info
        viewport   = GL.glGetIntegerv(GL.GL_VIEWPORT)           # get the viewport info
 
        # in OpenGL y soars (steigt) from bottom (0) to top
        y_new = viewport[3] - y     
 
        # read depth buffer at position (X/Y_new)
        z = GL.glReadPixels(x, y_new, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT)
        # z should not be 0!!!
        # error when projection matrix not identity (gluPerspective) 
        point[0], point[1], point[2] = GLU.gluUnProject(x, y_new, z, modelview, projection, viewport)                         
        
        return point
Exemple #37
0
 def send_mouseclick_to_caller(self, cursor_x, cursor_y, button='right'):
     client = zmq.Context.instance().socket(zmq.PUSH)
     client.connect('tcp://127.0.0.1:%d' % (self.mouseclick_port))
     cameras = self.on_draw(want_cameras=True)
     window_height = glut.glutGet(glut.GLUT_WINDOW_HEIGHT)
     depth_value = gl.glReadPixels(cursor_x, window_height - cursor_y, 1, 1,
                                   gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT)
     pyobj = {
         'event_type': 'mouse_click_%sbutton' % button,
         'u': None,
         'v': None,
         'x': None,
         'y': None,
         'z': None,
         'subwindow_row': None,
         'subwindow_col': None
     }
     for subwin_row, camera_list in enumerate(cameras):
         for subwin_col, camera in enumerate(camera_list):
             # test for out-of-bounds
             if cursor_x < camera['viewport'][0]:
                 continue
             if cursor_x > (camera['viewport'][0] + camera['viewport'][2]):
                 continue
             if window_height - cursor_y < camera['viewport'][1]:
                 continue
             if window_height - cursor_y > (camera['viewport'][1] +
                                            camera['viewport'][3]):
                 continue
             xx, yy, zz = glu.gluUnProject(cursor_x,
                                           window_height - cursor_y,
                                           depth_value,
                                           camera['modelview_matrix'],
                                           camera['projection_matrix'],
                                           camera['viewport'])
             pyobj = {
                 'event_type': 'mouse_click_%sbutton' % button,
                 'u': cursor_x - camera['viewport'][0],
                 'v': window_height - cursor_y - camera['viewport'][1],
                 'x': xx,
                 'y': yy,
                 'z': zz,
                 'which_subwindow': (subwin_row, subwin_col)
             }
     client.send_pyobj(pyobj)
     del self.mouseclick_port
    def tkTranslate(self, event):
        """Perform translation of scene."""

        self.activate()

        # Scale mouse translations to object viewplane so object tracks with mouse

        win_height = max(1, self.winfo_height())
        obj_c = (self.xcenter, self.ycenter, self.zcenter)
        win = GLU.gluProject(obj_c[0], obj_c[1], obj_c[2])
        obj = GLU.gluUnProject(win[0], win[1] + 0.5 * win_height, win[2])
        dist = math.sqrt(v3distsq(obj, obj_c))
        scale = abs(dist / (0.5 * win_height))

        glTranslateScene(scale, event.x, event.y, self.xmouse, self.ymouse)
        self.tkRedraw()
        self.tkRecordMouse(event)
    def paintGL(self):
        GL.glClearColor(0.5, 0.5, 0.5, 1.0)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glLoadIdentity()

        translateX = ( len(self.texture) * self._width ) / 2

        if len(self.texture) > 0:
            GL.glTranslatef( -translateX, -self._height/2, -self.zoom)

            GL.glDisable(GL.GL_TEXTURE_2D)
            GL.glColor4f(0.5, 0.5, 0.5, 1.0)
            GL.glBegin(GL.GL_QUADS)
            GL.glVertex3f(20, -20, -.001)
            GL.glVertex3f(20, 20, -.001)
            GL.glVertex3f(-20, 20, -.001)
            GL.glVertex3f(-20, -20, -.001)
            GL.glEnd()

            GL.glColor4f(1, 1, 1, 1.0)

            GL.glEnable(GL.GL_TEXTURE_2D)

            for texture_index in range(0, len(self.texture) ):
                if texture_index>0: GL.glTranslatef( self._width,  0, 0)

                GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture[ texture_index ])

                if self._mouseRightDown:
                    self.drawVideo( self._width, self._height, self._x- (self._lastGlX - self._glX) , self._y - (self._lastGlY - self._glY), 0.0)
                else:
                    self.drawVideo( self._width, self._height, self._x , self._y, 0.0)

            if self._mouseDown:
                modelview = GL.glGetDoublev( GL.GL_MODELVIEW_MATRIX )
                projection = GL.glGetDoublev( GL.GL_PROJECTION_MATRIX )
                viewport = GL.glGetIntegerv( GL.GL_VIEWPORT )
                winX = float(self._mouseX);
                winY = float(viewport[3] - self._mouseY)
                winZ = GL.glReadPixels(winX, winY, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT)
                self._glX, self._glY, self._glZ = GLU.gluUnProject( winX, winY, winZ[0][0], modelview, projection, viewport)
Exemple #40
0
    def find_data_pos(self):
        """
        Looks at mouse position and calculates data position of data point closest to cursor
        Returns: (tree number, point number, distance from cursor, x-position in view, y-position in view
        """
        x_, y_, z_ = glu.gluUnProject(self.mouse_pos.x(), self.height - self.mouse_pos.y(), 0)

        x_ /= (self.scale_x * self.ratio)
        y_ /= self.scale_y

        d = 99999999999
        pt_num = -1
        tree_num = -1
        for i in range(0, len(self.kd_tree)):
            _d, _pt_num = self.kd_tree[i].query([x_, y_])
            if _d < d:
                tree_num = i
                pt_num = _pt_num
                d = _d
        if pt_num == -1 or tree_num == -1:
            raise KeyError("Unable to get information")
        focus_x, focus_y = self.kd_tree[tree_num].data[pt_num]
        return tree_num, pt_num, d, focus_x * self.scale_x * self.ratio, focus_y * self.scale_y
Exemple #41
0
    def scale(self, mouse, mouse_release, window_height=None, view=None):

        if view is not None:
            self.orig_view_scale = view
            print ("view", view)
            print view

        """
        Scales the window according to movement
        mouse - Mouse Event passed in
        mouse_release - Boolean representing release
        """
        if self.prev_position_scale is None:
            self.prev_position_scale = mouse.pos()
            x, y, z = glu.gluUnProject(mouse.pos().x(), window_height - mouse.pos().y(), 0.0)
            self.anchor_position_scale = x, y
        elif not mouse_release:
            current_position = mouse.pos()
            return current_position.x() - self.prev_position_scale.x()
        else:
            # print "Mouse scaling released"
            current_position = mouse.pos()
            prev_xy = (self.prev_position_scale.x(), self.prev_position_scale.y())
            curr_xy = (current_position.x(), current_position.y())
            dist = math.hypot(curr_xy[0] - prev_xy[0], curr_xy[1] - prev_xy[1])
            if dist < 2:
                # print "Treat as a click"
                callback = Callbacks.CLICK
            else:
                # print "Zoom in"
                callback = Callbacks.RESIZE

            self.prev_position_scale = None
            self.anchor_position_scale = None
            self.orig_view_scale = None

            return callback
Exemple #42
0
 def mousePressEvent(self, event):
     if event.button() == Qt.LeftButton:
         self._last_camera = numpy.array(self.camera)
         self._lastMousePos = glu.gluUnProject(event.x(), event.y(), 0)
Exemple #43
0
def main():
    window_width = 800
    window_height = 600
    
    print "Loading locale ...",
    gettext.install('default', 'locale')
    po = { "position" :  _("Position"), "height" : _("Height"), "big" :  _("Big"), "small" : _("Small"), 
           "sides" : _("Sides"), "grid" : _("Grid"), "exit" : _("Exit"),
           "del" : _("Delete"), "clear" : _("Clear"), "options" : _("Options"),
           "up" : _("Up"), "down" : _("Down"), "left" : _("Left"), "right" : _("Right"), "forward" : _("Forward"), "back" : _("Back"),
           "motion_blur" : _("Motion Blur"), "light" : _("Light"),
           "few"      : _("Too few sides given to describe a rectangular shape"),
           "not_even" : _("Odd number of sides can't border a rectangular shape"),
           "both"     : _("Neither even nor odd indexed sides are closed"),
           "even"     : _("Even indexed sides are not closed"),
           "odd"      : _("Odd indexed sides are not closed")
           }
    for k in po:
        po[k] = unicode(po[k],'utf8')
    print "Done"
    
    print "Loading pygame ..." ,
    pygame.display.init()
    pygame.font.init()
    print "Done"
    
    print "Loading screen ...",
    pygame.display.set_mode ( (window_width,window_height), pygame.OPENGL|pygame.DOUBLEBUF|pygame.RESIZABLE, 24 )
    print "Done"
    
    print "Loading opengl ...",
    lego.gl_init( window_width, window_height )
    print "Done"
    
    # draw dialog screen 
    
    print "Loading layers ...",
    
    button_color = (0.8, 0.8, 0.0)
    button_focus_color = (0.3, 0.8, 0.5)
    button_error_color = (0.8, 0.4, 0.4)
    
    title      = pygame.font.SysFont("courier", 24, True,  True)
    error      = pygame.font.SysFont("courier", 18, True,  True)
    small      = pygame.font.SysFont("courier", 14, False, True)
    bold_small = pygame.font.SysFont("courier", 14, True,  True)
    title_scale = 15
    sub_scale = -5
    text_color = ( 192, 64, 128 )
    text_error_color = (204, 204, 0)
    
    piecesize = reference()
    grid = opposite(True)
    mblur = opposite(False)
    
    menu = reference()
    
    static = dict()
    static["options"] = gui.toggle(  10,10, 145, 37, button_color, button_focus_color, button_error_color, 2, menu )
    static["edit"]    = gui.toggle( 165,10, 145, 37, button_color, button_focus_color, button_error_color, 1, menu )
    
    static_layers = gui.layer_manager()
    static_layers.add( small.render(po["options"], True,text_color), 25, 17, (0,3) )
    static_layers.add( small.render("Edit", True,text_color), 180, 17, (0,3) )
    static_layers.load()
    
    
    edit = dict()
    edit["x"]          = gui.textbox(  11,107,  42, 32, button_color, button_focus_color, button_error_color, bold_small, text_color, text_error_color, (0,4) ) 
    edit["y"]          = gui.textbox( 113,105,  42, 32, button_color, button_focus_color, button_error_color, bold_small, text_color, text_error_color, (0,4) ) 
    edit["z"]          = gui.textbox(  62,107,  42, 32, button_color, button_focus_color, button_error_color, bold_small, text_color, text_error_color, (0,4) ) 
    edit["big"]        = gui.toggle (  33,207,  32, 32, button_color, button_focus_color, button_error_color, lego.LEGO_BIG,   piecesize ) 
    edit["small"]      = gui.toggle ( 101,207,  32, 32, button_color, button_focus_color, button_error_color, lego.LEGO_SMALL, piecesize ) 
    edit["sides"]      = gui.textbox(  11,277, 145,100, button_color, button_focus_color, button_error_color, bold_small, text_color, text_error_color ) 
    edit["grid"]       = gui.toggle ( 123,391,  32, 32, button_color, button_focus_color, button_error_color, True, grid ) 
    edit["raise_grid"] = gui.arrow  (  17,437,  32, 32, button_color, button_focus_color, button_error_color ) 
    edit["lower_grid"] = gui.arrow  ( 117,437,  32, 32, button_color, button_focus_color, button_error_color, False ) 
    edit["send"]       = gui.button (  11,487, 145, 37, button_color, button_focus_color, button_error_color )
    edit["del"]        = gui.button (  11,536,  70, 37, button_color, button_focus_color, button_error_color )
    edit["clear"]      = gui.button (  86,536,  70, 37, button_color, button_focus_color, button_error_color )
    
    edit_layers = gui.layer_manager()
    
    edit_layers.add( pygame.image.load(os.path.join('data','ui.png')), 0, 57, (0,0) )
    
    edit_layers.add( title.render(po["position"],True,text_color),  20,  67, (0,title_scale) )
    edit_layers.add( title.render(po["height"],  True,text_color),  20, 139, (0,title_scale) )
    edit_layers.add( title.render(po["big"],     True,text_color),  20, 179, (0,sub_scale)   )
    edit_layers.add( title.render(po["small"],   True,text_color),  85, 179, (0,sub_scale)   )
    edit_layers.add( title.render(po["sides"],   True,text_color),  20, 239, (0,title_scale) )
    edit_layers.add( title.render(po["grid"],    True,text_color),  20, 387, (0,title_scale) )
    edit_layers.add( title.render("OK",          True,text_color),  67, 494, (0,sub_scale) )
    edit_layers.add( small.render(po["del"],     True,text_color),  20, 545, (0,3) )
    edit_layers.add( small.render(po["clear"],   True,text_color),  95, 545, (0,3) )
    edit_layers.load()
    
    options = dict()
    options["mblur"]       = gui.toggle( 67,355, 32, 32, button_color, button_focus_color, button_error_color, True, mblur )
    options["raise_mblur"] = gui.arrow ( 17,388, 32, 32, button_color, button_focus_color, button_error_color ) 
    options["lower_mblur"] = gui.arrow ( 117,388, 32, 32, button_color, button_focus_color, button_error_color, False )
    options["raise_light"] = gui.arrow ( 17,487, 32, 32, button_color, button_focus_color, button_error_color ) 
    options["lower_light"] = gui.arrow ( 117,487, 32, 32, button_color, button_focus_color, button_error_color, False )
    
    options_layers = gui.layer_manager()
    
    options_layers.add( edit_layers.layer_list[0][0], 0, 57, (0,0) )
    
    options_layers.add( title.render(po["motion_blur"],True,text_color),  8,301, (0,title_scale) )
    options_layers.add( title.render(po["light"],      True,text_color),  8,434, (0,title_scale) )
    
    options_layers.add( small.render(' '.join(( po["forward"], ":" )), True,text_color),  10, 65, (0,3) )
    options_layers.add( small.render(' '.join(( po["back"], ":" )),    True,text_color),  10, 98, (0,3) )
    options_layers.add( small.render(' '.join(( po["left"], ":" )),    True,text_color),  10,131, (0,3) )
    options_layers.add( small.render(' '.join(( po["right"], ":" )),   True,text_color),  10,164, (0,3) )
    options_layers.add( small.render(' '.join(( po["up"], ":" )),      True,text_color),  10,197, (0,3) )
    options_layers.add( small.render(' '.join(( po["down"], ":" )),    True,text_color),  10,230, (0,3) )
    options_layers.add( small.render(' '.join(( po["exit"], ":" )),    True,text_color),  10,263, (0,3) )
    
    options_layers.add( small.render('W',    True,text_color),  100, 65, (0,3) )
    options_layers.add( small.render('S',    True,text_color),  100, 98, (0,3) )
    options_layers.add( small.render('A',    True,text_color),  100,131, (0,3) )
    options_layers.add( small.render('D',    True,text_color),  100,164, (0,3) )
    options_layers.add( small.render('Space',True,text_color),  100,197, (0,3) )
    options_layers.add( small.render('Ctrl', True,text_color),  100,230, (0,3) )
    options_layers.add( small.render('Esc',  True,text_color),  100,263, (0,3) )
    
    options_layers.load()
    
    controls = static.copy()
    menus = { 1 : edit, 2: options }
    
    print "Done"
    
    print "\nEntering drawing loop\n"
    pieces = list()
    pieces.append(lego.piece( (4,2,-2,2,-1,1,-2,-1,-1,-2,2,-2), lego.LEGO_BIG,   (1.0, 0.1, 0.2), (-6, 0,    -6) ))
    pieces.append(lego.piece( (1,-1,1,1,1,-1,1,4,-3,-1,-1,-2),  lego.LEGO_SMALL, (0.2, 0.8, 0.3), (-8, 2.0/3, 2) ))
    pieces.append(lego.piece( (2,2,1,1,-1,2,-2,-5),             lego.LEGO_SMALL, (0.2, 0.3, 0.8), ( 5, 1.0/3, 1) ))
    pieces.append(lego.piece( (5,6,-5,-1,-1,-4,1,-1),           lego.LEGO_BIG,   (0.8, 0.3, 0.8), ( 1, 0    ,-2) ))
    pieces.append(lego.piece( (2,7,-2,-5,-2,-1,2,-1),           lego.LEGO_SMALL, (0.9, 0.9, 0.3), ( 0, 1,     0) ))
    
    lightp = np.array((2.0, 4.0, -4.0, 1.0))
    
    light_int = 0.5
    light_amb = 1.0 - (1.0 - light_int) ** 2
    
    GL.glLightfv( GL.GL_LIGHT0, GL.GL_AMBIENT, (light_amb * 0.1, light_amb * 0.1, light_amb * 0.1, 1.0) )  # Setup The Ambient Light
    GL.glLightfv( GL.GL_LIGHT0, GL.GL_DIFFUSE, (light_amb, light_amb, light_amb, 1.0) )                    # Setup The Diffuse Light
    GL.glEnable(GL.GL_LIGHT0)                                                                              # Enable Light One
    
    eps = sys.float_info.epsilon
    
    ticker = pygame.time.Clock()
    
    running = True
    fps = 30
    
    accumulate = False
    mblur_rate = 0.5
    
    rotating = False
    rotrot = 0.0
    rot_speed = 1.5
    rotx = 0
    roty = -40.0
    mouse_sens = -120
    
    position = np.array(( 2.0, -15, 15, 0.0))
    move = np.array(( 0.0, 0.0, 0.0, 0.0))
    move_speed = 0.5
    
    slow = False
    
    focused = None
    mouse_down = None
    drag_from = None 
    key_hit = None
    
    errors = set()
    chosen_index = None
    
    grid_level = 0
    
    edit_dyn_layers = gui.dynamic_layer_manager()
    edit_dyn_layers.add( "grid-level", title.render(str(grid_level),True,text_color), 75, 437, (0,title_scale) )
    
    options_dyn_layers = gui.dynamic_layer_manager()
    options_dyn_layers.add( "mblur-level", title.render(str(mblur_rate),True,text_color), 61, 388, (0,title_scale) )
    options_dyn_layers.add( "light-level", title.render(str(light_int) ,True,text_color), 61, 487, (0,title_scale) )
    
    while running:
        cx = np.cos( rotx / 180.0 * np.pi )
        sx = np.sin( rotx / 180.0 * np.pi)
        cy = np.cos( roty / 180.0 * np.pi)
        sy = np.sin( roty / 180.0 * np.pi)

        xrot = np.array(( (cx,  0.0, -sx,  0.0), 
                          (0.0, 1.0,  0.0, 0.0),
                          (sx,  0.0,  cx,  0.0),
                          (0.0, 0.0,  0.0, 1.0)  ))
        
        yrot = np.array(( (1.0,  0.0, 0.0, 0.0), 
                          (0.0,  cy,  sy,  0.0),
                          (0.0, -sy,  cy,  0.0),
                          (0.0,  0.0, 0.0, 1.0)  ))
        
        rot = np.dot( xrot, yrot )
        
        # handle events
        
        for event in pygame.event.get():
            
            if event.type == pygame.QUIT:
                running = False
                
            elif event.type == pygame.VIDEORESIZE:
                window_width, window_height = event.size
                pygame.display.set_mode ( (window_width,window_height), pygame.OPENGL|pygame.DOUBLEBUF|pygame.RESIZABLE, 24 )
                lego.setviewport(event.w, event.h)
                clear_color = GL.glGetFloatv(GL.GL_COLOR_CLEAR_VALUE)
                GL.glClearColor( 0.0, 0.0, 0.0, 0.0 )
                GL.glClear( GL.GL_COLOR_BUFFER_BIT )
                GL.glClearColor( *clear_color )
                ticker.tick(fps/6)
                
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running=False
                if not slow and event.key == 304:
                    mouse_sens = -60
                    slow = True
                    move_speed = 0.1
                    
                # FIXME: after moving down (ctrl) and another direction same time it stucks moving
                if event.mod % 256 == 64 or event.key == 306:
                    move[1] =  1.0
                elif event.key == 32:
                    move[1] = -1.0
                elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    move[0] =  1.0
                elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    move[0] = -1.0
                elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    move[2] =  1.0
                elif event.key == pygame.K_w or event.key == pygame.K_UP:
                    move[2] = -1.0
                else:
                    key_hit = event
                    
            elif event.type == pygame.KEYUP:
                if event.mod % 256 == 64 or event.key == 306 or event.key == 32:
                    move[1] = 0.0
                elif ( event.key == pygame.K_a or event.key == pygame.K_LEFT or 
                     event.key == pygame.K_d or event.key == pygame.K_RIGHT   ):
                    move[0] = 0.0
                elif ( event.key == pygame.K_s or event.key == pygame.K_DOWN or
                       event.key == pygame.K_w or event.key == pygame.K_UP    ): 
                    move[2] = 0.0
                if slow and event.key == 304:
                    slow = False
                    mouse_sens = -120
                    move_speed = 0.5
                    
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                mouse_down = event.pos
                GL.glSelectBuffer( 64 )
                
                GL.glRenderMode( GL.GL_SELECT )
                lego.draw_mode_3d(event.pos)
                GL.glInitNames()
                GL.glMultMatrixf( rot )
                GL.glMultMatrixf( np.eye(4) + np.vstack(( np.zeros((3,4)), position )) )
                for i,p in enumerate( pieces ):
                    GL.glPushName( i )
                    p.draw()
                    GL.glPopName()
                hits = GL.glRenderMode( GL.GL_RENDER )
                distance = None
                if hits:
                    for j in hits:
                        if distance > j[1] or distance is None:
                            distance = j[1]
                            chosen_index = j[2][0]
                    start_position = pieces[chosen_index].position
                    if menu.get() == 1:
                        edit["x"].value = format( pieces[chosen_index].position[0], 'g' )
                        edit["y"].value = format( pieces[chosen_index].position[1], 'g')
                        edit["z"].value = format( pieces[chosen_index].position[2], 'g')
                        piecesize.set( pieces[chosen_index].size )
                        edit["sides"].value = ''
                        edit["sides"].value = ','.join(str(i) for i in pieces[chosen_index].sides)
                    
                GL.glRenderMode( GL.GL_SELECT )
                lego.draw_mode_2d(event.pos)
                GL.glInitNames()
                for i in range( 0, len(controls) ):
                    GL.glPushName( i )
                    controls.values()[i].draw()
                    GL.glPopName()
                hits = GL.glRenderMode( GL.GL_RENDER )
                focused = controls.values()[ hits.pop()[2][0] ] if hits else None
                
            elif mouse_down and event.type == pygame.MOUSEMOTION:
                if chosen_index is None or drag_from is None:
                    rotx += float(event.rel[0]) / window_width  * mouse_sens
                    roty += float(event.rel[1]) / window_height * mouse_sens
                else:
                    x,y = event.pos
                    y = window_height - y
                    lego.draw_mode_3d()
                    GL.glMultMatrixf( rot )
                    GL.glMultMatrixf( np.eye(4) + np.vstack(( np.zeros((3,4)), position )) )
                    far = np.array(GLU.gluUnProject( x, y, 1, GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX), GL.glGetDoublev(GL.GL_PROJECTION_MATRIX), GL.glGetIntegerv(GL.GL_VIEWPORT) )) 
                    near =  np.array(GLU.gluUnProject( x, y, 0, GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX), GL.glGetDoublev(GL.GL_PROJECTION_MATRIX), GL.glGetIntegerv(GL.GL_VIEWPORT) ))
                    
                    drag = (drag_from - ( (drag_from[1]-near[1]) / (far[1]-near[1]) ) * (far-near) - near) / -lego.LEGO_GRID
                    
                    prev_position = pieces[chosen_index].position
                    pieces[chosen_index].position = start_position + drag.round()
                    
                    for i,p in enumerate(pieces):
                        collision = collide( p, pieces[chosen_index] )
                        if collision:
                            break
                    if collision:
                        pieces[chosen_index].position = prev_position
                    
                    
                    
            elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
                mouse_down = None 
                drag_from = None
                if menu.get() != 1:
                    chosen_index = None
                    
        if not np.array_equal( move, np.zeros(4) ):
            move = move_speed * move / np.linalg.norm(move)
        position += np.dot( rot, move )
        
        # draw 3D stuff
        lego.draw_mode_3d()
        
        GL.glMultMatrixf( rot )
        GL.glMultMatrixf( np.eye(4) + np.vstack(( np.zeros((3,4)), position )) )
        
        if mblur.value:
            if not accumulate:
                GL.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT )
            else:
                GL.glAccum( GL.GL_ACCUM, 1.0 - mblur_rate )
                GL.glAccum( GL.GL_RETURN, 1.0 )
                GL.glAccum( GL.GL_MULT, mblur_rate ) 
            accumulate = not accumulate
            ticker.tick( 2*fps )
            if rotating:
                rotrot = (rotrot + rot_speed/2) % 360
                GL.glRotatef( rotrot, 0.0, 1.0, 0.0 )
                
        else:
            GL.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT )
            ticker.tick( fps )
            if rotating:
                rotrot = (rotrot + rot_speed) % 360
                GL.glRotatef( rotrot, 0.0, 1.0, 0.0 )
        
        GL.glLightfv( GL.GL_LIGHT0, GL.GL_POSITION, lightp )
        
        if edit["grid"].value:
            lego.draw_grid(grid_level)
        
        ps = GL.glGetInteger( GL.GL_POINT_SIZE )
        GL.glPointSize( 10 )
        GL.glColor3f( 1.0, 1.0, 0.5 )
        GL.glBindTexture(GL.GL_TEXTURE_2D, 0)
        GL.glNormal3fv(lightp[:3])
        GL.glBegin( GL.GL_POINTS )
        GL.glVertex3fv( lightp[:3] - np.array((0.1, 0.1, 0.1)) )
        GL.glEnd()
        GL.glPointSize( ps )
            
        for piece in pieces:
            piece.draw()
        
        if mouse_down and chosen_index is not None and drag_from is None:
            x,y = mouse_down
            y = window_height - y
            z = GL.glReadPixels(x,y,1,1,GL.GL_DEPTH_COMPONENT,GL.GL_FLOAT) 
            drag_from = np.array(GLU.gluUnProject( x, y, z, GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX), GL.glGetDoublev(GL.GL_PROJECTION_MATRIX), GL.glGetIntegerv(GL.GL_VIEWPORT) ))
        
        # draw 2D stuff
        lego.draw_mode_2d()
        
        if menu.get() == 1:
            edit_layers.draw( 1 )
        elif menu.get() == 2:
            options_layers.draw( 1 )
        
        for b in controls.values():
            
            decide = b is focused
            b.draw( decide, b in errors )
            
            if decide:
                
                if b is static["options"] or b is static["edit"]:
                    if b.value is None:
                        controls = dict_union(controls, menus[b.ref])
                        b.value = b.ref
                    elif b.value == b.ref:
                        b.value = None
                        controls = dict_substract(controls, menus[b.ref])
                    else:
                        controls = dict_substract(controls, menus[b.value])
                        b.value = b.ref
                        controls = dict_union(controls, menus[b.ref])
                elif isinstance(b, gui.toggle):
                    b.value = b.ref
                elif key_hit is not None and isinstance(b, gui.textbox):
                    b.append(key_hit)
                
                if b is edit["raise_grid"]:
                    grid_level += 1
                    edit_dyn_layers.add( "grid-level", title.render(str(grid_level),True,text_color), edit_dyn_layers.layer_list["grid-level"][1], edit_dyn_layers.layer_list["grid-level"][2], (0,title_scale) )
                elif b is edit["lower_grid"]:
                    grid_level -= 1
                    edit_dyn_layers.add( "grid-level", title.render(str(grid_level),True,text_color), edit_dyn_layers.layer_list["grid-level"][1], edit_dyn_layers.layer_list["grid-level"][2], (0,title_scale) )
                elif b is edit["send"]:
                    errors = set()
                    try:
                        try:
                            edit_dyn_layers.remove("error")
                        except KeyError:
                            pass
                        sides = tuple( int(i) for i in edit["sides"].value.split(',') )
                        lego.piece.is_closed(sides)
                    except lego.LoopError as e:
                        if e.errno == 1001:
                            edit_dyn_layers.add( "error", error.render(po["few"],True,text_color), 180, 5, )
                        elif e.errno == 1002:
                            edit_dyn_layers.add( "error", error.render(po["not_even"],True,text_color), 180, 5 )
                        elif e.errno == 1020:
                            edit_dyn_layers.add( "error", error.render(po["both"],True,text_color), 180, 5 )
                        elif e.errno == 1000:
                            edit_dyn_layers.add( "error", error.render(po["even"],True,text_color), 180, 5 )
                        elif e.errno == 1010:
                            edit_dyn_layers.add( "error", error.render(po["odd"],True,text_color), 180, 5 )
                        errors.add(edit["sides"])
                    except ValueError:
                        errors.add(edit["sides"])
                    try:
                        x = float( edit["x"].value )
                    except ValueError:
                        errors.add(edit["x"])
                    try:
                        y = round( float( edit["y"].value )*3 ) / 3.0
                    except ValueError:
                        errors.add(edit["y"])
                    try:
                        z = float( edit["y"].value )
                    except ValueError:
                        errors.add(edit["z"])
                    if piecesize.get() is None:
                        errors.add(edit["big"])
                        errors.add(edit["small"])
                        
                    if not errors:
                        pieces.append( lego.piece(sides, piecesize.value, (0.2, 0.1, 0.8), (x,y,z)) )
                        edit["sides"].value = ''
                        edit["x"].value = '' 
                        edit["y"].value = ''
                        edit["z"].value = ''
                        piecesize.value = None
                elif b is edit["clear"] or menu.get() != 1:
                    edit["sides"].value = ''
                    edit["x"].value = '' 
                    edit["y"].value = ''
                    edit["z"].value = ''
                    piecesize.value = None
                    chosen_index = None
                elif b is edit["del"] and chosen_index is not None:
                    pieces.pop( chosen_index )
                    edit["sides"].value = ''
                    edit["x"].value = '' 
                    edit["y"].value = ''
                    edit["z"].value = ''
                    piecesize.value = None
                    chosen_index = None
            
                if b is options["raise_mblur"] and mblur_rate < 0.9 - eps:
                    mblur_rate += 0.1
                    options_dyn_layers.add( "mblur-level", title.render(str(mblur_rate),True,text_color), options_dyn_layers.layer_list["mblur-level"][1], options_dyn_layers.layer_list["mblur-level"][2], (0,title_scale) )
                elif b is options["lower_mblur"] and mblur_rate > 0.1 + eps:
                    mblur_rate -= 0.1
                    options_dyn_layers.add( "mblur-level", title.render(str(mblur_rate),True,text_color), options_dyn_layers.layer_list["mblur-level"][1], options_dyn_layers.layer_list["mblur-level"][2], (0,title_scale) )
                elif b is options["lower_light"] and light_int > 0.0:
                    light_int -= 0.1
                    if light_int < eps:
                        light_int = 0.0
                    light_amb = 1.0 - (1.0 - light_int) ** 2
                    GL.glLightfv( GL.GL_LIGHT0, GL.GL_AMBIENT, (light_amb * 0.2, light_amb * 0.2, light_amb * 0.2, 1.0) )  # Setup The Ambient Light
                    GL.glLightfv( GL.GL_LIGHT0, GL.GL_DIFFUSE, (light_amb, light_amb, light_amb, 1.0) )
                    options_dyn_layers.add( "light-level", title.render(str(light_int) ,True,text_color), options_dyn_layers.layer_list["light-level"][1], options_dyn_layers.layer_list["light-level"][2], (0,title_scale) )
                elif b is options["raise_light"] and light_int < 1.0 - eps:
                    light_int += 0.1
                    light_amb = 1.0 - (1.0 - light_int) ** 2
                    GL.glLightfv( GL.GL_LIGHT0, GL.GL_AMBIENT, (light_amb * 0.2, light_amb * 0.2, light_amb * 0.2, 0.2) )  # Setup The Ambient Light
                    GL.glLightfv( GL.GL_LIGHT0, GL.GL_DIFFUSE, (light_amb, light_amb, light_amb, 1.0) )
                    options_dyn_layers.add( "light-level", title.render(str(light_int) ,True,text_color), options_dyn_layers.layer_list["light-level"][1], options_dyn_layers.layer_list["light-level"][2], (0,title_scale) )
                        
            if decide and not b.keepfocus:
                focused = None
                
        key_hit = None
        
        static_layers.draw()
        
        if menu.get() == 1:
            edit_layers.draw()
            edit_dyn_layers.draw()
        elif menu.get() == 2:
            options_layers.draw()
            options_dyn_layers.draw()
        
        pygame.display.flip()
            
            
    lego.finish()    
    pygame.quit()
    print "Bye!"
Exemple #44
0
    def keystroke(self, *args):
        # If escape is pressed, kill everything.

        key = args[0]
        x = args[1]
        y = args[2]
        if key == 's':
            self.save()
        if key == 'v':
            if self.recordingon == False:
                self.recordingon = True
                print('Recording is on')
            else:
                self.recordingon = False
                print('Recording is off')

        if key == '9':  #zoom out

            self.glu_perspect[0] += 10.  #self.glu_perspect[0]
            glut.glutPostRedisplay()

        if key == '0':  #zoom in

            self.glu_perspect[0] -= 10.  #0.5*self.glu_perspect[0]
            glut.glutPostRedisplay()

        if key == 'o':

            self.isperspect = int(not (self.isperspect))
            if self.isperspect:
                print('perspective projection on')
            else:
                print('orthogonal projection on')

            px, py, w, h = self.viewport
            self.reshape(w, h)
            glut.glutPostRedisplay()

        if args[0] == 'r':

            self.glu_perspect[0] = 60.
            self.mouse.rotationMatrix.reset()
            self.mouse.translationMatrix.reset()
            glut.glutPostRedisplay()

        if key == 'j':

            cube.position[0] += 10.
            glut.glutPostRedisplay()

        if key == 'k':

            cube.position[0] -= 10.
            glut.glutPostRedisplay()

        if key == 'p':

            viewport = gl.glGetDoublev(gl.GL_VIEWPORT)
            xn, yn, zn = glu.gluUnProject(np.double(x),
                                          viewport[3] - np.double(y), 0.)

            #print 'World Coordinates Near'
            #print xn,yn,zn

            xf, yf, zf = glu.gluUnProject(np.double(x),
                                          viewport[3] - np.double(y), 1.)
            near = np.array([xn, yn, zn])
            far = np.array([xf, yf, zf])

            self.plot.update_pick_ray(near, far)

        if key == '\033':

            sys.exit()
Exemple #45
0
    def keystroke(self,*args):
	# If escape is pressed, kill everything.

        key=args[0]
        x=args[1]
        y=args[2]
        if key == 's':
            self.save()
        if key == 'v':
            if self.recordingon==False:
                self.recordingon=True
                print('Recording is on')
            else:
                self.recordingon=False
                print('Recording is off')
                

        if key == '9': #zoom out

            
            self.glu_perspect[0]+=10.#self.glu_perspect[0]
            glut.glutPostRedisplay()

        if key == '0': #zoom in

            self.glu_perspect[0]-=10. #0.5*self.glu_perspect[0]
            glut.glutPostRedisplay()
                

        if key == 'o':
            
            self.isperspect=int(not(self.isperspect))
            if self.isperspect:
                print('perspective projection on')
            else:
                print('orthogonal projection on')
           
            px,py,w,h=self.viewport
            self.reshape(w,h)
            glut.glutPostRedisplay()

        if args[0] == 'r':

            self.glu_perspect[0]=60.
            self.mouse.rotationMatrix.reset()
            self.mouse.translationMatrix.reset()
            glut.glutPostRedisplay()
            
        if key == 'j':

            cube.position[0]+=10.
            glut.glutPostRedisplay()

        if key == 'k':

            cube.position[0]-=10.
            glut.glutPostRedisplay()


        if key == 'p':

            viewport=gl.glGetDoublev(gl.GL_VIEWPORT)
            xn,yn,zn=glu.gluUnProject(np.double(x),viewport[3]-np.double(y),0.)

            #print 'World Coordinates Near'
            #print xn,yn,zn

            xf,yf,zf=glu.gluUnProject(np.double(x),viewport[3]-np.double(y),1.)
            near=np.array([xn,yn,zn])
            far =np.array([xf,yf,zf])

            self.plot.update_pick_ray(near,far)
            
           
	if key == '\033':
            
            sys.exit()
Exemple #46
0
    def paintGL(self):
        '''
		Renders the OpenGL scene. Gets called whenever the widget needs to be updated.
		'''
        GL.glClearColor(0.5, 0.5, 0.5, 1.0)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glLoadIdentity()

        # Correct a bug related with the overlap of contexts between simultaneous OpenGL windows.
        for index, frame in enumerate(self._pending_frames):

            #if self.zoom>=0:
            #	frame = cv2.resize(frame, (int(frame.shape[0]/(2*(1-self.zoom))),int( frame.shape[1]/(2*(1-self.zoom))) ))

            color = GL.GL_LUMINANCE if len(frame.shape) == 2 else GL.GL_BGR
            w, h = len(frame[0]), len(frame)  #Size of the image

            if len(self.textures) < len(self.image_2_display):
                self.textures.append(GL.glGenTextures(1))

            #Load the textures to opengl
            GL.glEnable(GL.GL_TEXTURE_2D)
            GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1)
            GL.glBindTexture(GL.GL_TEXTURE_2D, self.textures[index])
            GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S,
                               GL.GL_CLAMP_TO_BORDER)
            GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T,
                               GL.GL_CLAMP_TO_BORDER)
            GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,
                               GL.GL_LINEAR)
            GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,
                               GL.GL_LINEAR)
            GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, w, h, 0, color,
                            GL.GL_UNSIGNED_BYTE, frame)

        self._pending_frames = []

        GL.glTranslatef(0, 0, -1)
        GL.glTranslatef(0, 0, -self.zoom)

        if len(self.image_2_display) > 1:
            #in case of having more images to display, it centers the images
            translate_x = float(
                (len(self.image_2_display) - 1) * self._width) / 2.0
            GL.glTranslatef(-translate_x, 0, 0)

        if self._point is not None:
            GL.glColor4f(0, 0, 1, 1.0)
            GL.glPushMatrix()
            GL.glTranslatef(self._point[0], self._point[1], self._point[2])
            self.draw_pyramid()
            GL.glPopMatrix()
            GL.glColor4f(1, 1, 1, 1.0)

        GL.glRotatef(self._rotateX, -1, 0, 0)
        GL.glRotatef(self._rotateZ, 0, 0, 1)

        GL.glDisable(GL.GL_TEXTURE_2D)
        GL.glColor4f(0, 0, 0, .0)
        GL.glBegin(GL.GL_QUADS)
        GL.glVertex3f(20, -20, -.01)
        GL.glVertex3f(20, 20, -.001)
        GL.glVertex3f(-20, 20, -.001)
        GL.glVertex3f(-20, -20, -.001)
        GL.glEnd()

        GL.glColor4f(1, 1, 1, 1.0)

        # mouse events: find the image position where the mouse is
        if self._mouse_pressed:
            modelview = GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX)
            projection = GL.glGetDoublev(GL.GL_PROJECTION_MATRIX)
            viewport = GL.glGetIntegerv(GL.GL_VIEWPORT)
            winX = float(self._mouseX)
            winY = float(viewport[3] - self._mouseY)
            winZ = GL.glReadPixels(winX, winY, 1, 1, GL.GL_DEPTH_COMPONENT,
                                   GL.GL_FLOAT)
            self._glX, self._glY, self._glZ = GLU.gluUnProject(
                winX, winY, winZ[0][0], modelview, projection, viewport)

            if not self._last_mouse_gl_pos:
                self._last_mouse_gl_pos = self._glX, self._glY, self._glZ

            #mouse click event
            if self._mouse_clicked_event is not None:

                if hasattr(self, 'imgWidth'):
                    self.onClick(self._mouse_clicked_event,
                                 self._get_current_x(), self._get_current_y())

                if self._mouse_clicked_event.button == 1:
                    self._mouse_leftbtn_pressed = True
                    self._mouseStartDragPoint = self._get_current_mouse_point()

                if self._mouse_clicked_event.button == 4:
                    self._mouseStartDragPoint = self._get_current_mouse_point()
                    self._move_img = True
                    self._last_mouse_gl_pos = None
                    self._lastGlX = self._glX
                    self._lastGlY = self._glY
                self._mouse_clicked_event = None

            #mouse double click event
            if self._mouse_dblclicked_event is not None:
                if hasattr(self, 'imgWidth'):
                    self.onDoubleClick(self._mouse_dblclicked_event,
                                       self._get_current_x(),
                                       self._get_current_y())
                self._mouse_dblclicked_event = None

            #mouse move event
            if self._mouse_move_event is not None:
                if self._mouse_leftbtn_pressed and self._mouse_pressed:
                    p1 = self._mouseStartDragPoint
                    p2 = self._get_current_mouse_point()
                    self.onDrag(p1, p2)

                if self._move_img and self._mouse_pressed:
                    p1 = self._mouseStartDragPoint
                    p2 = self._get_current_mouse_point()
                    self.onDrag(p1, p2)
                self._mouse_move_event = None

        # end of the mouse events #################################

        GL.glEnable(GL.GL_TEXTURE_2D)
        GL.glDisable(GL.GL_DEPTH_TEST)

        if self._move_img and self._last_mouse_gl_pos is not None:
            self._x -= (self._last_mouse_gl_pos[0] - self._glX)
            self._y -= (self._last_mouse_gl_pos[1] - self._glY)

        for texture_index in range(0, len(self.image_2_display)):
            if texture_index > 0: GL.glTranslatef(self._width, 0, 0)
            GL.glBindTexture(GL.GL_TEXTURE_2D, self.textures[texture_index])

            self.draw_video(self._width, self._height, self._x, self._y, 0.0)

        GL.glEnable(GL.GL_DEPTH_TEST)

        if self._helpText is not None:
            self.qglColor(QtCore.Qt.black)
            self.renderText(5, 31, self._helpText, font=self._font)
            self.qglColor(QtCore.Qt.white)
            self.renderText(4, 30, self._helpText, font=self._font)

        if self._tmp_msg is not None:
            self.qglColor(QtCore.Qt.black)
            self.renderText(5,
                            self.height() - 19,
                            self._tmp_msg,
                            font=self._font)
            self.qglColor(QtCore.Qt.white)
            self.renderText(4,
                            self.height() - 20,
                            self._tmp_msg,
                            font=self._font)

        if self._move_img:
            self._last_mouse_gl_pos = self._glX, self._glY, self._glZ
Exemple #47
0
    def _handle_event(self, event):
        # let imgui process events and tell us whether it needs to do anything first
        if event.type != pg.VIDEORESIZE:
            self.imgui_impl.process_event(event)

        # --- deal with key events ---
        if not self.imgui_io.want_capture_keyboard and event.type == pg.KEYDOWN:
            if event.key == pg.K_ESCAPE:
                self.running = False
            if event.key == pg.K_F10:
                self.recording = not self.recording
            else:
                try:
                    key = chr(event.key)
                    self.key_callback(key)
                except ValueError:
                    pass
        elif event.type == pg.QUIT:
            self.running = False

        # --- mouse events ---
        elif event.type in (
                pg.MOUSEMOTION, pg.MOUSEBUTTONUP, pg.MOUSEBUTTONDOWN,
                pg.MOUSEWHEEL) and not self.imgui_io.want_capture_mouse:
            # only deal with mouse if imgui doesn't process it
            if pg.key.get_mods() == 0 or pg.key.get_mods() == 4096:
                # only process events with no keys pressed for viewer
                if event.type == pg.MOUSEMOTION:
                    # check for mouse motion
                    pos = np.array(pg.mouse.get_pos())
                    diff = pos - self.last_mouse_pos
                    self.last_mouse_pos = pos
                    if pg.mouse.get_pressed()[0]:
                        # rotation
                        self.camera_angles += diff * 0.5
                        self.camera_angles[1] = np.clip(
                            self.camera_angles[1], -90.0, 90.0)
                    elif pg.mouse.get_pressed()[1]:
                        # translation in screen directions
                        # check if on object
                        x = event.pos[0]
                        y = self.window_size[1] - event.pos[1] - 1
                        depth = gl.glReadPixelsf(x - diff[0], y + diff[1], 1,
                                                 1, gl.GL_DEPTH_COMPONENT)
                        if depth < 1.0:
                            # yes? make sure the point stays under the mouse cursor
                            point_before = np.array(
                                glu.gluUnProject(x - diff[0], y + diff[1],
                                                 depth))
                            point_after = np.array(
                                glu.gluUnProject(x, y, depth))
                            self.camera_center += (point_before - point_after)
                        else:
                            # approximate translation speed by scale
                            scale = -0.0012 * self.camera_distance
                            self.camera_center -= diff[0] * \
                                self.modelview[0:3, 0] * scale
                            self.camera_center += diff[1] * \
                                self.modelview[0:3, 1] * scale
                elif event.type == pg.MOUSEBUTTONUP:
                    # check for mouse button up for double click
                    self.last_mouse_button = event.button
                    self.last_mouse_button_time = pg.time.get_ticks()
                elif event.type == pg.MOUSEBUTTONDOWN:
                    # check for double-click
                    if event.button == 1 and self.last_mouse_button == 1:
                        t = pg.time.get_ticks() - self.last_mouse_button_time
                        if t < 120:
                            # double click, focus on point under mouse
                            x = event.pos[0]
                            y = self.window_size[1] - event.pos[1] - 1
                            depth = gl.glReadPixelsf(x, y, 1, 1,
                                                     gl.GL_DEPTH_COMPONENT)
                            if depth < 1.0:
                                self.camera_center = glu.gluUnProject(
                                    x, y, depth)
                    elif event.button == 4:
                        # mouse wheel
                        self.camera_distance *= 0.9
                    elif event.button == 5:
                        # mouse wheel
                        self.camera_distance /= 0.9

        # --- video resize events ---
        elif event.type == pg.VIDEORESIZE:
            # resize the viewport
            self.window_size = event.size
            gl.glMatrixMode(gl.GL_PROJECTION)
            gl.glLoadIdentity()
            glu.gluPerspective(45, (self.window_size[0] / self.window_size[1]),
                               1.0, 1000.0)
            gl.glViewport(0, 0, self.window_size[0], self.window_size[1])
            gl.glMatrixMode(gl.GL_MODELVIEW)
            self.imgui_io.display_size = self.window_size

            # prepare storage for data reading
            self.image_data = np.empty(
                (self.window_size[1], self.window_size[0], 3), dtype=np.uint8)
Exemple #48
0
 def unProject(self,x,y,z):
     "Map the window coordinates (x,y,z) to object coordinates."""
     self.set3DMatrices()
     return GLU.gluUnProject(x,y,z,self.m.astype(double),self.p,self.v)
Exemple #49
0
    def unprojectView(self):
        _left, _bottom, z = glu.gluUnProject(self.left, self.bottom, 0.0)
        _right, _top, z = glu.gluUnProject(self.right, self.top, 0.0)

        # Not sure why but I had to switch the bottom and top or the vertices flipped
        return _left, _right, _top, _bottom
    def paintGL(self):
        GL.glClearColor(0.5, 0.5, 0.5, 1.0)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glLoadIdentity()

        #Currect a bug related with the overlap of contexts between simulatanious OpenGL windows.
        if self._pendingFrames!=None:
            for index, frame in enumerate(self._pendingFrames):
                if len(frame.shape) == 2:
                    color = GL.GL_LUMINANCE
                else:
                    color = GL.GL_BGR

                if len(self.texture) < len(self.image2Display): self.texture.append(GL.glGenTextures(1))

                w = len(frame[0])
                h = len(frame)

                GL.glEnable(GL.GL_TEXTURE_2D)
                GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1)
                GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture[index])
                GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_BORDER)
                GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_BORDER)
                GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR)
                GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR)
                GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, w, h, 0, color, GL.GL_UNSIGNED_BYTE, frame)
            self._pendingFrames = None

        translateX = (len(self.texture) * self._width) / 2

        if len(self.texture) > 0:
            GL.glTranslatef(-translateX, -self._height / 2, -self.zoom)
            GL.glTranslatef(0, self._height / 2.0, 0)

            if self._point is not None:
                GL.glColor4f(0, 0, 1, 1.0)
                GL.glPushMatrix()
                GL.glTranslatef(self._point[0], self._point[1], self._point[2])
                self.drawPyramid()
                GL.glPopMatrix()
                GL.glColor4f(1, 1, 1, 1.0)

            GL.glRotatef(self._rotateX, -1, 0, 0)
            GL.glRotatef(self._rotateZ, 0, 0, 1)

            GL.glDisable(GL.GL_TEXTURE_2D)
            GL.glColor4f(0.5, 0.5, 0.5, 1.0)
            GL.glBegin(GL.GL_QUADS)
            GL.glVertex3f(20, -20, -.01)
            GL.glVertex3f(20, 20, -.001)
            GL.glVertex3f(-20, 20, -.001)
            GL.glVertex3f(-20, -20, -.001)
            GL.glEnd()

            GL.glColor4f(1, 1, 1, 1.0)

            if self._mouseDown:
                modelview = GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX)
                projection = GL.glGetDoublev(GL.GL_PROJECTION_MATRIX)
                viewport = GL.glGetIntegerv(GL.GL_VIEWPORT)
                winX = float(self._mouseX)
                winY = float(viewport[3] - self._mouseY)
                winZ = GL.glReadPixels(
                    winX, winY, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT)
                self._glX, self._glY, self._glZ = GLU.gluUnProject(
                    winX, winY, winZ[0][0], modelview, projection, viewport)
                # self.logger.debug("Paint GL mouse down")
                # self.logger.debug("%s", "GLX: {0} | GLY: {1}".format(self._glX, self._glY))

            GL.glEnable(GL.GL_TEXTURE_2D)
            GL.glDisable(GL.GL_DEPTH_TEST)

            for texture_index in range(0, len(self.texture)):
                if texture_index > 0:
                    GL.glTranslatef(self._width,  0, 0)

                GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture[texture_index])

                if self._mouseRightDown:
                    self.drawVideo(self._width, self._height, self._x - (
                        self._lastGlX - self._glX), self._y - (self._lastGlY - self._glY), 0.0)
                else:
                    self.drawVideo(
                        self._width, self._height, self._x, self._y, 0.0)

            GL.glEnable(GL.GL_DEPTH_TEST)

        if self._helpText is not None:
            self.qglColor(QtCore.Qt.white)
            self.renderText(4, 15, self._helpText)
    def mouseReleaseEvent(self, event):
		global winx_inside, winy_inside, winz_inside, indices_selected, indices_all
		self.dragging = False
		if len(self.lasso_list) >= 3:
			points = np.array(self.lasso_list)
			hull = scipy.spatial.ConvexHull(points)
			hullx = points[hull.vertices,0].astype(np.float32)
			hully = points[hull.vertices,1].astype(np.float32)
			
			meanx = hullx.mean()
			meany = hully.mean()
			radius = np.sqrt((points[hull.vertices,0] - meanx)**2 + (points[hull.vertices,1] - meany)**2).max()

			project(data_x, data_y, data_z, self.model.reshape(-1), self.proj.reshape(-1), self.view.reshape(-1), winx, winy, winz)
			pnpoly(hullx, hully, winx, self.height() - winy - 1, inside, meanx, meany, radius)
			winx_inside = winx[inside==1]
			winy_inside = winy[inside==1]
			winz_inside = winz[inside==1]
			#zmin, zmax = winz_inside.min(), winz_inside.max()
			#counts = 
			#vaex.histogram.hist1d(
			fraction = 0.005
			N = len(winz_inside)
			indices = np.argsort(winz_inside)
			i1, i2 = indices[int(N*fraction)], indices[int(N*(1-fraction))]
			print i1, i2
			zmin = winz_inside[i1]
			zmax = winz_inside[i2]
			xmin, xmax = winx_inside.min(), winx_inside.max()
			ymin, ymax = winy_inside.min(), winy_inside.max()
			#zmin, zmax = winz_inside.min(), winz_inside.max()
			print 
			print "x:", xmin, xmax
			print "y:", ymin, ymax
			print "z:", zmin, zmax
			
			
			M = np.matrix(self.model)
			P = np.matrix(self.proj)
			T = (P * M)
			print "M", self.model
			print "P", self.proj
			print "v", self.view
			#xmin, xmax = 0, 1
			#ymin, ymax = 0, 1
			#zmin, zmax = 0, 1
			self.bbox = []
			for z in [zmin, zmax]:
				for y in [ymin, ymax]:
					for x in [xmin, xmax]:
						xc = ((x - self.view[0])/self.view[2] * 2 - 1)
						yc = ((y - self.view[1])/self.view[3] * 2 - 1 )
						zc = z*2.-1.
						clip = np.array([[xc, yc, zc, 1]]).T
						#view = P.I * clip
						eye = (P.T*M.T).I * clip
						print eye
						eye[0:3] /= eye[3]
						print x, y, z
						print "->", eye
						#print clip
						#print (P*M) * (P*M).I
						#print GLU.gluUnProject
						self.bbox.append((eye[0], eye[1], eye[2]))
					
						xu,yu,zu = GLU.gluUnProject(x, y, z, self.model.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), self.proj.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), self.view.ctypes.data_as(ctypes.POINTER(ctypes.c_float)))
						print "glu:", xu, yu, zu
			indices_selected = np.arange(len(winx), dtype=np.uint32)[inside==1]
			indices_all = np.arange(len(winx), dtype=np.uint32)[inside==0]
			print data_x[indices_selected].min(), data_x[indices_selected].max()
			print data_y[indices_selected].min(), data_y[indices_selected].max()
			print data_z[indices_selected].min(), data_z[indices_selected].max()
			#import pdb
			#pdb.set_trace()
			
			
			x, y, z = 1,1,1
			xu,yu,zu = GLU.gluUnProject(x, y, z, self.model.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), self.proj.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), self.view.ctypes.data_as(ctypes.POINTER(ctypes.c_float)))
			print
			print xu, yu, zu
			print GLU.gluProject(xu, yu, zu, self.model.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), self.proj.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), self.view.ctypes.data_as(ctypes.POINTER(ctypes.c_float)))
			
			
			
			self.lasso_list = []
			self.updateGL()
    def paintGL(self):
        """
		Renders the OpenGL scene. Gets called whenever the widget needs to be updated.
		"""
        GL.glClearColor(0.5, 0.5, 0.5, 1.0)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        GL.glMatrixMode(GL.GL_MODELVIEW)
        GL.glLoadIdentity()

        # Correct a bug related with the overlap of contexts between simultaneous OpenGL windows.
        if self._pending_frames != None:

            for index, frame in enumerate(self._pending_frames):

                color = GL.GL_LUMINANCE if len(frame.shape) == 2 else GL.GL_BGR
                w, h = len(frame[0]), len(frame)  # Size of the image

                if len(self.textures) < len(self.image_2_display):
                    self.textures.append(GL.glGenTextures(1))

                # Load the textures to opengl
                GL.glEnable(GL.GL_TEXTURE_2D)
                GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1)
                GL.glBindTexture(GL.GL_TEXTURE_2D, self.textures[index])
                GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_BORDER)
                GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_BORDER)
                GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR)
                GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR)
                GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, w, h, 0, color, GL.GL_UNSIGNED_BYTE, frame)

            self._pending_frames = None

        if len(self.image_2_display) > 0:

            GL.glTranslatef(0, 0, -1)
            GL.glTranslatef(0, 0, -self.zoom)

            if len(self.image_2_display) > 1:
                # in case of having more images to display, it centers the images
                translate_x = float((len(self.image_2_display) - 1) * self._width) / 2.0
                GL.glTranslatef(-translate_x, 0, 0)

            if self._point is not None:
                GL.glColor4f(0, 0, 1, 1.0)
                GL.glPushMatrix()
                GL.glTranslatef(self._point[0], self._point[1], self._point[2])
                self.draw_pyramid()
                GL.glPopMatrix()
                GL.glColor4f(1, 1, 1, 1.0)

            GL.glRotatef(self._rotateX, -1, 0, 0)
            GL.glRotatef(self._rotateZ, 0, 0, 1)

            GL.glDisable(GL.GL_TEXTURE_2D)
            GL.glColor4f(0.5, 0.5, 0.5, 1.0)
            GL.glBegin(GL.GL_QUADS)
            GL.glVertex3f(20, -20, -0.01)
            GL.glVertex3f(20, 20, -0.001)
            GL.glVertex3f(-20, 20, -0.001)
            GL.glVertex3f(-20, -20, -0.001)
            GL.glEnd()

            GL.glColor4f(1, 1, 1, 1.0)

            if self._mouseDown:
                modelview = GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX)
                projection = GL.glGetDoublev(GL.GL_PROJECTION_MATRIX)
                viewport = GL.glGetIntegerv(GL.GL_VIEWPORT)

                winX = float(self._mouseX)
                winY = float(viewport[3] - self._mouseY)
                winZ = GL.glReadPixels(winX, winY, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT)

                self._glX, self._glY, self._glZ = GLU.gluUnProject(
                    winX, winY, winZ[0][0], modelview, projection, viewport
                )

                if not self._last_mouse_gl_pos:
                    self._last_mouse_gl_pos = self._glX, self._glY, self._glZ

            GL.glEnable(GL.GL_TEXTURE_2D)
            GL.glDisable(GL.GL_DEPTH_TEST)

            if self._move_img:
                self._x -= self._last_mouse_gl_pos[0] - self._glX
                self._y -= self._last_mouse_gl_pos[1] - self._glY

            for texture_index in range(0, len(self.image_2_display)):
                if texture_index > 0:
                    GL.glTranslatef(self._width, 0, 0)
                GL.glBindTexture(GL.GL_TEXTURE_2D, self.textures[texture_index])

                self.draw_video(self._width, self._height, self._x, self._y, 0.0)

            GL.glEnable(GL.GL_DEPTH_TEST)

        if self._helpText is not None:
            self.qglColor(QtCore.Qt.black)
            self.renderText(5, 31, self._helpText, font=self._font)
            self.qglColor(QtCore.Qt.white)
            self.renderText(4, 30, self._helpText, font=self._font)

        if self._tmp_msg is not None:
            self.qglColor(QtCore.Qt.black)
            self.renderText(5, self.height() - 19, self._tmp_msg, font=self._font)
            self.qglColor(QtCore.Qt.white)
            self.renderText(4, self.height() - 20, self._tmp_msg, font=self._font)

        if self._move_img:
            self._last_mouse_gl_pos = self._glX, self._glY, self._glZ
 def unProject(self,x,y,z):
     "Map the window coordinates (x,y,z) to object coordinates."""
     self.set3DMatrices()
     return GLU.gluUnProject(x,y,z,self.m,self.p,self.v)
Exemple #54
0
def unproject(x, y, z):
    try:
        return GLU.gluUnProject(x, y, z)
    except ValueError:  # projection failed
        return 0, 0, 0