def inputhook_glut(): """Run the pyglet event loop by processing pending events only. This keeps processing pending events until stdin is ready. After processing all pending events, a call to time.sleep is inserted. This is needed, otherwise, CPU usage is at 100%. This sleep time should be tuned though for best performance. """ # We need to protect against a user pressing Control-C when IPython is # idle and this is running. We trap KeyboardInterrupt and pass. signal.signal(signal.SIGINT, glut_int_handler) try: t = clock() # Make sure the default window is set after a window has been closed if glut.glutGetWindow() == 0: glut.glutSetWindow( 1 ) glutMainLoopEvent() return 0 while not stdin_ready(): glutMainLoopEvent() # We need to sleep at this point to keep the idle CPU load # low. However, if sleep to long, GUI response is poor. As # a compromise, we watch how often GUI events are being processed # and switch between a short and long sleep time. Here are some # stats useful in helping to tune this. # time CPU load # 0.001 13% # 0.005 3% # 0.01 1.5% # 0.05 0.5% used_time = clock() - t if used_time > 5*60.0: # print 'Sleep for 5 s' # dbg time.sleep(5.0) elif used_time > 10.0: # print 'Sleep for 1 s' # dbg time.sleep(1.0) elif used_time > 0.1: # Few GUI events coming in, so we can sleep longer # print 'Sleep for 0.05 s' # dbg time.sleep(0.05) else: # Many GUI events coming in, so sleep only very little time.sleep(0.001) except KeyboardInterrupt: pass return 0
def main(self, neuronObject, displaysize=(800, 600), radius=5, poly=True, fast=False, multisample=True, graph=True): self.poly = poly self.displaysize = displaysize self.graph = graph title = 'btmorph OpenGL Viewer' GLUT.glutInit(sys.argv) if multisample: GLUT.glutInitDisplayMode(GLUT.GLUT_RGBA | GLUT.GLUT_DOUBLE) else: GLUT.glutInitDisplayMode(GLUT.GLUT_RGBA | GLUT.GLUT_DOUBLE | GLUT.GLUT_MULTISAMPLE) GLUT.glutInitWindowSize(self.displaysize[0], self.displaysize[1]) GLUT.glutInitWindowPosition(50, 50) GLUT.glutCreateWindow(title) GLUT.glutDisplayFunc(self.display) GLUT.glutReshapeFunc(self.reshape) GLUT.glutMouseFunc(self.mouse) GLUT.glutMotionFunc(self.mouseMove) GLUT.glutMouseWheelFunc(self.mouseWheel) GLUT.glutKeyboardFunc(self.keyDown) GLUT.glutKeyboardUpFunc(self.keyUp) mb = modelbuilder() self.root, self.vertices_gl, self.colors_gl, self.Neuron = \ mb.build(neuronObject, self.poly, 100, fast) if graph: self.gb = graphbuilder() self.Graph, mid = \ self.gb.build(neuronObject, scalefactor=100) self.initGL(multisample) self.camera.rad = radius self.camera.focus = mid while self.running: GLUT.glutMainLoopEvent() GLUT.glutDestroyWindow(GLUT.glutGetWindow())
def close(): glut.glutDestroyWindow(glut.glutGetWindow())
def display(): gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) glut.glutSwapBuffers() def resize(width, height): gl.glViewport(0, 0, width, height + 4) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, width, 0, height + 4, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) if glut.glutGetWindow() > 0: interactive = True glut.glutInit(sys.argv) glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH) else: interactive = False glut.glutCreateWindow(b'gui-glut') glut.glutDisplayFunc(display) glut.glutReshapeFunc(resize) # This is necessary on osx to be able to close the window # (else the close button is disabled) if sys.platform == 'darwin' and not bool(glut.HAVE_FREEGLUT): glut.glutWMCloseFunc(close) gl.glClearColor(0, 0, 0, 1)
def __init__(self, size=None, position=None, title=None, fullscreen=False,enableAlpha=True,pointSize=2): ''' Constructor ''' self._mouse_x = 0 self._mouse_y = 0 self._button = mouse.NONE self._modifiers = None self._motionEventCounter=0 self._time = None self._timer_stack = [] self._timer_date = [] self._title = title or "PyGLer" # FIXME: get default name from a central location self._fullscreen = -1 self.dragSensitivity = 5 # Is there any glut loop already running ? if glut.glutGetWindow( ) == 0: glut.glutInit() glut.glutInitDisplayMode( glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH ) self._interactive = False else: self._interactive = True self._id = glut.glutCreateWindow( self._title ) glut.glutShowWindow() glut.glutDisplayFunc( self.redraw ) glut.glutReshapeFunc( self._reshape ) glut.glutKeyboardFunc( self._keyboard ) glut.glutKeyboardUpFunc( self._keyboard_up ) glut.glutMouseFunc( self._mouse ) glut.glutMotionFunc( self._motion ) glut.glutPassiveMotionFunc( self._passive_motion ) glut.glutVisibilityFunc( self._visibility ) glut.glutEntryFunc( self._entry ) glut.glutSpecialFunc( self._special ) glut.glutSpecialUpFunc( self._special_up ) GL.glEnable(GL.GL_DEPTH_TEST) GL.glEnable(GL.GL_BLEND) if enableAlpha: GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); GL.glPolygonOffset(1, 1); GL.glEnable(GL.GL_POLYGON_OFFSET_FILL); GL.glPointSize(pointSize) GL.glEnable(GL.GL_PROGRAM_POINT_SIZE) if size is not None: width, height = size glut.glutReshapeWindow( width, height ) width = glut.glutGet( glut.GLUT_WINDOW_WIDTH ) height= glut.glutGet( glut.GLUT_WINDOW_HEIGHT ) self._width = width self._height = height if position is not None: x,y = position glut.glutPositionWindow( x, y ) x = glut.glutGet( glut.GLUT_WINDOW_X ) y = glut.glutGet( glut.GLUT_WINDOW_X ) self._x, self._y = x, y # These ones will be used when exiting fullscreen self._saved_width = self._width self._saved_height = self._height self._saved_x = self._x self._saved_y = self._y self._time = glut.glutGet( glut.GLUT_ELAPSED_TIME ) self._fullscreen = fullscreen if fullscreen: self.set_fullscreen(True)
def __init__( self, size=None, position=None, title=None, fullscreen=False): ''' Create a window with given sise, position and title. :param (int,int) size: Initial window size as (width,height) in pixels. :param (int,int) position: Initial window position. Depending on the window-manager, the position request may or may not be honored. :param string title: The title of the window. ''' global _window window.Window.__init__( self, size, position, title ) self._mouse_x = 0 self._mouse_y = 0 self._button = mouse.NONE self._modifiers = None self._time = None self._timer_stack = [] self._timer_date = [] self._title = title or sys.argv[0] self._fullscreen = -1 # Is there any glut loop already running ? if glut.glutGetWindow( ) == 0: glut.glutInit( sys.argv ) glut.glutInitDisplayMode( glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH ) self._interactive = False else: self._interactive = True self._id = glut.glutCreateWindow( self._title ) glut.glutShowWindow( ) glut.glutDisplayFunc( self._display ) glut.glutReshapeFunc( self._reshape ) glut.glutKeyboardFunc( self._keyboard ) glut.glutKeyboardUpFunc( self._keyboard_up ) glut.glutMouseFunc( self._mouse ) glut.glutMotionFunc( self._motion ) glut.glutPassiveMotionFunc( self._passive_motion ) glut.glutVisibilityFunc( self._visibility ) glut.glutEntryFunc( self._entry ) glut.glutSpecialFunc( self._special ) glut.glutSpecialUpFunc( self._special_up ) if size is not None: width, height = size glut.glutReshapeWindow( width, height ) width = glut.glutGet( glut.GLUT_WINDOW_WIDTH ) height= glut.glutGet( glut.GLUT_WINDOW_HEIGHT ) self._width = width self._height = height if position is not None: x,y = position glut.glutPositionWindow( x, y ) #else: # screen_width = glut.glutGet( glut.GLUT_SCREEN_WIDTH ) # screen_height= glut.glutGet( glut.GLUT_SCREEN_HEIGHT ) # x = (screen_width - self._size[0]) / 2 # y = (screen_width - self._size[0]) / 2 # glut.glutPositionWindow( x, y ) x = glut.glutGet( glut.GLUT_WINDOW_X ) y = glut.glutGet( glut.GLUT_WINDOW_X ) self._x, self._y = x, y # These ones will be used when exiting fullscreen self._saved_width = self._width self._saved_height = self._height self._saved_x = self._x self._saved_y = self._y self._time = glut.glutGet( glut.GLUT_ELAPSED_TIME ) _window = self self._fullscreen = False if fullscreen: self.set_fullscreen(True)
def close(): glut.glutDestroyWindow(glut.glutGetWindow()) def display(): gl.glClear (gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) glut.glutSwapBuffers() def resize(width,height): gl.glViewport(0, 0, width, height+4) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, width, 0, height+4, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) if glut.glutGetWindow() > 0: interactive = True glut.glutInit(sys.argv) glut.glutInitDisplayMode(glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH) else: interactive = False glut.glutCreateWindow(b'gui-glut') glut.glutDisplayFunc(display) glut.glutReshapeFunc(resize) # This is necessary on osx to be able to close the window # (else the close button is disabled) if sys.platform == 'darwin' and not bool(glut.HAVE_FREEGLUT): glut.glutWMCloseFunc(close)