Example #1
0
def save(display_func, filename="screenshot.png"):
    """
    """

    try:
        import OpenGL.GL.EXT.framebuffer_object as fbo
    except ImportError:
        print 'You do not have the framebuffer extension on your video card'
        print 'Cannot save figure'
        return

    x, y, w, h = gl.glGetIntegerv(gl.GL_VIEWPORT)

    # Setup framebuffer
    framebuffer = fbo.glGenFramebuffersEXT(1)
    fbo.glBindFramebufferEXT(fbo.GL_FRAMEBUFFER_EXT, framebuffer)

    # Setup depthbuffer
    depthbuffer = fbo.glGenRenderbuffersEXT(1)
    fbo.glBindRenderbufferEXT(fbo.GL_RENDERBUFFER_EXT, depthbuffer)
    fbo.glRenderbufferStorageEXT(fbo.GL_RENDERBUFFER_EXT,
                                 gl.GL_DEPTH_COMPONENT, w, h)

    # Create texture to render to
    data = np.zeros((w, h, 4), dtype=np.ubyte)
    texture = gl.glGenTextures(1)
    gl.glBindTexture(gl.GL_TEXTURE_2D, texture)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER,
                       gl.GL_LINEAR)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER,
                       gl.GL_LINEAR)
    gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, w, h, 0, gl.GL_RGBA,
                    gl.GL_UNSIGNED_BYTE, data)
    fbo.glFramebufferTexture2DEXT(gl.GL_FRAMEBUFFER_EXT,
                                  gl.GL_COLOR_ATTACHMENT0_EXT,
                                  gl.GL_TEXTURE_2D, texture, 0)
    fbo.glFramebufferRenderbufferEXT(gl.GL_FRAMEBUFFER_EXT,
                                     gl.GL_DEPTH_ATTACHMENT_EXT,
                                     gl.GL_RENDERBUFFER_EXT, depthbuffer)
    status = fbo.glCheckFramebufferStatusEXT(fbo.GL_FRAMEBUFFER_EXT)

    if status != fbo.GL_FRAMEBUFFER_COMPLETE_EXT:
        raise (RuntimeError, 'Error in framebuffer activation')

    display_func()
    glut.glutSwapBuffers()
    data = gl.glReadPixels(x, y, w, h, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE)

    from PIL import Image
    image = Image.fromstring('RGBA', (w, h), data)
    image = image.transpose(Image.FLIP_TOP_BOTTOM)
    image.save(filename)

    # Cleanup
    fbo.glBindRenderbufferEXT(fbo.GL_RENDERBUFFER_EXT, 0)
    fbo.glBindFramebufferEXT(fbo.GL_FRAMEBUFFER_EXT, 0)
    gl.glDeleteTextures(texture)
    fbo.glDeleteFramebuffersEXT([
        framebuffer,
    ])
Example #2
0
    def __init__(self, width, height, drawFunc):
        tex = GL.glGenTextures(1)
        GL.glBindTexture(GL.GL_TEXTURE_2D, tex)
        GL.glTexParameter(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST)
        GL.glTexParameter(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST)
        GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA8, width, height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, None)
        self.enabled = False
        self._texID = tex
        if FBO.glGenFramebuffersEXT:
            buf = FBO.glGenFramebuffersEXT(1)
            FBO.glBindFramebufferEXT(FBO.GL_FRAMEBUFFER_EXT, buf)
            depthbuffer = FBO.glGenRenderbuffersEXT(1)
            FBO.glBindRenderbufferEXT(FBO.GL_RENDERBUFFER_EXT, depthbuffer)
            FBO.glRenderbufferStorageEXT(FBO.GL_RENDERBUFFER_EXT, GL.GL_DEPTH_COMPONENT, width, height)
            FBO.glFramebufferRenderbufferEXT(FBO.GL_FRAMEBUFFER_EXT, FBO.GL_DEPTH_ATTACHMENT_EXT, FBO.GL_RENDERBUFFER_EXT, depthbuffer)
            FBO.glFramebufferTexture2DEXT(FBO.GL_FRAMEBUFFER_EXT, FBO.GL_COLOR_ATTACHMENT0_EXT, GL.GL_TEXTURE_2D, tex, 0)
            status = FBO.glCheckFramebufferStatusEXT(FBO.GL_FRAMEBUFFER_EXT)
            if status != FBO.GL_FRAMEBUFFER_COMPLETE_EXT:
                print "glCheckFramebufferStatusEXT", status
                self.enabled = False
                return

            FBO.glBindFramebufferEXT(FBO.GL_FRAMEBUFFER_EXT, buf)
            GL.glPushAttrib(GL.GL_VIEWPORT_BIT)
            GL.glViewport(0, 0, width, height)

            drawFunc()
            GL.glPopAttrib()
            FBO.glBindFramebufferEXT(FBO.GL_FRAMEBUFFER_EXT, 0)
            FBO.glDeleteFramebuffersEXT(1, [buf])
            FBO.glDeleteRenderbuffersEXT(1, [depthbuffer])
            self.enabled = True
