2011-11-12 18:48:27 +00:00
|
|
|
// fgviewer.cxx -- alternative flightgear viewer application
|
|
|
|
//
|
2012-07-21 13:23:34 +00:00
|
|
|
// Copyright (C) 2009 - 2012 Mathias Froehlich
|
2011-11-12 18:48:27 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2019-02-06 03:48:05 +00:00
|
|
|
#include <osg/Version>
|
2009-05-22 18:23:01 +00:00
|
|
|
#include <osgDB/ReadFile>
|
2009-05-23 12:04:54 +00:00
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
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/scene/material/matlib.hxx>
|
2011-11-12 18:45:20 +00:00
|
|
|
#include <simgear/scene/util/SGReaderWriterOptions.hxx>
|
2012-03-15 20:11:44 +00:00
|
|
|
#include <simgear/scene/util/SGSceneFeatures.hxx>
|
2009-05-22 18:23:01 +00:00
|
|
|
#include <simgear/scene/tgdb/userdata.hxx>
|
|
|
|
#include <simgear/scene/model/ModelRegistry.hxx>
|
2012-07-21 13:23:34 +00:00
|
|
|
#include <simgear/misc/ResourceManager.hxx>
|
2009-05-22 18:23:01 +00:00
|
|
|
|
2021-03-09 14:47:18 +00:00
|
|
|
#include "ArgumentParser.hxx"
|
2012-12-16 15:43:22 +00:00
|
|
|
#include "Renderer.hxx"
|
|
|
|
#include "Viewer.hxx"
|
|
|
|
|
2013-10-20 14:56:33 +00:00
|
|
|
#if FG_HAVE_HLA
|
2012-12-16 15:43:22 +00:00
|
|
|
#include "HLACameraManipulator.hxx"
|
|
|
|
#include "HLAViewerFederate.hxx"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-05-22 18:23:01 +00:00
|
|
|
int
|
|
|
|
main(int argc, char** argv)
|
|
|
|
{
|
2012-07-21 13:23:34 +00:00
|
|
|
/// Read arguments and environment variables.
|
2021-03-09 14:47:18 +00:00
|
|
|
ArgumentParser arguments(argc, argv);
|
2012-03-15 20:11:44 +00:00
|
|
|
|
2013-02-07 18:00:54 +00:00
|
|
|
sglog().set_log_classes(SG_ALL);
|
|
|
|
sglog().set_log_priority(SG_ALERT);
|
2012-12-16 15:43:22 +00:00
|
|
|
|
2016-06-23 13:26:34 +00:00
|
|
|
SGPath fg_root;
|
|
|
|
std::string r;
|
|
|
|
if (arguments.read("--fg-root", r)) {
|
|
|
|
fg_root = SGPath::fromLocal8Bit(r.c_str());
|
|
|
|
} else if (std::getenv("FG_ROOT")) {
|
|
|
|
fg_root = SGPath::fromEnv("FG_ROOT");
|
2012-03-15 20:11:44 +00:00
|
|
|
} else {
|
2016-06-23 13:26:34 +00:00
|
|
|
fg_root = SGPath(PKGLIBDIR);
|
2012-03-15 20:11:44 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 13:26:34 +00:00
|
|
|
SGPath fg_scenery;
|
|
|
|
std::string s;
|
|
|
|
if (arguments.read("--fg-scenery", s)) {
|
|
|
|
fg_scenery = SGPath::fromLocal8Bit(s.c_str());
|
|
|
|
} else if (std::getenv("FG_SCENERY")) {
|
|
|
|
fg_scenery = SGPath::fromEnv("FG_SCENERY");
|
2012-03-15 20:11:44 +00:00
|
|
|
} else {
|
|
|
|
SGPath path(fg_root);
|
|
|
|
path.append("Scenery");
|
2016-06-23 13:26:34 +00:00
|
|
|
fg_scenery = path;
|
2012-03-15 20:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
|
|
|
|
try {
|
|
|
|
SGPath preferencesFile = fg_root;
|
2017-01-11 17:58:22 +00:00
|
|
|
preferencesFile.append("defaults.xml");
|
2016-06-23 13:26:34 +00:00
|
|
|
readProperties(preferencesFile, props);
|
2012-03-15 20:11:44 +00:00
|
|
|
} catch (...) {
|
|
|
|
// In case of an error, at least make summer :)
|
|
|
|
props->getNode("sim/startup/season", true)->setStringValue("summer");
|
|
|
|
|
2012-07-21 13:23:34 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear preferences.\n"
|
|
|
|
<< "Probably FG_ROOT is not properly set.");
|
2012-03-15 20:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string config;
|
|
|
|
while (arguments.read("--config", config)) {
|
|
|
|
try {
|
|
|
|
readProperties(config, props);
|
|
|
|
} catch (...) {
|
2012-07-21 13:23:34 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading config file \"" << config
|
|
|
|
<< "\" given on the command line.");
|
2012-03-15 20:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string prop, value;
|
|
|
|
while (arguments.read("--prop", prop, value)) {
|
|
|
|
props->setStringValue(prop, value);
|
|
|
|
}
|
|
|
|
|
2012-12-16 15:43:22 +00:00
|
|
|
std::string renderer;
|
|
|
|
while (arguments.read("--renderer", renderer));
|
|
|
|
|
|
|
|
if (arguments.read("--hla")) {
|
|
|
|
props->setStringValue("hla/federate/federation", "rti:///FlightGear");
|
|
|
|
}
|
|
|
|
std::string federation;
|
|
|
|
if (arguments.read("--federation", federation)) {
|
|
|
|
props->setStringValue("hla/federate/federation", federation);
|
|
|
|
}
|
|
|
|
|
2012-07-21 13:23:34 +00:00
|
|
|
/// Start setting up the viewer windows and start feeding them.
|
2009-05-22 18:23:01 +00:00
|
|
|
|
|
|
|
// construct the viewer.
|
2012-12-16 15:43:22 +00:00
|
|
|
fgviewer::Viewer viewer(arguments);
|
|
|
|
|
|
|
|
if (renderer.empty()) {
|
|
|
|
// Currently just the defautl renderer. More to come.
|
|
|
|
viewer.setRenderer(new fgviewer::Renderer);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "Unknown renderer configuration \"" << renderer
|
|
|
|
<< "\" given on the command line.");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A viewer configuration
|
|
|
|
if (const SGPropertyNode* viewerNode = props->getChild("viewer")) {
|
|
|
|
if (!viewer.readCameraConfig(*viewerNode)) {
|
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "Reading camera configuration failed.");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
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
|
2012-12-16 15:43:22 +00:00
|
|
|
viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getSceneDataGroup()->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);
|
|
|
|
|
2012-07-21 13:23:34 +00:00
|
|
|
// We want on demand database paging
|
|
|
|
viewer.setDatabasePager(new osgDB::DatabasePager);
|
|
|
|
viewer.getDatabasePager()->setUpThreads(1, 1);
|
|
|
|
|
|
|
|
/// now set up the simgears required model stuff
|
|
|
|
|
|
|
|
simgear::ResourceManager::instance()->addBasePath(fg_root, simgear::ResourceManager::PRIORITY_DEFAULT);
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
// FIXME Ok, replace this by querying the root of the property tree
|
2011-06-22 20:15:04 +00:00
|
|
|
sgUserDataInit(props.get());
|
2012-03-15 20:11:44 +00:00
|
|
|
SGSceneFeatures::instance()->setTextureCompression(SGSceneFeatures::DoNotUseCompression);
|
2014-01-19 16:49:32 +00:00
|
|
|
SGMaterialLibPtr ml = new SGMaterialLib;
|
2009-05-22 18:23:01 +00:00
|
|
|
SGPath mpath(fg_root);
|
2012-07-21 13:23:34 +00:00
|
|
|
mpath.append("Materials/default/materials.xml");
|
2009-05-25 05:02:11 +00:00
|
|
|
try {
|
2016-06-23 13:26:34 +00:00
|
|
|
ml->load(fg_root.local8BitStr(), mpath.local8BitStr(), props);
|
2009-05-25 05:02:11 +00:00
|
|
|
} catch (...) {
|
2012-07-21 13:23:34 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear materials.\n"
|
|
|
|
<< "Probably FG_ROOT is not properly set.");
|
2009-05-25 05:02:11 +00:00
|
|
|
}
|
2016-06-23 13:26:34 +00:00
|
|
|
simgear::SGModelLib::init(fg_root.local8BitStr(), 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;
|
2016-06-23 13:26:34 +00:00
|
|
|
osgDB::convertStringPathIntoFilePathList(fg_scenery.local8BitStr(),
|
2012-03-08 06:32:18 +00:00
|
|
|
options->getDatabasePathList());
|
2011-11-12 18:45:20 +00:00
|
|
|
options->setMaterialLib(ml);
|
|
|
|
options->setPropertyNode(props);
|
2016-06-23 13:26:34 +00:00
|
|
|
options->setPluginStringData("SimGear::FG_ROOT", fg_root.local8BitStr());
|
2012-03-15 20:11:44 +00:00
|
|
|
// Omit building bounding volume trees, as the viewer will not run a simulation
|
|
|
|
options->setPluginStringData("SimGear::BOUNDINGVOLUMES", "OFF");
|
2018-11-24 19:19:02 +00:00
|
|
|
GLint max_texture_size;
|
|
|
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);
|
|
|
|
options->setPluginStringData("SimGear::MAXTEXTURESIZE", std::to_string(max_texture_size? max_texture_size:8192));
|
2012-12-16 15:43:22 +00:00
|
|
|
viewer.setReaderWriterOptions(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);
|
|
|
|
|
2012-12-16 15:43:22 +00:00
|
|
|
if (props->getNode("hla/federate/federation")) {
|
2013-10-20 14:56:33 +00:00
|
|
|
#if FG_HAVE_HLA
|
2012-12-16 15:43:22 +00:00
|
|
|
const SGPropertyNode* federateNode = props->getNode("hla/federate");
|
|
|
|
|
|
|
|
SGSharedPtr<fgviewer::HLAViewerFederate> viewerFederate;
|
|
|
|
viewerFederate = new fgviewer::HLAViewerFederate;
|
|
|
|
viewerFederate->setVersion(federateNode->getStringValue("version", "RTI13"));
|
|
|
|
// viewerFederate->setConnectArguments(federateNode->getStringValue("connect-arguments", ""));
|
|
|
|
viewerFederate->setFederateType(federateNode->getStringValue("type", "ViewerFederate"));
|
|
|
|
viewerFederate->setFederateName(federateNode->getStringValue("name", ""));
|
|
|
|
viewerFederate->setFederationExecutionName(federateNode->getStringValue("federation", ""));
|
|
|
|
std::string objectModel;
|
|
|
|
objectModel = federateNode->getStringValue("federation-object-model", "HLA/fg-local-fom.xml");
|
|
|
|
if (SGPath(objectModel).isRelative()) {
|
|
|
|
SGPath path = fg_root;
|
|
|
|
path.append(objectModel);
|
|
|
|
objectModel = path.str();
|
|
|
|
}
|
|
|
|
viewerFederate->setFederationObjectModel(objectModel);
|
2013-10-20 14:56:33 +00:00
|
|
|
|
2012-12-16 15:43:22 +00:00
|
|
|
if (!viewerFederate->init()) {
|
|
|
|
SG_LOG(SG_NETWORK, SG_ALERT, "Got error from federate init!");
|
|
|
|
} else {
|
|
|
|
viewer.setViewerFederate(viewerFederate.get());
|
|
|
|
viewer.setCameraManipulator(new fgviewer::HLACameraManipulator(viewerFederate->getViewer()));
|
|
|
|
}
|
2013-10-20 14:56:33 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "Unable to enter HLA/RTI viewer mode: HLA/RTI disabled at compile time.");
|
2012-12-16 15:43:22 +00:00
|
|
|
#endif
|
|
|
|
}
|
2012-07-21 13:23:34 +00:00
|
|
|
|
|
|
|
/// Read the model files that are configured.
|
|
|
|
|
2009-05-22 18:23:01 +00:00
|
|
|
osg::ref_ptr<osg::Node> loadedModel;
|
2012-07-21 13:23:34 +00:00
|
|
|
if (1 < arguments.argc()) {
|
2012-03-04 19:31:14 +00:00
|
|
|
// read the scene from the list of file specified command line args.
|
2019-01-20 22:30:21 +00:00
|
|
|
loadedModel = osgDB::readRefNodeFiles(arguments, options.get());
|
2012-03-04 19:31:14 +00:00
|
|
|
} else {
|
|
|
|
// if no arguments given resort to the whole world scenery
|
|
|
|
options->setPluginStringData("SimGear::FG_EARTH", "ON");
|
2019-01-20 22:30:21 +00:00
|
|
|
loadedModel = osgDB::readRefNodeFile("w180s90-360x180.spt", options.get());
|
2012-03-04 19:31:14 +00:00
|
|
|
}
|
2009-05-22 18:23:01 +00:00
|
|
|
|
|
|
|
// if no model has been successfully loaded report failure.
|
|
|
|
if (!loadedModel.valid()) {
|
2012-07-21 13:23:34 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, arguments.getApplicationName()
|
|
|
|
<< ": No data loaded");
|
2009-05-22 18:23:01 +00:00
|
|
|
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.
|
2012-12-16 15:43:22 +00:00
|
|
|
viewer.insertSceneData(loadedModel.get());
|
|
|
|
|
|
|
|
// Note that this does not affect the hla camera manipulator
|
|
|
|
viewer.home();
|
2011-06-22 20:15:04 +00:00
|
|
|
|
2009-05-22 18:23:01 +00:00
|
|
|
return viewer.run();
|
|
|
|
}
|