2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgsimulation.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-07-18 00:01:05 +08:00
|
|
|
#if defined(_WIN32) && !(defined(__CYGWIN__) || defined(__MINGW32__))
|
2004-11-30 02:59:36 +08:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Disable unavoidable warning messages:
|
|
|
|
|
|
|
|
// 4103: used #pragma pack to change alignment
|
|
|
|
// 4114: same type qualifier used more than once
|
|
|
|
// 4201: nonstandard extension used : nameless struct/union
|
|
|
|
// 4237: "keyword" reserved for future use
|
|
|
|
// 4251: class needs to have dll-interface to export class
|
|
|
|
// 4275: non DLL-interface class used as base for DLL-interface class
|
|
|
|
// 4290: C++ Exception Specification ignored
|
|
|
|
// 4503: decorated name length exceeded, name was truncated
|
|
|
|
// 4786: string too long - truncated to 255 characters
|
|
|
|
|
|
|
|
#pragma warning(disable : 4103 4114 4201 4237 4251 4275 4290 4503 4335 4786)
|
|
|
|
|
2020-07-18 00:01:05 +08:00
|
|
|
#endif // _WIN32
|
2004-11-30 02:59:36 +08:00
|
|
|
|
2007-01-11 19:47:01 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2007-05-15 00:14:40 +08:00
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
2003-09-01 17:36:03 +08:00
|
|
|
|
2004-05-14 06:09:23 +08:00
|
|
|
#include <osg/Group>
|
|
|
|
#include <osg/Geode>
|
2004-05-20 18:15:48 +08:00
|
|
|
#include <osg/ShapeDrawable>
|
|
|
|
#include <osg/Texture2D>
|
|
|
|
#include <osg/PositionAttitudeTransform>
|
2004-07-01 03:07:05 +08:00
|
|
|
#include <osg/MatrixTransform>
|
2004-09-13 21:53:45 +08:00
|
|
|
#include <osg/CoordinateSystemNode>
|
2004-05-20 18:15:48 +08:00
|
|
|
|
2004-09-13 21:53:45 +08:00
|
|
|
#include <osgDB/FileUtils>
|
2008-11-07 23:08:08 +08:00
|
|
|
#include <osgDB/fstream>
|
2004-05-20 18:15:48 +08:00
|
|
|
#include <osgDB/ReadFile>
|
2003-09-01 17:36:03 +08:00
|
|
|
|
2004-07-01 03:07:05 +08:00
|
|
|
#include <osgText/Text>
|
|
|
|
|
2007-02-28 05:12:48 +08:00
|
|
|
#include <osg/CoordinateSystemNode>
|
2003-09-01 17:36:03 +08:00
|
|
|
|
2005-08-31 23:04:27 +08:00
|
|
|
#include <osgSim/OverlayNode>
|
2006-06-08 19:18:27 +08:00
|
|
|
#include <osgSim/SphereSegment>
|
2005-08-31 23:04:27 +08:00
|
|
|
|
2004-08-30 18:03:18 +08:00
|
|
|
#include <osgGA/NodeTrackerManipulator>
|
2007-05-15 00:14:40 +08:00
|
|
|
#include <osgGA/StateSetManipulator>
|
2007-05-18 17:49:30 +08:00
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
#include <osgGA/FlightManipulator>
|
|
|
|
#include <osgGA/DriveManipulator>
|
|
|
|
#include <osgGA/KeySwitchMatrixManipulator>
|
|
|
|
#include <osgGA/AnimationPathManipulator>
|
|
|
|
#include <osgGA/TerrainManipulator>
|
2004-07-30 23:44:59 +08:00
|
|
|
|
2008-03-17 18:29:23 +08:00
|
|
|
#include <osgParticle/FireEffect>
|
|
|
|
|
2007-01-11 19:47:01 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2004-09-13 21:53:45 +08:00
|
|
|
osg::Node* createEarth()
|
2004-07-01 03:07:05 +08:00
|
|
|
{
|
2007-02-28 05:12:48 +08:00
|
|
|
osg::TessellationHints* hints = new osg::TessellationHints;
|
|
|
|
hints->setDetailRatio(5.0f);
|
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2007-02-28 05:12:48 +08:00
|
|
|
osg::ShapeDrawable* sd = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0,0.0,0.0), osg::WGS_84_RADIUS_POLAR), hints);
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2007-02-28 05:12:48 +08:00
|
|
|
osg::Geode* geode = new osg::Geode;
|
|
|
|
geode->addDrawable(sd);
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2007-02-28 05:12:48 +08:00
|
|
|
std::string filename = osgDB::findDataFile("Images/land_shallow_topo_2048.jpg");
|
2015-10-22 21:42:19 +08:00
|
|
|
geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, new osg::Texture2D(osgDB::readRefImageFile(filename)));
|
|
|
|
|
2007-02-28 05:12:48 +08:00
|
|
|
osg::CoordinateSystemNode* csn = new osg::CoordinateSystemNode;
|
|
|
|
csn->setEllipsoidModel(new osg::EllipsoidModel());
|
|
|
|
csn->addChild(geode);
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2007-02-28 05:12:48 +08:00
|
|
|
return csn;
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2003-09-01 17:36:03 +08:00
|
|
|
}
|
|
|
|
|
2004-09-13 21:53:45 +08:00
|
|
|
|
2004-09-22 05:33:52 +08:00
|
|
|
class ModelPositionCallback : public osg::NodeCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2007-05-18 17:43:25 +08:00
|
|
|
ModelPositionCallback(double speed):
|
2004-09-22 05:33:52 +08:00
|
|
|
_latitude(0.0),
|
|
|
|
_longitude(0.0),
|
2007-05-18 17:43:25 +08:00
|
|
|
_height(100000.0),
|
|
|
|
_speed(speed)
|
2005-03-22 20:04:03 +08:00
|
|
|
{
|
|
|
|
_rotation.makeRotate(osg::DegreesToRadians(90.0),0.0,0.0,1.0);
|
|
|
|
}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2004-09-22 05:33:52 +08:00
|
|
|
void updateParameters()
|
|
|
|
{
|
2007-05-18 17:43:25 +08:00
|
|
|
_longitude += _speed * ((2.0*osg::PI)/360.0)/20.0;
|
2004-09-22 05:33:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
|
|
|
|
{
|
|
|
|
updateParameters();
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2004-09-22 05:33:52 +08:00
|
|
|
osg::NodePath nodePath = nv->getNodePath();
|
|
|
|
|
2005-04-08 17:55:52 +08:00
|
|
|
osg::MatrixTransform* mt = nodePath.empty() ? 0 : dynamic_cast<osg::MatrixTransform*>(nodePath.back());
|
2004-09-22 05:33:52 +08:00
|
|
|
if (mt)
|
|
|
|
{
|
|
|
|
osg::CoordinateSystemNode* csn = 0;
|
|
|
|
|
|
|
|
// find coordinate system node from our parental chain
|
|
|
|
unsigned int i;
|
|
|
|
for(i=0; i<nodePath.size() && csn==0; ++i)
|
|
|
|
{
|
|
|
|
csn = dynamic_cast<osg::CoordinateSystemNode*>(nodePath[i]);
|
|
|
|
}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2004-09-22 05:33:52 +08:00
|
|
|
if (csn)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
osg::EllipsoidModel* ellipsoid = csn->getEllipsoidModel();
|
|
|
|
if (ellipsoid)
|
|
|
|
{
|
2005-11-17 17:57:40 +08:00
|
|
|
osg::Matrix inheritedMatrix;
|
2004-09-22 05:33:52 +08:00
|
|
|
for(i+=1; i<nodePath.size()-1; ++i)
|
|
|
|
{
|
|
|
|
osg::Transform* transform = nodePath[i]->asTransform();
|
2005-11-17 17:57:40 +08:00
|
|
|
if (transform) transform->computeLocalToWorldMatrix(inheritedMatrix, nv);
|
2004-09-22 05:33:52 +08:00
|
|
|
}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2005-11-17 17:57:40 +08:00
|
|
|
osg::Matrixd matrix(inheritedMatrix);
|
2004-09-22 05:33:52 +08:00
|
|
|
|
2005-04-08 17:55:52 +08:00
|
|
|
//osg::Matrixd matrix;
|
2004-09-22 05:33:52 +08:00
|
|
|
ellipsoid->computeLocalToWorldTransformFromLatLongHeight(_latitude,_longitude,_height,matrix);
|
2008-09-18 00:14:28 +08:00
|
|
|
matrix.preMultRotate(_rotation);
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2005-04-08 17:55:52 +08:00
|
|
|
mt->setMatrix(matrix);
|
2004-09-22 05:33:52 +08:00
|
|
|
}
|
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
}
|
2004-09-22 05:33:52 +08:00
|
|
|
}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2004-09-22 05:33:52 +08:00
|
|
|
traverse(node,nv);
|
2015-10-22 21:42:19 +08:00
|
|
|
}
|
|
|
|
|
2004-09-22 05:33:52 +08:00
|
|
|
double _latitude;
|
|
|
|
double _longitude;
|
|
|
|
double _height;
|
|
|
|
osg::Quat _rotation;
|
2007-05-18 17:43:25 +08:00
|
|
|
double _speed;
|
2004-09-22 05:33:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-07-30 23:44:59 +08:00
|
|
|
class FindNamedNodeVisitor : public osg::NodeVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FindNamedNodeVisitor(const std::string& name):
|
|
|
|
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
|
|
|
|
_name(name) {}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2004-07-30 23:44:59 +08:00
|
|
|
virtual void apply(osg::Node& node)
|
|
|
|
{
|
|
|
|
if (node.getName()==_name)
|
|
|
|
{
|
|
|
|
_foundNodes.push_back(&node);
|
|
|
|
}
|
|
|
|
traverse(node);
|
|
|
|
}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2004-07-30 23:44:59 +08:00
|
|
|
typedef std::vector< osg::ref_ptr<osg::Node> > NodeList;
|
|
|
|
|
|
|
|
std::string _name;
|
|
|
|
NodeList _foundNodes;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-05-14 06:09:23 +08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2003-09-01 17:36:03 +08:00
|
|
|
// use an ArgumentParser object to manage the program arguments.
|
|
|
|
osg::ArgumentParser arguments(&argc,argv);
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2003-09-01 17:36:03 +08:00
|
|
|
// set up the usage document, in case we need to print out how to use this program.
|
2006-07-18 21:37:42 +08:00
|
|
|
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of node tracker.");
|
|
|
|
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName());
|
2003-09-01 17:36:03 +08:00
|
|
|
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2003-09-01 17:36:03 +08:00
|
|
|
|
|
|
|
// construct the viewer.
|
2007-06-13 23:32:17 +08:00
|
|
|
osgViewer::Viewer viewer(arguments);
|
2003-09-01 17:36:03 +08:00
|
|
|
|
2007-05-15 00:14:40 +08:00
|
|
|
// add the state manipulator
|
|
|
|
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2007-05-15 00:14:40 +08:00
|
|
|
// add the thread model handler
|
|
|
|
viewer.addEventHandler(new osgViewer::ThreadingHandler);
|
|
|
|
|
|
|
|
// add the window size toggle handler
|
|
|
|
viewer.addEventHandler(new osgViewer::WindowSizeHandler);
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2007-05-15 00:14:40 +08:00
|
|
|
// add the stats handler
|
|
|
|
viewer.addEventHandler(new osgViewer::StatsHandler);
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2007-06-01 19:21:57 +08:00
|
|
|
// add the record camera path handler
|
|
|
|
viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
|
2007-05-15 00:14:40 +08:00
|
|
|
|
|
|
|
// add the help handler
|
|
|
|
viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
|
|
|
|
|
|
|
|
// set the near far ration computation up.
|
2007-01-11 19:47:01 +08:00
|
|
|
viewer.getCamera()->setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES);
|
2007-05-19 21:38:38 +08:00
|
|
|
viewer.getCamera()->setNearFarRatio(0.000003f);
|
2004-07-30 23:44:59 +08:00
|
|
|
|
2003-09-01 17:36:03 +08:00
|
|
|
|
2007-05-18 17:43:25 +08:00
|
|
|
double speed = 1.0;
|
|
|
|
while (arguments.read("-f") || arguments.read("--fixed")) speed = 0.0;
|
|
|
|
|
|
|
|
|
2004-10-14 18:38:29 +08:00
|
|
|
osg::Quat rotation;
|
|
|
|
osg::Vec4 vec4;
|
|
|
|
while (arguments.read("--rotate-model",vec4[0],vec4[1],vec4[2],vec4[3]))
|
|
|
|
{
|
|
|
|
osg::Quat local_rotate;
|
|
|
|
local_rotate.makeRotate(osg::DegreesToRadians(vec4[0]),vec4[1],vec4[2],vec4[3]);
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2004-10-14 18:38:29 +08:00
|
|
|
rotation = rotation * local_rotate;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::NodeCallback* nc = 0;
|
|
|
|
std::string flightpath_filename;
|
|
|
|
while (arguments.read("--flight-path",flightpath_filename))
|
|
|
|
{
|
2008-11-07 23:08:08 +08:00
|
|
|
osgDB::ifstream fin(flightpath_filename.c_str());
|
2004-10-14 18:38:29 +08:00
|
|
|
if (fin)
|
|
|
|
{
|
|
|
|
osg::AnimationPath* path = new osg::AnimationPath;
|
|
|
|
path->read(fin);
|
|
|
|
nc = new osg::AnimationPathCallback(path);
|
|
|
|
}
|
|
|
|
}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2004-10-14 18:38:29 +08:00
|
|
|
osgGA::NodeTrackerManipulator::TrackerMode trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_ROTATION;
|
|
|
|
std::string mode;
|
|
|
|
while (arguments.read("--tracker-mode",mode))
|
|
|
|
{
|
|
|
|
if (mode=="NODE_CENTER_AND_ROTATION") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_ROTATION;
|
|
|
|
else if (mode=="NODE_CENTER_AND_AZIM") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_AZIM;
|
|
|
|
else if (mode=="NODE_CENTER") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout<<"Unrecognized --tracker-mode option "<<mode<<", valid options are:"<<std::endl;
|
|
|
|
std::cout<<" NODE_CENTER_AND_ROTATION"<<std::endl;
|
|
|
|
std::cout<<" NODE_CENTER_AND_AZIM"<<std::endl;
|
|
|
|
std::cout<<" NODE_CENTER"<<std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
|
|
|
|
2004-10-14 18:38:29 +08:00
|
|
|
osgGA::NodeTrackerManipulator::RotationMode rotationMode = osgGA::NodeTrackerManipulator::TRACKBALL;
|
|
|
|
while (arguments.read("--rotation-mode",mode))
|
|
|
|
{
|
|
|
|
if (mode=="TRACKBALL") rotationMode = osgGA::NodeTrackerManipulator::TRACKBALL;
|
|
|
|
else if (mode=="ELEVATION_AZIM") rotationMode = osgGA::NodeTrackerManipulator::ELEVATION_AZIM;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout<<"Unrecognized --rotation-mode option "<<mode<<", valid options are:"<<std::endl;
|
|
|
|
std::cout<<" TRACKBALL"<<std::endl;
|
|
|
|
std::cout<<" ELEVATION_AZIM"<<std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2007-05-15 22:53:58 +08:00
|
|
|
|
|
|
|
bool useOverlay = true;
|
|
|
|
while (arguments.read("--no-overlay") || arguments.read("-n")) useOverlay = false;
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2007-05-15 04:23:10 +08:00
|
|
|
osgSim::OverlayNode::OverlayTechnique technique = osgSim::OverlayNode::OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY;
|
|
|
|
while (arguments.read("--object")) technique = osgSim::OverlayNode::OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY;
|
|
|
|
while (arguments.read("--ortho") || arguments.read("--orthographic")) technique = osgSim::OverlayNode::VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY;
|
|
|
|
while (arguments.read("--persp") || arguments.read("--perspective")) technique = osgSim::OverlayNode::VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY;
|
2007-05-29 21:28:40 +08:00
|
|
|
|
2008-04-11 22:14:28 +08:00
|
|
|
unsigned int overlayTextureUnit = 1;
|
|
|
|
while (arguments.read("--unit", overlayTextureUnit)) {}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2007-06-02 03:01:06 +08:00
|
|
|
std::string pathfile;
|
|
|
|
while (arguments.read("-p",pathfile)) {}
|
|
|
|
|
2008-03-17 18:29:23 +08:00
|
|
|
bool addFireEffect = arguments.read("--fire");
|
2007-05-15 22:53:58 +08:00
|
|
|
|
2003-09-01 17:36:03 +08:00
|
|
|
// if user request help write it out to cout.
|
|
|
|
if (arguments.read("-h") || arguments.read("--help"))
|
|
|
|
{
|
|
|
|
arguments.getApplicationUsage()->write(std::cout);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
|
|
|
|
2007-06-11 04:46:53 +08:00
|
|
|
osg::ref_ptr<osgGA::NodeTrackerManipulator> tm;
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2007-06-11 04:46:53 +08:00
|
|
|
std::string overlayFilename;
|
|
|
|
while(arguments.read("--overlay", overlayFilename)) {}
|
|
|
|
|
2005-08-31 23:04:27 +08:00
|
|
|
// read the scene from the list of file specified commandline args.
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Node> root = osgDB::readRefNodeFiles(arguments);
|
2005-08-31 23:04:27 +08:00
|
|
|
|
|
|
|
if (!root) root = createEarth();
|
2004-10-06 03:12:46 +08:00
|
|
|
|
2007-06-11 04:46:53 +08:00
|
|
|
if (!root) return 0;
|
2004-10-06 03:12:46 +08:00
|
|
|
|
2007-05-18 17:49:30 +08:00
|
|
|
|
2007-06-11 04:46:53 +08:00
|
|
|
if (!overlayFilename.empty())
|
2005-04-08 17:55:52 +08:00
|
|
|
{
|
2007-06-11 04:46:53 +08:00
|
|
|
//osg::Object *pObj = osgDB::readObjectFile("alaska_clean.shp");
|
|
|
|
//osg::ref_ptr<osg::Geode> shapefile = dynamic_cast<osg::Geode*> (pObj);
|
|
|
|
//
|
|
|
|
//ConvertLatLon2EllipsoidCoordinates latlon2em;
|
|
|
|
//shapefile->accept(latlon2em);
|
2005-08-31 23:04:27 +08:00
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Node> shapefile = osgDB::readRefNodeFile(overlayFilename);
|
|
|
|
|
2007-06-11 04:46:53 +08:00
|
|
|
if (!shapefile)
|
2005-08-31 23:04:27 +08:00
|
|
|
{
|
2007-06-11 04:46:53 +08:00
|
|
|
osg::notify(osg::NOTICE)<<"File `"<<overlayFilename<<"` not found"<<std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(root.get());
|
|
|
|
if (csn)
|
|
|
|
{
|
|
|
|
|
|
|
|
osgSim::OverlayNode* overlayNode = new osgSim::OverlayNode(technique);
|
|
|
|
overlayNode->getOrCreateStateSet()->setTextureAttribute(1, new osg::TexEnv(osg::TexEnv::DECAL));
|
|
|
|
overlayNode->setOverlaySubgraph(shapefile.get());
|
|
|
|
overlayNode->setOverlayTextureSizeHint(1024);
|
2008-04-11 22:14:28 +08:00
|
|
|
overlayNode->setOverlayTextureUnit(overlayTextureUnit);
|
2007-06-11 04:46:53 +08:00
|
|
|
|
2005-09-07 03:54:29 +08:00
|
|
|
// insert the OverlayNode between the coordinate system node and its children.
|
2005-08-31 23:04:27 +08:00
|
|
|
for(unsigned int i=0; i<csn->getNumChildren(); ++i)
|
|
|
|
{
|
|
|
|
overlayNode->addChild( csn->getChild(i) );
|
|
|
|
}
|
2006-04-05 23:13:17 +08:00
|
|
|
|
2006-05-02 17:45:31 +08:00
|
|
|
csn->removeChildren(0, csn->getNumChildren());
|
2007-06-11 04:46:53 +08:00
|
|
|
csn->addChild(overlayNode);
|
|
|
|
|
|
|
|
viewer.setSceneData(csn);
|
2005-08-31 23:04:27 +08:00
|
|
|
}
|
2007-06-11 04:46:53 +08:00
|
|
|
else
|
2005-04-08 17:55:52 +08:00
|
|
|
{
|
2007-06-11 04:46:53 +08:00
|
|
|
osgSim::OverlayNode* overlayNode = new osgSim::OverlayNode(technique);
|
|
|
|
overlayNode->getOrCreateStateSet()->setTextureAttribute(1, new osg::TexEnv(osg::TexEnv::DECAL));
|
|
|
|
overlayNode->setOverlaySubgraph(shapefile.get());
|
|
|
|
overlayNode->setOverlayTextureSizeHint(1024);
|
|
|
|
overlayNode->addChild(root.get());
|
|
|
|
|
|
|
|
viewer.setSceneData(overlayNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2007-06-11 04:46:53 +08:00
|
|
|
|
|
|
|
// add a viewport to the viewer and attach the scene graph.
|
|
|
|
viewer.setSceneData(root.get());
|
|
|
|
|
|
|
|
osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(root.get());
|
|
|
|
if (csn)
|
|
|
|
{
|
|
|
|
|
|
|
|
osg::ref_ptr<osgSim::OverlayNode> overlayNode;
|
|
|
|
if (useOverlay)
|
2006-06-08 19:18:27 +08:00
|
|
|
{
|
2007-06-11 04:46:53 +08:00
|
|
|
overlayNode = new osgSim::OverlayNode(technique);
|
|
|
|
|
|
|
|
// insert the OverlayNode between the coordinate system node and its children.
|
|
|
|
for(unsigned int i=0; i<csn->getNumChildren(); ++i)
|
|
|
|
{
|
|
|
|
overlayNode->addChild( csn->getChild(i) );
|
|
|
|
}
|
|
|
|
|
|
|
|
csn->removeChildren(0, csn->getNumChildren());
|
|
|
|
csn->addChild(overlayNode.get());
|
|
|
|
|
2016-05-30 19:30:05 +08:00
|
|
|
// tell the overlay node to continuously update its overlay texture
|
2007-06-11 04:46:53 +08:00
|
|
|
// as we know we'll be tracking a moving target.
|
|
|
|
overlayNode->setContinuousUpdate(true);
|
2006-06-08 19:18:27 +08:00
|
|
|
}
|
|
|
|
|
2005-04-08 17:55:52 +08:00
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Node> cessna = osgDB::readRefNodeFile("cessna.osgt");
|
2007-06-11 04:46:53 +08:00
|
|
|
if (cessna)
|
|
|
|
{
|
|
|
|
double s = 200000.0 / cessna->getBound().radius();
|
2006-05-02 17:45:31 +08:00
|
|
|
|
2007-06-11 04:46:53 +08:00
|
|
|
osg::MatrixTransform* scaler = new osg::MatrixTransform;
|
|
|
|
scaler->addChild(cessna);
|
|
|
|
scaler->setMatrix(osg::Matrixd::scale(s,s,s)*osg::Matrixd::rotate(rotation));
|
2008-03-17 18:29:23 +08:00
|
|
|
scaler->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL,osg::StateAttribute::ON);
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2008-03-17 18:29:23 +08:00
|
|
|
if (addFireEffect)
|
2015-10-22 21:42:19 +08:00
|
|
|
{
|
2008-03-17 18:29:23 +08:00
|
|
|
osg::Vec3d center = cessna->getBound().center();
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2008-03-17 18:29:23 +08:00
|
|
|
osgParticle::FireEffect* fire = new osgParticle::FireEffect(center, 10.0f);
|
|
|
|
scaler->addChild(fire);
|
|
|
|
}
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2004-10-06 03:12:46 +08:00
|
|
|
|
2007-06-11 04:46:53 +08:00
|
|
|
if (false)
|
|
|
|
{
|
|
|
|
osgSim::SphereSegment* ss = new osgSim::SphereSegment(
|
|
|
|
osg::Vec3(0.0f,0.0f,0.0f), // center
|
|
|
|
19.9f, // radius
|
|
|
|
osg::DegreesToRadians(135.0f),
|
|
|
|
osg::DegreesToRadians(240.0f),
|
|
|
|
osg::DegreesToRadians(-10.0f),
|
|
|
|
osg::DegreesToRadians(30.0f),
|
|
|
|
60);
|
|
|
|
|
|
|
|
scaler->addChild(ss);
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::MatrixTransform* mt = new osg::MatrixTransform;
|
|
|
|
mt->addChild(scaler);
|
|
|
|
|
|
|
|
|
|
|
|
if (!nc) nc = new ModelPositionCallback(speed);
|
|
|
|
|
|
|
|
mt->setUpdateCallback(nc);
|
|
|
|
|
|
|
|
csn->addChild(mt);
|
|
|
|
|
|
|
|
// if we are using an overaly node, use the cessna subgraph as the overlay subgraph
|
|
|
|
if (overlayNode.valid())
|
|
|
|
{
|
|
|
|
overlayNode->setOverlaySubgraph(mt);
|
|
|
|
}
|
|
|
|
|
|
|
|
tm = new osgGA::NodeTrackerManipulator;
|
|
|
|
tm->setTrackerMode(trackerMode);
|
|
|
|
tm->setRotationMode(rotationMode);
|
|
|
|
tm->setTrackNode(scaler);
|
|
|
|
}
|
|
|
|
else
|
2006-05-02 17:45:31 +08:00
|
|
|
{
|
2011-06-15 00:54:20 +08:00
|
|
|
std::cout<<"Failed to read cessna.osgt"<<std::endl;
|
2006-05-02 17:45:31 +08:00
|
|
|
}
|
2005-04-08 17:55:52 +08:00
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
}
|
2007-06-11 04:46:53 +08:00
|
|
|
}
|
2007-05-18 17:49:30 +08:00
|
|
|
|
|
|
|
// set up camera manipulators.
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
|
|
|
|
|
2007-06-01 19:21:57 +08:00
|
|
|
if (tm.valid()) keyswitchManipulator->addMatrixManipulator( '0', "NodeTracker", tm.get() );
|
|
|
|
|
2007-05-18 17:49:30 +08:00
|
|
|
keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
|
|
|
|
keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
|
|
|
|
keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
|
|
|
|
keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
|
|
|
|
|
2007-06-02 03:01:06 +08:00
|
|
|
if (!pathfile.empty())
|
2007-06-01 19:21:57 +08:00
|
|
|
{
|
|
|
|
osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
|
2015-10-22 21:42:19 +08:00
|
|
|
if (apm || !apm->valid())
|
2007-06-01 19:21:57 +08:00
|
|
|
{
|
|
|
|
unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
|
|
|
|
keyswitchManipulator->addMatrixManipulator( '5', "Path", apm );
|
|
|
|
keyswitchManipulator->selectMatrixManipulator(num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-18 17:49:30 +08:00
|
|
|
viewer.setCameraManipulator( keyswitchManipulator.get() );
|
|
|
|
}
|
|
|
|
|
2007-05-26 03:32:15 +08:00
|
|
|
// viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
|
2007-05-17 03:19:43 +08:00
|
|
|
|
2007-01-11 19:47:01 +08:00
|
|
|
return viewer.run();
|
2003-09-01 17:36:03 +08:00
|
|
|
}
|