## Offscreen Buffers

Offscreen Buffers — Functions for creating and manipulating offscreen framebuffers.

## Functions

CoglOffscreen * | cogl_offscreen_new_with_texture ()
CoglOffscreen * | cogl_offscreen_new_to_texture ()
CoglBool | cogl_is_offscreen ()
void | cogl_set_framebuffer ()
void | cogl_push_framebuffer ()
void | cogl_pop_framebuffer ()
void | cogl_set_draw_buffer ()
void | cogl_pop_draw_buffer ()
void | cogl_push_draw_buffer ()

## Description

Cogl allows creating and operating on offscreen framebuffers.

## Functions

### cogl_offscreen_new_with_texture ()

    CoglOffscreen *
    cogl_offscreen_new_with_texture (CoglTexture *texture);

This creates an offscreen framebuffer object using the given `texture` as the primary color buffer. It doesn't just initialize the contents of the offscreen buffer with the `texture` ; they are tightly bound so that drawing to the offscreen buffer effectively updates the contents of the given texture. You don't need to destroy the offscreen buffer before you can use the `texture` again.

This api only works with low-level CoglTexture types such as CoglTexture2D, CoglTexture3D and CoglTextureRectangle, and not with meta-texture types such as CoglTexture2DSliced.

The storage for the framebuffer is actually allocated lazily so this function will never return `NULL` to indicate a runtime error. This means it is still possible to configure the framebuffer before it is really allocated.

Simple applications without full error handling can simply rely on Cogl to lazily allocate the storage of framebuffers but you should be aware that if Cogl encounters an error (such as running out of GPU memory) then your application will simply abort with an error message. If you need to be able to catch such exceptions at runtime then you can explicitly allocate your framebuffer when you have finished configuring it by calling `cogl_framebuffer_allocate()` and passing in a CoglError argument to catch any exceptions.

#### Parameters

texture | A CoglTexture pointer |

#### Returns

a newly instantiated CoglOffscreen framebuffer.

[transfer full]

### cogl_offscreen_new_to_texture ()

    CoglOffscreen *
    cogl_offscreen_new_to_texture (CoglTexture *texture);

`cogl_offscreen_new_to_texture` has been deprecated since version 1.16 and should not be used in newly-written code.

Use cogl_offscreen_new_with_texture instead.

This creates an offscreen buffer object using the given `texture` as the primary color buffer. It doesn't just initialize the contents of the offscreen buffer with the `texture` ; they are tightly bound so that drawing to the offscreen buffer effectivly updates the contents of the given texture. You don't need to destroy the offscreen buffer before you can use the `texture` again.

This only works with low-level CoglTexture types such as CoglTexture2D, CoglTexture3D and CoglTextureRectangle, and not with meta-texture types such as CoglTexture2DSliced.

#### Parameters

texture | A CoglTexture pointer |

#### Returns

a newly instantiated CoglOffscreen framebuffer or `NULL` if it wasn't possible to create the buffer.

[transfer full]

### cogl_is_offscreen ()

    CoglBool
    cogl_is_offscreen (void *object);

Determines whether the given CoglObject references an offscreen framebuffer object.

#### Parameters

object | A pointer to a CoglObject |

#### Returns

`TRUE` if `object` is a CoglOffscreen framebuffer, `FALSE` otherwise

### cogl_set_framebuffer ()

    void
    cogl_set_framebuffer (CoglFramebuffer *buffer);

`cogl_set_framebuffer` has been deprecated since version 1.16 and should not be used in newly-written code.

The latest drawing apis take explicit CoglFramebuffer arguments so this stack of framebuffers shouldn't be used anymore.

This redirects all subsequent drawing to the specified framebuffer. This can either be an offscreen buffer created with `cogl_offscreen_new_to_texture()` or in the future it may be an onscreen framebuffers too.

#### Parameters

buffer | A CoglFramebuffer object, either onscreen or offscreen. |

Since: 1.2

### cogl_push_framebuffer ()

    void
    cogl_push_framebuffer (CoglFramebuffer *buffer);

`cogl_push_framebuffer` has been deprecated since version 1.16 and should not be used in newly-written code.

The latest drawing apis take explicit CoglFramebuffer arguments so this stack of framebuffers shouldn't be used anymore.

Redirects all subsequent drawing to the specified framebuffer. This can either be an offscreen buffer created with `cogl_offscreen_new_to_texture()` or in the future it may be an onscreen framebuffer too.

