def loadProjection(self,force=False,pick=None): """Load the projection/perspective matrix. The caller will have to setup the correct GL environment beforehand. No need to set matrix mode though. This function will switch to GL_PROJECTION mode before loading the matrix, and go back to GL_MODELVIEW mode on exit. A pick region can be defined to use the camera in picking mode. pick defines the picking region center and size (x,y,w,h). This function does it best at autodetecting changes in the lens settings, and will only reload the matrix if such changes are detected. You can optionally force loading the matrix. """ if self.lensChanged or force: GL.glMatrixMode(GL.GL_PROJECTION) GL.glLoadIdentity() if pick: #print 'PICK: %s' % str(pick) GLU.gluPickMatrix(*pick) if self.perspective: GLU.gluPerspective(self.fovy,self.aspect,self.near,self.far) else: #print "FOVY: %s" % self.fovy top = tand(self.fovy*0.5) * self.dist bottom = -top right = top * self.aspect left = bottom * self.aspect #print "Ortho %s" % [left,right,bottom,top,self.near,self.far] GL.glOrtho(left,right,bottom,top,self.near,self.far) GL.glMatrixMode(GL.GL_MODELVIEW)
def Pick(self): self.globFrame.named = False for element in self.elements: element.named = False gl.glSelectBuffer(512) gl.glRenderMode(gl.GL_SELECT) gl.glInitNames() gl.glPushMatrix() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() glu.gluPickMatrix(self.lastx, self.size.height-self.lasty ,0.25, 0.25, [0, 0, self.size.width, self.size.height]) glu.gluPerspective(self.fov, self.aspect, 0.2, 200.0) gl.glMatrixMode(gl.GL_MODELVIEW) self.CameraTransformation() self.OnDraw() my_buffer = gl.glRenderMode(gl.GL_RENDER) gl.glPopMatrix() gl.glPushMatrix() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() glu.gluPerspective(self.fov, self.aspect, 0.2, 200.0) gl.glMatrixMode(gl.GL_MODELVIEW) self.CameraTransformation() self.OnDraw() self.Redraw() gl.glPopMatrix() return my_buffer
def draw_mode_2d( pick=(0,0) ): global SCREEN_WIDTH, SCREEN_HEIGHT GL.glMatrixMode(GL.GL_PROJECTION) GL.glLoadIdentity() if GL.glGetInteger(GL.GL_RENDER_MODE) == GL.GL_SELECT: GLU.gluPickMatrix( pick[0], SCREEN_HEIGHT-pick[1], 10, 10, np.array((0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)) ) GL.glOrtho(0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, -1.0, 10.0) GL.glMatrixMode(GL.GL_MODELVIEW) GL.glLoadIdentity() GL.glClear(GL.GL_DEPTH_BUFFER_BIT) # GL.glDisable(GL_COLOR_MATERIAL) GL.glDisable(GL.GL_DEPTH_TEST) GL.glDisable(GL.GL_LIGHTING)
def draw_mode_3d( pick=(0,0) ): global SCREEN_WIDTH, SCREEN_HEIGHT GL.glMatrixMode(GL.GL_PROJECTION) GL.glLoadIdentity() if GL.glGetInteger(GL.GL_RENDER_MODE) == GL.GL_SELECT: GLU.gluPickMatrix( pick[0], SCREEN_HEIGHT-pick[1], 1.0, 1.0, np.array((0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)) ) GLU.gluPerspective(45.0, float(SCREEN_WIDTH)/float(SCREEN_HEIGHT), 0.1, 100.0) GL.glMatrixMode(GL.GL_MODELVIEW) GL.glLoadIdentity() GL.glScale( 1.0, 1.0, -1.0 ) # GL.glEnable(GL_COLOR_MATERIAL) GL.glEnable(GL.GL_DEPTH_TEST) GL.glEnable(GL.GL_LIGHTING)
def objects_under_cursor(self, x, y): """Writes to `self.highlighted` list of objects under cursor, in descending order (the first is the top element). """ if not self.children: return [] # TODO simple rectangular check to improve performance viewport = GL.glGetIntegerv(GL.GL_VIEWPORT) GL.glSelectBuffer(512) GL.glRenderMode(GL.GL_SELECT) GL.glInitNames() GL.glMatrixMode(GL.GL_PROJECTION) GL.glPushMatrix() # copying projection # WARNING this approach can be unstable! M = GL.glGetFloatv(GL.GL_PROJECTION_MATRIX) GL.glLoadIdentity() GLU.gluPickMatrix(x, y, 1., 1., viewport) GL.glMultMatrixf(M) GL.glMatrixMode(GL.GL_MODELVIEW) super(SmartLayer, self).visit() hits = GL.glRenderMode(GL.GL_RENDER) result = [] for near, far, names in hits: result = [] # XXX we just wish to get last hit record. for name in names: obj = SmartNode.nodes_by_id.get(name) if obj is not None: result.append(obj) GL.glMatrixMode(GL.GL_PROJECTION) GL.glPopMatrix() GL.glMatrixMode(GL.GL_MODELVIEW) old_result = self.highlighted new_result = list(result) self.highlighted = new_result for obj in set(old_result) - set(new_result): obj.mouse_out() for obj in set(new_result) - set(old_result): obj.mouse_enter() self.highlighted.reverse()
def keyboard(key, x, y): #global day,year,zdist if key == chr(27): sys.exit(0) if key == 'a': glut.glutIdleFunc(spin) if key == 'p' or key == 'P': # picking viewport=gl.glGetIntegerv(gl.GL_VIEWPORT) #print viewport w=viewport[2]-viewport[0] h=viewport[3]-viewport[1] #print 'SBS',SELECT_BUFFER_SIZE gl.glSelectBuffer(SELECT_BUFFER_SIZE) gl.glRenderMode(gl.GL_SELECT) gl.glInitNames() gl.glPushName(0) gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() glu.gluPickMatrix(x,viewport[3]-y,.1, .1, viewport) glu.gluPerspective(60.0, w/ h , 1.0, 20.0) #gl.glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5); drawRects(gl.GL_SELECT) gl.glPopMatrix() gl.glFlush() buffer = gl.glRenderMode(gl.GL_RENDER) glut.glutPostRedisplay() #print buffer for hit_record in buffer: min_depth, max_depth, names = hit_record print min_depth, max_depth, names
def keyboard(key, x, y): #global day,year,zdist if key == chr(27): sys.exit(0) if key == 'a': glut.glutIdleFunc(spin) if key == 'p' or key == 'P': # picking viewport = gl.glGetIntegerv(gl.GL_VIEWPORT) #print viewport w = viewport[2] - viewport[0] h = viewport[3] - viewport[1] #print 'SBS',SELECT_BUFFER_SIZE gl.glSelectBuffer(SELECT_BUFFER_SIZE) gl.glRenderMode(gl.GL_SELECT) gl.glInitNames() gl.glPushName(0) gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() glu.gluPickMatrix(x, viewport[3] - y, .1, .1, viewport) glu.gluPerspective(60.0, w / h, 1.0, 20.0) #gl.glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5); drawRects(gl.GL_SELECT) gl.glPopMatrix() gl.glFlush() buffer = gl.glRenderMode(gl.GL_RENDER) glut.glutPostRedisplay() #print buffer for hit_record in buffer: min_depth, max_depth, names = hit_record print min_depth, max_depth, names
def loadProjection(self,force=False,pick=None,keepmode=False): """Load the projection/perspective matrix. The caller will have to setup the correct GL environment beforehand. No need to set matrix mode though. This function will switch to GL_PROJECTION mode before loading the matrix If keepmode=True, does not switch back to GL_MODELVIEW mode. A pick region can be defined to use the camera in picking mode. pick defines the picking region center and size (x,y,w,h). This function does it best at autodetecting changes in the lens settings, and will only reload the matrix if such changes are detected. You can optionally force loading the matrix. """ GL.glMatrixMode(GL.GL_PROJECTION) if self.lensChanged or force: GL.glLoadIdentity() if pick: GLU.gluPickMatrix(*pick) fv = tand(self.fovy*0.5) if self.perspective: fv *= self.near else: fv *= self.dist fh = fv * self.aspect x0,x1 = 2*self.area - 1.0 frustum = (fh*x0[0],fh*x1[0],fv*x0[1],fv*x1[1],self.near,self.far) if self.perspective: GL.glFrustum(*frustum) else: GL.glOrtho(*frustum) try: self.projection_callback(self) except: pass if not keepmode: GL.glMatrixMode(GL.GL_MODELVIEW)
def loadProjection(self,force=False,pick=None,keepmode=False): """Load the projection/perspective matrix. The caller will have to setup the correct GL environment beforehand. No need to set matrix mode though. This function will switch to GL_PROJECTION mode before loading the matrix !! CHANGED: does not switch back to GL_MODELVIEW mode! A pick region can be defined to use the camera in picking mode. pick defines the picking region center and size (x,y,w,h). This function does it best at autodetecting changes in the lens settings, and will only reload the matrix if such changes are detected. You can optionally force loading the matrix. """ GL.glMatrixMode(GL.GL_PROJECTION) if self.lensChanged or force: GL.glLoadIdentity() if pick: GLU.gluPickMatrix(*pick) fv = tand(self.fovy*0.5) if self.perspective: fv *= self.near else: fv *= self.dist fh = fv * self.aspect x0,x1 = 2*self.area - 1.0 frustum = (fh*x0[0],fh*x1[0],fv*x0[1],fv*x1[1],self.near,self.far) if self.perspective: GL.glFrustum(*frustum) else: GL.glOrtho(*frustum) if not keepmode: GL.glMatrixMode(GL.GL_MODELVIEW)