glReadPixels始终返回黑色图像

时间:2016-12-23 02:46:37

标签: image opengl get lazarus glreadpixels

有些时候我编写了一个代码,用于在Delphi RAD Studio XE7中将一个OpenGL场景绘制成一个位图,效果很好。此代码绘制并完成一个场景,然后使用glReadPixels函数获取像素。我最近尝试在Lazarus上编译完全相同的代码,但是我只得到一个黑色图像。

这是代码

// create main render buffer
glGenFramebuffers(1, @m_OverlayFrameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, m_OverlayFrameBuffer);

// create and link color buffer to render to
glGenRenderbuffers(1, @m_OverlayRenderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_OverlayRenderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                          GL_COLOR_ATTACHMENT0,
                          GL_RENDERBUFFER,
                          m_OverlayRenderBuffer);

// create and link depth buffer to use
glGenRenderbuffers(1, @m_OverlayDepthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_OverlayDepthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                          GL_DEPTH_ATTACHMENT,
                          GL_RENDERBUFFER,
                          m_OverlayDepthBuffer);

// check if render buffers were created correctly and return result
Result := (glCheckFramebufferStatus(GL_FRAMEBUFFER) = GL_FRAMEBUFFER_COMPLETE);

...

// flush OpenGL
glFinish;
glPixelStorei(GL_PACK_ALIGNMENT,   4);
glPixelStorei(GL_PACK_ROW_LENGTH,  0);
glPixelStorei(GL_PACK_SKIP_ROWS,   0);
glPixelStorei(GL_PACK_SKIP_PIXELS, 0);

// create pixels buffer
SetLength(pixels, (m_pOwner.ClientWidth * m_Factor) * (m_pOwner.ClientHeight * m_Factor) * 4);

// is alpha blending or antialiasing enabled?
if (m_Transparent or (m_Factor <> 1)) then
    // notify that pixels will be read from color buffer
    glReadBuffer(GL_COLOR_ATTACHMENT0);

// copy scene from OpenGL to pixels buffer
glReadPixels(0,
             0,
             m_pOwner.ClientWidth  * m_Factor,
             m_pOwner.ClientHeight * m_Factor,
             GL_RGBA,
             GL_UNSIGNED_BYTE,
             pixels);

由于我已经验证并且我100%确定在我的场景(也在Lazarus方面)上绘制了某些内容,因此GLext已初始化并且帧缓冲区已正确构建(条件glCheckFramebufferStatus(GL_FRAMEBUFFER) = GL_FRAMEBUFFER_COMPLETE有效地返回true),如果有人能指出我在我的代码中做错了什么,我会非常感激,再次知道在同一台计算机上它在Delphi RAD Studio XE7中运行良好但在Lazarus中运行不正常

此致

0 个答案:

没有答案