Example #3
0
    def _setupFrameBuffer(self):
        # Setup framebuffer
        self.frameBuffer = FB.glGenFramebuffersEXT(1)

        FB.glBindFramebufferEXT(FB.GL_FRAMEBUFFER_EXT, self.frameBuffer)
        # Setup depthbuffer
        self.depthBuffer = FB.glGenRenderbuffersEXT(1)
        FB.glBindRenderbufferEXT (FB.GL_RENDERBUFFER_EXT,self.depthBuffer)
        FB.glRenderbufferStorageEXT (FB.GL_RENDERBUFFER_EXT, GL.GL_DEPTH_COMPONENT, int(self.size[0]), int(self.size[1]))

        # Create texture to render to
        self.frameTexture = c_uint(0)
        GL.glGenTextures (1, self.frameTexture)
        GL.glBindTexture (GL.GL_TEXTURE_2D, self.frameTexture)
        GL.glTexParameteri (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR)
        GL.glTexParameteri (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR)
        GL.glTexImage2D (GL.GL_TEXTURE_2D, 0, GL.GL_RGBA32F_ARB, int(self.size[0]), int(self.size[1]), 0,
                         GL.GL_RGBA, GL.GL_FLOAT, None)

        #attach texture to the frame buffer
        FB.glFramebufferTexture2DEXT (FB.GL_FRAMEBUFFER_EXT, GL.GL_COLOR_ATTACHMENT0_EXT,
                                      GL.GL_TEXTURE_2D, self.frameTexture, 0)
        FB.glFramebufferRenderbufferEXT(FB.GL_FRAMEBUFFER_EXT, GL.GL_DEPTH_ATTACHMENT_EXT,
                                        FB.GL_RENDERBUFFER_EXT, self.depthBuffer)

        status = FB.glCheckFramebufferStatusEXT (FB.GL_FRAMEBUFFER_EXT)
        if status != FB.GL_FRAMEBUFFER_COMPLETE_EXT:
            logging.warning("Error in framebuffer activation")
            return
        GL.glDisable(GL.GL_TEXTURE_2D)
Example #4
0
    def __init__(self, name, rttype='cds', **kwargs):
        """Create a ``RenderTexture``. All keyword arguments are passed through to the
        :meth:`.Texture2D.__init__` method.

        :arg name:   Unique name for this texture

        :arg rttype: ``RenderTexture`` configuration. If provided, must be
                     passed as a keyword argument. Valid values are:

                       - ``'c'``:    Only a colour attachment is configured
                       - ``'cd'``:   Colour and depth attachments are
                                     configured
                       - ``'cds'`` : Colour, depth, and stencil attachments
                                     are configured. This is the default.

        .. note:: A rendering target must have been set for the GL context
                  before a frame buffer can be created ... in other words,
                  call ``context.SetCurrent`` before creating a
                  ``RenderTexture``.
        """

        if rttype not in ('c', 'cd', 'cds'):
            raise ValueError('Invalid rttype: {}'.format(rttype))

        texture2d.Texture2D.__init__(self,
                                     name,
                                     ndim=2,
                                     nvals=4,
                                     dtype=np.uint8,
                                     **kwargs)

        self.__frameBuffer = glfbo.glGenFramebuffersEXT(1)
        self.__rttype = rttype
        self.__renderBuffer = None
        self.__depthTexture = None
        self.__oldSize = None
        self.__oldProjMat = None
        self.__oldMVMat = None
        self.__oldFrameBuffer = None
        self.__oldRenderBuffer = None

        # Use a single renderbuffer as
        # the depth+stencil attachment
        if rttype == 'cds':
            self.__renderBuffer = glfbo.glGenRenderbuffersEXT(1)

        # Or use a texture as the depth
        # attachment (with no stencil attachment)
        elif rttype == 'cd':
            self.__depthTexture = texture2d.DepthTexture('{}_depth'.format(
                self.name))

            # We also need a shader program in
            # case the creator is intending to
            # use the depth information in draws.
            self.__initShader()

        log.debug('Created fbo %s [%s]', self.__frameBuffer, rttype)
