【问题标题】:FBOs render to texture and glReadPixelsFBO 渲染到纹理和 glReadPixels
【发布时间】:2010-10-21 14:12:20
【问题描述】:

我已经设置了 FBO(GL_FRAMEBUFFER_EXT、颜色和深度缓冲区),并且我的应用正在使用 NULL 图像将简单的 OpenGL 3D 对象(旋转茶壶)渲染为纹理。我正在尝试使用 glReadPixels 捕获像素数据。我在屏幕上正确地看到了 3D 对象。但是 glReadPixels 给出了垃圾数据。我想知道我哪里出错了。任何帮助/指针表示赞赏。

   Code is given below (in brief) -

    glEnable(GL_TEXTURE_2D); // Enable texturing so we can bind our frame buffer texture
    glEnable(GL_DEPTH_TEST); // Enable depth testing

    // Initialize the FBO
    int WIDTH=window_width;
    int HEIGHT=window_height;

    glGenTextures(1, &fbo_texture); // Generate one texture
    glBindTexture(GL_TEXTURE_2D, fbo_texture); // Bind the texture fbo_texture

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, window_width, window_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); // Create a standard texture with the width and height of our window

        // Setup the basic texture parameters
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(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);

   ......
   ......
   // setup depth buffer
   glGenRenderbuffersEXT(1, &fbo_depth); // Generate one render buffer and store the ID in fbo_depth
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo_depth); // Bind the fbo_depth render buffer

glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, window_width, window_height); // Set the render buffer storage to be a depth component, with a width and height of the window

glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fbo_depth); // Set the render buffer of this buffer to the depth buffer

   ....
   .....
  //set up FBO
  glGenFramebuffersEXT(1, &fbo); // Generate one frame buffer and store the ID in fbo
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); // Bind our frame buffer

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, fbo_texture, 0); // Attach the texture fbo_texture to the color buffer in our frame buffer

glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fbo_depth); // Attach the depth buffer fbo_depth to our frame buffer

GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); // Check that status of our generated frame buffer

if (status != GL_FRAMEBUFFER_COMPLETE_EXT) // If the frame buffer does not report back as complete
{
    std::cout << "Couldn't create frame buffer" << std::endl; // Output an error to the console
    exit(0); // Exit the application
}


    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); // Bind our frame buffer for rendering
    glPushAttrib(GL_VIEWPORT_BIT | GL_ENABLE_BIT); // Push our glEnable and glViewport states
    glViewport(0, 0, window_width, window_height); // Set the size of the frame buffer view port

    glClearColor (0.0f, 0.0f, 1.0f, 1.0f); // Set the clear colour
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the depth and colour buffers
    glLoadIdentity(); // Reset the modelview matrix

    glTranslatef(0.0f, 0.0f, -5.0f); // Translate back 5 units

    glRotatef(rotation_degree, 1.0f, 1.0f, 0.0f); // Rotate according to our rotation_degree value

    glutSolidTeapot(1.0f); // Render a teapot
    glPopAttrib(); // Restore our glEnable and glViewport states

    glReadPixels(0, 0, window_width, window_height, GL_RGBA, GL_UNSIGNED_BYTE, pixels[i]);

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // Unbind our texture

    //now display

    ....
    ....

    glutSwapBuffers();
    }

【问题讨论】:

    标签: opengl


    【解决方案1】:

    您需要先绑定您的 FBO,然后调用 glFramebufferTexture2D 和其他 FBO 调用。

    同样为避免此类问题,您需要使用 glCheckFramebufferStatus 检查帧缓冲区状态,首先绑定 FBO,它会告诉您帧缓冲区是否完整并准备好渲染。

    另外,如果你绘制到一个纹理,你可以使用 glGetTexImage 获取纹理数据。

    【讨论】:

    • 我正在绑定 FBO。正如我所说,我在屏幕上看到了 3D 对象,它工作正常。我只是无法让 glReadPixels 工作。
    • 绑定贴图,解绑FBO。
    • glGetTexImage 成功,但所有像素都设置为 ff 00 00, 00 ff 00, 00 00 ff 并重复。
    【解决方案2】:

    您正在传递像素[i]。看起来很可疑。像素被声明为什么?从你发的内容我看不出来。我最希望看到类似

    glReadPixels(0, 0, window_width, window_height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

    或

    glReadPixels(0, 0, window_width, window_height, GL_RGBA, GL_UNSIGNED_BYTE, &amp;pixels[0]);

    但这一切都取决于您对像素数组的意图。

    【讨论】:

      猜你喜欢
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多