## The Top-Level Context

The Top-Level Context — The top level application context.

## Synopsis

    #define             cogl_context_new
    #define             cogl_context_get_display

    void                cogl_push_matrix                    (void);
    void                cogl_pop_matrix                     (void);
    void                cogl_scale                          (float x,
                                                             float y,
                                                             float z);
    void                cogl_translate                      (float x,
                                                             float y,
                                                             float z);
    void                cogl_rotate                         (float angle,
                                                             float x,
                                                             float y,
                                                             float z);
    void                cogl_transform                      (const CoglMatrix *matrix);
    void                cogl_frustum                        (float left,
                                                             float right,
                                                             float bottom,
                                                             float top,
                                                             float z_near,
                                                             float z_far);
    void                cogl_perspective                    (float fovy,
                                                             float aspect,
                                                             float z_near,
                                                             float z_far);
    void                cogl_ortho                          (float left,
                                                             float right,
                                                             float bottom,
                                                             float top,
                                                             float near,
                                                             float far);

    void                cogl_get_modelview_matrix           (CoglMatrix *matrix);
    void                cogl_set_modelview_matrix           (CoglMatrix *matrix);
    void                cogl_get_projection_matrix          (CoglMatrix *matrix);
    void                cogl_set_projection_matrix          (CoglMatrix *matrix);
    void                cogl_set_viewport                   (int x,
                                                             int y,
                                                             int width,
                                                             int height);
    void                cogl_get_viewport                   (float v[4]);

    void                cogl_set_source                     (void *material);
    void                cogl_set_source_color               (const CoglColor *color);
    void                cogl_set_source_color4ub            (guint8 red,
                                                             guint8 green,
                                                             guint8 blue,
                                                             guint8 alpha);
    void                cogl_set_source_color4f             (float red,
                                                             float green,
                                                             float blue,
                                                             float alpha);
    void                cogl_set_source_texture             (CoglHandle texture_handle);
    void *              cogl_get_source                     (void);
    void                cogl_push_source                    (void *material);
    void                cogl_pop_source                     (void);

    #define             COGL_TYPE_BUFFER_BIT
    void                cogl_clear                          (const CoglColor *color,
                                                             unsigned long  buffers);

    enum                CoglReadPixelsFlags;
    void                cogl_read_pixels                    (int x,
                                                             int y,
                                                             int width,
                                                             int height,
                                                             CoglReadPixelsFlags source,
                                                             CoglPixelFormat format,
                                                             guint8 *pixels);

    void                cogl_flush                          (void);

## Description

A CoglContext is the topmost sandbox of Cogl state for an application or toolkit. Its main purpose is to bind together the key state objects at any one time; with the most significant being the current framebuffer being drawn too (See CoglFramebuffer for more details) and the current GPU pipeline configuration (See CoglPipeline for more details).

## Details

### cogl_context_new

    #define cogl_context_new cogl_context_new_EXP

### cogl_context_get_display

    #define cogl_context_get_display cogl_context_get_display_EXP

### cogl_push_matrix ()

    void                cogl_push_matrix                    (void);

Stores the current model-view matrix on the matrix stack. The matrix can later be restored with `cogl_pop_matrix()`.

### cogl_pop_matrix ()

    void                cogl_pop_matrix                     (void);

Restores the current model-view matrix from the matrix stack.

### cogl_scale ()

    void                cogl_scale                          (float x,
                                                             float y,
                                                             float z);

Multiplies the current model-view matrix by one that scales the x, y and z axes by the given values.

`x` : | Amount to scale along the x-axis
`y` : | Amount to scale along the y-axis
`z` : | Amount to scale along the z-axis

### cogl_translate ()

    void                cogl_translate                      (float x,
                                                             float y,
                                                             float z);

Multiplies the current model-view matrix by one that translates the model along all three axes according to the given values.

`x` : | Distance to translate along the x-axis
`y` : | Distance to translate along the y-axis
`z` : | Distance to translate along the z-axis

### cogl_rotate ()

    void                cogl_rotate                         (float angle,
                                                             float x,
                                                             float y,
                                                             float z);

Multiplies the current model-view matrix by one that rotates the model around the vertex specified by `x`, `y` and `z`. The rotation follows the right-hand thumb rule so for example rotating by 10 degrees about the vertex (0, 0, 1) causes a small counter-clockwise rotation.

`angle` : | Angle in degrees to rotate.
`x` : | X-component of vertex to rotate around.
`y` : | Y-component of vertex to rotate around.
`z` : | Z-component of vertex to rotate around.

### cogl_transform ()

    void                cogl_transform                      (const CoglMatrix *matrix);

Multiplies the current model-view matrix by the given matrix.

`matrix` : | the matrix to multiply with the current model-view

Since 1.4

### cogl_frustum ()

    void                cogl_frustum                        (float left,
                                                             float right,
                                                             float bottom,
                                                             float top,
                                                             float z_near,
                                                             float z_far);

