Framebuffer

Fbo is like an offscreen window. You can activate the fbo for rendering into a texture, and use your fbo as a texture for another drawing.

Fbo act as a kivy.graphics.instructions.Canvas.

Exemple of using an fbo for some color rectangles

from kivy.graphics import Fbo, Color, Rectangle

class FboTest(Widget):
    def __init__(self, **kwargs):
        super(FboTest, self).__init__(**kwargs)

        # first step is to create the fbo and use the fbo texture on other
        # rectangle

        with self.canvas:
            # create the fbo
            self.fbo = Fbo(size=(256, 256))

            # show our fbo on the widget in different size
            Color(1, 1, 1)
            Rectangle(size=(32, 32), texture=self.fbo.texture)
            Rectangle(pos=(32, 0), size=(64, 64), texture=self.fbo.texture)
            Rectangle(pos=(96, 0), size=(128, 128), texture=self.fbo.texture)

        # in the second step, you can draw whatever you want on the fbo
        with self.fbo:
            Color(1, 0, 0, .8)
            Rectangle(size=(256, 64))
            Color(0, 1, 0, .8)
            Rectangle(size=(64, 256))

If you change anything in the self.fbo object, it will be automaticly updated, and canvas where the fbo is putted will be automaticly updated too.

Reloading the FBO content

New in version 1.2.0.

If the OpenGL context is lost, then the FBO is lost too. You need to reupload data on it yourself. Use the Fbo.add_reload_observer() to add a reloading function that will be automatically called when needed:

def __init__(self, **kwargs):
    super(...).__init__(**kwargs)
    self.fbo = Fbo(size=(512, 512))
    self.fbo.add_reload_observer(self.populate_fbo)

    # and load the data now.
    self.populate_fbo(self.fbo)


def populate_fbo(self, fbo):
    with fbo:
        # .. put your Color / Rectangle / ... here

This way, you could use the same method for initialization and for reloading. But it’s up to you.

class kivy.graphics.fbo.Fbo

Bases: kivy.graphics.instructions.RenderContext

Fbo class for wrapping the OpenGL Framebuffer extension. The Fbo support “with” statement.

Parameters :
clear_color: tuple, default to (0, 0, 0, 0)

Define the default color for clearing the framebuffer

size: tuple, default to (1024, 1024)

Default size of the framebuffer

push_viewport: bool, default to True

If True, the OpenGL viewport will be set to the framebuffer size, and will be automatically restored when the framebuffer released.

with_depthbuffer: bool, default to False

If True, the framebuffer will be allocated with a Z buffer.

texture: Texture, default to None

If None, a default texture will be created.

add_reload_observer()

Add a callback to be called after the whole graphics context have been reloaded. This is where you can reupload your custom data in GPU.

New in version 1.2.0.

Parameters :
callback: func(context) -> return None

The first parameter will be the context itself

bind()

Bind the FBO to the current opengl context. Bind mean that you enable the Framebuffer, and all the drawing operations will act inside the Framebuffer, until release() is called.

The bind/release operation are automatically done when you add graphics object in it. But if you want to manipulate a Framebuffer yourself, you can use it like this:

self.fbo = FBO()
self.fbo.bind()
# do any drawing command
self.fbo.release()

# then, your fbo texture is available at
print self.fbo.texture
clear_buffer()

Clear the framebuffer with the clear_color.

You need to bound the framebuffer yourself before calling this method:

fbo.bind()
fbo.clear_buffer()
fbo.release()
clear_color

Clear color in (red, green, blue, alpha) format.

release()

Release the Framebuffer (unbind).

remove_reload_observer()

Remove a callback from the observer list, previously added by add_reload_observer().

New in version 1.2.0.

size

Size of the framebuffer, in (width, height) format.

If you change the size, the framebuffer content will be lost.

texture

Return the framebuffer texture