OpenGL片段着色器不写入fbo颜色缓冲区
我无法从片段着色器向FBO中的颜色缓冲区写入数据。 这个想法是绘制到这个纹理,然后将这个纹理绘制成四边形。 事情是,着色器在绘制到默认窗口缓冲区时起作用,因为如果我移除FBO,就会显示场景。 四边形的纹理也是如此,我用一个导入的纹理进行了尝试,结果如图所示。 我所得到的只是一个黑屏。 出了什么问题? :(
创建FBO的代码:
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glGenRenderbuffers(1, &depthbuff); //Add a depth renderbuffer for depth testing
glBindRenderbuffer(GL_RENDERBUFFER, depthbuff);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthbuff);
//Add the buffer to be drawn to and the buffer for the picking information
glGenTextures(1, &drawbuff);
glBindTexture(GL_TEXTURE_2D, drawbuff);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, drawbuff, 0);
glGenTextures(1, &pickingbuff);
glBindTexture(GL_TEXTURE_2D, pickingbuff);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, pickingbuff, 0);
代码渲染:
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
GLenum buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
glDrawBuffers(2, buffers);
shader->bind(); //If the three lines above are removed, it draws correctly to the window
drawStuff();
代码为四:
quadshader->bind();
glEnable(GL_TEXTURE_2D);
int drawTexID = quadshader->uniformLocation("drawTex");
glActiveTexture(GL_TEXTURE0);
//glBindTexture(GL_TEXTURE_2D, anotherTexture); //This works. A texture imported from a TGA
glBindTexture(GL_TEXTURE_2D, drawbuff);
glUniform1i(drawTexID, 0);
drawQuad();
最后,碎片着色器,我将它设置为将所有内容都绘制成纯色:
void main(void) {
gl_FragData[0] = vec4(1.0, 1.0, 0.0, 1.0);
gl_FragData[1] = vec4(0.0, 1.0, 1.0, 1.0);
}
编辑:如果它有任何帮助,整个事情是在一个Qt GLWidget。
你是否注意确保你的纹理不是同时绑定到采样器(glBindTexture)和帧缓冲区? 我相信如果您尝试在绑定到活动的采样器时写入纹理,或者尝试在附加到活动帧缓冲区时尝试采样纹理,则行为可能是错误或未定义。
链接地址: http://www.djcxy.com/p/33921.html上一篇: OpenGL fragment shader not writing to fbo color buffer
下一篇: OpenGL: are texture combiner functions supported when rendering to FBO?