[CPP]View PlainCopy
- #pragma comment (lib, "Osg.lib")
- #pragma comment (lib, "OsgDB.lib")
- #pragma comment (lib, "OsgViewer.lib")
- #include "Osgviewer/viewer"
- #include "Osgdb/readfile"
- #include "Osg/node"
- #include "Osg/shape"
- #include "Osg/geode"
- #include "osg/shapedrawable"
- int main () {
- //Initialize the scene viewer
- osg::ref_ptr<osgviewer::viewer> viewer=New Osgviewer::viewer;
- //Initialize the scene root node
- osg::ref_ptr<osg::group> root=New Osg::group;
- //Scene data
- Osg::ref_ptr<osg::node> node=osgdb::readnodefile ("GLIDER.OSG");
- Root->addchild (node);
- //Add scene data to the Vista
- Viewer->setscenedata (root);
- //Get the camera default parameter settings
- Osg::vec3d Eye,center,up;
- Viewer->getcamera ()->getviewmatrixaslookat (eye,center,up);
- //Print out the camera parameters
- printf ("init Eye:%f,%f,%f\n", eye._v[0],eye._v[1],eye._v[2]);
- printf ("Init Center:%f,%f,%f\n", center._v[0],center._v[1],center._v[2]);
- printf ("init up:%f,%f,%f\n", up._v[0],up._v[1],up._v[2]);
- //Modify camera Parameters
- Eye=osg::vec3d (0.0,-10.0,0.0);
- Center=osg::vec3d (0.0,0.0,0.0);
- Up=osg::vec3d (0.0,0.0,1.0);
- //Set the parameters to the camera and get the camera parameters immediately
- Viewer->getcamera ()->setviewmatrixaslookat (eye,center,up);
- Viewer->getcamera ()->getviewmatrixaslookat (eye,center,up);
- //Print out the parameters
- printf ("Eye:%f,%f,%f new\n", eye._v[0],eye._v[1],eye._v[2]);
- printf ("Center:%f,%f,%f new\n", center._v[0],center._v[1],center._v[2]);
- printf ("Up:%f,%f,%f new\n", up._v[0],up._v[1],up._v[2]);
- //simulation loop, take care not to use Viewer->run (), if you use this parameter, all the changes on the camera above will be invalid
- While (!viewer->done ())
- {
- Viewer->frame ();
- }
- return 1;
- }
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
- #pragma comment (lib, "Osg.lib")
- #pragma comment (lib, "OsgDB.lib")
- #pragma comment (lib, "OsgViewer.lib")
- #include "Osgviewer/viewer"
- #include "Osgdb/readfile"
- #include "Osg/node"
- #include "Osg/shape"
- #include "Osg/geode"
- #include "osg/shapedrawable"
- int main () {
- //Initialize the scene viewer
- osg::ref_ptr<osgviewer::viewer> viewer=New Osgviewer::viewer;
- //Initialize the scene root node
- osg::ref_ptr<osg::group> root=New Osg::group;
- //Scene data
- Osg::ref_ptr<osg::node> node=osgdb::readnodefile ("GLIDER.OSG");
- Root->addchild (node);
- //Add scene data to the Vista
- Viewer->setscenedata (root);
- //Get the camera default parameter settings
- Osg::vec3d Eye,center,up;
- Viewer->getcamera ()->getviewmatrixaslookat (eye,center,up);
- //Print out the camera parameters
- printf ("init Eye:%f,%f,%f\n", eye._v[0],eye._v[1],eye._v[2]);
- printf ("Init Center:%f,%f,%f\n", center._v[0],center._v[1],center._v[2]);
- printf ("init up:%f,%f,%f\n", up._v[0],up._v[1],up._v[2]);
- ////Modifying camera parameters
- //eye=osg::vec3d (0.0,-10.0,0.0);
- //center=osg::vec3d (0.0,0.0,0.0);
- //up=osg::vec3d (0.0,0.0,1.0);
- ////set parameters to the camera and get the camera parameters immediately
- //viewer->getcamera ()->setviewmatrixaslookat (eye,center,up);
- //viewer->getcamera ()->getviewmatrixaslookat (eye,center,up);
- ////to print out parameters
- //printf ("Eye:%f,%f,%f new\n", eye._v[0],eye._v[1],eye._v[2]);
- //printf ("Center:%f,%f,%f new\n", center._v[0],center._v[1],center._v[2]);
- //printf ("Up:%f,%f,%f new\n", up._v[0],up._v[1],up._v[2]);
- //simulation loop, take care not to use Viewer->run (), if you use this parameter, all the changes on the camera above will be invalid
- While (!viewer->done ())
- {
- Viewer->frame ();
- }
- return 1;
- }
In this case the effect is like, nothing is seen:
After using the following set of test data:
[CPP]View PlainCopy
- modifying camera parameters
- Eye=osg::vec3d ( -10.0,0.0,0.0);
- Center=osg::vec3d (0.0,0.0,0.0);
- 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
- Viewer->run ();
How to modify the camera parameters is not valid,
Even if you add
[CPP]View PlainCopy
- Viewer->setcameramanipulator (NULL);
is also the same because in
[CPP]View PlainCopy
- 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
- int Viewer::run ()
- {
- if (!getcameramanipulator () && Getcamera ()->getalloweventfocus ())
- {
- Setcameramanipulator (new Osgga::trackballmanipulator ());
- }
- Setreleasecontextatendofframehint (false);
- return Viewerbase::run ();
- }
It's easy to see where the problem lies.
Changes to camera parameters in OSG