I am trying to initialize a texture with all zeros, using DRAW framebuffer as suggested by this post. However, I'm quite puzzled that my DRAW framebuffer is only cleared when I attached it to GL_COLOR_ATTACHMENT0
:
我正在尝试使用这个帖子的建议使用DRAW帧缓冲来初始化全零的纹理。但是,当我将它附加到GL_COLOR_ATTACHMENT0时,我很清楚我的DRAW帧缓冲区被清除了:
int levels = 2;
int potW = 2; int potH = 2;
GLuint _potTextureName;
glGenTextures(1, &_potTextureName);
glBindTexture(GL_TEXTURE_2D, _potTextureName);
glTexStorage2D(GL_TEXTURE_2D, levels, GL_RGBA32F, potW, potH);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _potTextureName, 0);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
GLuint clearColor[4] = {0,0,0,0};
glClearBufferuiv(GL_COLOR, 0, clearColor);
Modifying the snippet to use GL_COLOR_ATTACHMENT1
, retaining everything else, will NOT clear the framebuffer:
修改代码段以使用GL_COLOR_ATTACHMENT1,保留其他所有内容,不会清除帧缓冲区:
int levels = 2;
int potW = 2; int potH = 2;
GLuint _potTextureName;
glGenTextures(1, &_potTextureName);
glBindTexture(GL_TEXTURE_2D, _potTextureName);
glTexStorage2D(GL_TEXTURE_2D, levels, GL_RGBA32F, potW, potH);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, _potTextureName, 0);
glDrawBuffer(GL_COLOR_ATTACHMENT1);
GLuint clearColor[4] = {0,0,0,0};
glClearBufferuiv(GL_COLOR, 0, clearColor);
I tried using glDrawBuffers
instead as suggested here, and I also tried using glClearColor
and glClear
, but they all behave the same way. What am I missing here?
我尝试使用glDrawBuffers而不是这里建议的,我也尝试使用glClearColor和glClear,但它们的行为方式相同。我在这里错过了什么?
1 个解决方案
#1
It turns out that it has todo with what I previously bind to GL_COLOR_ATTACHMENT0
.
事实证明它与之前绑定到GL_COLOR_ATTACHMENT0的内容有关系。
In the second case, GL_COLOR_ATTACHMENT0
was already bound to a texture of smaller size. There is a note related to Framebuffer Completeness Rules that although there is no restriction on the texture size, the effective size of the FBO is the intersection of the sizes of all bound images. Therefore, in my second case, if the texture (1) bound to GL_COLOR_ATTACHMENT1
is bigger than what I bound to GL_COLOR_ATTACHMENT0
, then the texture (1) will only be cleared partially, no matter what clear operation I used (glClear
or glClearBuffer*
).
在第二种情况下,GL_COLOR_ATTACHMENT0已经绑定到较小尺寸的纹理。有一个与帧缓冲完整性规则相关的注释虽然对纹理大小没有限制,但FBO的有效大小是所有绑定图像的大小的交集。因此,在我的第二种情况下,如果绑定到GL_COLOR_ATTACHMENT1的纹理(1)大于我绑定到GL_COLOR_ATTACHMENT0的纹理(1),那么纹理(1)将仅部分清除,无论我使用什么清晰操作(glClear或glClearBuffer *) 。
The first case turns out to work for me since I only have one texture bound to the FBO, in GL_COLOR_ATTACHMENT0
.
第一种情况对我有用,因为我只有一个纹理绑定到FBO,在GL_COLOR_ATTACHMENT0中。
#1
It turns out that it has todo with what I previously bind to GL_COLOR_ATTACHMENT0
.
事实证明它与之前绑定到GL_COLOR_ATTACHMENT0的内容有关系。
In the second case, GL_COLOR_ATTACHMENT0
was already bound to a texture of smaller size. There is a note related to Framebuffer Completeness Rules that although there is no restriction on the texture size, the effective size of the FBO is the intersection of the sizes of all bound images. Therefore, in my second case, if the texture (1) bound to GL_COLOR_ATTACHMENT1
is bigger than what I bound to GL_COLOR_ATTACHMENT0
, then the texture (1) will only be cleared partially, no matter what clear operation I used (glClear
or glClearBuffer*
).
在第二种情况下,GL_COLOR_ATTACHMENT0已经绑定到较小尺寸的纹理。有一个与帧缓冲完整性规则相关的注释虽然对纹理大小没有限制,但FBO的有效大小是所有绑定图像的大小的交集。因此,在我的第二种情况下,如果绑定到GL_COLOR_ATTACHMENT1的纹理(1)大于我绑定到GL_COLOR_ATTACHMENT0的纹理(1),那么纹理(1)将仅部分清除,无论我使用什么清晰操作(glClear或glClearBuffer *) 。
The first case turns out to work for me since I only have one texture bound to the FBO, in GL_COLOR_ATTACHMENT0
.
第一种情况对我有用,因为我只有一个纹理绑定到FBO,在GL_COLOR_ATTACHMENT0中。