Changes to camera parameters in OSG

Source: Internet
Author: User
Tags addchild readfile

[CPP]View PlainCopy
  1. #pragma comment (lib, "Osg.lib")
  2. #pragma comment (lib, "OsgDB.lib")
  3. #pragma comment (lib, "OsgViewer.lib")
  4. #include "Osgviewer/viewer"
  5. #include "Osgdb/readfile"
  6. #include "Osg/node"
  7. #include "Osg/shape"
  8. #include "Osg/geode"
  9. #include "osg/shapedrawable"
  10. int main () {
  11. //Initialize the scene viewer
  12. osg::ref_ptr<osgviewer::viewer> viewer=New Osgviewer::viewer;
  13. //Initialize the scene root node
  14. osg::ref_ptr<osg::group> root=New Osg::group;
  15. //Scene data
  16. Osg::ref_ptr<osg::node> node=osgdb::readnodefile ("GLIDER.OSG");
  17. Root->addchild (node);
  18. //Add scene data to the Vista
  19. Viewer->setscenedata (root);
  20. //Get the camera default parameter settings
  21. Osg::vec3d Eye,center,up;
  22. Viewer->getcamera ()->getviewmatrixaslookat (eye,center,up);
  23. //Print out the camera parameters
  24. printf ("init Eye:%f,%f,%f\n", eye._v[0],eye._v[1],eye._v[2]);
  25. printf ("Init Center:%f,%f,%f\n", center._v[0],center._v[1],center._v[2]);
  26. printf ("init up:%f,%f,%f\n", up._v[0],up._v[1],up._v[2]);
  27. //Modify camera Parameters
  28. Eye=osg::vec3d (0.0,-10.0,0.0);
  29. Center=osg::vec3d (0.0,0.0,0.0);
  30. Up=osg::vec3d (0.0,0.0,1.0);
  31. //Set the parameters to the camera and get the camera parameters immediately
  32. Viewer->getcamera ()->setviewmatrixaslookat (eye,center,up);
  33. Viewer->getcamera ()->getviewmatrixaslookat (eye,center,up);
  34. //Print out the parameters
  35. printf ("Eye:%f,%f,%f new\n", eye._v[0],eye._v[1],eye._v[2]);
  36. printf ("Center:%f,%f,%f new\n", center._v[0],center._v[1],center._v[2]);
  37. printf ("Up:%f,%f,%f new\n", up._v[0],up._v[1],up._v[2]);
  38. //simulation loop, take care not to use Viewer->run (), if you use this parameter, all the changes on the camera above will be invalid
  39. While (!viewer->done ())
  40. {
  41. Viewer->frame ();
  42. }
  43. return 1;
  44. }


Results

The above code shows that the initial camera parameters are:

This parameter is not visible by default because the default coordinate system in OSG is this:

This way, if the eye is at the origin, center is not visible in the case of the z-axis negative half axis, so if you do not manually set the parameters you cannot see things. The code that sets the camera position is commented out as follows:

[CPP]View PlainCopy
  1. #pragma comment (lib, "Osg.lib")
  2. #pragma comment (lib, "OsgDB.lib")
  3. #pragma comment (lib, "OsgViewer.lib")
  4. #include "Osgviewer/viewer"
  5. #include "Osgdb/readfile"
  6. #include "Osg/node"
  7. #include "Osg/shape"
  8. #include "Osg/geode"
  9. #include "osg/shapedrawable"
  10. int main () {
  11. //Initialize the scene viewer
  12. osg::ref_ptr<osgviewer::viewer> viewer=New Osgviewer::viewer;
  13. //Initialize the scene root node
  14. osg::ref_ptr<osg::group> root=New Osg::group;
  15. //Scene data
  16. Osg::ref_ptr<osg::node> node=osgdb::readnodefile ("GLIDER.OSG");
  17. Root->addchild (node);
  18. //Add scene data to the Vista
  19. Viewer->setscenedata (root);
  20. //Get the camera default parameter settings
  21. Osg::vec3d Eye,center,up;
  22. Viewer->getcamera ()->getviewmatrixaslookat (eye,center,up);
  23. //Print out the camera parameters
  24. printf ("init Eye:%f,%f,%f\n", eye._v[0],eye._v[1],eye._v[2]);
  25. printf ("Init Center:%f,%f,%f\n", center._v[0],center._v[1],center._v[2]);
  26. printf ("init up:%f,%f,%f\n", up._v[0],up._v[1],up._v[2]);
  27. ////Modifying camera parameters
  28. //eye=osg::vec3d (0.0,-10.0,0.0);
  29. //center=osg::vec3d (0.0,0.0,0.0);
  30. //up=osg::vec3d (0.0,0.0,1.0);
  31. ////set parameters to the camera and get the camera parameters immediately
  32. //viewer->getcamera ()->setviewmatrixaslookat (eye,center,up);
  33. //viewer->getcamera ()->getviewmatrixaslookat (eye,center,up);
  34. ////to print out parameters
  35. //printf ("Eye:%f,%f,%f new\n", eye._v[0],eye._v[1],eye._v[2]);
  36. //printf ("Center:%f,%f,%f new\n", center._v[0],center._v[1],center._v[2]);
  37. //printf ("Up:%f,%f,%f new\n", up._v[0],up._v[1],up._v[2]);
  38. //simulation loop, take care not to use Viewer->run (), if you use this parameter, all the changes on the camera above will be invalid
  39. While (!viewer->done ())
  40. {
  41. Viewer->frame ();
  42. }
  43. return 1;
  44. }



In this case the effect is like, nothing is seen:

After using the following set of test data:

[CPP]View PlainCopy
    1. modifying camera parameters
    2. Eye=osg::vec3d ( -10.0,0.0,0.0);
    3. Center=osg::vec3d (0.0,0.0,0.0);
    4. Up=osg::vec3d (0.0,0.0,1.0);


That is, from the x-axis negative half-axis, see should be the front of the aircraft, the effect such as:

Normal. In this case, there is no bots in the scene, there is no default bots this argument, if you need to implement the scene roaming, you can add a robot.

Note: If used directly:

[CPP]View PlainCopy
    1. Viewer->run ();

How to modify the camera parameters is not valid,
Even if you add

[CPP]View PlainCopy
    1. Viewer->setcameramanipulator (NULL);

is also the same because in

[CPP]View PlainCopy
    1. Viewer->run ();

This function will judge whether a robot is present in the scene, and if it does not have a bot, it will add a trackballmanipulator bot itself.

The run function is implemented as follows:

[CPP]View PlainCopy
    1. int Viewer::run ()
    2. {
    3. if (!getcameramanipulator () && Getcamera ()->getalloweventfocus ())
    4. {
    5. Setcameramanipulator (new Osgga::trackballmanipulator ());
    6. }
    7. Setreleasecontextatendofframehint (false);
    8. return Viewerbase::run ();
    9. }

It's easy to see where the problem lies.

Changes to camera parameters in OSG

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.