[osg-users] HUD with slave camera PRE_RENDER

263 views
Skip to first unread message

Chris Hidden

unread,
Nov 19, 2014, 4:35:36 AM11/19/14
to osg-...@lists.openscenegraph.org
Hi everyone.

I have a HUD in my application that I want to contain 3d Models with animations. I have taken a look at the osghud example in the build and have a HUD working with a slave camera.

I want the HUD to be behind my main scene object though ( opposite of most HUD implementations ). I can't seem to succeed in doing this. I set the slave camera render implementation to PRE_RENDER and even tried setting my main camera to POST_RENDER.

But no matter what I do my my main scene models always end up behind the HUD. I also tried setting the draw and read buffer to GL_BACK as well but to no avail.

I also realized while doing this that my understanding of how the cameras and view work is quite poor. Can someone recommend a good book or resource that I can start learning 3D scene graph camera and view implementations with? Much appreciated! Thanks!

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61680#61680





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

Robert Osfield

unread,
Nov 19, 2014, 4:49:45 AM11/19/14
to OpenSceneGraph Users
Hi Chris,

The key to managing multiple Camera's that overlay each other is to decide how you want to mange the depth buffer for each of the cameras.  For the setup in your description - a Camera to render the background, then a main camera to render the main scene I'd use a PRE_RENDER for the background as you have done but then you need to set up the Camera for the main scene so that it clears the depth buffer.  The osghud example illustrates clearing of the depth buffer - but it does this for the slave camera, while you want to do this for the main camera.

Robert.

Chris Hidden

unread,
Nov 19, 2014, 5:25:26 AM11/19/14
to osg-...@lists.openscenegraph.org
Thanks Robert.

I tried not setting any clear mask on the slave camera and making sure the depth clear was on my main camera. I haven't got the desired effect yet.

My main camera has these settings:

Camera mainCam = new Camera(*(view->getCamera()));
mainCam->setGraphicsContext(osg::GraphicsContext::createGraphicsContext(traits->get()));
mainCam->setViewMatrixAsLookAt(eye, center, up);
mainCam->setViewport(0, 0, screen->width(), screen->height());
mainCam->setClearColor(clearColor);
mainCam->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
mainCam->setRenderOrder(osg::Camera::POST_RENDER);
mainCam->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);

HUD Camera has these settings:

Camera hudCam = new Camera();
hudCam->setGraphicsContext(osg::GraphicsContext::createGraphicsContext(t->get()));
hudCam->setViewport(0, 0, screen->width(), screen->height());
hudCam->setRenderOrder(osg::Camera::PRE_RENDER);
hudCam->setAllowEventFocus(false);
hudCam->addChild(HUD);
hudCam->addChild(lights->getLight());

I attached a screen shot of what it looks like. The hand is always underneath my HUD. I've tried playing around with a lot of different settings, with the buffer bit and color bit but to no avail :(

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61682#61682



Screenshot 2014-11-19 11.12.33.png

Robert Osfield

unread,
Nov 19, 2014, 5:32:24 AM11/19/14
to OpenSceneGraph Users
Hi Chris,

You code is creating two graphics windows one for each camera, this of course will result in the camera not appearing overlapping on the same window.

Have a look at the osghud example - the context is shared.  For the main camera you should *just* clear the depth buffer, in your code you have the main camera clearing  both the colour and depth buffer.

Robert.

Chris Hidden

unread,
Nov 19, 2014, 6:13:46 AM11/19/14
to osg-...@lists.openscenegraph.org
Ok, I think I understand what was going on. I am now using

osgViewer::Viewer::Windows windows;
viwer->getWindows(windows);

if (windows.empty()) return 1;

hudCam->setGraphicsContext(windows[0]);

However, if I disable the color bit clear and only use the depth bit. The scene never clears my previous frames for my main scene. So I get a paint effect with the hands that move around.

The hands also still remain under the HUD. Im assuming this is what you meant by creating 2 graphics contexts.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61685#61685



Screenshot 2014-11-19 11.49.41.png

Robert Osfield

unread,
Nov 19, 2014, 6:19:45 AM11/19/14
to OpenSceneGraph Users
On 19 November 2014 10:57, Chris Hidden <Ch...@erghis.com> wrote:
Ok, I think I understand what was going on.  I am now using

osgViewer::Viewer::Windows windows;
        viwer->getWindows(windows);

        if (windows.empty()) return 1;

        hudCam->setGraphicsContext(windows[0]);

