5

Writing a small openscenegraph application, and in need of a way to change the Camera height. Essentially, the eye is looking straight at a Ball in the space. What I want to do is be able to lower the Camera height so I am able at the ball from below, and also raise the camera height if I need to. How do I achieve this either with oPengl code or OpenScenegraph?

int main(int argc, char* argv[])
{ 
    osg::ref_ptr<osg::ShapeDrawable> shape2 = new osg::ShapeDrawable; 
    shape2->setShape( new osg::Sphere(osg::Vec3(3.0f, 0.0f, 0.0f),1.0f) ); 
    shape2->setColor( osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) ); 
    osg::ref_ptr<osg::Geode> root = new osg::Geode; 
    root->addDrawable( shape2.get() );///add first osgshapeDrawable2  
    osgViewer::Viewer viewer;
    viewer.setSceneData( root.get() );///set the Geode as scenedata for the viewer
    return viewer.run();
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Kobojunkie
  • 6,375
  • 31
  • 109
  • 164

2 Answers2

2

You need to take over control of the osgViewer::Camera, you should not try to do this with basic OpenGL.

osgViewer::Viewer::getCameraWithFocus should get you the Camera. From here you can set the position and lookat of the Camera.

Keep in mind, in a basic app like you have there, the Camera Manipulator is setting the position of the camera (based on mouse interaction) once per frame.

You will need to decide how you want to deal with mouse input and possibly take over the task the Camera Manipulator is doing.

emartel
  • 7,712
  • 1
  • 30
  • 58
XenonofArcticus
  • 573
  • 2
  • 7
  • I have a manipulator and have the camera. What is left is changing the height itself. How do I achieve this? What property of the camera do I modify to move it up or down in this case? – Kobojunkie Dec 20 '12 at 02:00
  • osgViewer::Viewer::getCameraWithFocus does not exist. – Richard Jessop Jan 23 '20 at 02:15
0

Use the setViewMatrixAsLookAt method (link) on your Camera object. This method takes three parameters:

  • eye: The position of your camera - this you can use to set its height.
  • center: The point your camera is looking at - set this to the center of the observed object.
  • up: The up-vector of your camera - this controls how your viewport will be rotated about its center and should be equal to [0, 1, 0] in a conventional graphics coordinate system.

Because this method takes care of the rotation of the camera by specifying the point at which it's looking, you should only modify the y-coordinate of the eye vector to accomplish what you're asking for.

You can see more details on the 'look at' vector triplet here.

Zoltán
  • 21,321
  • 14
  • 93
  • 134