Example #5
0
def save(display_func, filename="screenshot.png"):
    """  """

    try:
        import OpenGL.GL.EXT.framebuffer_object as fbo
    except ImportError:
        print 'You do not have the framebuffer extension on your video card'
        print 'Cannot save figure'
        return

    x,y,w,h = gl.glGetIntegerv(gl.GL_VIEWPORT)

    # Setup framebuffer
    framebuffer = fbo.glGenFramebuffersEXT(1)
    fbo.glBindFramebufferEXT( fbo.GL_FRAMEBUFFER_EXT, framebuffer)

    # Setup depthbuffer
    depthbuffer = fbo.glGenRenderbuffersEXT( 1 )
    fbo.glBindRenderbufferEXT( fbo.GL_RENDERBUFFER_EXT, depthbuffer )
    fbo.glRenderbufferStorageEXT( fbo.GL_RENDERBUFFER_EXT, gl.GL_DEPTH_COMPONENT, w, h)
    
    # Create texture to render to
    data = np.zeros((w,h,4), dtype=np.ubyte)
    texture = gl.glGenTextures(1)
    gl.glBindTexture( gl.GL_TEXTURE_2D, texture)
    gl.glTexParameteri( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
    gl.glTexParameteri( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)
    gl.glTexImage2D( gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, w, h, 0,
                     gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, data)
    fbo.glFramebufferTexture2DEXT( gl.GL_FRAMEBUFFER_EXT, gl.GL_COLOR_ATTACHMENT0_EXT,
                                   gl.GL_TEXTURE_2D, texture, 0)
    fbo.glFramebufferRenderbufferEXT( gl.GL_FRAMEBUFFER_EXT, gl.GL_DEPTH_ATTACHMENT_EXT, 
                                      gl.GL_RENDERBUFFER_EXT, depthbuffer)
    status = fbo.glCheckFramebufferStatusEXT( fbo.GL_FRAMEBUFFER_EXT )

    if status != fbo.GL_FRAMEBUFFER_COMPLETE_EXT:
        raise(RuntimeError, 'Error in framebuffer activation')

    display_func()
    glut.glutSwapBuffers()
    data = gl.glReadPixels (x,y,w,h, gl.GL_RGB, gl.GL_UNSIGNED_BYTE)

    from PIL import Image
    #from PIL.ImageCms import profileToProfile
    #ADOBE_RGB_PROFILE = "AdobeRGB1998.icc"
    #SRGB_PROFILE = "./sRGB.icc"
    #RGB_PROFILE = "./RGB.icc"
    image = Image.fromstring('RGB', (w,h), data)
    #profileToProfile(image, RGB_PROFILE, ADOBE_RGB_PROFILE, inPlace=1)
    image = image.transpose(Image.FLIP_TOP_BOTTOM)
    image.save (filename)


    # Cleanup
    fbo.glBindRenderbufferEXT( fbo.GL_RENDERBUFFER_EXT, 0 )
    fbo.glBindFramebufferEXT( fbo.GL_FRAMEBUFFER_EXT, 0 )
    gl.glDeleteTextures( texture )
    fbo.glDeleteFramebuffersEXT( [framebuffer,] )
Example #6
0
    def __init__(self, width, height, drawFunc):
        tex = GL.glGenTextures(1)
        GL.glBindTexture(GL.GL_TEXTURE_2D, tex)
        GL.glTexParameter(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST)
        GL.glTexParameter(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST)
        GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA8, width, height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, None)
        self.enabled = False
        self._texID = tex
        if bool(FBO.glGenFramebuffersEXT):
            buf = FBO.glGenFramebuffersEXT(1)
            depthbuffer = FBO.glGenRenderbuffersEXT(1)

            FBO.glBindFramebufferEXT(FBO.GL_FRAMEBUFFER_EXT, buf)

            FBO.glBindRenderbufferEXT(FBO.GL_RENDERBUFFER_EXT, depthbuffer)
            FBO.glRenderbufferStorageEXT(FBO.GL_RENDERBUFFER_EXT, GL.GL_DEPTH_COMPONENT, width, height)

            FBO.glFramebufferRenderbufferEXT(FBO.GL_FRAMEBUFFER_EXT, FBO.GL_DEPTH_ATTACHMENT_EXT, FBO.GL_RENDERBUFFER_EXT, depthbuffer)
            FBO.glFramebufferTexture2DEXT(FBO.GL_FRAMEBUFFER_EXT, FBO.GL_COLOR_ATTACHMENT0_EXT, GL.GL_TEXTURE_2D, tex, 0)

            status = FBO.glCheckFramebufferStatusEXT(FBO.GL_FRAMEBUFFER_EXT)
            if status != FBO.GL_FRAMEBUFFER_COMPLETE_EXT:
                print "glCheckFramebufferStatusEXT", status
                self.enabled = False
                return

            FBO.glBindFramebufferEXT(FBO.GL_FRAMEBUFFER_EXT, buf)

            with gl.glPushAttrib(GL.GL_VIEWPORT_BIT):
                GL.glViewport(0, 0, width, height)
                drawFunc()

            FBO.glBindFramebufferEXT(FBO.GL_FRAMEBUFFER_EXT, 0)
            FBO.glDeleteFramebuffersEXT(1, [buf])
            FBO.glDeleteRenderbuffersEXT(1, [depthbuffer])
            self.enabled = True
        else:
            GL.glReadBuffer(GL.GL_BACK)
            if bool(window_pos.glWindowPos2dARB):
                pixels = GL.glReadPixels(0, 0, width, height, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE)

            GL.glPushAttrib(GL.GL_VIEWPORT_BIT | GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_TEST | GL.GL_STENCIL_BUFFER_BIT)
            GL.glDisable(GL.GL_STENCIL_TEST)

            GL.glViewport(0, 0, width, height)
            GL.glScissor(0, 0, width, height)
            with gl.glEnable(GL.GL_SCISSOR_TEST):
                drawFunc()

            GL.glBindTexture(GL.GL_TEXTURE_2D, tex)
            GL.glReadBuffer(GL.GL_BACK)
            GL.glCopyTexSubImage2D(GL.GL_TEXTURE_2D, 0, 0, 0, 0, 0, width, height)

            if bool(window_pos.glWindowPos2dARB):
                window_pos.glWindowPos2dARB(0,0)
                GL.glDrawPixels(width, height, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, pixels)

            GL.glPopAttrib()
