Light | Dark

glGenFramebuffers

Name

glGenFramebuffers — generate framebuffer object names

C Specification

void glGenFramebuffers( GLsizei n,
GLuint *framebuffers);

Parameters

n

Specifies the number of framebuffer object names to generate.

framebuffers

Specifies an array in which the generated framebuffer object names are stored.

Description

glGenFramebuffers returns n framebuffer object names in framebuffers. There is no guarantee that the names form a contiguous set of integers; however, it is guaranteed that none of the returned names was in use immediately before the call to glGenFramebuffers.

Framebuffer object names returned by a call to glGenFramebuffers are not returned by subsequent calls, unless they are first deleted with glDeleteFramebuffers.

The names returned in framebuffers are marked as used, for the purposes of glGenFramebuffers only, but they acquire state and type only when they are first bound.

Errors

GL_INVALID_VALUE is generated if n is negative.

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);
Create a framebuffer object with a renderbuffer-based color attachment and a renderbuffer-based depth attachment.
// 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 color_renderbuffer;
glGenRenderbuffers(1, &color_renderbuffer);
glBindRenderbuffer( GL_RENDERBUFFER, (GLuint)color_renderbuffer );
glRenderbufferStorage( GL_RENDERBUFFER, GL_RGBA8, fbo_width, fbo_height );
glBindRenderbuffer( GL_RENDERBUFFER, 0 );

// Build the texture that will serve as the depth attachment for the framebuffer.
GLuint depth_renderbuffer;
glGenRenderbuffers(1, &depth_renderbuffer);
glBindRenderbuffer( GL_RENDERBUFFER, (GLuint)depth_renderbuffer );
glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fbo_width, fbo_height );
glBindRenderbuffer( GL_RENDERBUFFER, 0 );

// Build the framebuffer.
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, (GLuint)framebuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color_renderbuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_renderbuffer);

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

glBindFramebuffer(GL_FRAMEBUFFER, 0);

API Version Support

OpenGL ES API Version
Function Name 2.0 3.0 3.1
glGenFramebuffers
Think you can improve this page? Edit this page on GitHub.