However, if I disable the color bit clear and only use the depth bit.  The scene never clears my previous frames for my main scene.  So I get a paint effect with the hands that move around.

If the slave Camera is being rendered first then it needs to clear both the depth and colour buffers - it's effectively taking over the role that your main camera originally had.

The hands also still remain under the HUD.  Im assuming this is what you meant by creating 2 graphics contexts.

I meant exactly what I wrote you were creating two graphics contexts.  Normally in the set up you create a single window then share it.  The new code above should achieve this but the osghud example is simpler.

Robert.
 

Chris Hidden

unread,
Nov 19, 2014, 8:38:58 AM11/19/14
to osg-...@lists.openscenegraph.org
Thanks again Robert.

I believe the hudCamera->setGraphicsContext(windows[0]); approach is exactly as its written in the osghud example?

But I have fixed it now. As you said, once I applied both a color and depth clear to the slave camera with pre render and only a depth buffer for the main camera it started working.

If you don't mind I would kindly re ask if there is perhaps a resource or good book you could recommend for learning more about scene view and camera rendering? I can always just google away of course, but recommendations are always great especially if its maybe a resource that is better tailored towards working with OSG. I do already have the cook book and the OSG beginners guide.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61691#61691

Robert Osfield

unread,
Nov 19, 2014, 9:18:39 AM11/19/14
to OpenSceneGraph Users
Hi Chris,

On 19 November 2014 13:43, Chris Hidden <Ch...@erghis.com> wrote:
Thanks again Robert.

I believe the  hudCamera->setGraphicsContext(windows[0]); approach is exactly as its written in the osghud example?

I just checked and it is :-)

Just shows I can't remember everything off the top of my head.  What I was thinking of was manually creating the graphics context as you did in your intro to this thread, but only creating one context and then sharing it between both Camera.  I've just checked and this is what the osgdistortion example does.

But I have fixed it now.  As you said, once I applied both a color and depth clear to the slave camera with pre render and only a depth buffer for the main camera it started working.

Horaahh!!

 
If you don't mind I would kindly re ask if there is perhaps a resource or good book you could recommend for learning more about scene view and camera rendering?  I can always just google away of course, but recommendations are always great especially if its maybe a resource that is better tailored towards working with OSG. I do already have the cook book and the OSG beginners guide.

I'm afraid there aren't any modern OSG books beyond the cook book and the 3.0 beginners book. 

The other resources are the OSG - get used to doing searches for all use a different classes to see how they are used, both in examples and the core implementations - for instance you'll find a range of Camera setup examples that do different things with Cameras, and you'll also find a range of different usages in the src/osgViewer/config directory.

What you won't find much of is examples of how to use osgUtil::SceneView, this class is not one that most users will touch with modern OSG applications, and is only now kept around as an implementation detail.

Robert.

Chris Hidden

unread,
Nov 19, 2014, 10:12:40 AM11/19/14
to osg-...@lists.openscenegraph.org
No worries on the books then. I was more thinking like a good 3D graphics programming book to get my head around the concepts of things. Since I still struggle in understanding some of the basics I believe.

I have one last question now regarding my HUD. I just moved the 3D models of the HUD to their correct position and they are now under my projection. As the 3D models are children of the HUD Im not sure how to bring them above the projection. Right now the projection and 3d models are children of the HUD group.

Would I create another slave camera under the HUDcam for the models? or is there a way I can affect the models draw order within the HUD group?

Cheers!

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61693#61693

Robert Osfield

unread,
Nov 19, 2014, 10:54:25 AM11/19/14
to OpenSceneGraph Users
Hi Chris,

On 19 November 2014 15:17, Chris Hidden <Ch...@erghis.com> wrote:
No worries on the books then.  I was more thinking like a good 3D graphics programming book to get my head around the concepts of things.  Since I still struggle in understanding some of the basics I believe.

I'll defer to others as to the best current books on intro to computer graphics - it's two decades since I learnt GL ;-)

I have one last question now regarding my HUD.  I just moved the 3D models of the HUD to their correct position and they are now under my projection.  As the 3D models are children of the HUD Im not sure how to bring them above the projection.  Right now the projection and 3d models are children of the HUD group.

Would I create another slave camera under the HUDcam for the models? or is there a way I can affect the models draw order within the HUD group?

I can't work out what you mean by "projection" in this context.

Robert.

 

Chris Hidden

unread,
Nov 19, 2014, 11:15:51 AM11/19/14
to osg-...@lists.openscenegraph.org
Ah! Infamously ambiguous as always.