You should understand that a framebuffer owns the following state:

- The projection matrix
- The modelview matrix stack
- The viewport
- The clip stack

So these items will automatically be saved and restored when you push and pop between different framebuffers.

Also remember a newly allocated framebuffer will have an identity matrix for the projection and modelview matrices which gives you a coordinate space like OpenGL with (-1, -1) corresponding to the top left of the viewport, (1, 1) corresponding to the bottom right and +z coming out towards the viewer.

If you want to set up a coordinate space like Clutter does with (0, 0) corresponding to the top left and (framebuffer_width, framebuffer_height) corresponding to the bottom right you can do so like this:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43

    static void
    setup_viewport (unsigned int width,
                    unsigned int height,
                    float fovy,
                    float aspect,
                    float z_near,
                    float z_far)
    {
      float z_camera;
      CoglMatrix projection_matrix;
      CoglMatrix mv_matrix;

      cogl_set_viewport (0, 0, width, height);
      cogl_perspective (fovy, aspect, z_near, z_far);

      cogl_get_projection_matrix (&projection_matrix);
      z_camera = 0.5 * projection_matrix.xx;

      cogl_matrix_init_identity (&mv_matrix);
      cogl_matrix_translate (&mv_matrix, -0.5f, -0.5f, -z_camera);
      cogl_matrix_scale (&mv_matrix, 1.0f / width, -1.0f / height, 1.0f / width);
      cogl_matrix_translate (&mv_matrix, 0.0f, -1.0 * height, 0.0f);
      cogl_set_modelview_matrix (&mv_matrix);
    }

    static void
    my_init_framebuffer (ClutterStage *stage,
                         CoglFramebuffer *framebuffer,
                         unsigned int framebuffer_width,
                         unsigned int framebuffer_height)
    {
      ClutterPerspective perspective;

      clutter_stage_get_perspective (stage, &perspective);

      cogl_push_framebuffer (framebuffer);
      setup_viewport (framebuffer_width,
                      framebuffer_height,
                      perspective.fovy,
                      perspective.aspect,
                      perspective.z_near,
                      perspective.z_far);
    }

The previous framebuffer can be restored by calling `cogl_pop_framebuffer()`

#### Parameters

buffer | A CoglFramebuffer object, either onscreen or offscreen. |

Since: 1.2

### cogl_pop_framebuffer ()

    void
    cogl_pop_framebuffer (void);

`cogl_pop_framebuffer` has been deprecated since version 1.16 and should not be used in newly-written code.

The latest drawing apis take explicit CoglFramebuffer arguments so this stack of framebuffers shouldn't be used anymore.

Restores the framebuffer that was previously at the top of the stack. All subsequent drawing will be redirected to this framebuffer.

Since: 1.2

### cogl_set_draw_buffer ()

    void
    cogl_set_draw_buffer (CoglBufferTarget target,
                          CoglHandle offscreen);

`cogl_set_draw_buffer` has been deprecated since version 1.16 and should not be used in newly-written code.

The latest drawing apis take explicit CoglFramebuffer arguments so this stack of framebuffers shouldn't be used anymore.

Redirects all subsequent drawing to the specified framebuffer. This can either be an offscreen buffer created with `cogl_offscreen_new_to_texture()` or you can revert to your original on screen window buffer.

#### Parameters

target | A CoglBufferTarget that specifies what kind of framebuffer you are setting as the render target. |
offscreen | If you are setting a framebuffer of type COGL_OFFSCREEN_BUFFER then this is a CoglHandle for the offscreen buffer. |

### cogl_pop_draw_buffer ()

    void
    cogl_pop_draw_buffer (void);

`cogl_pop_draw_buffer` has been deprecated since version 1.16 and should not be used in newly-written code.

The latest drawing apis take explicit CoglFramebuffer arguments so this stack of framebuffers shouldn't be used anymore.

Restore `cogl_set_draw_buffer()` state.

### cogl_push_draw_buffer ()

    void
    cogl_push_draw_buffer (void);

`cogl_push_draw_buffer` has been deprecated since version 1.16 and should not be used in newly-written code.

The latest drawing apis take explicit CoglFramebuffer arguments so this stack of framebuffers shouldn't be used anymore.

Save `cogl_set_draw_buffer()` state.