GLSL:无法从 FBO 读取纹理并使用片段着色器渲染到另一个 FBO
我正在尝试“读取”附加到第一个 FBO (fboA) 的纹理,修改它(使用片段着色器)并渲染到第二个 FBO (fboB)。
我无法弄清楚,我得到的只是黑色或白色的纹理。
这是代码:
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboB);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureFromFboA);
GLuint t1Location = glGetUniformLocation(shaderProgram, "texture");
glUniform1i(t1Location, 0);
glUseProgram(shaderProgram);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f (0.0f, 0.0f);
glTexCoord2f(imageRect.size.width, 0.0f);
glVertex2f (imageRect.size.width, 0.0f);
glTexCoord2f(imageRect.size.width, imageRect.size.height);
glVertex2f (imageRect.size.width, imageRect.size.height);
glTexCoord2f(0.0f, imageRect.size.height);
glVertex2f (0.0f,imageRect.size.height);
glEnd();
glDisable(GL_TEXTURE_RECTANGLE_ARB);
glFlush();
glUseProgram(0);
这是片段着色器代码:
#version 120
uniform sampler2D texture;
void main(void)
{
gl_FragColor = texture2D(texture, gl_TexCoord[0].st) * 0.8;
}
我希望在 fboB 中呈现较暗的纹理,但我只得到全黑纹理。如果我编写 gl_FragColor =texture2D(texture, gl_TexCoord[0].st);
,也会发生这种情况。
相反,如果我写 gl_FragColor = vec2(1.0, 0.0, 0.0, 1.0);
我正确地得到了全红色纹理作为输出。
如果我注释掉 glUseProgram()
语句,则代码可以正常工作,并且 fboB 中的纹理是 fboA 中纹理的精确副本。
为什么会出现这种情况?我错过了什么吗?
I'm trying to "read" the texture attached to a first FBO (fboA), modifying it (with fragment shader) and render to a second FBO (fboB).
I'm not able to figure it out, all I got is a black or white texture.
Here is the code:
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboB);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureFromFboA);
GLuint t1Location = glGetUniformLocation(shaderProgram, "texture");
glUniform1i(t1Location, 0);
glUseProgram(shaderProgram);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f (0.0f, 0.0f);
glTexCoord2f(imageRect.size.width, 0.0f);
glVertex2f (imageRect.size.width, 0.0f);
glTexCoord2f(imageRect.size.width, imageRect.size.height);
glVertex2f (imageRect.size.width, imageRect.size.height);
glTexCoord2f(0.0f, imageRect.size.height);
glVertex2f (0.0f,imageRect.size.height);
glEnd();
glDisable(GL_TEXTURE_RECTANGLE_ARB);
glFlush();
glUseProgram(0);
This is the fragment shader code:
#version 120
uniform sampler2D texture;
void main(void)
{
gl_FragColor = texture2D(texture, gl_TexCoord[0].st) * 0.8;
}
I would expect to have a darker texture rendered in fboB but I only get an all black texture. This happens also if I write gl_FragColor = texture2D(texture, gl_TexCoord[0].st);
.
On the contrary, if I write gl_FragColor = vec2(1.0, 0.0, 0.0, 1.0);
I correctly have an all red texture as output.
If I comment out the glUseProgram()
statement, the code works fine and texture in fboB is an exact copy of texture in fboA.
Why this happens? Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
矩形纹理不一样纹理类型 作为 2D 纹理。是的,它们是二维的,但它们仍然保持独特的纹理类型。因此,无法通过
sampler2D
访问它们。因此将其更改为
samplerRect
。您还需要使用正确的纹理坐标,因为矩形纹理采用纹素空间坐标而不是标准化坐标。或者,您可以只使用 2D 纹理。 NPOT 纹理已经存在了五年多了;您不必使用矩形纹理来获得非二次幂的渲染目标。
Rectangle textures are not the same texture type as 2D textures. Yes, they're two-dimensional, but they still maintain a distinct texture type. Therefore, they cannot be accessed via a
sampler2D
.So change that to a
samplerRect
. You will also need to use proper texture coordinates, because rectangle textures take texel-space coordinates instead of normalized coordinates.Alternatively, you can just use a 2D texture. NPOT textures have been around for well over half a decade; you don't have to use rectangle textures to have non-power-of-two render targets.
您不能将sampler2D用于纹理矩形...您必须使用GL_TEXTURE_2D而不是GL_TEXTURE_RECTANGLE_ARB。
这里有一个关于 FBO 的很好的教程:
http://www.songho.ca/opengl/gl_fbo.html
You cannot use sampler2D for texture rectangles... you must use GL_TEXTURE_2D not GL_TEXTURE_RECTANGLE_ARB.
here you have a nice tutorial on the FBO:
http://www.songho.ca/opengl/gl_fbo.html