예제 #1
0
    def gl_init(self):
        # sys.stderr.write("Starting GLUTContext.gl_init\n")
        if self is not GLUTContext._default_instance:
            # sys.stderr.write("Making a GLUT window.\n")
            GLUT.glutInitWindowSize(self._width, self._height)
            GLUT.glutInitWindowPosition(0, 0)
            self.window = GLUT.glutCreateWindow(
                bytes(self._title, encoding="UTF-8"))
        GLUT.glutSetWindow(self.window)
        GLUT.glutMouseFunc(lambda button, state, x, y: self.
                           mouse_button_handler(button, state, x, y))
        GLUT.glutReshapeFunc(
            lambda width, height: self.resize2d(width, height))
        GLUT.glutDisplayFunc(lambda: self.draw())
        GLUT.glutVisibilityFunc(
            lambda state: self.window_visibility_handler(state))
        # Right now, the timer just prints FPS
        GLUT.glutTimerFunc(0, lambda val: self.timer(val), 0)
        GLUT.glutCloseFunc(lambda: self.cleanup())

        for coltype in object_collection.GLObjectCollection.collection_classes:
            self.object_collections.append(
                object_collection.GLObjectCollection.
                collection_classes[coltype](self))
        # self.object_collections.append(object_collection.SimpleObjectCollection(self))
        # self.object_collections.append(object_collection.CurveCollection(self))

        self.window_is_initialized = True
        GLUTContext._instances.append(self)
예제 #2
0
파일: _glut.py 프로젝트: dengemann/vispy
    def __init__(self, name='glut window', *args, **kwargs):
        BaseCanvasBackend.__init__(self)
        self._id = glut.glutCreateWindow(name)
        global ALL_WINDOWS
        ALL_WINDOWS.append(self)

        # Cache of modifiers so we can send modifiers along with mouse motion
        self._modifiers_cache = ()

        # Note: this seems to cause the canvas to ignore calls to show()
        # about half of the time.
        # glut.glutHideWindow()  # Start hidden, like the other backends

        # Register callbacks
        glut.glutDisplayFunc(self.on_draw)
        glut.glutReshapeFunc(self.on_resize)
        # glut.glutVisibilityFunc(self.on_show)
        glut.glutKeyboardFunc(self.on_key_press)
        glut.glutSpecialFunc(self.on_key_press)
        glut.glutKeyboardUpFunc(self.on_key_release)
        glut.glutMouseFunc(self.on_mouse_action)
        glut.glutMotionFunc(self.on_mouse_motion)
        glut.glutPassiveMotionFunc(self.on_mouse_motion)

        # Set close function. See issue #10. For some reason, the function
        # can still not exist even if we checked its boolean status.
        closeFuncSet = False
        if bool(glut.glutWMCloseFunc):  # OSX specific test
            try:
                glut.glutWMCloseFunc(self.on_close)
                closeFuncSet = True
            except OpenGL.error.NullFunctionError:
                pass
        if not closeFuncSet:
            try:
                glut.glutCloseFunc(self.on_close)
                closeFuncSet = True
            except OpenGL.error.NullFunctionError:
                pass

        # glut.glutFunc(self.on_)

        self._initialized = False

        # LC: I think initializing here makes it more consistent with other
        # backends
        glut.glutTimerFunc(0, self._emit_initialize, None)
예제 #3
0
    def __init__(self, name='glut window', *args, **kwargs):
        BaseCanvasBackend.__init__(self)
        self._id = glut.glutCreateWindow(name)
        global ALL_WINDOWS
        ALL_WINDOWS.append(self)

        # Cache of modifiers so we can send modifiers along with mouse motion
        self._modifiers_cache = ()

        # Note: this seems to cause the canvas to ignore calls to show()
        # about half of the time.
        # glut.glutHideWindow()  # Start hidden, like the other backends

        # Register callbacks
        glut.glutDisplayFunc(self.on_draw)
        glut.glutReshapeFunc(self.on_resize)
        # glut.glutVisibilityFunc(self.on_show)
        glut.glutKeyboardFunc(self.on_key_press)
        glut.glutSpecialFunc(self.on_key_press)
        glut.glutKeyboardUpFunc(self.on_key_release)
        glut.glutMouseFunc(self.on_mouse_action)
        glut.glutMotionFunc(self.on_mouse_motion)
        glut.glutPassiveMotionFunc(self.on_mouse_motion)

        # Set close function. See issue #10. For some reason, the function
        # can still not exist even if we checked its boolean status.
        closeFuncSet = False
        if bool(glut.glutWMCloseFunc):  # OSX specific test
            try:
                glut.glutWMCloseFunc(self.on_close)
                closeFuncSet = True
            except OpenGL.error.NullFunctionError:
                pass
        if not closeFuncSet:
            try:
                glut.glutCloseFunc(self.on_close)
                closeFuncSet = True
            except OpenGL.error.NullFunctionError:
                pass

        # glut.glutFunc(self.on_)

        self._initialized = False

        # LC: I think initializing here makes it more consistent with other
        # backends
        glut.glutTimerFunc(0, self._emit_initialize, None)
