Esempio n. 1
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)
Esempio n. 2
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,
    ])
Esempio n. 3
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
Esempio n. 4
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,] )
Esempio n. 5
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()
Esempio n. 6
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,] )
Esempio n. 7
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
Esempio n. 8
0
File: fbo.py Progetto: 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)
Esempio n. 9
0
    def refresh(self):
        """Overrides :meth:`.Texture2D.refresh`. Calls the base-class
        implementation, and ensures that the frame buffer and render buffer
        of this ``RenderTexture`` are configured correctly.
        """
        texture.Texture2D.refresh(self)

        width, height = self.getSize()

        # Bind the colour buffer
        self.bindAsRenderTarget()
        glfbo.glFramebufferTexture2DEXT(glfbo.GL_FRAMEBUFFER_EXT,
                                        glfbo.GL_COLOR_ATTACHMENT0_EXT,
                                        gl.GL_TEXTURE_2D,
                                        self.getTextureHandle(), 0)

        # Combined depth/stencil attachment
        if self.__rttype == 'cds':

            # Configure the render buffer
            glfbo.glRenderbufferStorageEXT(glfbo.GL_RENDERBUFFER_EXT,
                                           gl.GL_DEPTH24_STENCIL8, width,
                                           height)

            # Bind the render buffer
            glfbo.glFramebufferRenderbufferEXT(glfbo.GL_FRAMEBUFFER_EXT,
                                               gl.GL_DEPTH_STENCIL_ATTACHMENT,
                                               glfbo.GL_RENDERBUFFER_EXT,
                                               self.__renderBuffer)

        # Or a depth texture
        elif self.__rttype == 'cd':

            glfbo.glFramebufferTexture2DEXT(
                glfbo.GL_FRAMEBUFFER_EXT, glfbo.GL_DEPTH_ATTACHMENT_EXT,
                gl.GL_TEXTURE_2D, self.__depthTexture.getTextureHandle(), 0)

        # Get the FBO status before unbinding it -
        # the Apple software renderer will return
        # FRAMEBUFFER_UNDEFINED otherwise.
        status = glfbo.glCheckFramebufferStatusEXT(glfbo.GL_FRAMEBUFFER_EXT)

        self.unbindAsRenderTarget()

        # Complain if something is not right
        if status != glfbo.GL_FRAMEBUFFER_COMPLETE_EXT:
            raise RuntimeError('An error has occurred while configuring '
                               'the frame buffer [{}]'.format(status))
Esempio n. 10
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)
Esempio n. 11
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
Esempio n. 12
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