title: How to change background at runtime on openscenegraph
body: <p>I try to add an 3d object on viewer and change background dynamically. I capture webcam using opencv <code>VideoCapture</code>.</p>
  <p>I did below steps :</p>
  <ol>
  <li><p>Open video capture and get frame</p>
  </li>
  <li><p>Create openscenegraph root</p>
  </li>
  <li><p>Add a child to root ( read from <code>.osg</code> file 3d object)</p>
  </li>
  <li><p>Create a texture2d object for background</p>
  </li>
  <li><p>Set image of background</p>
  </li>
  <li><p>Create a camera to view background</p>
  </li>
  <li><p>Add camera to root</p>
  </li>
  <li><p>Set data of viewer (<code>viewer.setScenedat(root)</code>)</p>
  </li>
  <li><p>Run <code>viewer.run()</code></p>
  <p>These steps add first frame as background and add a 3d object to scene. But I can't change background each frame. How can I do it?</p>
  </li>
  </ol>
  <p>Code :</p>
  <pre><code>cv::VideoCapture cap(0);  
  cv::Mat frame;
  
  if(!cap.isOpened())  
  {
      std::cout &lt;&lt; &quot;Webcam cannot open!\n&quot;;
      return;
  }  
  
  osgViewer::Viewer viewer;
  osg::ref_ptr&lt;osg::Group&gt; root = new osg::Group();
  osg::ref_ptr&lt;osg::Texture2D&gt; bg = new osg::Texture2D();
  
  root-&gt;addChild(osgDB::readNodeFile(&quot;object.osg&quot;));
  bg-&gt;setFilter(osg::Texture::FilterParameter::MIN_FILTER, osg::Texture::FilterMode::LINEAR);
  bg-&gt;setFilter(osg::Texture::FilterParameter::MAG_FILTER, osg::Texture::FilterMode::LINEAR);    
  bg-&gt;setDataVariance(osg::Object::DYNAMIC);
  
  cap &gt;&gt; frame;
  osg::ref_ptr&lt;osg::Image&gt; osgImage = new osg::Image;
  osgImage-&gt;setImage(frame.cols, frame.rows, 3,
      GL_RGB, GL_RGB, GL_UNSIGNED_BYTE,
      (uchar*)(frame.data),
      osg::Image::AllocationMode::NO_DELETE, 1);
  bg-&gt;setImage(osgImage);
  
  osg::ref_ptr&lt;osg::Camera&gt; bg_cam = new osg::Camera();
  bg_cam-&gt;setProjectionMatrixAsOrtho2D(-0.5, 0.5, -0.5, 0.5);
  bg_cam-&gt;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-&gt;setRenderOrder(osg::Camera::PRE_RENDER);
  bg_cam-&gt;setReferenceFrame(osg::Camera::ABSOLUTE_RF);
  bg_cam-&gt;addChild(initializeBackground(bg));
  root-&gt;addChild(bg_cam);
  
  viewer.setSceneData(root);
  viewer.getCamera()-&gt;setProjectionMatrixAsPerspective(
      40., 1., 1., 100.);
  viewer.getCamera()-&gt;setClearMask(GL_DEPTH_BUFFER_BIT);
  viewer.getCamera()-&gt;setClearColor(osg::Vec4(1.0, 0.0, 0.0, 1.0));
    
  viewer.run();
  </code></pre>