2018-05-31 21:45:37 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2009-04-28 19:57:51 +08:00
|
|
|
*
|
2018-05-31 21:45:37 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2013-08-19 02:10:39 +08:00
|
|
|
*
|
2018-05-31 21:45:37 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2009-04-28 19:57:51 +08:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2013-08-19 02:10:39 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2018-05-31 21:45:37 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
2009-04-28 19:57:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ReadShowFile.h"
|
|
|
|
#include "ShowEventHandler.h"
|
|
|
|
|
2015-03-05 02:36:44 +08:00
|
|
|
#include <osgPresentation/SlideEventHandler>
|
2009-06-25 00:03:49 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
#include <osg/ImageStream>
|
|
|
|
#include <osg/Shape>
|
|
|
|
#include <osg/ShapeDrawable>
|
|
|
|
#include <osg/Switch>
|
|
|
|
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <osgDB/FileNameUtils>
|
|
|
|
#include <osgDB/FileUtils>
|
|
|
|
|
|
|
|
#include <osgVolume/VolumeTile>
|
|
|
|
|
2009-04-30 04:30:21 +08:00
|
|
|
#include <osgDB/XmlParser>
|
2009-04-28 19:57:51 +08:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
class AddVolumeEditingCallbackVisitor : public osg::NodeVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AddVolumeEditingCallbackVisitor():
|
|
|
|
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
|
2013-08-19 02:10:39 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
void apply(osg::Group& group)
|
|
|
|
{
|
|
|
|
osgVolume::VolumeTile* volumeTile = dynamic_cast<osgVolume::VolumeTile*>(&group);
|
|
|
|
if (volumeTile)
|
|
|
|
{
|
2010-06-17 00:49:45 +08:00
|
|
|
if (dynamic_cast<osgVolume::PropertyAdjustmentCallback*>(volumeTile->getEventCallback())==0)
|
|
|
|
{
|
|
|
|
volumeTile->addEventCallback(new osgVolume::PropertyAdjustmentCallback());
|
|
|
|
}
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
traverse(group);
|
|
|
|
}
|
|
|
|
}
|
2013-08-19 02:10:39 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool p3d::getFileNames(osg::ArgumentParser& arguments, FileNameList& xmlFiles, FileNameList& normalFiles)
|
|
|
|
{
|
|
|
|
// note currently doesn't delete the loaded file entries from the command line yet...
|
|
|
|
for(int pos=1;pos<arguments.argc();++pos)
|
|
|
|
{
|
|
|
|
if (!arguments.isOption(pos))
|
|
|
|
{
|
|
|
|
std::string ext = osgDB::getFileExtension(arguments[pos]);
|
2013-08-19 02:10:39 +08:00
|
|
|
if (osgDB::equalCaseInsensitive(ext,"xml") || osgDB::equalCaseInsensitive(ext,"p3d"))
|
2009-04-28 19:57:51 +08:00
|
|
|
{
|
|
|
|
xmlFiles.push_back(arguments[pos]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
normalFiles.push_back(arguments[pos]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (!xmlFiles.empty() || !normalFiles.empty());
|
2013-08-19 02:10:39 +08:00
|
|
|
}
|
2009-04-28 19:57:51 +08:00
|
|
|
|
|
|
|
bool p3d::readEnvVars(osg::ArgumentParser& arguments)
|
|
|
|
{
|
|
|
|
bool readVars = false;
|
|
|
|
|
2009-04-28 22:03:02 +08:00
|
|
|
for(int i=1; i<arguments.argc(); ++i)
|
2009-04-28 19:57:51 +08:00
|
|
|
{
|
|
|
|
if (!arguments.isOption(i))
|
|
|
|
{
|
|
|
|
std::string ext = osgDB::getLowerCaseFileExtension(arguments[i]);
|
|
|
|
if (ext=="xml" || ext=="p3d")
|
|
|
|
{
|
|
|
|
std::string file = osgDB::findDataFile(arguments[i]);
|
|
|
|
if (!file.empty())
|
|
|
|
{
|
|
|
|
std::string path = osgDB::getFilePath(file);
|
|
|
|
if (!path.empty())
|
|
|
|
{
|
|
|
|
osgDB::getDataFilePathList().push_front(path);
|
|
|
|
}
|
2013-08-19 02:10:39 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
if (p3d::readEnvVars(file)) readVars = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-19 02:10:39 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
return readVars;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3d::readEnvVars(const std::string& fileName)
|
|
|
|
{
|
|
|
|
std::string ext = osgDB::getFileExtension(fileName);
|
2013-08-19 02:10:39 +08:00
|
|
|
if (!osgDB::equalCaseInsensitive(ext,"xml") &&
|
2009-04-28 19:57:51 +08:00
|
|
|
!osgDB::equalCaseInsensitive(ext,"p3d")) return false;
|
2013-08-19 02:10:39 +08:00
|
|
|
|
|
|
|
|
2009-04-30 04:30:21 +08:00
|
|
|
osg::ref_ptr<osgDB::XmlNode> doc = new osgDB::XmlNode;
|
|
|
|
osgDB::XmlNode* root = 0;
|
2009-04-28 19:57:51 +08:00
|
|
|
|
2009-04-30 04:30:21 +08:00
|
|
|
osgDB::XmlNode::Input input;
|
|
|
|
input.open(fileName);
|
|
|
|
input.readAllDataIntoBuffer();
|
|
|
|
|
|
|
|
doc->read(input);
|
2009-04-28 19:57:51 +08:00
|
|
|
|
|
|
|
if (doc == NULL )
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Document not parsed successfully. \n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-30 04:30:21 +08:00
|
|
|
for(osgDB::XmlNode::Children::iterator itr = doc->children.begin();
|
|
|
|
itr != doc->children.end() && !root;
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
if ((*itr)->name=="presentation") root = itr->get();
|
|
|
|
}
|
2009-04-28 19:57:51 +08:00
|
|
|
|
2009-04-30 04:30:21 +08:00
|
|
|
if (root == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"empty document\n");
|
|
|
|
return false;
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
|
2009-04-30 04:30:21 +08:00
|
|
|
if (root->name!="presentation")
|
|
|
|
{
|
|
|
|
fprintf(stderr,"document of the wrong type, root node != presentation");
|
|
|
|
return false;
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
2009-04-30 04:30:21 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
bool readVars = false;
|
2013-08-19 02:10:39 +08:00
|
|
|
|
2009-04-30 04:30:21 +08:00
|
|
|
for(osgDB::XmlNode::Children::iterator itr = root->children.begin();
|
|
|
|
itr != root->children.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
osgDB::XmlNode* cur = itr->get();
|
2009-04-28 19:57:51 +08:00
|
|
|
|
2009-04-30 04:30:21 +08:00
|
|
|
if (cur->name=="env")
|
2009-04-28 19:57:51 +08:00
|
|
|
{
|
2009-04-30 04:30:21 +08:00
|
|
|
char* str = strdup(cur->contents.c_str());
|
|
|
|
osg::notify(osg::INFO)<<"putenv("<<str<<")"<<std::endl;
|
|
|
|
putenv(str);
|
|
|
|
readVars = true;
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-30 04:30:21 +08:00
|
|
|
return readVars;
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
|
2009-06-25 00:03:49 +08:00
|
|
|
osgDB::Options* createOptions(const osgDB::ReaderWriter::Options* options)
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osgDB::Options> local_options = options ? options->cloneOptions() : 0;
|
|
|
|
if (!local_options)
|
|
|
|
{
|
|
|
|
local_options = osgDB::Registry::instance()->getOptions() ?
|
|
|
|
osgDB::Registry::instance()->getOptions()->cloneOptions() :
|
|
|
|
new osgDB::Options;
|
|
|
|
}
|
|
|
|
|
|
|
|
local_options->setPluginStringData("P3D_EVENTHANDLER","none");
|
|
|
|
return local_options.release();
|
|
|
|
}
|
|
|
|
|
2010-04-29 01:29:31 +08:00
|
|
|
osg::ref_ptr<osg::Node> p3d::readHoldingSlide(const std::string& filename)
|
2009-04-28 19:57:51 +08:00
|
|
|
{
|
|
|
|
std::string ext = osgDB::getFileExtension(filename);
|
2013-08-19 02:10:39 +08:00
|
|
|
if (!osgDB::equalCaseInsensitive(ext,"xml") &&
|
2009-04-28 19:57:51 +08:00
|
|
|
!osgDB::equalCaseInsensitive(ext,"p3d")) return 0;
|
|
|
|
|
2009-07-16 18:12:33 +08:00
|
|
|
osg::ref_ptr<osgDB::ReaderWriter::Options> options = createOptions(0);
|
2009-04-28 19:57:51 +08:00
|
|
|
options->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_NONE);
|
2009-07-16 18:12:33 +08:00
|
|
|
options->setOptionString("preview");
|
2009-04-28 19:57:51 +08:00
|
|
|
|
2010-04-29 01:29:31 +08:00
|
|
|
return osgDB::readRefNodeFile(filename, options.get());
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
|
2010-04-29 01:29:31 +08:00
|
|
|
osg::ref_ptr<osg::Node> p3d::readPresentation(const std::string& filename,const osgDB::ReaderWriter::Options* options)
|
2009-04-28 19:57:51 +08:00
|
|
|
{
|
|
|
|
std::string ext = osgDB::getFileExtension(filename);
|
|
|
|
if (!osgDB::equalCaseInsensitive(ext,"xml") &&
|
|
|
|
!osgDB::equalCaseInsensitive(ext,"p3d")) return 0;
|
2009-06-25 00:03:49 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osgDB::Options> local_options = createOptions(options);
|
2009-07-16 18:12:33 +08:00
|
|
|
local_options->setOptionString("main");
|
|
|
|
|
2010-04-29 01:29:31 +08:00
|
|
|
return osgDB::readRefNodeFile(filename, local_options.get());
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
|
2010-04-29 01:29:31 +08:00
|
|
|
osg::ref_ptr<osg::Node> p3d::readShowFiles(osg::ArgumentParser& arguments,const osgDB::ReaderWriter::Options* options)
|
2009-04-28 19:57:51 +08:00
|
|
|
{
|
2009-06-25 00:03:49 +08:00
|
|
|
osg::ref_ptr<osgDB::Options> local_options = createOptions(options);
|
2009-07-16 18:12:33 +08:00
|
|
|
local_options->setOptionString("main");
|
2009-04-28 19:57:51 +08:00
|
|
|
|
|
|
|
typedef std::vector< osg::ref_ptr<osg::Node> > NodeList;
|
|
|
|
NodeList nodeList;
|
|
|
|
|
|
|
|
std::string filename;
|
|
|
|
while (arguments.read("--image",filename))
|
|
|
|
{
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Image> image = readRefImageFile(filename.c_str(), local_options.get());
|
|
|
|
if (image.valid()) nodeList.push_back(osg::createGeodeForImage(image));
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (arguments.read("--movie",filename))
|
|
|
|
{
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Image> image = readRefImageFile(filename.c_str(), local_options.get());
|
2016-06-23 17:05:50 +08:00
|
|
|
osg::ImageStream* imageStream = dynamic_cast<osg::ImageStream*>(image.get());
|
|
|
|
if (imageStream)
|
2009-04-28 19:57:51 +08:00
|
|
|
{
|
|
|
|
imageStream->play();
|
2015-10-22 21:42:19 +08:00
|
|
|
nodeList.push_back(osg::createGeodeForImage(imageStream));
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (arguments.read("--dem",filename))
|
|
|
|
{
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::HeightField> hf = readRefHeightFieldFile(filename.c_str(), local_options.get());
|
2009-04-28 19:57:51 +08:00
|
|
|
if (hf)
|
|
|
|
{
|
|
|
|
osg::Geode* geode = new osg::Geode;
|
|
|
|
geode->addDrawable(new osg::ShapeDrawable(hf));
|
|
|
|
nodeList.push_back(geode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// note currently doesn't delete the loaded file entries from the command line yet...
|
|
|
|
for(int pos=1;pos<arguments.argc();++pos)
|
|
|
|
{
|
|
|
|
if (!arguments.isOption(pos))
|
|
|
|
{
|
|
|
|
// not an option so assume string is a filename.
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Node> node = osgDB::readRefNodeFile( arguments[pos], local_options.get());
|
2009-04-28 19:57:51 +08:00
|
|
|
|
|
|
|
if(node)
|
|
|
|
{
|
2011-11-04 20:50:05 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
if (node->getName().empty()) node->setName( arguments[pos] );
|
|
|
|
nodeList.push_back(node);
|
2011-11-04 20:50:05 +08:00
|
|
|
|
|
|
|
// make sure that this presentation isn't cached
|
2017-12-05 00:43:08 +08:00
|
|
|
osgDB::Registry::instance()->removeFromObjectCache( arguments[pos], local_options.get());
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-19 02:10:39 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
if (nodeList.empty())
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-19 02:10:39 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
osg::ref_ptr<osg::Node> root;
|
|
|
|
|
|
|
|
if (nodeList.size()==1)
|
|
|
|
{
|
|
|
|
root = nodeList.front().get();
|
|
|
|
}
|
|
|
|
else // size >1
|
|
|
|
{
|
2013-08-19 02:10:39 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
osg::Switch* sw = new osg::Switch;
|
|
|
|
for(NodeList::iterator itr=nodeList.begin();
|
|
|
|
itr!=nodeList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
sw->addChild((*itr).get());
|
|
|
|
}
|
|
|
|
sw->setSingleChildOn(0);
|
2013-08-19 02:10:39 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
sw->setEventCallback(new p3d::ShowEventHandler());
|
|
|
|
|
|
|
|
root = sw;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (root.valid())
|
|
|
|
{
|
|
|
|
osg::notify(osg::INFO)<<"Got node now adding callback"<<std::endl;
|
2013-08-19 02:10:39 +08:00
|
|
|
|
2009-04-28 19:57:51 +08:00
|
|
|
AddVolumeEditingCallbackVisitor avecv;
|
|
|
|
root->accept(avecv);
|
|
|
|
}
|
|
|
|
|
2010-04-29 01:29:31 +08:00
|
|
|
return root;
|
2009-04-28 19:57:51 +08:00
|
|
|
}
|