2011-11-12 18:48:27 +00:00
|
|
|
// fgviewer.cxx -- alternative flightgear viewer application
|
|
|
|
//
|
|
|
|
// Copyright (C) 2009 - 2011 Mathias Froehlich
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2009-05-22 18:23:01 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include <osg/ArgumentParser>
|
2011-06-22 20:15:04 +00:00
|
|
|
#include <osg/Fog>
|
2009-05-22 18:23:01 +00:00
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <osgViewer/Viewer>
|
2009-05-23 12:04:54 +00:00
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
2011-06-22 20:15:04 +00:00
|
|
|
#include <osgViewer/Renderer>
|
2009-06-03 19:31:59 +00:00
|
|
|
#include <osgGA/KeySwitchMatrixManipulator>
|
2011-12-16 20:45:09 +00:00
|
|
|
#include <osgGA/StateSetManipulator>
|
2009-06-03 19:31:59 +00:00
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
#include <osgGA/FlightManipulator>
|
|
|
|
#include <osgGA/DriveManipulator>
|
|
|
|
#include <osgGA/TerrainManipulator>
|
2009-05-22 18:23:01 +00:00
|
|
|
|
|
|
|
#include <simgear/props/props.hxx>
|
2009-06-06 10:40:50 +00:00
|
|
|
#include <simgear/props/props_io.hxx>
|
2009-05-22 18:23:01 +00:00
|
|
|
#include <simgear/misc/sg_path.hxx>
|
2011-06-22 20:15:04 +00:00
|
|
|
#include <simgear/scene/material/EffectCullVisitor.hxx>
|
2009-05-22 18:23:01 +00:00
|
|
|
#include <simgear/scene/material/matlib.hxx>
|
2012-03-03 12:37:43 +00:00
|
|
|
#include <simgear/scene/util/OsgMath.hxx>
|
2011-11-12 18:45:20 +00:00
|
|
|
#include <simgear/scene/util/SGReaderWriterOptions.hxx>
|
2009-05-22 18:23:01 +00:00
|
|
|
#include <simgear/scene/tgdb/userdata.hxx>
|
|
|
|
#include <simgear/scene/tgdb/TileEntry.hxx>
|
|
|
|
#include <simgear/scene/model/ModelRegistry.hxx>
|
|
|
|
#include <simgear/scene/model/modellib.hxx>
|
|
|
|
|
|
|
|
class DummyLoadHelper : public simgear::ModelLoadHelper {
|
|
|
|
public:
|
|
|
|
virtual osg::Node *loadTileModel(const string& modelPath, bool)
|
|
|
|
{
|
|
|
|
try {
|
2011-06-22 20:15:04 +00:00
|
|
|
return simgear::SGModelLib::loadModel(modelPath, simgear::getPropertyRoot());
|
2009-05-22 18:23:01 +00:00
|
|
|
} catch (...) {
|
|
|
|
std::cerr << "Error loading \"" << modelPath << "\"" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
// Just reference simgears reader writer stuff so that the globals get
|
|
|
|
// pulled in by the linker ...
|
|
|
|
// FIXME: make that more explicit clear and call an initialization function
|
|
|
|
simgear::ModelRegistry::instance();
|
|
|
|
DummyLoadHelper dummyLoadHelper;
|
|
|
|
simgear::TileEntry::setModelLoadHelper(&dummyLoadHelper);
|
|
|
|
|
|
|
|
// use an ArgumentParser object to manage the program arguments.
|
|
|
|
osg::ArgumentParser arguments(&argc, argv);
|
|
|
|
|
|
|
|
// construct the viewer.
|
|
|
|
osgViewer::Viewer viewer(arguments);
|
2011-06-22 20:15:04 +00:00
|
|
|
|
2009-06-03 19:31:59 +00:00
|
|
|
// set up the camera manipulators.
|
|
|
|
osgGA::KeySwitchMatrixManipulator* keyswitchManipulator;
|
|
|
|
keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
|
2011-06-22 20:15:04 +00:00
|
|
|
|
2010-05-31 05:06:55 +00: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);
|
2011-06-22 20:15:04 +00:00
|
|
|
|
2009-06-03 19:31:59 +00:00
|
|
|
viewer.setCameraManipulator(keyswitchManipulator);
|
|
|
|
|
2009-05-23 12:04:54 +00:00
|
|
|
// Usefull stats
|
2011-12-16 20:45:09 +00:00
|
|
|
viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
|
2009-05-23 12:04:54 +00:00
|
|
|
viewer.addEventHandler(new osgViewer::HelpHandler);
|
|
|
|
viewer.addEventHandler(new osgViewer::StatsHandler);
|
2011-06-22 20:15:04 +00:00
|
|
|
viewer.addEventHandler(new osgViewer::ThreadingHandler);
|
2009-05-23 12:04:54 +00:00
|
|
|
viewer.addEventHandler(new osgViewer::LODScaleHandler);
|
|
|
|
viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
|
2011-06-22 20:15:04 +00:00
|
|
|
viewer.addEventHandler(new osgViewer::WindowSizeHandler);
|
|
|
|
|
|
|
|
// Sigh, we need our own cull visitor ...
|
|
|
|
osg::Camera* camera = viewer.getCamera();
|
|
|
|
osgViewer::Renderer* renderer = static_cast<osgViewer::Renderer*>(camera->getRenderer());
|
|
|
|
for (int j = 0; j < 2; ++j) {
|
|
|
|
osgUtil::SceneView* sceneView = renderer->getSceneView(j);
|
|
|
|
sceneView->setCullVisitor(new simgear::EffectCullVisitor);
|
|
|
|
}
|
|
|
|
// Shaders expect valid fog
|
|
|
|
osg::Fog* fog = new osg::Fog;
|
|
|
|
fog->setMode(osg::Fog::EXP2);
|
|
|
|
fog->setColor(osg::Vec4(1, 1, 1, 1));
|
|
|
|
fog->setDensity(1e-6);
|
|
|
|
camera->getOrCreateStateSet()->setAttribute(fog);
|
2009-05-23 12:04:54 +00:00
|
|
|
|
2009-05-22 18:23:01 +00:00
|
|
|
std::string fg_root;
|
2011-07-15 15:42:19 +00:00
|
|
|
if (arguments.read("--fg-root", fg_root)) {
|
|
|
|
} else if (const char *fg_root_env = std::getenv("FG_ROOT")) {
|
2009-05-22 18:23:01 +00:00
|
|
|
fg_root = fg_root_env;
|
2011-07-15 15:42:19 +00:00
|
|
|
} else {
|
2011-11-12 18:48:27 +00:00
|
|
|
#if defined(PKGLIBDIR)
|
|
|
|
fg_root = PKGLIBDIR;
|
2009-06-04 04:30:51 +00:00
|
|
|
#else
|
2009-05-22 18:23:01 +00:00
|
|
|
fg_root = ".";
|
2009-06-04 04:30:51 +00:00
|
|
|
#endif
|
2011-07-15 15:42:19 +00:00
|
|
|
}
|
2009-05-22 18:23:01 +00:00
|
|
|
|
2011-07-15 15:42:19 +00:00
|
|
|
std::string fg_scenery;
|
|
|
|
if (arguments.read("--fg-scenery", fg_scenery)) {
|
|
|
|
} else if (const char *fg_scenery_env = std::getenv("FG_SCENERY")) {
|
2011-11-12 18:48:27 +00:00
|
|
|
fg_scenery = fg_scenery_env;
|
2009-05-22 18:23:01 +00:00
|
|
|
} else {
|
|
|
|
SGPath path(fg_root);
|
|
|
|
path.append("Scenery");
|
2011-11-12 18:48:27 +00:00
|
|
|
fg_scenery = path.str();
|
2009-05-22 18:23:01 +00:00
|
|
|
}
|
2011-11-12 18:48:27 +00:00
|
|
|
string_list path_list = sgPathSplit(fg_scenery);
|
2011-07-15 15:42:19 +00:00
|
|
|
osgDB::FilePathList filePathList;
|
|
|
|
filePathList.push_back(fg_root);
|
2009-05-22 18:23:01 +00:00
|
|
|
for (unsigned i = 0; i < path_list.size(); ++i) {
|
|
|
|
SGPath pt(path_list[i]), po(path_list[i]);
|
|
|
|
pt.append("Terrain");
|
|
|
|
po.append("Objects");
|
|
|
|
filePathList.push_back(path_list[i]);
|
|
|
|
filePathList.push_back(pt.str());
|
|
|
|
filePathList.push_back(po.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
|
2011-06-22 20:15:04 +00:00
|
|
|
sgUserDataInit(props.get());
|
2009-06-06 10:40:50 +00:00
|
|
|
try {
|
|
|
|
SGPath preferencesFile = fg_root;
|
|
|
|
preferencesFile.append("preferences.xml");
|
|
|
|
readProperties(preferencesFile.str(), props);
|
|
|
|
} catch (...) {
|
|
|
|
// In case of an error, at least make summer :)
|
|
|
|
props->getNode("sim/startup/season", true)->setStringValue("summer");
|
|
|
|
|
|
|
|
std::cerr << "Problems loading FlightGear preferences.\n"
|
|
|
|
<< "Probably FG_ROOT is not properly set." << std::endl;
|
|
|
|
}
|
2009-05-22 18:23:01 +00:00
|
|
|
SGMaterialLib* ml = new SGMaterialLib;
|
|
|
|
SGPath mpath(fg_root);
|
|
|
|
mpath.append("materials.xml");
|
2009-05-25 05:02:11 +00:00
|
|
|
try {
|
|
|
|
ml->load(fg_root, mpath.str(), props);
|
|
|
|
} catch (...) {
|
|
|
|
std::cerr << "Problems loading FlightGear materials.\n"
|
|
|
|
<< "Probably FG_ROOT is not properly set." << std::endl;
|
|
|
|
}
|
2011-06-22 20:15:04 +00:00
|
|
|
simgear::SGModelLib::init(fg_root, props);
|
2009-06-03 19:31:59 +00:00
|
|
|
|
2011-12-17 07:28:33 +00:00
|
|
|
// Set up the reader/writer options
|
|
|
|
osg::ref_ptr<simgear::SGReaderWriterOptions> options;
|
|
|
|
if (osgDB::Options* ropt = osgDB::Registry::instance()->getOptions())
|
|
|
|
options = new simgear::SGReaderWriterOptions(*ropt);
|
|
|
|
else
|
|
|
|
options = new simgear::SGReaderWriterOptions;
|
2011-11-12 18:45:20 +00:00
|
|
|
options->getDatabasePathList() = filePathList;
|
|
|
|
options->setMaterialLib(ml);
|
|
|
|
options->setPropertyNode(props);
|
2011-12-17 07:28:33 +00:00
|
|
|
options->setPluginStringData("SimGear::FG_ROOT", fg_root);
|
|
|
|
options->setPluginStringData("SimGear::FG_SCENERY", fg_scenery);
|
|
|
|
osgDB::Registry::instance()->setOptions(options.get());
|
2011-06-22 20:15:04 +00:00
|
|
|
|
2011-07-15 15:42:19 +00:00
|
|
|
// Here, all arguments are processed
|
|
|
|
arguments.reportRemainingOptionsAsUnrecognized();
|
|
|
|
arguments.writeErrorMessages(std::cerr);
|
|
|
|
|
2009-05-22 18:23:01 +00:00
|
|
|
// read the scene from the list of file specified command line args.
|
|
|
|
osg::ref_ptr<osg::Node> loadedModel;
|
2011-12-17 07:28:33 +00:00
|
|
|
loadedModel = osgDB::readNodeFiles(arguments, options.get());
|
2009-05-22 18:23:01 +00:00
|
|
|
|
|
|
|
// if no model has been successfully loaded report failure.
|
|
|
|
if (!loadedModel.valid()) {
|
2009-05-25 05:02:11 +00:00
|
|
|
std::cerr << arguments.getApplicationName()
|
2009-05-22 18:23:01 +00:00
|
|
|
<< ": No data loaded" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2011-06-22 20:15:04 +00:00
|
|
|
|
2009-05-22 18:23:01 +00:00
|
|
|
// pass the loaded scene graph to the viewer.
|
|
|
|
viewer.setSceneData(loadedModel.get());
|
2011-06-22 20:15:04 +00:00
|
|
|
|
2011-12-16 20:45:33 +00:00
|
|
|
// We want on demand database paging
|
|
|
|
viewer.setDatabasePager(new osgDB::DatabasePager);
|
|
|
|
viewer.getDatabasePager()->setUpThreads(1, 1);
|
|
|
|
|
2009-05-22 18:23:01 +00:00
|
|
|
return viewer.run();
|
|
|
|
}
|