[osg-users] Hard time with slave cameras and viewports.

124 views
Skip to first unread message

Andrés Barrionuevo

unread,
Jul 24, 2014, 5:53:14 AM7/24/14
to osg-...@lists.openscenegraph.org
Hi,

I've been able to setup a HUD via a slave camera in my app to draw a cross that would always face the user. Since I wanted that cross to be able to "move" across all the scene, I did the setup of the slave as the following:


Code:

osg::ref_ptr<osg::Camera> hud = new osg::Camera;
hud->setCullMask( HUD_CAM );
hud->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
hud->setClearMask( GL_DEPTH_BUFFER_BIT /*| GL_COLOR_BUFFER_BIT*/ );
hud->setRenderOrder( osg::Camera::POST_RENDER );
hud->setAllowEventFocus( false );

osgViewer::Viewer::Windows windows;
getWindows( windows );
hud->setGraphicsContext( windows.front() );
hud->setClearColor( osg::Vec4d( 0.0, 1.0, 0.0, 1.0 ) );
hud->getOrCreateStateSet()->setMode( GL_DEPTH, osg::StateAttribute::OFF );


Please, correct me if I'm wrong: it's my understanding that if I use the same graphics context as the master camera, the slave camera's scene will be rendered in the same "window" as the main camera, whereas if I use a new graphics context, a new window will appear with the slave's scene (I tried this with the Cookbook power wall example).

Now, for the projection matrix:


Code:

osg::observer_ptr<osg::Camera> cam = viewer.getCamera();
osg::observer_ptr<osg::Viewport> vport = cam->getViewport();
hud->setViewport( vport.get() );

hud->setProjectionMatrix( osg::Matrix::ortho2D( vport->x(), vport->width(), vport->y(), vport->height() ) );


Specifying the same viewport as the master camera, I was able to move the cross all around the scene. I tried to specify a window, let's say:

Code:
setViewport( 0, 0, 300, 300 )

and the projection matrix:

Code:
setProjectionMatrix( osg::Matrix::ortho2D( 0, 300, 0, 300 ) );

This will make the cross appear only in that little window.

So!

I thought of adding the axes to the scene (as stated in my other post (forum.openscenegraph.org/viewtopic.php?t=14088)). I want the axes to appear in a corner of the scene and to update them when I rotate the main scene. I don't want the axes to be zoomed or panned.


Code:

osg::ref_ptr<osg::Camera> slave = new osg::Camera;
slave->setGraphicsContext( viewer.getCamera()->getGraphicsContext() );
slave->setViewport( 0, 0, 300, 300 );
slave->setClearColor( osg::Vec4d(0.0,1.0,0.0,0.5) );
slave->setReferenceFrame( osg::Camera::ABSOLUTE_RF );


What I'm not sure is:


1) Should the slave's render mode be PRE or POST?

2) How should I specify the projection matrix for this camera? I'm having a hard time to do this for the 3D axes.

3) Playing a bit with the viewport in 2D, I realized that if I move the master camera's child near the slave's "area", once ir crosses the "border" between them, the main model will disappear. If I want that everything that is rendered by the main camera to be able to appear as well in the slave's viewport when panning, what would the cull mask in this case?


NOTE: Both the master and slave cameras have their own cull mask. The nodes I add to the scene also have their node masks set accordingly to the camera that will host them.
...

Thank you!

Cheers,
Andrés

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

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

Bram Vaessen

unread,
Jul 25, 2014, 4:20:24 AM7/25/14
to osg-...@lists.openscenegraph.org
I'm not a huge expert, but:

1. the slave should be post, because you want to render the cross/axes on top of the rest.
2. you can just use a perspective projection if you want a 3d looking axis or a orthogonal view if you want a 'less' 3d look to it. Be sure to set the near and far correct, I think a orthogonal might standard be limited to rendering z=0 only?
3. I think this is because you use a clear color on the slave cam. That means that each frame it will first clear the viewport (therefor your main model doesn't show it that area). For the axis: don't use a clear color, but instead use setClearMask to clear the zbuffer (or else it might use the zvalues from your main scene in the viewport and that doesn't make sense). Then it just should draw the axis on whatever you rendered in your main scene.

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

Andrés Barrionuevo

unread,
Jul 28, 2014, 12:07:02 PM7/28/14
to osg-...@lists.openscenegraph.org
Hi STTrife (),

Yeah, I also thought of the POST_RENDER camera, but I wanted another opinion :D

About the cull masks, I switched them off. I thought it would be more efficient using them, but that's not my main concern right now. The clear color was a just a test to see what the specified viewport was. I forgot to remove it from the posted code. Sorry.

Okay, about the perspective. I think the problem is my poor understanding of the math involved. I will have to start again with the theory.

Also, using an ABSOLUTE reference frame, the axis appear but with too much zoom; almost all of the viewport is filled with the red axis. And I think the callback is not working properly here, as I can see no change in the axis (with the compositeviewer example it worked, though).

Changind the RF to RELATIVE, the axes appear, but very very small. I have to zoom them in to get a suitable size. But I don't want to have any events in that viewport.

So, as I said before, I'll check the theory again. Besides the OpenGL Red Book, do you know of any link or book that may be useful?

...

Thank you!

Cheers,
Andrés

------------------
Read this topic online here:

http://forum.openscenegraph.org/viewtopic.php?p=60493#60493

Bram Vaessen

unread,
Jul 28, 2014, 2:19:03 PM7/28/14
to osg-...@lists.openscenegraph.org
a quote from Robert: "ABSOLTE_RF means that the when the cull traversal encounters a Transform node it ignores the inherited modelview matrix so that the local transform matrix on the Transform node becomes the new modelview matrix. This means that the Transform nodes subgraph is placed in new coordinate frame rather than that of it's parents. "

This is probably what you want. This way your camera is not affected by any other nodes (it sort of resets the view-matrix) There are probably some situations where it might be useful to go for the relative reference frame, but in your case it isn't.

about orthogonal and perspective, here is nice explanation, look at the pictures too.

http://blender.stackexchange.com/questions/648/what-are-the-differences-between-orthographic-and-perspective-views

basically orthogonal camera renders everything in a box, looking exactly from one side, without perspective correction (so stuff that's far away is not any smaller than stuff closer to the camera).
In perspective there is a correction for depth, like in real life, where stuff in the distance looks smaller.

For these axis of yours, I think perspective might look slightly better, specially if they have anything 3d to the look of them (if they are just 3 drawn lines, then it might be hard to see the perspective...

In the case of orthogonal you set it to render a 300 by 300 area with setProjectionMatrix( osg::Matrix::ortho2D( 0, 300, 0, 300 ) ); , so to make sure the axes are rendered at the correct size, just make the axis the correct size (or pick a larger box to render)
you set the viewport the same size as the orthogonal rendering box, so then it quite literary translates; if you draw a line from (0,0,0) to (10,10,0) you will see a line from (0,0) to (10,10) on your screen.

if you go for perspective, you can also just choose re-size the axes to your liking, but you can also move the camera further away, because there is perspective correction. I personally use the camera->setViewMatrixAsLookAt(eye,center,up) to set up and move my camera the way I want. In my world z = up, so the up vector is always (0,0,1). The eye is the position of the camera, and the center is the place where you want to point your camera at.

I don't really have a good book that I can recommend you, sorry, but there must be some good ones out there I guess.

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

Andrés Barrionuevo

unread,
Jul 31, 2014, 12:25:44 PM7/31/14
to osg-...@lists.openscenegraph.org
Hi STTrife,

thanks for your help. I'll check the info you gave me!
...

Thank you!

Cheers,
Andrés

------------------
Read this topic online here:

http://forum.openscenegraph.org/viewtopic.php?p=60520#60520

Reply all
Reply to author
Forward
0 new messages