Example #7
0
def main(path=None):
    glutInit(sys.argv)

    if sys.platform == 'darwin':
        if not path:
            path = dialog()

    if not path:
        sys.exit(0)

    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE)
    glutInitWindowPosition(0, 0)
    glutInitWindowSize(730, 650)

    win = glutCreateWindow(b'MIDI Player')

    (width, height, img) = read_image('mixer.ppm')
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1)

    rbo = c_uint(int(glGenRenderbuffersEXT(1)))
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rbo)
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, width, height)

    fbo = c_uint(int(glGenFramebuffersEXT(1)))
    glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, fbo)
    glFramebufferRenderbufferEXT(GL_READ_FRAMEBUFFER_EXT,
                                 GL_COLOR_ATTACHMENT0_EXT,
                                 GL_RENDERBUFFER_EXT, rbo)

    glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, fbo)
    glClear(GL_COLOR_BUFFER_BIT)
    glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, img)
    glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0)

    player = Player(win, path, width, height)

    glutDisplayFunc(player.display_func)
    glutKeyboardFunc(player.keyboard_func)
    glutMouseFunc(player.mouse_func)
    glutMotionFunc(player.motion_func)
    glutIdleFunc(player.process_events)

    submenus = []
    for instrument in range(128):
        if instrument % 8 == 0:
            submenus.append([families[instrument // 8],
                             glutCreateMenu(player.change_instrument)])
        glutAddMenuEntry(instruments[instrument].encode('ascii'), instrument)
    glutCreateMenu(player.change_instrument)
    for family, submenu in submenus:
        glutAddSubMenu(family.encode('ascii'), submenu)
    glutAttachMenu(GLUT_RIGHT_BUTTON)

    glutMainLoop()
Example #8
0
    def save(self, filename):
        '''
        '''

        try:
            import OpenGL.GL.EXT.framebuffer_object as fbo
        except ImportError:
            print 'You do not have the framebuffer extension on your video card'
            print 'Cannot save figure'
            return

        w,h = self.window.width, self.window.height
        # Setup framebuffer
        framebuffer = fbo.glGenFramebuffersEXT(1)
        fbo.glBindFramebufferEXT( fbo.GL_FRAMEBUFFER_EXT, framebuffer)

        # Setup depthbuffer
        depthbuffer = fbo.glGenRenderbuffersEXT( 1 )
        fbo.glBindRenderbufferEXT( fbo.GL_RENDERBUFFER_EXT, depthbuffer )
        fbo.glRenderbufferStorageEXT( fbo.GL_RENDERBUFFER_EXT, gl.GL_DEPTH_COMPONENT, w, h)
    
        # Create texture to render to
        data = np.zeros((w,h,4), dtype=np.ubyte)
        texture = gl.glGenTextures(1)
        gl.glBindTexture( gl.GL_TEXTURE_2D, texture)
        gl.glTexParameteri( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
        gl.glTexParameteri( gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)
        gl.glTexImage2D( gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, w, h, 0,
                         gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, data)
        fbo.glFramebufferTexture2DEXT( gl.GL_FRAMEBUFFER_EXT, gl.GL_COLOR_ATTACHMENT0_EXT,
                                       gl.GL_TEXTURE_2D, texture, 0)
        fbo.glFramebufferRenderbufferEXT( gl.GL_FRAMEBUFFER_EXT, gl.GL_DEPTH_ATTACHMENT_EXT, 
                                          gl.GL_RENDERBUFFER_EXT, depthbuffer)
        status = fbo.glCheckFramebufferStatusEXT( fbo.GL_FRAMEBUFFER_EXT )

        if status != fbo.GL_FRAMEBUFFER_COMPLETE_EXT:
            raise(RuntimeError, 'Error in framebuffer activation')

        self.redraw()
        self.window.refresh()
        
        x,y,w,h = self.viewport
        data = gl.glReadPixels (x,y,w, h, gl.GL_RGBA,  gl.GL_UNSIGNED_BYTE)
        from PIL import Image
        image = Image.fromstring('RGBA', (w,h), data)
        image = image.transpose(Image.FLIP_TOP_BOTTOM)
        image.save (filename)

        # Cleanup
        fbo.glBindRenderbufferEXT( fbo.GL_RENDERBUFFER_EXT, 0 )
        fbo.glBindFramebufferEXT( fbo.GL_FRAMEBUFFER_EXT, 0 )
        gl.glDeleteTextures( texture )
        fbo.glDeleteFramebuffersEXT( [framebuffer,] )
Example #9
0
def createFBO(size=None):
    """ Offscreen rendering
    
    Save an offscreen rendering of size (w,h) to filename.
    """
    def round2(n):
        """ Get nearest power of two superior to n """
        f = 1
        while f < n:
            f *= 2
        return f

    if size == None:
        size = (512, 512)
    w = round2(size[0])
    h = round2(size[1])

    # Setup framebuffer
    frameBuffer = FB.glGenFramebuffersEXT(1)
    FB.glBindFramebufferEXT(FB.GL_FRAMEBUFFER_EXT, frameBuffer)

    # Setup depthbuffer
    depthBuffer = FB.glGenRenderbuffersEXT(1)
    FB.glBindRenderbufferEXT(FB.GL_RENDERBUFFER_EXT, depthBuffer)
    FB.glRenderbufferStorageEXT(FB.GL_RENDERBUFFER_EXT, GL.GL_DEPTH_COMPONENT,
                                w, h)

    # Create texture to render to
    texture = GL.glGenTextures(1)
    GL.glBindTexture(GL.GL_TEXTURE_2D, texture)
    GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,
                       GL.GL_LINEAR)
    GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,
                       GL.GL_LINEAR)
    GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA32F_ARB, w, h, 0,
                    GL.GL_RGBA, GL.GL_FLOAT, None)

    #attach texture to the frame buffer
    FB.glFramebufferTexture2DEXT(FB.GL_FRAMEBUFFER_EXT,
                                 GL.GL_COLOR_ATTACHMENT0_EXT, GL.GL_TEXTURE_2D,
                                 texture, 0)
    FB.glFramebufferRenderbufferEXT(FB.GL_FRAMEBUFFER_EXT,
                                    GL.GL_DEPTH_ATTACHMENT_EXT,
                                    FB.GL_RENDERBUFFER_EXT, depthBuffer)

    status = FB.glCheckFramebufferStatusEXT(FB.GL_FRAMEBUFFER_EXT)
    if status != FB.GL_FRAMEBUFFER_COMPLETE_EXT:
        print "Error in framebuffer activation"
        return
    GL.glDisable(GL.GL_TEXTURE_2D)
    return frameBuffer, texture, depthBuffer
