OpenGL Framebuffer - 渲染到纹理可以创建具有主框架内容的纹理

时间:2016-08-02 19:40:52

标签: c++ opengl framebuffer

这基本上是大项目的一部分,我用以下方式创建具有颜色,深度和模板缓冲区的帧缓冲区:

// Create texture
glGenTextures(1, &m_textureInput);
glBindTexture(GL_TEXTURE_2D, m_textureInput);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, getWidth(), getHeight(), 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
// Create FBOs
glGenFramebuffers(1, &m_fboInput);

glBindFramebuffer(GL_FRAMEBUFFER, m_fboInput);

// Attach texture to it
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_textureInput, 0);

// Render buffer, depth with stencil
glGenRenderbuffers(1, &m_rbDepthStencilInput);
glBindRenderbuffer(GL_RENDERBUFFER, m_rbDepthStencilInput);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, getWidth(), getHeight());
//Attach depth buffer to FBO
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_rbDepthStencilInput);
//Also attach as a stencil
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_rbDepthStencilInput);

status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
{
    // Restore current framebuffer
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    return false;
}

glBindFramebuffer(GL_FRAMEBUFFER, 0);

我用它绑定它来渲染它:

glBindFramebuffer(GL_FRAMEBUFFER, m_fboInput);
// render
glBindFramebuffer(GL_FRAMEBUFFER, 0);

当我使用以下内容转储附加纹理时

glBindTexture(GL_TEXTURE_2D, m_textureInput);
long imageSize = x * y * 4;
unsigned char *data = new unsigned char[imageSize];
glReadPixels(0,0,x,y, GL_BGRA,GL_UNSIGNED_BYTE,data);

我收到主帧缓冲区(0)的内容。

知道我错了什么? 提前致谢。

1 个答案:

答案 0 :(得分:4)

glReadPixels docs说它从帧缓冲区读取数据。因为你以前把它绑定到零,我的猜测就是它从默认的帧缓冲区中读取。

你可以绑定你的帧缓冲对象,然后用GL_COLOR_ATTACHMENT0调用glReadBuffer然后再做glReadPixels,或者更直接地使用glGetTexImage直接读取纹理。