예제 #4
0
def _set_close_fun(id_, fun):
    # Set close function. See issue #10. For some reason, the function
    # can still not exist even if we checked its boolean status.
    glut.glutSetWindow(id_)
    closeFuncSet = False
    if bool(glut.glutWMCloseFunc):  # OSX specific test
        try:
            glut.glutWMCloseFunc(fun)
            closeFuncSet = True
        except OpenGL.error.NullFunctionError:
            pass
    if not closeFuncSet:
        try:
            glut.glutCloseFunc(fun)
            closeFuncSet = True
        except OpenGL.error.NullFunctionError:
            pass
예제 #5
0
def _set_close_fun(id_, fun):
    # Set close function. See issue #10. For some reason, the function
    # can still not exist even if we checked its boolean status.
    glut.glutSetWindow(id_)
    closeFuncSet = False
    if bool(glut.glutWMCloseFunc):  # OSX specific test
        try:
            glut.glutWMCloseFunc(fun)
            closeFuncSet = True
        except OpenGL.error.NullFunctionError:
            pass
    if not closeFuncSet:
        try:
            glut.glutCloseFunc(fun)
            closeFuncSet = True
        except OpenGL.error.NullFunctionError:
            pass
예제 #6
0
    def glinit(self):
        self._sock_thread = threading.Thread(target=self._comm_thread,args=(self,))
        self._sock_thread.start()
        
        GLUT.glutInit()
        GLUT.glutSetOption(GLUT.GLUT_ACTION_ON_WINDOW_CLOSE,
                           GLUT.GLUT_ACTION_GLUTMAINLOOP_RETURNS)
        # Some window attributes
        GLUT.glutInitDisplayMode(GLUT.GLUT_RGBA | GLUT.GLUT_DOUBLE)
        GLUT.glutInitWindowSize(self._width, self._height)
        GLUT.glutInitWindowPosition(0, 0)
        self._win = GLUT.glutCreateWindow(self.title)

        # OpenGL callbacks
        GLUT.glutDisplayFunc(self._draw(self._render_func))
        GLUT.glutKeyboardFunc(self._on_key)
        GLUT.glutMouseFunc(self._on_click)
        GLUT.glutMotionFunc(self._on_mouse_motion)
        GLUT.glutCloseFunc(self._on_exit)
        GLUT.glutTimerFunc(self.refresh, self._timer, self.refresh)

        #set up 2d or 3d viewport in nice orthographic projection
        GL.glViewport(0, 0, self._width, self._height)
        GL.glMatrixMode(GL.GL_PROJECTION)
        GL.glLoadIdentity()
        if self._dim==3:
            GL.glOrtho(-1.,1.,-1,1.,-1000.,1000.)
            GL.glMatrixMode(GL.GL_MODELVIEW)
        else:
            GL.glMatrixMode(GL.GL_PROJECTION)
            GL.glLoadIdentity()
            GLU.gluOrtho2D(-1.,1.,-1.,1.)
        #smooth the crap out of everything
        GL.glEnable(GL.GL_POINT_SMOOTH)
        GL.glEnable(GL.GL_LINE_SMOOTH)
        GL.glEnable(GL.GL_POLYGON_SMOOTH)
        GL.glEnable(GL.GL_BLEND)
        GL.glBlendFunc(GL.GL_ONE,GL.GL_ONE_MINUS_SRC_ALPHA)
        GL.glHint(GL.GL_LINE_SMOOTH_HINT,GL.GL_NICEST)
        GL.glHint(GL.GL_POINT_SMOOTH_HINT,GL.GL_NICEST)
        GL.glHint(GL.GL_POLYGON_SMOOTH_HINT,GL.GL_NICEST)

        #Clear Everything and call the main loop
        GL.glClearColor(*self.background_color)
        self._window_open =True
        GLUT.glutMainLoop()