Example #10
0
File: fbo.py Project: hansent/pymt
    def __init__(self, **kwargs):
        super(HardwareFbo, self).__init__(**kwargs)
        self.framebuffer = None
        self.depthbuffer = None

        set_texture(self.texture)

        self.framebuffer = glGenFramebuffersEXT(1)
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, self.framebuffer)
        if self.framebuffer == 0:
            raise "Failed to initialize framebuffer"

        if self.with_depthbuffer:
            self.depthbuffer = glGenRenderbuffersEXT(1)
            glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, self.depthbuffer)
            glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, self.realsize[0], self.realsize[1])
            glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0)
            glFramebufferRenderbufferEXT(
                GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, self.depthbuffer
            )

        glFramebufferTexture2DEXT(
            GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, get_texture_id(self.texture), 0
        )

        # check the fbo status
        status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)
        if status != GL_FRAMEBUFFER_COMPLETE_EXT:
            pymt.pymt_logger.error("Fbo: Error in framebuffer activation")
            pymt.pymt_logger.error(
                "Fbo: Details: HardwareFbo size=%s, realsize=%s, format=GL_RGBA" % (str(self.size), str(self.realsize))
            )
            if status in HardwareFbo.gl_fbo_errors:
                pymt.pymt_logger.error("Fbo: Details: %s (%d)" % (HardwareFbo.gl_fbo_errors[status], status))
            else:
                pymt.pymt_logger.error("Fbo: Details: Unknown error (%d)" % status)

            pymt.pymt_logger.error("Fbo: ")
            pymt.pymt_logger.error("Fbo: You cannot use Hardware FBO.")
            pymt.pymt_logger.error("Fbo: Please change the configuration to use Software FBO.")
            pymt.pymt_logger.error("Fbo: You can use the pymt-config tools, or edit the configuration to set:")
            pymt.pymt_logger.error("Fbo: ")
            pymt.pymt_logger.error("Fbo: [graphics]")
            pymt.pymt_logger.error("Fbo: fbo = software")
            pymt.pymt_logger.error("Fbo: ")

            raise UnsupportedFboException()

        # unbind framebuffer
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0)
Example #11
0
    def __init__(self, **kwargs):
        super(HardwareFbo, self).__init__(**kwargs)
        self.framebuffer    = None
        self.depthbuffer    = None

        set_texture(self.texture)

        self.framebuffer = glGenFramebuffersEXT(1)
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, self.framebuffer)
        if self.framebuffer == 0:
            raise 'Failed to initialize framebuffer'

        if self.with_depthbuffer:
            self.depthbuffer = glGenRenderbuffersEXT(1)
            glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, self.depthbuffer)
            glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT,
                                     self.realsize[0], self.realsize[1])
            glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0)
            glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
                                         GL_RENDERBUFFER_EXT, self.depthbuffer)

        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
                GL_TEXTURE_2D, get_texture_id(self.texture), 0)

        # check the fbo status
        status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)
        if status != GL_FRAMEBUFFER_COMPLETE_EXT:
            pymt.pymt_logger.error('Fbo: Error in framebuffer activation')
            pymt.pymt_logger.error('Fbo: Details: HardwareFbo size=%s, realsize=%s, format=GL_RGBA' % (
                str(self.size), str(self.realsize)))
            if status in HardwareFbo.gl_fbo_errors:
                pymt.pymt_logger.error('Fbo: Details: %s (%d)' % (HardwareFbo.gl_fbo_errors[status], status))
            else:
                pymt.pymt_logger.error('Fbo: Details: Unknown error (%d)' % status)

            pymt.pymt_logger.error('Fbo: ')
            pymt.pymt_logger.error('Fbo: You cannot use Hardware FBO.')
            pymt.pymt_logger.error('Fbo: Please change the configuration to use Software FBO.')
            pymt.pymt_logger.error('Fbo: You can use the pymt-config tools, or edit the configuration to set:')
            pymt.pymt_logger.error('Fbo: ')
            pymt.pymt_logger.error('Fbo: [graphics]')
            pymt.pymt_logger.error('Fbo: fbo = software')
            pymt.pymt_logger.error('Fbo: ')

            raise UnsupportedFboException()

        # unbind framebuffer
        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0)