I meant that I have an osg::projection as the "background" for my HUD. I managed to solve it about 2 minutes before you posted it.

Like I mentioned I had two models playing animations on the HUD which is just a green 4 point rectangle geode I create with some opacity.

The models were underneath that geode and I wasn't sure why. Thought it might have something to do with the way an osg::projection was handled.

I fixed by altering its state set. Before I had:

osg::ref_ptr<osg::StateSet> HUDStateSet = new osg::StateSet();
HUDgeode->setStateSet(HUDStateSet);
HUDStateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
HUDStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
HUDStateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
HUDStateSet->setRenderBinDetails(11, "RenderBin");

But I don't want everything to be rendered with transparency. Just the actual Geode.

So I removed the renderingHint and the RenderDetails lines so I only had:

osg::ref_ptr<osg::StateSet> HUDStateSet = new osg::StateSet();
HUDgeode->setStateSet(HUDStateSet);
HUDStateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
HUDStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);

This fixed it and the models are now rendered OVER the transparent geode.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61697#61697

Chris Hidden

unread,
Nov 20, 2014, 9:45:02 AM11/20/14
to osg-...@lists.openscenegraph.org
Ran into a new problem. Its related directly to this HUD so I figured I would just continue this thread.

I want the HUD to slide on and off the screen. I figure the best way to do this would be with a nodecallback attached to the camera. Then i can just move the viewport or the viewmatrix to simulate the HUD moving off screen.

The problem is, is that the moment I add the updateCallback to the camera my models on my hud don't render properly. The two hands disappear and only the sphere remains, as well as its animation never plays.

I found a DrawCallback class as well but its a const operator() method so I can't change the properties of the class how I want to.

Im a bit stuck here. Also I wasn't really sure whether I should be moving the view matrix or view port to get the HUD to move. I tried adding the callback to the osg::Group as well that is under the HUDCam but as part of the HUD is an osg::projection there was no way to easily slide everything at once.

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61740#61740

Christian Buchner

unread,
Nov 20, 2014, 10:05:50 AM11/20/14
to OpenSceneGraph Users

Did the updateCallback you added call traverse() on its own node? If you missed that, you may have accidentially disabled some required functionality.

Christian


Robert Osfield

unread,
Nov 20, 2014, 10:06:22 AM11/20/14
to OpenSceneGraph Users
Hi Chris,

There is too many unknowns for the rest of us to be know what is going wrong with this latest issue - only you have your code and models, and can view what is happening first hand and use a debugger to step through what is happening.

The best we can do is provide general recommendations.

For moving graphics on and off screen you have a range of options - your could alter the Camera view or projection matrix, or even the viewport, or even place a transform above the scene graph that you want to animate, so all of these options are viable.  Which is best is probably down to how you manage things, conceptually I'd say animating things on/off screen is a viewer level operation, so I'd do it at the Camera level rather than a transform within the scene graph.  Personally I'd alter the view matrix, but this is just a general preference.

Robert.

Chris Hidden

unread,
Nov 20, 2014, 10:19:08 AM11/20/14
to osg-...@lists.openscenegraph.org
Ok, I understand. I also initially thought moving the viewmatrix would be the way to go.

I wish I knew what to provide as far as information on my code goes. Ive been testing trying to figure out why my models don't render properly on the HUD. I've narrowed it down to the UpdateCallback. For whatever reason, if I attach an empty UpdateCallback to my slave camera(literally just an empty class that inherits from NodeCallback) then it works.

The moment I add virtual void operator(Node *node, NodeVisitor nv); with an empty implementation it causes the problem. So somehow the operator method is creating or at least triggering the problem.

Would the updateCallback somehow cause a threading issue where the models don't have time to render properly or something along those lines? I just can't make logical sense of why it would do that, much less how to debug it :P

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61745#61745

Christian Buchner

unread,
Nov 20, 2014, 10:56:42 AM11/20/14
to OpenSceneGraph Users

> The moment I add virtual void operator(Node *node, NodeVisitor nv); with
> an empty implementation it causes the problem.  So somehow the
> operator method is creating or at least triggering the problem.

what I just said. Don't make it empty.

Add at least traverse(node, nv); to the implementation.

Christian

Chris Hidden

unread,
Nov 20, 2014, 11:10:18 AM11/20/14
to osg-...@lists.openscenegraph.org
:-* :-*

And this is what happens when you don't sleep enough.

sorry...

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61748#61748
Reply all
Reply to author
Forward
0 new messages