glGenFramebuffers — generate framebuffer object names
void glGenFramebuffers( | GLsizei n, |
GLuint *ids) ; |
n
Specifies the number of framebuffer object names to generate.
ids
Specifies an array in which the generated framebuffer object names are stored.
glGenFramebuffers
returns n
framebuffer object names in ids
. 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 ids
are marked as used, for the purposes of glGenFramebuffers
only, but they acquire state and type only when they are first bound.
// 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);
// 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);
Songho - OpenGL Frame Buffer Object (FBO)
open.gl - Framebuffers
opengl-tutorial.org - Tutorial 14 : Render To Texture
opengl-tutorial.org - Tutorial 16 : Shadow mapping
Copyright © 2010 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/.