[osg-users] Need help with switching between manual and automatic camera movement

2013-04-18 Thread Martin Záleta
Hi,

In my OSG application, I am trying to implement a functionality, that would 
allow me to switch between usual manual movement and an automatic one (such as 
spinning on spot), but I haven't had any success yet.

My last attempt was to set an update callback to the camera, that would spin it 
around, such as this:


Code:
void CameraCallback::operator()(Node* node, NodeVisitor* nv)
{
Camera * cam = static_castCamera *(node);

if(cam  Settings::Instantiate()-getCameraMode() == CAMERA_ROTATE)
{
Matrixd oldMatrix = cam-getViewMatrix();

Matrixd cameraRotation;
cameraRotation.makeRotate(oldMatrix.getRotate());
cameraRotation.rotate(DegreesToRadians(5.0), 0, 0, 1);

Matrixd cameraTrans;
cameraTrans.makeTranslate(oldMatrix.getTrans());

Matrixd newMatrix = cameraRotation * cameraTrans;
cam-setViewMatrix(newMatrix);
}

traverse(node, nv);
}



... and then I had the viewer object registered by the class that handles 
keyboard events and tried to update it accordingly:


Code:
void registerViewer(Viewer* v)  { viewer = v; man = 
viewer-getCameraManipulator(); }

bool KeyboardEventHandler::handle(const GUIEventAdapter ea, GUIActionAdapter 
aa)
{
...
case 'n':
case 'N':
Settings::Instantiate()-ToggleCamera();

if(Settings::Instantiate()-getCameraMode() == CAMERA_MANUAL)
{
viewer-setCameraManipulator(man);
viewer-getCamera()-setAllowEventFocus(true);
}
else
{
viewer-setCameraManipulator(NULL);
viewer-getCamera()-setAllowEventFocus(false);
}

break;
...
}



Hopefully these snippets are enough for someone to explain me, what I am doing 
wrong and how to do it the right way.

Thank you!

Cheers,
Martin

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





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


Re: [osg-users] Need help with switching between manual and automatic camera movement

2013-04-18 Thread Trajce Nikolov NICK
Hi Martin,

what are you experiencing, where you see the problems? It is sort of not
stated in your email. in your first code snipped in the update I would do
this to have the matrices in the world space:

Matrixd oldMatrix = cam-getInverseViewMatrix();

.
.
 cam-setViewMatrix(osg::Matrixd::inverse(newMatrix));


This should setup your matrices correctly. Also once the matrixes are in
world space then you can easelly transition from automatic to manual state
by setting the camera manipulator by matrix. I think when you add and
remove camera manipulators, it goes to your home position of the
manipulator (see CameraManipulator::setHomePosition), so you have to take
into account this as well. If you want to spin it around between two
rotations try using quaternions (from the matrix, it is getRotate()) and
slerp (quaternion method) between two different orientation. These above is
just guessing since you havn't stated your problem. Maybe the best would be
if you put your whole code into sample and post it here, someone might have
time to look and help

Nick


On Thu, Apr 18, 2013 at 6:53 PM, Martin Záleta zal...@atlas.sk wrote:

 Hi,

 In my OSG application, I am trying to implement a functionality, that
 would allow me to switch between usual manual movement and an automatic one
 (such as spinning on spot), but I haven't had any success yet.

 My last attempt was to set an update callback to the camera, that would
 spin it around, such as this:


 Code:
 void CameraCallback::operator()(Node* node, NodeVisitor* nv)
 {
 Camera * cam = static_castCamera *(node);

 if(cam  Settings::Instantiate()-getCameraMode() ==
 CAMERA_ROTATE)
 {
 Matrixd oldMatrix = cam-getViewMatrix();

 Matrixd cameraRotation;
 cameraRotation.makeRotate(oldMatrix.getRotate());
 cameraRotation.rotate(DegreesToRadians(5.0), 0, 0, 1);

 Matrixd cameraTrans;
 cameraTrans.makeTranslate(oldMatrix.getTrans());

 Matrixd newMatrix = cameraRotation * cameraTrans;
 cam-setViewMatrix(newMatrix);
 }

 traverse(node, nv);
 }



 ... and then I had the viewer object registered by the class that handles
 keyboard events and tried to update it accordingly:


 Code:
 void registerViewer(Viewer* v)  { viewer = v; man =
 viewer-getCameraManipulator(); }

 bool KeyboardEventHandler::handle(const GUIEventAdapter ea,
 GUIActionAdapter aa)
 {
 ...
 case 'n':
 case 'N':
 Settings::Instantiate()-ToggleCamera();

 if(Settings::Instantiate()-getCameraMode() == CAMERA_MANUAL)
 {
 viewer-setCameraManipulator(man);
 viewer-getCamera()-setAllowEventFocus(true);
 }
 else
 {
 viewer-setCameraManipulator(NULL);
 viewer-getCamera()-setAllowEventFocus(false);
 }

 break;
 ...
 }



 Hopefully these snippets are enough for someone to explain me, what I am
 doing wrong and how to do it the right way.

 Thank you!

 Cheers,
 Martin

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





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




-- 
trajce nikolov nick
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] Need help with switching between manual and automatic camera movement

2013-04-18 Thread Martin Záleta
Hi,

I guess I kinda forgot to describe my problem in detail. In part it was because 
I was hoping, that someone would suggest me a better solution to this problem, 
since registering viewer handle in a keyboard event handler doesn't really seem 
to me like much of a good solution, but it was the best I could come up with.

Anyway, what you wrote has helped me resolve the issue I had, so at least it's 
working somehow.

Thank you!

Cheers,
Martin

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





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