Light | Dark

# docs.GL

OpenGL 4
OpenGL 3
OpenGL ES 3
OpenGL ES 2

# glBindFramebuffer

## Name

glBindFramebuffer — bind a framebuffer to a framebuffer target

## C Specification

`void glBindFramebuffer(` | GLenum target,
| GLuint framebuffer`)`;

## Parameters

`target`

  Specifies the framebuffer target of the binding operation.

`framebuffer`

  Specifies the name of the framebuffer object to bind.

## Description

`glBindFramebuffer` binds the framebuffer object with name `framebuffer` to the framebuffer target specified by `target`. `target` must be either `GL_DRAW_FRAMEBUFFER`, `GL_READ_FRAMEBUFFER` or `GL_FRAMEBUFFER`. If a framebuffer object is bound to `GL_DRAW_FRAMEBUFFER` or `GL_READ_FRAMEBUFFER`, it becomes the target for rendering or readback operations, respectively, until it is deleted or another framebuffer is bound to the corresponding bind point. Calling `glBindFramebuffer` with `target` set to `GL_FRAMEBUFFER` binds `framebuffer` to both the read and draw framebuffer targets.

glGenFramebuffers may be used to generate a set of unused framebuffer object names.

The storage, dimensions, allocation, and format of the images attached to the default framebuffer are managed entirely by the window system. In order that access to the default framebuffer is not lost, it is treated as a framebuffer object with the name of zero. The default framebuffer is therefore rendered to and read from while zero is bound to the corresponding targets.

## Errors

`GL_INVALID_ENUM` is generated if `target` is not `GL_DRAW_FRAMEBUFFER`, `GL_READ_FRAMEBUFFER` or `GL_FRAMEBUFFER`.

## Examples

Create a framebuffer object with a texture-based color attachment and a texture-based depth attachment. Using texture-based attachments allows sampling of those textures in shaders.

    // fbo_width and fbo_height are the desired width and height of the FBO.
    // For Opengl <= 4.4 or if the GL_ARB_texture_non_power_of_two extension
    // is present, fbo_width and fbo_height can be values other than 2^n for
    // some integer n.

    // Build the texture that will serve as the color attachment for the framebuffer.
    GLuint texture_map;
    glGenTextures(1, &texture_map);
    glBindTexture(GL_TEXTURE_2D, texture_map);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fbo_width, fbo_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

    glBindTexture(GL_TEXTURE_2D, 0);

    // Build the texture that will serve as the depth attachment for the framebuffer.
    GLuint depth_texture;
    glGenTextures(1, &depth_texture);
    glBindTexture(GL_TEXTURE_2D, depth_texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, fbo_width, fbo_height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
    glBindTexture(GL_TEXTURE_2D, 0);

    // Build the framebuffer.
    GLuint framebuffer;
    glGenFramebuffers(1, &framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, (GLuint)framebuffer);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_map, 0);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_texture, 0);

    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (status != GL_FRAMEBUFFER_COMPLETE)
        // Error

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

Copy the contents of one framebuffer object to another by blitting.

    glBindFramebuffer(GL_READ_FRAMEBUFFER, source_fbo);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, destination_fbo);

    glBlitFramebuffer(0, 0, source_fbo_width, source_fbo_height, 0, 0, destination_fbo_width, destination_fbo_height, GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT, GL_NEAREST);

## Tutorials

Songho - OpenGL Frame Buffer Object (FBO)
open.gl - Framebuffers
opengl-tutorial.org - Tutorial 14 : Render To Texture
opengl-tutorial.org - Tutorial 16 : Shadow mapping

## API Version Support

| OpenGL ES API Version
--- | ---
Function Name | 2.0 | 3.0 | 3.1
glBindFramebuffer | ✔ | ✔ | ✔

## See Also

glGenFramebuffers, glDeleteFramebuffers, glFramebufferRenderbuffer, glFramebufferTexture2D, glFramebufferTextureLayer, glIsFramebuffer

## Copyright

Copyright © 2010-2014 Khronos Group. This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999. https://opencontent.org/openpub/.

Think you can improve this page? Edit this page on GitHub.