openGL API glFramebufferTexture2D详解
glFramebufferTexture2D — attach a texture image to a framebuffer object
C Specification
void glFramebufferTexture2D( GLenum target,
GLenum attachment,
GLenum textarget,
GLuint texture,
GLint level);
Parameters
target
Specifies the framebuffer target. The symbolic constant must be GL_FRAMEBUFFER.
attachment
Specifies the attachment point to which an image from texture should be attached. Must be one of the following symbolic constants: GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT, or GL_STENCIL_ATTACHMENT.
textarget
Specifies the texture target. Must be one of the following symbolic constants: GL_TEXTURE_2D, GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, or GL_TEXTURE_CUBE_MAP_NEGATIVE_Z.
texture
Specifies the texture object whose image is to be attached.
level
Specifies the mipmap level of the texture image to be attached, which must be 0.
Description
glFramebufferTexture2D attaches the texture image specified by texture and level as one of the logical buffers of the currently bound framebuffer object. attachment specifies whether the texture image should be attached to the framebuffer object’s color, depth, or stencil buffer. A texture image may not be attached to the default framebuffer object name 0.
If texture is not 0, the value of GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE for the specified attachment point is set to GL_TEXTURE, the value of GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME is set to texture, and the value of GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL is set to level. If texture is a cube map texture, the value of GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE is set to textarget; otherwise it is set to the default value GL_TEXTURE_CUBE_MAP_POSITIVE_X. Any previous attachment to the attachment logical buffer of the currently bound framebuffer object is broken.
If texture is 0, the current image, if any, attached to the attachment logical buffer of the currently bound framebuffer object is detached. The value of GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is set to GL_NONE. The value of GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME is set to 0. GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL and GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE are set to the default values 0 and GL_TEXTURE_CUBE_MAP_POSITIVE_X, respectively.
Notes
Special precautions need to be taken to avoid attaching a texture image to the currently bound framebuffer while the texture object is currently bound and potentially sampled by the current vertex or fragment shader. Doing so could lead to the creation of a “feedback loop” between the writing of pixels by rendering operations and the simultaneous reading of those same pixels when used as texels in the currently bound texture. In this scenario, the framebuffer will be considered framebuffer complete, but the values of fragments rendered while in this state will be undefined. The values of texture samples may be undefined as well.
If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is as if glFramebufferTexture2D had been called with a texture of 0 for the attachment point to which this image was attached in the currently bound framebuffer object. In other words, the texture image is detached from the currently bound framebuffer. Note that the texture image is specifically not detached from any non-bound framebuffers. Detaching the image from any non-bound framebuffers is the responsibility of the application.
Errors
GL_INVALID_ENUM is generated if target is not GL_FRAMEBUFFER.
GL_INVALID_ENUM is generated if textarget is not an accepted texture target and texture is not 0.
GL_INVALID_ENUM is generated if attachment is not an accepted attachment point.
GL_INVALID_VALUE is generated if level is not 0 and texture is not 0.
GL_INVALID_OPERATION is generated if the default framebuffer object name 0 is bound.
GL_INVALID_OPERATION is generated if texture is neither 0 nor the name of an existing texture object.
GL_INVALID_OPERATION is generated if texture is the name of an existing two-dimensional texture object but textarget is not GL_TEXTURE_2D or if texture is the name of an existing cube map texture object but textarget is GL_TEXTURE_2D.
Associated Gets
glGetFramebufferAttachmentParameteriv
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);
Tutorials
Songho - OpenGL Frame Buffer Object (FBO)
open.gl - Framebuffers
See Also
glBindFramebuffer, glBindTexture, glCheckFramebufferStatus, glDeleteFramebuffers, glDeleteTextures, glFramebufferRenderbuffer, glGenerateMipmap, glGetFramebufferAttachmentParameteriv, glTexImage2D
名称
glFramebufferTexture2D - 将纹理图像附加到帧缓冲对象
C规范
void glFramebufferTexture2D(GLenum target,
GLenum attachment,
GLenum textarget,
GLuint texture,
GLint level);
参数
target
指定帧缓冲目标。 符号常量必须是GL_FRAMEBUFFER。
attachment
指定应附加纹理图像的附着点。 必须是以下符号常量之一:GL_COLOR_ATTACHMENT0,GL_DEPTH_ATTACHMENT或GL_STENCIL_ATTACHMENT。
textarget
指定纹理目标。 必须是以下符号常量之一:GL_TEXTURE_2D,GL_TEXTURE_CUBE_MAP_POSITIVE_X,GL_TEXTURE_CUBE_MAP_NEGATIVE_X,GL_TEXTURE_CUBE_MAP_POSITIVE_Y,GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,GL_TEXTURE_CUBE_MAP_POSITIVE_Z或GL_TEXTURE_CUBE_MAP_NEGATIVE_Z。
texture
指定要附加图像的纹理对象。
level
指定要附加的纹理图像的mipmap级别,该级别必须为0。
描述
glFramebufferTexture2D将texture和level指定的纹理图像附加为当前绑定的帧缓冲区对象的逻辑缓冲区之一。 attachment指定是否应将纹理图像附加到帧缓冲对象的颜色,深度或模板缓冲区。 纹理图像不可以附加到默认(名称为0)的帧缓冲对象。
如果texture不为0,则指定附加点的GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE的值设置为GL_TEXTURE,GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME的值设置为texture,GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL的值设置为level。 如果纹理是立方体贴图纹理,则GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE的值设置为textarget; 否则将其设置为默认值GL_TEXTURE_CUBE_MAP_POSITIVE_X。 先前绑定的帧缓冲区对象的附件逻辑缓冲区都将被破坏。
如果texture为0,则分离附加到当前绑定的帧缓冲区对象的附件逻辑缓冲区的当前图像(如果有的话)。 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE的值设置为GL_NONE。 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME的值设置为0. GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL和GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE分别设置为默认值0和GL_TEXTURE_CUBE_MAP_POSITIVE_X。
注意
当纹理对象当前被绑定并可能被当前顶点或片段着色器采样时,需要采取特殊预防措施以避免将纹理图像附加到当前绑定的帧缓冲区。这样做可能导致在通过渲染操作写入像素和在当前绑定纹理中用作纹素时同时读取那些相同像素之间创建“反馈循环”。在这种情况下,帧缓冲区将被视为帧缓冲区完成,但在此状态下渲染的片段的值将是未定义的。纹理样本的值也可能是未定义的。
如果在将图像附加到当前绑定的帧缓冲区时删除纹理对象,这就好比使用纹理0调用glFramebufferTexture2D作为此图像附加到当前绑定的帧缓冲区对象中的附着点。换句话说,纹理图像与当前绑定的帧缓冲区分离了。请注意,纹理图像不会与任何未绑定的帧缓冲区分离。从任何非绑定帧缓冲区中分离映像是应用程序的责任。
错误
GL_INVALID_ENUM :target不是GL_FRAMEBUFFER。
GL_INVALID_ENUM :texture不为0时textarget不是可接收的纹理target。
GL_INVALID_ENUM :attachment是不可接收的附着点。
GL_INVALID_VALUE :level不是0时,texture不是0。
GL_INVALID_OPERATION :如果绑定了默认的帧缓冲对象名称0。
GL_INVALID_OPERATION :如果texture既不是0也不是现有纹理对象的名称。
GL_INVALID_OPERATION :如果texture是现有二维纹理对象的名称,但textarget不是GL_TEXTURE_2D,或者texture是现有立方体贴图纹理对象的名称,但textarget是GL_TEXTURE_2D。
相关Gets
glGetFramebufferAttachmentParameteriv
另见
glBindFramebuffer,glBindTexture,glCheckFramebufferStatus,glDeleteFramebuffers,glDeleteTextures,glFramebufferRenderbuffer,glGenerateMipmap,glGetFramebufferAttachmentParameteriv,glTexImage2D
版权
https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glFramebufferTexture2D.xml
https://blog.csdn.net/flycatdeng
参考:
https://blog.csdn.net/flycatdeng/article/details/82667117