我试图在我的 Qt 程序中将 OSG 场景渲染为图像。参考 SnapImageDrawCallback 的例子(https://www.mail-archive.com/osg-users@lists.openscenegraph.org/msg45360.html)。
class SnapImageDrawCallback : public osg::CameraNode::DrawCallback {
public:
SnapImageDrawCallback()
{
_snapImageOnNextFrame = false;
}
void setFileName(const std::string& filename) { _filename = filename; }
const std::string& getFileName() const { return _filename; }
void setSnapImageOnNextFrame(bool flag) { _snapImageOnNextFrame = flag;}
bool getSnapImageOnNextFrame() const { return _snapImageOnNextFrame; }
virtual void operator () (const osg::CameraNode& camera) const
{
if (!_snapImageOnNextFrame) return;
int x,y,width,height;
x = camera.getViewport()->x();
y = camera.getViewport()->y();
width = camera.getViewport()->width();
height = camera.getViewport()->height();
osg::ref_ptr<osg::Image> image = new osg::Image;
image->readPixels(x,y,width,height,GL_RGB,GL_UNSIGNED_BYTE);
if (osgDB::writeImageFile(*image,_filename))
{
std::cout << "Saved screen image to `"<<_filename
<<"`"<< std::endl;
}
_snapImageOnNextFrame = false;
}
protected:
std::string _filename;
mutable bool _snapImageOnNextFrame;
};
我将此设置为 osg::Viewer 的相机的 FinalDrawCallback,但我因空白图像而失败,并在调用图像时收到此警告“警告:在 State::apply() 开始时检测到 OpenGL 错误'无效操作'”-> readPixels,我的 osgViewer::Viewer 嵌入在 QQuickFramebufferObject 中。任何人都可以提供一些建议吗?