Example #12
0
def createFBO(size=None):
    """ Offscreen rendering
    
    Save an offscreen rendering of size (w,h) to filename.
    """
    def round2 (n):
        """ Get nearest power of two superior to n """
        f = 1
        while f<n:
            f*= 2
        return f

    if size == None:
        size = (512,512)
    w = round2 (size[0])
    h = round2 (size[1])

    # Setup framebuffer
    frameBuffer = FB.glGenFramebuffersEXT(1)
    FB.glBindFramebufferEXT(FB.GL_FRAMEBUFFER_EXT, frameBuffer)
    
    # Setup depthbuffer
    depthBuffer = FB.glGenRenderbuffersEXT(1)
    FB.glBindRenderbufferEXT (FB.GL_RENDERBUFFER_EXT,depthBuffer)
    FB.glRenderbufferStorageEXT (FB.GL_RENDERBUFFER_EXT, GL.GL_DEPTH_COMPONENT, w, h)
        
    # Create texture to render to
    texture = GL.glGenTextures (1)
    GL.glBindTexture (GL.GL_TEXTURE_2D, texture)
    GL.glTexParameteri (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR)
    GL.glTexParameteri (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR)
    GL.glTexImage2D (GL.GL_TEXTURE_2D, 0, GL.GL_RGBA32F_ARB, w, h, 0,
                    GL.GL_RGBA, GL.GL_FLOAT, None)
    
    #attach texture to the frame buffer
    FB.glFramebufferTexture2DEXT (FB.GL_FRAMEBUFFER_EXT, GL.GL_COLOR_ATTACHMENT0_EXT,
                               GL.GL_TEXTURE_2D, texture, 0);
    FB.glFramebufferRenderbufferEXT(FB.GL_FRAMEBUFFER_EXT, GL.GL_DEPTH_ATTACHMENT_EXT, 
                                 FB.GL_RENDERBUFFER_EXT, depthBuffer);
                                
    status = FB.glCheckFramebufferStatusEXT (FB.GL_FRAMEBUFFER_EXT);
    if status != FB.GL_FRAMEBUFFER_COMPLETE_EXT:
        print "Error in framebuffer activation"
        return    
    GL.glDisable(GL.GL_TEXTURE_2D);
    return frameBuffer, texture, depthBuffer