Replaces the current projection matrix with a perspective matrix for the given viewing frustum.

`left` : | Left clipping plane
`right` : | Right clipping plane
`bottom` : | Bottom clipping plane
`top` : | Top clipping plane
`z_near` : | Nearest visible point
`z_far` : | Furthest visible point along the z-axis

Since 0.8.2

### cogl_perspective ()

    void                cogl_perspective                    (float fovy,
                                                             float aspect,
                                                             float z_near,
                                                             float z_far);

Replaces the current projection matrix with a perspective matrix based on the provided values.

`fovy` : | Vertical of view angle in degrees.
`aspect` : | Aspect ratio of diesplay
`z_near` : | Nearest visible point
`z_far` : | Furthest visible point along the z-axis

### cogl_ortho ()

    void                cogl_ortho                          (float left,
                                                             float right,
                                                             float bottom,
                                                             float top,
                                                             float near,
                                                             float far);

Replaces the current projection matrix with an orthographic projection matrix. See Figure 1, “” to see how the matrix is calculated.

Figure 1.

### Note

This function copies the arguments from OpenGL's `glOrtho()` even though they are unnecessarily confusing due to the z near and z far arguments actually being a "distance" from the origin, where negative values are behind the viewer, instead of coordinates for the z clipping planes which would have been consistent with the left, right bottom and top arguments.

`left` : | The coordinate for the left clipping plane
`right` : | The coordinate for the right clipping plane
`bottom` : | The coordinate for the bottom clipping plane
`top` : | The coordinate for the top clipping plane
`near` : | The distance to the near clipping plane (negative if the plane is behind the viewer)
`far` : | The distance for the far clipping plane (negative if the plane is behind the viewer)

Since 1.0

### cogl_get_modelview_matrix ()

    void                cogl_get_modelview_matrix           (CoglMatrix *matrix);

Stores the current model-view matrix in `matrix`.

`matrix` : | return location for the model-view matrix. [out]

### cogl_set_modelview_matrix ()

    void                cogl_set_modelview_matrix           (CoglMatrix *matrix);

Loads `matrix` as the new model-view matrix.

`matrix` : | the new model-view matrix

### cogl_get_projection_matrix ()

    void                cogl_get_projection_matrix          (CoglMatrix *matrix);

Stores the current projection matrix in `matrix`.

`matrix` : | return location for the projection matrix. [out]

### cogl_set_projection_matrix ()

    void                cogl_set_projection_matrix          (CoglMatrix *matrix);

Loads matrix as the new projection matrix.

`matrix` : | the new projection matrix

### cogl_set_viewport ()

    void                cogl_set_viewport                   (int x,
                                                             int y,
                                                             int width,
                                                             int height);

Replaces the current viewport with the given values.

`x` : | X offset of the viewport
`y` : | Y offset of the viewport
`width` : | Width of the viewport
`height` : | Height of the viewport

Since 1.2

### cogl_get_viewport ()

    void                cogl_get_viewport                   (float v[4]);

Stores the current viewport in `v`. `v`[0] and `v`[1] get the x and y position of the viewport and `v`[2] and `v`[3] get the width and height.

`v` : | pointer to a 4 element array of floats to receive the viewport dimensions. [out][array fixed-size=4]

### cogl_set_source ()

    void                cogl_set_source                     (void *material);

This function changes the material at the top of the source stack. The material at the top of this stack defines the GPU state used to process subsequent primitives, such as rectangles drawn with `cogl_rectangle()` or vertices drawn using `cogl_vertex_buffer_draw()`.

`material` : | A CoglMaterial

Since 1.0

### cogl_set_source_color ()

    void                cogl_set_source_color               (const CoglColor *color);

This is a convenience function for creating a solid fill source material from the given color. This color will be used for any subsequent drawing operation.

The color will be premultiplied by Cogl, so the color should be non-premultiplied. For example: use (1.0, 0.0, 0.0, 0.5) for semi-transparent red.

See also `cogl_set_source_color4ub()` and `cogl_set_source_color4f()` if you already have the color components.

`color` : | a CoglColor

Since 1.0

### cogl_set_source_color4ub ()

    void                cogl_set_source_color4ub            (guint8 red,
                                                             guint8 green,
                                                             guint8 blue,
                                                             guint8 alpha);

This is a convenience function for creating a solid fill source material from the given color using unsigned bytes for each component. This color will be used for any subsequent drawing operation.

The value for each component is an unsigned byte in the range between 0 and 255.

`red` : | value of the red channel, between 0 and 255
`green` : | value of the green channel, between 0 and 255
`blue` : | value of the blue channel, between 0 and 255
`alpha` : | value of the alpha channel, between 0 and 255

Since 1.0

### cogl_set_source_color4f ()

    void                cogl_set_source_color4f             (float red,
                                                             float green,
                                                             float blue,
                                                             float alpha);