예제 #7
0
    def init_glut(self, args):
        """Initialize the GLUT state."""

        # Initialize GLUT.
        GLUT.glutInit(args)

        GLUT.glutInitContextVersion(
            self.context_version[0], self.context_version[1])
        GLUT.glutInitContextFlags(GLUT.GLUT_FORWARD_COMPATIBLE)
        GLUT.glutInitContextProfile(GLUT.GLUT_CORE_PROFILE)

        GLUT.glutSetOption(
            GLUT.GLUT_ACTION_ON_WINDOW_CLOSE,
            GLUT.GLUT_ACTION_GLUTMAINLOOP_RETURNS)

        # Initialize and create the main window.
        GLUT.glutInitDisplayMode(
            GLUT.GLUT_DOUBLE | GLUT.GLUT_RGBA | GLUT.GLUT_DEPTH)
        GLUT.glutInitWindowSize(
            self.window_size[0], self.window_size[1])
        GLUT.glutInitWindowPosition(
            self.window_position[0], self.window_position[1])
        GLUT.glutCreateWindow(self.window_title)

        GLUT.glutDisplayFunc(self.render)
        GLUT.glutIdleFunc(self.idle)
        GLUT.glutReshapeFunc(self.resize)
        GLUT.glutKeyboardFunc(self.keyboard)
        GLUT.glutTimerFunc(0, self.timer, 0)
        GLUT.glutMouseFunc(self.mouse)
        GLUT.glutMotionFunc(self.motion)
        GLUT.glutCloseFunc(self.cleanup)

        aspects = [('Vendor', GL.GL_VENDOR),
                   ('Renderer', GL.GL_RENDERER),
                   ('Version', GL.GL_VERSION),]
        if self.context_version[0] > 1:
            aspects.append(('GLSL', GL.GL_SHADING_LANGUAGE_VERSION))

        for i in aspects:
            print('{}: {}'.format(i[0],
                                  GL.glGetString(i[1]).decode()),
                  file=sys.stderr, flush=True)
예제 #8
0
	def __init__( self, mesh, width=1024, height=768 ) :
		# Initialise OpenGL / GLUT
		glut.glutInit()
		glut.glutInitDisplayMode( glut.GLUT_DOUBLE | glut.GLUT_RGBA | glut.GLUT_DEPTH | glut.GLUT_MULTISAMPLE )
		glut.glutInitWindowSize( width, height )
		glut.glutInitWindowPosition( 100, 100 )
		glut.glutCreateWindow( mesh.name )
		# Mesh viewer
		self.meshviewer = mtk.MeshViewer()
		self.meshviewer.InitialiseOpenGL( width, height )
		self.meshviewer.LoadMesh( mesh )
		self.antialiasing = True
		# GLUT function binding
		glut.glutCloseFunc( self.meshviewer.Close )
		glut.glutDisplayFunc( self.Display )
		glut.glutIdleFunc( self.Idle )
		glut.glutKeyboardFunc( self.Keyboard )
		glut.glutMouseFunc( self.Mouse )
		glut.glutMotionFunc( self.meshviewer.trackball.MouseMove )
		glut.glutReshapeFunc( self.meshviewer.Resize )
예제 #9
0
파일: gl_windows.py 프로젝트: caosuomo/rads
    def __init__(self,
                 dim=3,
                 width=800,
                 height=800,
                 title="",
                 refresh=15,
                 background_color = (0.,0.,0.,0.),
                 render_func=lambda window: None,
                 rf_args=(),
                 rf_kwargs = {},
                 save_file='./',
                 save_file_type=None):
        
        pid = os.fork() # Hack to get interpetor back
        if not pid==0:
            return None
        self.mouse_down = False
        self.mouse_old = [0., 0.]
        self.rotate = [0., 0., 0.]
        self.translate = [0., 0., 0.]
        self.scale = 1.0
        self.dim=dim

        # grab the file extension if its there.
        split_path_ext = os.path.splitext(save_file)
        self.save_file = split_path_ext[0]
        if save_file_type is None:
            self.save_file_ext = split_path_ext[1]
            if self.save_file_ext == '':
                self.save_file_ext = '.png'
        else:
            self.save_file_ext = save_file_type
        self.save_count = 0

        self.width = width
        self.height = height
        self.lights = False
        
         

        GLUT.glutInit()

        # Some window attributes
        GLUT.glutInitDisplayMode(GLUT.GLUT_RGBA | GLUT.GLUT_DOUBLE)
        GLUT.glutInitWindowSize(self.width, self.height)
        GLUT.glutInitWindowPosition(0, 0)
        self.win = GLUT.glutCreateWindow(title)

        # OpenGL callbacks
        GLUT.glutDisplayFunc(self.draw(render_func,*rf_args,**rf_kwargs))
        GLUT.glutKeyboardFunc(self.on_key)
        GLUT.glutMouseFunc(self.on_click)
        GLUT.glutMotionFunc(self.on_mouse_motion)
        try:
            GLUT.glutCloseFunc(self.on_exit)
        # JJB - 6/6/12 -- 
        except GLUT.error.NullFunctionError:
            GLUT.glutWMCloseFunc(self.on_exit)
        GLUT.glutTimerFunc(refresh, self.timer, refresh)

        self.glinit()
        # Clean the slate
        GL.glClearColor(*background_color)
        # and start
        GLUT.glutMainLoop()