Example #13
0
def createFBO(size):
    """ Offscreen rendering
    """
    w = size[0]
    h = size[1]

    # Setup framebuffer
    frameBuffer = FB.glGenFramebuffersEXT(1)
    FB.glBindFramebufferEXT(FB.GL_FRAMEBUFFER_EXT, frameBuffer)

    # Setup depthbuffer
    depthBuffer = FB.glGenRenderbuffersEXT(1)
    FB.glBindRenderbufferEXT(FB.GL_RENDERBUFFER_EXT, depthBuffer)
    FB.glRenderbufferStorageEXT(FB.GL_RENDERBUFFER_EXT, GL.GL_DEPTH_COMPONENT,
                                w, h)

    # Create texture to render to
    GL.glEnable(GL.GL_TEXTURE_2D)
    texture = GL.glGenTextures(1)
    GL.glBindTexture(GL.GL_TEXTURE_2D, texture)
    GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,
                       GL.GL_LINEAR)
    GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,
                       GL.GL_LINEAR)
    GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA32F_ARB, w, h, 0,
                    GL.GL_RGBA, GL.GL_FLOAT, None)

    #attach texture to the frame buffer
    FB.glFramebufferTexture2DEXT(FB.GL_FRAMEBUFFER_EXT,
                                 GL.GL_COLOR_ATTACHMENT0_EXT, GL.GL_TEXTURE_2D,
                                 texture, 0)
    FB.glFramebufferRenderbufferEXT(FB.GL_FRAMEBUFFER_EXT,
                                    GL.GL_DEPTH_ATTACHMENT_EXT,
                                    FB.GL_RENDERBUFFER_EXT, depthBuffer)

    status = FB.glCheckFramebufferStatusEXT(FB.GL_FRAMEBUFFER_EXT)
    if status != FB.GL_FRAMEBUFFER_COMPLETE_EXT:
        print "Error in framebuffer activation"
        return
    GL.glBindTexture(GL.GL_TEXTURE_2D, 0)  #unbind that name to the target
    GL.glDisable(GL.GL_TEXTURE_2D)
    return frameBuffer, texture, depthBuffer
