Hello, I have a quick question regarding adding a HUD to an
osgViewer::Viewer window.

I had been using the legacy osgUtil SceneView for a while and decided to
make a new app using the new viewer library to get used to it. I had no
problems until I tried to add a HUD camera and found nothing would
display.

Following the example code, my scene graph looked something like:

Viewer (has my primary camera)
 + Root node (set using setSceneData)
   + Actual scene contents
   + Orthographic HUD camera
     + HUD contents

I also tried:
Setting my HUD camera as a slave camera and added the HUD contents as it's
child.
Setting both cameras as slave cameras and adding their respective contents
as children.

Nothing seems to work.
Any help would be appreciated. Please be gentle, this is my first time
using the Viewer library.

Here is the code I used to set up the viewer and cameras:

osg::ref_ptr<osg::GraphicsContext> graphicsContext;
  {
    osg::ref_ptr<osg::GraphicsContext::Traits> graphicsContextTraits =
        new osg::GraphicsContext::Traits();
    graphicsContextTraits->x = windowPosition.x();
    graphicsContextTraits->y = windowPosition.y();
    graphicsContextTraits->width = windowWidth;
    graphicsContextTraits->height = windowHeight;
    graphicsContextTraits->windowDecoration = true;
    graphicsContextTraits->doubleBuffer = true;
    graphicsContextTraits->sharedContext = 0;
    graphicsContext =
osg::GraphicsContext::createGraphicsContext(graphicsContextTraits.get());
  }

  osg::ref_ptr<osg::Camera> camera = new osg::Camera;
  {
    camera->setGraphicsContext(graphicsContext.get());
    camera->setViewport(new osg::Viewport(0,0, windowWidth, windowHeight));
    camera->setDrawBuffer(GL_BACK);
    camera->setReadBuffer(GL_BACK);

    osg::Matrixd viewMatrix;
    viewMatrix.makeLookAt(osg::Vec3(CAMERA_LOCATION.x(),
CAMERA_LOCATION.y(), CAMERA_LOCATION.z()),
        osg::Vec3(0.0, 0.0, 0.0), osg::Vec3(0.0, 1.0, 0.0));
    camera->setViewMatrix(viewMatrix);

    osg::Matrixd projectionMatrix;

    //Perspective projection.
    const double perspectiveProjectionYFOV = PERPSECTIVE_PROJECTION_XFOV /
m_windowAspectRatio;
    const double perspectiveProjectionYFOV_degrees = 180.0 *
perspectiveProjectionYFOV / M_PI;
    const double aspectRatioAsExpectedByOSG =
tan(PERPSECTIVE_PROJECTION_XFOV / 2.0) /
        tan(perspectiveProjectionYFOV / 2.0);
    projectionMatrix.makePerspective(perspectiveProjectionYFOV_degrees,
        aspectRatioAsExpectedByOSG, NEAR_CULL_DISTANCE, FAR_CULL_DISTANCE);

    camera->setProjectionMatrix(projectionMatrix);

    camera->addChild(m_rootNode.get());
  }

  osg::ref_ptr<osg::Camera> hudCamera = new osg::Camera;
  {
    hudCamera->setGraphicsContext(graphicsContext.get());
    hudCamera->setViewport(new osg::Viewport(0,0, windowWidth,
windowHeight));
    hudCamera->setDrawBuffer(GL_BACK);
    hudCamera->setReadBuffer(GL_BACK);
    hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
    hudCamera->setRenderOrder(osg::Camera::POST_RENDER);
    hudCamera->setAllowEventFocus(false);

    osg::Matrixd viewMatrix;
    viewMatrix.makeLookAt(osg::Vec3(CAMERA_LOCATION.x(),
CAMERA_LOCATION.y(), CAMERA_LOCATION.z()),
        osg::Vec3(0.0, 0.0, 0.0), osg::Vec3(0.0, 1.0, 0.0));
    hudCamera->setViewMatrix(viewMatrix);

    osg::Matrixd projectionMatrix;

    //Ortho projection
    projectionMatrix.makeOrtho(-1.0, 1.0, -1.0/m_windowAspectRatio,
1.0/m_windowAspectRatio,
        NEAR_CULL_DISTANCE, FAR_CULL_DISTANCE);

    hudCamera->setProjectionMatrix(projectionMatrix);

    osg::ref_ptr<osg::Group> hudRootNode = new osg::Group();
    hudRootNode->addChild(m_sceneSpecificHudRootNode.get());
    hudRootNode->addChild(m_genericHudRootNode.get());
    hudCamera->addChild(hudRootNode.get());
  }

  osg::ref_ptr<osg::Group> rootRoot = new osg::Group();
  rootRoot->addChild(m_rootNode.get());
  rootRoot->addChild(hudCamera.get());

  m_viewer->setSceneData(rootRoot.get());
  m_viewer->setCamera(camera.get());


_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to