This is a convenience function for creating a solid fill source material from the given color using normalized values for each component. This color will be used for any subsequent drawing operation.

The value for each component is a fixed point number in the range between 0 and `1`.0. If the values passed in are outside that range, they will be clamped.

`red` : | value of the red channel, between 0 and `1`.0
`green` : | value of the green channel, between 0 and `1`.0
`blue` : | value of the blue channel, between 0 and `1`.0
`alpha` : | value of the alpha channel, between 0 and `1`.0

Since 1.0

### cogl_set_source_texture ()

    void                cogl_set_source_texture             (CoglHandle texture_handle);

This is a convenience function for creating a material with the first layer set to texture_handle and setting that material as the source with cogl_set_source.

Note: There is no interaction between calls to cogl_set_source_color and cogl_set_source_texture. If you need to blend a texture with a color then you can create a simple material like this:

    material = cogl_material_new ();
    cogl_material_set_color4ub (material, 0xff, 0x00, 0x00, 0x80);
    cogl_material_set_layer (material, 0, tex_handle);
    cogl_set_source (material);

`texture_handle` : | The Cogl texture you want as your source

Since 1.0

### cogl_get_source ()

    void *              cogl_get_source                     (void);

Returns the current source material as previously set using `cogl_set_source()`.

### Note

You should typically consider the returned material immutable and not try to change any of its properties unless you own a reference to that material. At times you may be able to get a reference to an internally managed materials and the result of modifying such materials is undefined.

Returns : | The current source material.

Since 1.6

### cogl_push_source ()

    void                cogl_push_source                    (void *material);

Pushes the given `material` to the top of the source stack. The material at the top of this stack defines the GPU state used to process later primitives as defined by `cogl_set_source()`.

`material` : | A CoglMaterial

Since 1.6

### cogl_pop_source ()

    void                cogl_pop_source                     (void);

Removes the material at the top of the source stack. The material at the top of this stack defines the GPU state used to process later primitives as defined by `cogl_set_source()`.

Since 1.6

### COGL_TYPE_BUFFER_BIT

    #define COGL_TYPE_BUFFER_BIT (cogl_buffer_bit_get_type())

### cogl_clear ()

    void                cogl_clear                          (const CoglColor *color,
                                                             unsigned long  buffers);

Clears all the auxiliary buffers identified in the `buffers` mask, and if that includes the color buffer then the specified `color` is used.

`color` : | Background color to clear to
`buffers` : | A mask of CoglBufferBit's identifying which auxiliary buffers to clear

### enum CoglReadPixelsFlags

    typedef enum { /*< prefix=COGL_READ_PIXELS >*/
      COGL_READ_PIXELS_COLOR_BUFFER = 1L << 0
    } CoglReadPixelsFlags;

Flags for `cogl_read_pixels()`

`COGL_READ_PIXELS_COLOR_BUFFER` | Read from the color buffer

Since 1.0

### cogl_read_pixels ()

    void                cogl_read_pixels                    (int x,
                                                             int y,
                                                             int width,
                                                             int height,
                                                             CoglReadPixelsFlags source,
                                                             CoglPixelFormat format,
                                                             guint8 *pixels);

This reads a rectangle of pixels from the current framebuffer where position (0, 0) is the top left. The pixel at (x, y) is the first read, and the data is returned with a rowstride of (width * 4).

Currently Cogl assumes that the framebuffer is in a premultiplied format so if `format` is non-premultiplied it will convert it. To read the pixel values without any conversion you should either specify a format that doesn't use an alpha channel or use one of the formats ending in PRE.

`x` : | The window x position to start reading from
`y` : | The window y position to start reading from
`width` : | The width of the rectangle you want to read
`height` : | The height of the rectangle you want to read
`source` : | Identifies which auxillary buffer you want to read (only COGL_READ_PIXELS_COLOR_BUFFER supported currently)
`format` : | The pixel format you want the result in (only COGL_PIXEL_FORMAT_RGBA_8888 supported currently)
`pixels` : | The location to write the pixel data.

### cogl_flush ()

    void                cogl_flush                          (void);

This function should only need to be called in exceptional circumstances.

As an optimization Cogl drawing functions may batch up primitives internally, so if you are trying to use raw GL outside of Cogl you stand a better chance of being successful if you ask Cogl to flush any batched geometry before making your state changes.

It only ensure that the underlying driver is issued all the commands necessary to draw the batched primitives. It provides no guarantees about when the driver will complete the rendering.

This provides no guarantees about the GL state upon returning and to avoid confusing Cogl you should aim to restore any changes you make before resuming use of Cogl.

If you are making state changes with the intention of affecting Cogl drawing primitives you are 100% on your own since you stand a good chance of conflicting with Cogl internals. For example clutter-gst which currently uses direct GL calls to bind ARBfp programs will very likely break when Cogl starts to use ARBfb programs itself for the material API.

Since 1.0