如何在运行时在 openscenegraph 上更改背景
How to change background at runtime on openscenegraph
我尝试在查看器上添加 3d object 并动态更改背景。我使用 opencv VideoCapture
.
捕获网络摄像头
我做了以下步骤:
打开视频捕获并获取帧
创建 openscenegraph root
添加一个child到root(从.osg
文件读取3d object)
为背景创建 texture2d object
设置背景图片
创建相机查看背景
将摄像头添加到root
查看器设置数据(viewer.setScenedat(root)
)
运行 viewer.run()
这些步骤将第一帧添加为背景并向场景添加 3d object。但我不能改变每一帧的背景。我该怎么做?
代码:
cv::VideoCapture cap(0);
cv::Mat frame;
if(!cap.isOpened())
{
std::cout << "Webcam cannot open!\n";
return;
}
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Group> root = new osg::Group();
osg::ref_ptr<osg::Texture2D> bg = new osg::Texture2D();
root->addChild(osgDB::readNodeFile("object.osg"));
bg->setFilter(osg::Texture::FilterParameter::MIN_FILTER, osg::Texture::FilterMode::LINEAR);
bg->setFilter(osg::Texture::FilterParameter::MAG_FILTER, osg::Texture::FilterMode::LINEAR);
bg->setDataVariance(osg::Object::DYNAMIC);
cap >> frame;
osg::ref_ptr<osg::Image> osgImage = new osg::Image;
osgImage->setImage(frame.cols, frame.rows, 3,
GL_RGB, GL_RGB, GL_UNSIGNED_BYTE,
(uchar*)(frame.data),
osg::Image::AllocationMode::NO_DELETE, 1);
bg->setImage(osgImage);
osg::ref_ptr<osg::Camera> bg_cam = new osg::Camera();
bg_cam->setProjectionMatrixAsOrtho2D(-0.5, 0.5, -0.5, 0.5);
bg_cam->setViewMatrixAsLookAt(
osg::Vec3(0.5, 0.5, -1.0),
osg::Vec3(0.5, 0.5, 0.0),
osg::Vec3(0.0, -1.0, 0.0)
);
bg_cam->setRenderOrder(osg::Camera::PRE_RENDER);
bg_cam->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
bg_cam->addChild(initializeBackground(bg));
root->addChild(bg_cam);
viewer.setSceneData(root);
viewer.getCamera()->setProjectionMatrixAsPerspective(
40., 1., 1., 100.);
viewer.getCamera()->setClearMask(GL_DEPTH_BUFFER_BIT);
viewer.getCamera()->setClearColor(osg::Vec4(1.0, 0.0, 0.0, 1.0));
viewer.run();
viewer.getCamera()->setClearMask(GL_DEPTH_BUFFER_BIT);
viewer.getCamera()->setClearColor(osg::Vec4(1.0, 0.0, 0.0, 1.0));
bg->setDataVariance(osg::Object::DYNAMIC);
viewer.realize(); // set up windows and associated threads.
while(!viewer.done())
{
cap >> frame;
osg::ref_ptr<osg::Image> osgImage = new osg::Image;
osgImage->setImage(frame.cols, frame.rows, 3,
GL_RGB, GL_BGR, GL_UNSIGNED_BYTE,
(uchar*)(frame.data),
osg::Image::AllocationMode::NO_DELETE, 1);
bg->setImage(osgImage);
viewer.frame();
}
此代码动态更改背景。
我尝试在查看器上添加 3d object 并动态更改背景。我使用 opencv VideoCapture
.
我做了以下步骤:
打开视频捕获并获取帧
创建 openscenegraph root
添加一个child到root(从
.osg
文件读取3d object)为背景创建 texture2d object
设置背景图片
创建相机查看背景
将摄像头添加到root
查看器设置数据(
viewer.setScenedat(root)
)运行
viewer.run()
这些步骤将第一帧添加为背景并向场景添加 3d object。但我不能改变每一帧的背景。我该怎么做?
代码:
cv::VideoCapture cap(0);
cv::Mat frame;
if(!cap.isOpened())
{
std::cout << "Webcam cannot open!\n";
return;
}
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Group> root = new osg::Group();
osg::ref_ptr<osg::Texture2D> bg = new osg::Texture2D();
root->addChild(osgDB::readNodeFile("object.osg"));
bg->setFilter(osg::Texture::FilterParameter::MIN_FILTER, osg::Texture::FilterMode::LINEAR);
bg->setFilter(osg::Texture::FilterParameter::MAG_FILTER, osg::Texture::FilterMode::LINEAR);
bg->setDataVariance(osg::Object::DYNAMIC);
cap >> frame;
osg::ref_ptr<osg::Image> osgImage = new osg::Image;
osgImage->setImage(frame.cols, frame.rows, 3,
GL_RGB, GL_RGB, GL_UNSIGNED_BYTE,
(uchar*)(frame.data),
osg::Image::AllocationMode::NO_DELETE, 1);
bg->setImage(osgImage);
osg::ref_ptr<osg::Camera> bg_cam = new osg::Camera();
bg_cam->setProjectionMatrixAsOrtho2D(-0.5, 0.5, -0.5, 0.5);
bg_cam->setViewMatrixAsLookAt(
osg::Vec3(0.5, 0.5, -1.0),
osg::Vec3(0.5, 0.5, 0.0),
osg::Vec3(0.0, -1.0, 0.0)
);
bg_cam->setRenderOrder(osg::Camera::PRE_RENDER);
bg_cam->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
bg_cam->addChild(initializeBackground(bg));
root->addChild(bg_cam);
viewer.setSceneData(root);
viewer.getCamera()->setProjectionMatrixAsPerspective(
40., 1., 1., 100.);
viewer.getCamera()->setClearMask(GL_DEPTH_BUFFER_BIT);
viewer.getCamera()->setClearColor(osg::Vec4(1.0, 0.0, 0.0, 1.0));
viewer.run();
viewer.getCamera()->setClearMask(GL_DEPTH_BUFFER_BIT);
viewer.getCamera()->setClearColor(osg::Vec4(1.0, 0.0, 0.0, 1.0));
bg->setDataVariance(osg::Object::DYNAMIC);
viewer.realize(); // set up windows and associated threads.
while(!viewer.done())
{
cap >> frame;
osg::ref_ptr<osg::Image> osgImage = new osg::Image;
osgImage->setImage(frame.cols, frame.rows, 3,
GL_RGB, GL_BGR, GL_UNSIGNED_BYTE,
(uchar*)(frame.data),
osg::Image::AllocationMode::NO_DELETE, 1);
bg->setImage(osgImage);
viewer.frame();
}
此代码动态更改背景。