def createFBO(size):
    """ Offscreen rendering
    """
    w =  size[0]
    h = size[1]

    # Setup framebuffer
    frameBuffer = FB.glGenFramebuffersEXT(1)
    FB.glBindFramebufferEXT(FB.GL_FRAMEBUFFER_EXT, frameBuffer)
    
    # Setup depthbuffer
    depthBuffer = FB.glGenRenderbuffersEXT(1)
    FB.glBindRenderbufferEXT (FB.GL_RENDERBUFFER_EXT,depthBuffer)
    FB.glRenderbufferStorageEXT (FB.GL_RENDERBUFFER_EXT, GL.GL_DEPTH_COMPONENT, w, h)
        
    # Create texture to render to
    GL.glEnable(GL.GL_TEXTURE_2D)
    texture = GL.glGenTextures (1)
    GL.glBindTexture (GL.GL_TEXTURE_2D, texture)
    GL.glTexParameteri (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR)
    GL.glTexParameteri (GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR)
    GL.glTexImage2D (GL.GL_TEXTURE_2D, 0, GL.GL_RGBA32F_ARB, w, h, 0,
                    GL.GL_RGBA, GL.GL_FLOAT, None)
    
    #attach texture to the frame buffer
    FB.glFramebufferTexture2DEXT (FB.GL_FRAMEBUFFER_EXT, GL.GL_COLOR_ATTACHMENT0_EXT,
                               GL.GL_TEXTURE_2D, texture, 0);
    FB.glFramebufferRenderbufferEXT(FB.GL_FRAMEBUFFER_EXT, GL.GL_DEPTH_ATTACHMENT_EXT, 
                                 FB.GL_RENDERBUFFER_EXT, depthBuffer);
                                
    status = FB.glCheckFramebufferStatusEXT (FB.GL_FRAMEBUFFER_EXT);
    if status != FB.GL_FRAMEBUFFER_COMPLETE_EXT:
        print "Error in framebuffer activation"
        return    
    GL.glBindTexture(GL.GL_TEXTURE_2D, 0)#unbind that name to the target
    GL.glDisable(GL.GL_TEXTURE_2D)
    return frameBuffer, texture, depthBuffer