2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgterrain.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2007-07-19 23:24:11 +08:00
|
|
|
#include <OpenThreads/Block>
|
|
|
|
|
2007-03-14 20:43:29 +08:00
|
|
|
#include <osg/Group>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/ShapeDrawable>
|
|
|
|
#include <osg/Texture2D>
|
|
|
|
#include <osg/PositionAttitudeTransform>
|
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
#include <osg/CoordinateSystemNode>
|
|
|
|
#include <osg/ClusterCullingCallback>
|
2007-03-19 18:38:44 +08:00
|
|
|
#include <osg/ArgumentParser>
|
2007-03-14 20:43:29 +08:00
|
|
|
|
2007-07-19 23:24:11 +08:00
|
|
|
|
2007-03-14 20:43:29 +08:00
|
|
|
#include <osgDB/FileUtils>
|
2008-11-07 23:08:08 +08:00
|
|
|
#include <osgDB/fstream>
|
2007-03-14 20:43:29 +08:00
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
2009-03-12 23:21:04 +08:00
|
|
|
#include <osgUtil/IncrementalCompileOperation>
|
2007-07-17 18:54:17 +08:00
|
|
|
|
2007-03-14 20:43:29 +08:00
|
|
|
#include <osgText/FadeText>
|
|
|
|
|
2007-03-29 22:45:35 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2007-06-07 00:23:20 +08:00
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
2007-03-29 22:45:35 +08:00
|
|
|
|
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
#include <osgGA/FlightManipulator>
|
|
|
|
#include <osgGA/DriveManipulator>
|
|
|
|
#include <osgGA/KeySwitchMatrixManipulator>
|
|
|
|
#include <osgGA/StateSetManipulator>
|
|
|
|
#include <osgGA/AnimationPathManipulator>
|
|
|
|
#include <osgGA/TerrainManipulator>
|
|
|
|
|
2008-03-27 18:55:39 +08:00
|
|
|
#include <osgTerrain/TerrainTile>
|
2007-03-22 00:34:04 +08:00
|
|
|
#include <osgTerrain/GeometryTechnique>
|
2007-03-19 18:38:44 +08:00
|
|
|
#include <osgTerrain/Layer>
|
2007-03-14 20:43:29 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2007-07-16 20:37:39 +08:00
|
|
|
|
2007-07-19 20:09:25 +08:00
|
|
|
typedef std::vector< osg::ref_ptr<osg::GraphicsThread> > GraphicsThreads;
|
|
|
|
|
2009-03-08 20:00:36 +08:00
|
|
|
struct ReleaseBlockOnCompileCompleted : public osgUtil::IncrementalCompileOperation::CompileCompletedCallback
|
|
|
|
{
|
|
|
|
|
|
|
|
ReleaseBlockOnCompileCompleted(osg::RefBlockCount* block):
|
|
|
|
_block(block) {}
|
|
|
|
|
|
|
|
virtual bool compileCompleted(osgUtil::IncrementalCompileOperation::CompileSet* compileSet)
|
|
|
|
{
|
|
|
|
if (_block.valid()) _block->completed();
|
|
|
|
|
|
|
|
// tell IncrementalCompileOperation that it's now safe to remove the compileSet
|
|
|
|
|
|
|
|
osg::notify(osg::NOTICE)<<"compileCompleted("<<compileSet<<")"<<std::endl;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::RefBlockCount> _block;
|
|
|
|
};
|
2007-07-19 20:09:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
class LoadAndCompileOperation : public osg::Operation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2009-03-08 20:00:36 +08:00
|
|
|
LoadAndCompileOperation(const std::string& filename, osgUtil::IncrementalCompileOperation* ico , osg::RefBlockCount* block):
|
2007-07-19 20:09:25 +08:00
|
|
|
Operation("Load and compile Operation", false),
|
|
|
|
_filename(filename),
|
2009-03-08 20:00:36 +08:00
|
|
|
_incrementalCompileOperation(ico),
|
2007-07-19 20:09:25 +08:00
|
|
|
_block(block) {}
|
|
|
|
|
|
|
|
virtual void operator () (osg::Object* object)
|
|
|
|
{
|
|
|
|
// osg::notify(osg::NOTICE)<<"LoadAndCompileOperation "<<_filename<<std::endl;
|
|
|
|
|
|
|
|
_loadedModel = osgDB::readNodeFile(_filename);
|
2009-06-17 18:39:39 +08:00
|
|
|
|
2009-03-08 20:00:36 +08:00
|
|
|
if (_loadedModel.valid() && _incrementalCompileOperation.valid())
|
2007-07-19 20:09:25 +08:00
|
|
|
{
|
2009-03-08 20:00:36 +08:00
|
|
|
osg::ref_ptr<osgUtil::IncrementalCompileOperation::CompileSet> compileSet =
|
2009-06-17 18:39:39 +08:00
|
|
|
new osgUtil::IncrementalCompileOperation::CompileSet(_loadedModel.get());
|
|
|
|
|
2009-03-08 20:00:36 +08:00
|
|
|
compileSet->_compileCompletedCallback = new ReleaseBlockOnCompileCompleted(_block.get());
|
2009-06-17 18:39:39 +08:00
|
|
|
|
2009-03-08 20:00:36 +08:00
|
|
|
_incrementalCompileOperation->add(compileSet.get());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_block.valid()) _block->completed();
|
|
|
|
}
|
2007-07-19 20:09:25 +08:00
|
|
|
|
|
|
|
// osg::notify(osg::NOTICE)<<"done LoadAndCompileOperation "<<_filename<<std::endl;
|
|
|
|
}
|
|
|
|
|
2009-03-08 20:00:36 +08:00
|
|
|
std::string _filename;
|
|
|
|
osg::ref_ptr<osg::Node> _loadedModel;
|
|
|
|
osg::ref_ptr<osgUtil::IncrementalCompileOperation> _incrementalCompileOperation;
|
|
|
|
osg::ref_ptr<osg::RefBlockCount> _block;
|
2007-07-19 20:09:25 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2007-07-17 01:40:38 +08:00
|
|
|
class MasterOperation : public osg::Operation
|
2007-06-18 20:10:46 +08:00
|
|
|
{
|
2007-07-17 01:40:38 +08:00
|
|
|
public:
|
2007-06-18 20:10:46 +08:00
|
|
|
|
2007-07-17 05:39:30 +08:00
|
|
|
typedef std::set<std::string> Files;
|
|
|
|
typedef std::map<std::string, osg::ref_ptr<osg::Node> > FilenameNodeMap;
|
|
|
|
typedef std::vector< osg::ref_ptr<osg::Node> > Nodes;
|
|
|
|
|
|
|
|
|
2009-03-08 20:00:36 +08:00
|
|
|
MasterOperation(const std::string& filename, osgUtil::IncrementalCompileOperation* ico):
|
2007-07-17 01:40:38 +08:00
|
|
|
Operation("Master reading operation",true),
|
2009-03-08 20:00:36 +08:00
|
|
|
_filename(filename),
|
|
|
|
_incrementalCompileOperation(ico)
|
2007-07-17 05:39:30 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-07-19 20:09:25 +08:00
|
|
|
/** Set the OperationQueue that the MasterOperation can use to place tasks like file loading on for other processes to handle.*/
|
|
|
|
void setOperationQueue(osg::OperationQueue* oq) { _operationQueue = oq; }
|
|
|
|
|
|
|
|
osg::OperationQueue* getOperationQueue() { return _operationQueue.get(); }
|
|
|
|
|
2007-07-17 05:39:30 +08:00
|
|
|
bool readMasterFile(Files& files) const
|
2007-07-17 01:40:38 +08:00
|
|
|
{
|
2008-11-07 23:08:08 +08:00
|
|
|
osgDB::ifstream fin(_filename.c_str());
|
2007-07-17 01:40:38 +08:00
|
|
|
if (fin)
|
2007-06-18 20:10:46 +08:00
|
|
|
{
|
2007-07-17 01:40:38 +08:00
|
|
|
osgDB::Input fr;
|
|
|
|
fr.attach(&fin);
|
|
|
|
|
2009-01-07 19:24:47 +08:00
|
|
|
bool readFilename = false;
|
2007-07-17 05:39:30 +08:00
|
|
|
|
2007-07-17 01:40:38 +08:00
|
|
|
while(!fr.eof())
|
2007-06-18 20:10:46 +08:00
|
|
|
{
|
2007-07-17 01:40:38 +08:00
|
|
|
bool itrAdvanced = false;
|
|
|
|
if (fr.matchSequence("file %s") || fr.matchSequence("file %w") )
|
2007-06-18 20:10:46 +08:00
|
|
|
{
|
2007-07-17 01:40:38 +08:00
|
|
|
files.insert(fr[1].getStr());
|
|
|
|
fr += 2;
|
|
|
|
itrAdvanced = true;
|
2007-07-17 05:39:30 +08:00
|
|
|
readFilename = true;
|
2007-07-17 01:40:38 +08:00
|
|
|
}
|
2007-07-17 05:39:30 +08:00
|
|
|
|
2007-07-17 01:40:38 +08:00
|
|
|
if (!itrAdvanced)
|
|
|
|
{
|
|
|
|
++fr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-17 05:39:30 +08:00
|
|
|
return readFilename;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool open(osg::Group* group)
|
|
|
|
{
|
|
|
|
Files files;
|
|
|
|
readMasterFile(files);
|
|
|
|
for(Files::iterator itr = files.begin();
|
|
|
|
itr != files.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
osg::Node* model = osgDB::readNodeFile(*itr);
|
|
|
|
if (model)
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"open: Loaded file "<<*itr<<std::endl;
|
|
|
|
group->addChild(model);
|
|
|
|
_existingFilenameNodeMap[*itr] = model;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-17 01:40:38 +08:00
|
|
|
|
2007-08-11 20:43:31 +08:00
|
|
|
virtual void operator () (osg::Object* callingObject)
|
2007-07-17 05:39:30 +08:00
|
|
|
{
|
2007-08-11 20:43:31 +08:00
|
|
|
// decided which method to call according to whole has called me.
|
|
|
|
osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(callingObject);
|
|
|
|
|
|
|
|
if (viewer) update(viewer->getSceneData());
|
|
|
|
else load();
|
|
|
|
}
|
|
|
|
|
|
|
|
void load()
|
|
|
|
{
|
|
|
|
//osg::notify(osg::NOTICE)<<"void load(Object)"<<std::endl;
|
2007-07-20 01:02:37 +08:00
|
|
|
Files filesA;
|
|
|
|
Files filesB;
|
|
|
|
|
|
|
|
readMasterFile(filesB);
|
|
|
|
// osg::notify(osg::NOTICE)<<"First read "<<filesA.size()<<std::endl;
|
|
|
|
|
|
|
|
// itererate until the master file is stable
|
|
|
|
do
|
|
|
|
{
|
2007-07-22 04:11:16 +08:00
|
|
|
OpenThreads::Thread::microSleep(100000);
|
2007-07-20 01:02:37 +08:00
|
|
|
|
|
|
|
filesB.swap(filesA);
|
|
|
|
filesB.clear();
|
|
|
|
readMasterFile(filesB);
|
|
|
|
|
|
|
|
// osg::notify(osg::NOTICE)<<"second read "<<filesB.size()<<std::endl;
|
|
|
|
|
|
|
|
} while (filesA!=filesB);
|
|
|
|
|
2007-07-17 05:39:30 +08:00
|
|
|
Files files;
|
2007-07-20 01:02:37 +08:00
|
|
|
files.swap(filesB);
|
|
|
|
|
|
|
|
// osg::notify(osg::NOTICE)<<"Now equal "<<files.size()<<std::endl;
|
2007-07-17 01:40:38 +08:00
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
|
2007-07-17 05:39:30 +08:00
|
|
|
Files newFiles;
|
|
|
|
Files removedFiles;
|
2007-07-17 01:40:38 +08:00
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
// find out which files are new, and which ones have been removed.
|
2007-07-17 05:39:30 +08:00
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
|
2007-07-17 01:40:38 +08:00
|
|
|
|
2007-07-17 05:39:30 +08:00
|
|
|
for(Files::iterator fitr = files.begin();
|
|
|
|
fitr != files.end();
|
|
|
|
++fitr)
|
2007-07-17 01:40:38 +08:00
|
|
|
{
|
2007-07-17 05:39:30 +08:00
|
|
|
if (_existingFilenameNodeMap.count(*fitr)==0) newFiles.insert(*fitr);
|
2007-07-17 01:40:38 +08:00
|
|
|
}
|
|
|
|
|
2007-07-17 05:39:30 +08:00
|
|
|
for(FilenameNodeMap::iterator litr = _existingFilenameNodeMap.begin();
|
|
|
|
litr != _existingFilenameNodeMap.end();
|
|
|
|
++litr)
|
2007-07-17 01:40:38 +08:00
|
|
|
{
|
2007-07-17 05:39:30 +08:00
|
|
|
if (files.count(litr->first)==0)
|
|
|
|
{
|
|
|
|
removedFiles.insert(litr->first);
|
|
|
|
}
|
2007-07-17 01:40:38 +08:00
|
|
|
}
|
2007-07-17 05:39:30 +08:00
|
|
|
}
|
2007-07-17 18:54:17 +08:00
|
|
|
|
2007-07-20 02:59:20 +08:00
|
|
|
#if 0
|
|
|
|
if (!newFiles.empty() || !removedFiles.empty())
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<std::endl<<std::endl<<"void operator () files.size()="<<files.size()<<std::endl;
|
|
|
|
}
|
|
|
|
#endif
|
2007-07-17 01:40:38 +08:00
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
// first load the new files.
|
2007-07-17 05:39:30 +08:00
|
|
|
FilenameNodeMap nodesToAdd;
|
2007-07-18 00:18:13 +08:00
|
|
|
if (!newFiles.empty())
|
2007-07-17 05:39:30 +08:00
|
|
|
{
|
2007-07-18 00:18:13 +08:00
|
|
|
|
|
|
|
typedef std::vector< osg::ref_ptr<osg::GraphicsThread> > GraphicsThreads;
|
|
|
|
GraphicsThreads threads;
|
|
|
|
|
|
|
|
for(unsigned int i=0; i<= osg::GraphicsContext::getMaxContextID(); ++i)
|
2007-07-17 18:54:17 +08:00
|
|
|
{
|
2007-07-18 00:18:13 +08:00
|
|
|
osg::GraphicsContext* gc = osg::GraphicsContext::getCompileContext(i);
|
|
|
|
osg::GraphicsThread* gt = gc ? gc->getGraphicsThread() : 0;
|
|
|
|
if (gt) threads.push_back(gt);
|
|
|
|
}
|
2007-07-17 05:39:30 +08:00
|
|
|
|
2007-07-19 20:09:25 +08:00
|
|
|
if (_operationQueue.valid())
|
|
|
|
{
|
|
|
|
// osg::notify(osg::NOTICE)<<"Using OperationQueue"<<std::endl;
|
|
|
|
|
2007-07-19 23:24:11 +08:00
|
|
|
_endOfLoadBlock = new osg::RefBlockCount(newFiles.size());
|
2007-07-20 02:59:20 +08:00
|
|
|
|
|
|
|
_endOfLoadBlock->reset();
|
2007-07-19 20:09:25 +08:00
|
|
|
|
|
|
|
typedef std::list< osg::ref_ptr<LoadAndCompileOperation> > LoadAndCompileList;
|
|
|
|
LoadAndCompileList loadAndCompileList;
|
|
|
|
|
|
|
|
for(Files::iterator nitr = newFiles.begin();
|
|
|
|
nitr != newFiles.end();
|
|
|
|
++nitr)
|
|
|
|
{
|
2007-07-20 02:59:20 +08:00
|
|
|
// osg::notify(osg::NOTICE)<<"Adding LoadAndCompileOperation "<<*nitr<<std::endl;
|
|
|
|
|
2009-03-08 20:00:36 +08:00
|
|
|
osg::ref_ptr<LoadAndCompileOperation> loadAndCompile = new LoadAndCompileOperation( *nitr, _incrementalCompileOperation.get(), _endOfLoadBlock.get() );
|
2007-07-19 20:09:25 +08:00
|
|
|
loadAndCompileList.push_back(loadAndCompile);
|
|
|
|
_operationQueue->add( loadAndCompile.get() );
|
|
|
|
}
|
2007-07-20 02:59:20 +08:00
|
|
|
|
|
|
|
#if 1
|
2007-07-19 20:09:25 +08:00
|
|
|
osg::ref_ptr<osg::Operation> operation;
|
|
|
|
while ((operation=_operationQueue->getNextOperation()).valid())
|
|
|
|
{
|
|
|
|
// osg::notify(osg::NOTICE)<<"Local running of operation"<<std::endl;
|
|
|
|
(*operation)(0);
|
|
|
|
}
|
2007-07-20 02:59:20 +08:00
|
|
|
#endif
|
2007-07-19 20:09:25 +08:00
|
|
|
// osg::notify(osg::NOTICE)<<"Waiting for completion of LoadAndCompile operations"<<std::endl;
|
|
|
|
_endOfLoadBlock->block();
|
|
|
|
// osg::notify(osg::NOTICE)<<"done ... Waiting for completion of LoadAndCompile operations"<<std::endl;
|
2007-07-18 00:18:13 +08:00
|
|
|
|
2007-07-19 20:09:25 +08:00
|
|
|
for(LoadAndCompileList::iterator litr = loadAndCompileList.begin();
|
|
|
|
litr != loadAndCompileList.end();
|
|
|
|
++litr)
|
|
|
|
{
|
|
|
|
if ((*litr)->_loadedModel.valid())
|
2007-07-17 18:54:17 +08:00
|
|
|
{
|
2007-07-19 20:09:25 +08:00
|
|
|
nodesToAdd[(*litr)->_filename] = (*litr)->_loadedModel;
|
2007-07-17 18:54:17 +08:00
|
|
|
}
|
|
|
|
}
|
2007-07-19 20:09:25 +08:00
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
}
|
|
|
|
|
2007-07-19 20:09:25 +08:00
|
|
|
else
|
|
|
|
{
|
2009-03-08 20:00:36 +08:00
|
|
|
|
|
|
|
_endOfLoadBlock = new osg::RefBlockCount(newFiles.size());
|
|
|
|
|
|
|
|
_endOfLoadBlock->reset();
|
|
|
|
|
2007-07-19 20:09:25 +08:00
|
|
|
for(Files::iterator nitr = newFiles.begin();
|
|
|
|
nitr != newFiles.end();
|
|
|
|
++nitr)
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(*nitr);
|
|
|
|
|
|
|
|
if (loadedModel.get())
|
|
|
|
{
|
|
|
|
nodesToAdd[*nitr] = loadedModel;
|
|
|
|
|
2009-03-08 20:00:36 +08:00
|
|
|
if (_incrementalCompileOperation.valid())
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osgUtil::IncrementalCompileOperation::CompileSet> compileSet =
|
|
|
|
new osgUtil::IncrementalCompileOperation::CompileSet(loadedModel.get());
|
|
|
|
|
|
|
|
compileSet->_compileCompletedCallback = new ReleaseBlockOnCompileCompleted(_endOfLoadBlock.get());
|
2007-07-19 20:09:25 +08:00
|
|
|
|
2009-03-08 20:00:36 +08:00
|
|
|
_incrementalCompileOperation->add(compileSet.get());
|
|
|
|
}
|
|
|
|
else
|
2007-07-19 20:09:25 +08:00
|
|
|
{
|
2009-03-08 20:00:36 +08:00
|
|
|
_endOfLoadBlock->completed();
|
2007-07-19 20:09:25 +08:00
|
|
|
}
|
|
|
|
}
|
2009-03-08 20:00:36 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_endOfLoadBlock->completed();
|
|
|
|
}
|
2007-07-19 20:09:25 +08:00
|
|
|
}
|
|
|
|
|
2009-03-08 20:00:36 +08:00
|
|
|
_endOfLoadBlock->block();
|
|
|
|
|
2007-07-17 18:54:17 +08:00
|
|
|
}
|
2009-03-08 20:00:36 +08:00
|
|
|
|
2007-07-17 18:54:17 +08:00
|
|
|
}
|
2007-07-18 00:18:13 +08:00
|
|
|
|
|
|
|
bool requiresBlock = false;
|
|
|
|
|
|
|
|
// pass the locally peppared data to MasterOperations shared data
|
|
|
|
// so that updated thread can merge these changes with the main scene
|
|
|
|
// graph. This merge is carried out via the update(..) method.
|
|
|
|
if (!removedFiles.empty() || !nodesToAdd.empty())
|
2007-07-17 05:39:30 +08:00
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
|
|
|
|
_nodesToRemove.swap(removedFiles);
|
|
|
|
_nodesToAdd.swap(nodesToAdd);
|
2007-07-18 00:18:13 +08:00
|
|
|
requiresBlock = true;
|
2007-07-17 05:39:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// now block so we don't try to load anything till the new data has been merged
|
|
|
|
// otherwise _existingFilenameNodeMap will get out of sync.
|
2007-07-18 00:18:13 +08:00
|
|
|
if (requiresBlock)
|
|
|
|
{
|
|
|
|
_updatesMergedBlock.block();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OpenThreads::Thread::YieldCurrentThread();
|
|
|
|
}
|
2007-07-17 05:39:30 +08:00
|
|
|
|
2007-07-17 01:40:38 +08:00
|
|
|
}
|
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
// merge the changes with the main scene graph.
|
2007-07-19 00:17:06 +08:00
|
|
|
void update(osg::Node* scene)
|
2007-07-17 01:40:38 +08:00
|
|
|
{
|
2007-08-11 20:43:31 +08:00
|
|
|
// osg::notify(osg::NOTICE)<<"void update(Node*)"<<std::endl;
|
|
|
|
|
2007-07-19 00:17:06 +08:00
|
|
|
osg::Group* group = dynamic_cast<osg::Group*>(scene);
|
|
|
|
if (!group)
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Error, MasterOperation::update(Node*) can only work with a Group as Viewer::getSceneData()."<<std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-07-17 01:40:38 +08:00
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
|
|
|
|
|
2007-07-19 00:17:06 +08:00
|
|
|
if (!_nodesToRemove.empty() || !_nodesToAdd.empty())
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"update().................. "<<std::endl;
|
|
|
|
}
|
|
|
|
|
2007-07-17 01:40:38 +08:00
|
|
|
if (!_nodesToRemove.empty())
|
|
|
|
{
|
|
|
|
for(Files::iterator itr = _nodesToRemove.begin();
|
|
|
|
itr != _nodesToRemove.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
FilenameNodeMap::iterator fnmItr = _existingFilenameNodeMap.find(*itr);
|
|
|
|
if (fnmItr != _existingFilenameNodeMap.end())
|
|
|
|
{
|
2007-07-19 00:17:06 +08:00
|
|
|
osg::notify(osg::NOTICE)<<" update():removing "<<*itr<<std::endl;
|
2007-07-17 05:39:30 +08:00
|
|
|
|
2007-07-19 00:17:06 +08:00
|
|
|
group->removeChild(fnmItr->second.get());
|
2007-07-17 01:40:38 +08:00
|
|
|
_existingFilenameNodeMap.erase(fnmItr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_nodesToRemove.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_nodesToAdd.empty())
|
|
|
|
{
|
|
|
|
for(FilenameNodeMap::iterator itr = _nodesToAdd.begin();
|
|
|
|
itr != _nodesToAdd.end();
|
|
|
|
++itr)
|
|
|
|
{
|
2007-07-19 00:17:06 +08:00
|
|
|
osg::notify(osg::NOTICE)<<" update():inserting "<<itr->first<<std::endl;
|
|
|
|
group->addChild(itr->second.get());
|
2007-07-17 01:40:38 +08:00
|
|
|
_existingFilenameNodeMap[itr->first] = itr->second;
|
|
|
|
}
|
2007-06-18 20:10:46 +08:00
|
|
|
|
2007-07-17 01:40:38 +08:00
|
|
|
_nodesToAdd.clear();
|
2007-06-18 20:10:46 +08:00
|
|
|
}
|
|
|
|
|
2007-07-17 01:40:38 +08:00
|
|
|
_updatesMergedBlock.release();
|
|
|
|
|
|
|
|
}
|
2007-07-16 20:37:39 +08:00
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
// add release implementation so that any thread cancellation can
|
|
|
|
// work even when blocks and barriers are used.
|
2007-07-17 01:40:38 +08:00
|
|
|
virtual void release()
|
|
|
|
{
|
2007-07-19 20:09:25 +08:00
|
|
|
if (_operationQueue.valid()) _operationQueue->removeAllOperations();
|
|
|
|
|
2007-07-17 01:40:38 +08:00
|
|
|
_updatesMergedBlock.release();
|
2007-07-19 20:09:25 +08:00
|
|
|
if (_endOfCompilebarrier.valid()) _endOfCompilebarrier.release();
|
|
|
|
if (_endOfLoadBlock.valid()) _endOfLoadBlock.release();
|
2007-07-17 01:40:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
std::string _filename;
|
2007-07-17 01:40:38 +08:00
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
OpenThreads::Mutex _mutex;
|
|
|
|
FilenameNodeMap _existingFilenameNodeMap;
|
|
|
|
Files _nodesToRemove;
|
|
|
|
FilenameNodeMap _nodesToAdd;
|
|
|
|
OpenThreads::Block _updatesMergedBlock;
|
2007-07-19 20:09:25 +08:00
|
|
|
|
2009-03-08 20:00:36 +08:00
|
|
|
osg::ref_ptr<osgUtil::IncrementalCompileOperation> _incrementalCompileOperation;
|
2007-07-19 20:09:25 +08:00
|
|
|
osg::ref_ptr<osg::BarrierOperation> _endOfCompilebarrier;
|
2007-07-19 23:24:11 +08:00
|
|
|
osg::ref_ptr<osg::RefBlockCount> _endOfLoadBlock;
|
2007-07-19 20:09:25 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::OperationQueue> _operationQueue;
|
2007-07-17 01:40:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class FilterHandler : public osgGA::GUIEventHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
FilterHandler(osgTerrain::GeometryTechnique* gt):
|
|
|
|
_gt(gt) {}
|
|
|
|
|
|
|
|
bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
|
|
|
|
{
|
|
|
|
if (!_gt) return false;
|
|
|
|
|
|
|
|
switch(ea.getEventType())
|
|
|
|
{
|
|
|
|
case(osgGA::GUIEventAdapter::KEYDOWN):
|
|
|
|
{
|
|
|
|
if (ea.getKey() == 'g')
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Gaussian"<<std::endl;
|
|
|
|
_gt->setFilterMatrixAs(osgTerrain::GeometryTechnique::GAUSSIAN);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ea.getKey() == 's')
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Smooth"<<std::endl;
|
|
|
|
_gt->setFilterMatrixAs(osgTerrain::GeometryTechnique::SMOOTH);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ea.getKey() == 'S')
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Sharpen"<<std::endl;
|
|
|
|
_gt->setFilterMatrixAs(osgTerrain::GeometryTechnique::SHARPEN);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ea.getKey() == '+')
|
|
|
|
{
|
|
|
|
_gt->setFilterWidth(_gt->getFilterWidth()*1.1);
|
|
|
|
osg::notify(osg::NOTICE)<<"Filter width = "<<_gt->getFilterWidth()<<std::endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ea.getKey() == '-')
|
|
|
|
{
|
|
|
|
_gt->setFilterWidth(_gt->getFilterWidth()/1.1);
|
|
|
|
osg::notify(osg::NOTICE)<<"Filter width = "<<_gt->getFilterWidth()<<std::endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ea.getKey() == '>')
|
|
|
|
{
|
|
|
|
_gt->setFilterBias(_gt->getFilterBias()+0.1);
|
|
|
|
osg::notify(osg::NOTICE)<<"Filter bias = "<<_gt->getFilterBias()<<std::endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ea.getKey() == '<')
|
|
|
|
{
|
|
|
|
_gt->setFilterBias(_gt->getFilterBias()-0.1);
|
|
|
|
osg::notify(osg::NOTICE)<<"Filter bias = "<<_gt->getFilterBias()<<std::endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
osg::observer_ptr<osgTerrain::GeometryTechnique> _gt;
|
2007-06-18 20:10:46 +08:00
|
|
|
|
|
|
|
};
|
2007-03-14 20:43:29 +08:00
|
|
|
|
2007-03-26 23:52:22 +08:00
|
|
|
|
|
|
|
|
2007-07-07 00:47:08 +08:00
|
|
|
class LayerHandler : public osgGA::GUIEventHandler
|
|
|
|
{
|
2007-07-17 01:40:38 +08:00
|
|
|
public:
|
2007-07-07 00:47:08 +08:00
|
|
|
|
2007-07-17 01:40:38 +08:00
|
|
|
LayerHandler(osgTerrain::Layer* layer):
|
|
|
|
_layer(layer) {}
|
|
|
|
|
|
|
|
bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
|
|
|
|
{
|
|
|
|
if (!_layer) return false;
|
2007-07-07 00:47:08 +08:00
|
|
|
|
2007-07-17 01:40:38 +08:00
|
|
|
float scale = 1.2;
|
|
|
|
|
|
|
|
switch(ea.getEventType())
|
2007-07-07 00:47:08 +08:00
|
|
|
{
|
2007-07-17 01:40:38 +08:00
|
|
|
case(osgGA::GUIEventAdapter::KEYDOWN):
|
2007-07-07 00:47:08 +08:00
|
|
|
{
|
2007-07-17 01:40:38 +08:00
|
|
|
if (ea.getKey() == 'q')
|
2007-07-07 00:47:08 +08:00
|
|
|
{
|
2007-07-17 01:40:38 +08:00
|
|
|
_layer->transform(0.0, scale);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ea.getKey() == 'a')
|
|
|
|
{
|
|
|
|
_layer->transform(0.0, 1.0f/scale);
|
|
|
|
return true;
|
2007-07-07 00:47:08 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-07-17 01:40:38 +08:00
|
|
|
default:
|
|
|
|
break;
|
2007-07-07 00:47:08 +08:00
|
|
|
}
|
2007-07-17 01:40:38 +08:00
|
|
|
return false;
|
2007-07-07 00:47:08 +08:00
|
|
|
|
2007-07-17 01:40:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2007-07-07 00:47:08 +08:00
|
|
|
|
2007-07-17 01:40:38 +08:00
|
|
|
osg::observer_ptr<osgTerrain::Layer> _layer;
|
2007-07-07 00:47:08 +08:00
|
|
|
};
|
|
|
|
|
2007-03-19 18:38:44 +08:00
|
|
|
int main(int argc, char** argv)
|
2007-03-14 20:43:29 +08:00
|
|
|
{
|
2007-03-19 18:38:44 +08:00
|
|
|
osg::ArgumentParser arguments(&argc, argv);
|
2007-03-14 20:43:29 +08:00
|
|
|
|
2007-03-19 18:38:44 +08:00
|
|
|
// construct the viewer.
|
2007-08-11 22:49:14 +08:00
|
|
|
osgViewer::Viewer viewer(arguments);
|
2007-03-14 20:43:29 +08:00
|
|
|
|
2007-03-29 22:45:35 +08:00
|
|
|
// set up the camera manipulators.
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
|
|
|
|
|
|
|
|
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() );
|
|
|
|
|
|
|
|
std::string pathfile;
|
|
|
|
char keyForAnimationPath = '5';
|
|
|
|
while (arguments.read("-p",pathfile))
|
|
|
|
{
|
|
|
|
osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
|
|
|
|
if (apm || !apm->valid())
|
|
|
|
{
|
|
|
|
unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
|
|
|
|
keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
|
|
|
|
keyswitchManipulator->selectMatrixManipulator(num);
|
|
|
|
++keyForAnimationPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
viewer.setCameraManipulator( keyswitchManipulator.get() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// add the state manipulator
|
|
|
|
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
|
|
|
|
|
|
|
|
// add the stats handler
|
|
|
|
viewer.addEventHandler(new osgViewer::StatsHandler);
|
|
|
|
|
2007-06-18 20:10:46 +08:00
|
|
|
// add the record camera path handler
|
|
|
|
viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
|
|
|
|
|
2009-03-08 20:00:36 +08:00
|
|
|
// attach an IncrementaCompileOperation to allow the master loading
|
|
|
|
// to be handled with an incremental compile to avoid frame drops when large objects are added.
|
|
|
|
viewer.setIncrementalCompileOperation(new osgUtil::IncrementalCompileOperation());
|
2007-06-18 20:10:46 +08:00
|
|
|
|
2007-03-19 18:38:44 +08:00
|
|
|
double x = 0.0;
|
|
|
|
double y = 0.0;
|
|
|
|
double w = 1.0;
|
|
|
|
double h = 1.0;
|
2007-07-17 05:39:30 +08:00
|
|
|
|
2007-07-19 20:09:25 +08:00
|
|
|
unsigned int numLoadThreads = 1;
|
|
|
|
while (arguments.read("--load-threads",numLoadThreads)) { }
|
2007-07-17 18:54:17 +08:00
|
|
|
|
2007-07-17 05:39:30 +08:00
|
|
|
osg::ref_ptr<MasterOperation> masterOperation;
|
|
|
|
std::string masterFilename;
|
|
|
|
while(arguments.read("-m",masterFilename))
|
2007-07-17 01:40:38 +08:00
|
|
|
{
|
2009-03-08 20:00:36 +08:00
|
|
|
masterOperation = new MasterOperation(masterFilename, viewer.getIncrementalCompileOperation());
|
2007-07-17 01:40:38 +08:00
|
|
|
}
|
2007-07-16 20:37:39 +08:00
|
|
|
|
2007-03-14 20:43:29 +08:00
|
|
|
|
2008-03-27 18:55:39 +08:00
|
|
|
osg::ref_ptr<osgTerrain::TerrainTile> terrainTile = new osgTerrain::TerrainTile;
|
2007-08-28 00:59:51 +08:00
|
|
|
osg::ref_ptr<osgTerrain::Locator> locator = new osgTerrain::Locator;
|
2007-05-12 02:25:06 +08:00
|
|
|
osg::ref_ptr<osgTerrain::ValidDataOperator> validDataOperator = new osgTerrain::NoDataValue(0.0);
|
2007-07-07 00:47:08 +08:00
|
|
|
osg::ref_ptr<osgTerrain::Layer> lastAppliedLayer;
|
2007-03-26 23:52:22 +08:00
|
|
|
|
2007-08-28 00:59:51 +08:00
|
|
|
locator->setCoordinateSystemType(osgTerrain::Locator::GEOCENTRIC);
|
2007-08-31 03:26:52 +08:00
|
|
|
locator->setTransformAsExtents(-osg::PI, -osg::PI*0.5, osg::PI, osg::PI*0.5);
|
2007-08-28 00:59:51 +08:00
|
|
|
|
2007-04-11 19:20:42 +08:00
|
|
|
unsigned int layerNum = 0;
|
|
|
|
|
2007-04-30 17:47:35 +08:00
|
|
|
std::string filterName;
|
|
|
|
|
2008-10-20 16:43:25 +08:00
|
|
|
osg::Texture::FilterMode filter = osg::Texture::LINEAR;
|
2008-02-22 19:52:23 +08:00
|
|
|
|
2007-03-19 18:38:44 +08:00
|
|
|
bool readParameter = false;
|
2007-04-11 19:20:42 +08:00
|
|
|
float minValue, maxValue;
|
2007-05-11 02:07:54 +08:00
|
|
|
float scale = 1.0f;
|
|
|
|
float offset = 0.0f;
|
|
|
|
|
2007-04-11 19:20:42 +08:00
|
|
|
int pos = 1;
|
|
|
|
while(pos<arguments.argc())
|
2007-03-14 20:43:29 +08:00
|
|
|
{
|
2007-03-19 18:38:44 +08:00
|
|
|
readParameter = false;
|
|
|
|
std::string filename;
|
|
|
|
|
2007-04-11 19:20:42 +08:00
|
|
|
if (arguments.read(pos, "--layer",layerNum))
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Set layer number to "<<layerNum<<std::endl;
|
|
|
|
readParameter = true;
|
|
|
|
}
|
|
|
|
|
2007-05-13 17:54:51 +08:00
|
|
|
else if (arguments.read(pos, "-b"))
|
|
|
|
{
|
2008-03-27 18:55:39 +08:00
|
|
|
terrainTile->setTreatBoundariesToValidDataAsDefaultValue(true);
|
2007-05-13 17:54:51 +08:00
|
|
|
}
|
|
|
|
|
2007-04-11 19:20:42 +08:00
|
|
|
else if (arguments.read(pos, "-e",x,y,w,h))
|
2007-03-26 23:52:22 +08:00
|
|
|
{
|
2007-03-27 19:23:57 +08:00
|
|
|
// define the extents.
|
2007-08-28 00:59:51 +08:00
|
|
|
locator->setCoordinateSystemType(osgTerrain::Locator::GEOCENTRIC);
|
2007-08-31 03:26:52 +08:00
|
|
|
locator->setTransformAsExtents(x,y,x+w,y+h);
|
2007-03-26 23:52:22 +08:00
|
|
|
readParameter = true;
|
|
|
|
}
|
2007-03-19 18:38:44 +08:00
|
|
|
|
2007-05-11 02:07:54 +08:00
|
|
|
else if (arguments.read(pos, "--transform",offset, scale) || arguments.read(pos, "-t",offset, scale))
|
|
|
|
{
|
|
|
|
// define the extents.
|
|
|
|
readParameter = true;
|
|
|
|
}
|
|
|
|
|
2007-07-13 04:10:56 +08:00
|
|
|
else if (arguments.read(pos, "--cartesian",x,y,w,h))
|
2007-05-04 00:23:19 +08:00
|
|
|
{
|
|
|
|
// define the extents.
|
2007-08-28 00:59:51 +08:00
|
|
|
locator->setCoordinateSystemType(osgTerrain::Locator::PROJECTED);
|
2007-08-31 03:26:52 +08:00
|
|
|
locator->setTransformAsExtents(x,y,x+w,y+h);
|
2007-05-04 00:23:19 +08:00
|
|
|
}
|
|
|
|
|
2007-04-11 19:20:42 +08:00
|
|
|
else if (arguments.read(pos, "--hf",filename))
|
2007-03-14 20:43:29 +08:00
|
|
|
{
|
2007-03-19 18:38:44 +08:00
|
|
|
readParameter = true;
|
2007-03-26 23:52:22 +08:00
|
|
|
|
2007-03-29 00:28:20 +08:00
|
|
|
osg::notify(osg::NOTICE)<<"--hf "<<filename<<std::endl;
|
2007-03-26 23:52:22 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::HeightField> hf = osgDB::readHeightFieldFile(filename);
|
|
|
|
if (hf.valid())
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osgTerrain::HeightFieldLayer> hfl = new osgTerrain::HeightFieldLayer;
|
|
|
|
hfl->setHeightField(hf.get());
|
|
|
|
|
|
|
|
hfl->setLocator(locator.get());
|
2007-05-12 02:25:06 +08:00
|
|
|
hfl->setValidDataOperator(validDataOperator.get());
|
2008-10-20 16:43:25 +08:00
|
|
|
hfl->setMagFilter(filter);
|
2007-03-26 23:52:22 +08:00
|
|
|
|
2007-05-11 02:07:54 +08:00
|
|
|
if (offset!=0.0f || scale!=1.0f)
|
|
|
|
{
|
|
|
|
hfl->transform(offset,scale);
|
|
|
|
}
|
|
|
|
|
2008-03-27 18:55:39 +08:00
|
|
|
terrainTile->setElevationLayer(hfl.get());
|
2007-03-26 23:52:22 +08:00
|
|
|
|
2007-07-07 00:47:08 +08:00
|
|
|
lastAppliedLayer = hfl.get();
|
|
|
|
|
2007-03-26 23:52:22 +08:00
|
|
|
osg::notify(osg::NOTICE)<<"created osgTerrain::HeightFieldLayer"<<std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"failed to create osgTerrain::HeightFieldLayer"<<std::endl;
|
|
|
|
}
|
|
|
|
|
2007-05-11 02:07:54 +08:00
|
|
|
scale = 1.0f;
|
|
|
|
offset = 0.0f;
|
|
|
|
|
2007-03-19 18:38:44 +08:00
|
|
|
}
|
2007-04-11 19:20:42 +08:00
|
|
|
|
|
|
|
else if (arguments.read(pos, "-d",filename) || arguments.read(pos, "--elevation-image",filename))
|
2007-03-26 23:52:22 +08:00
|
|
|
{
|
|
|
|
readParameter = true;
|
2007-03-29 00:28:20 +08:00
|
|
|
osg::notify(osg::NOTICE)<<"--elevation-image "<<filename<<std::endl;
|
2007-03-26 23:52:22 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::Image> image = osgDB::readImageFile(filename);
|
|
|
|
if (image.valid())
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osgTerrain::ImageLayer> imageLayer = new osgTerrain::ImageLayer;
|
|
|
|
imageLayer->setImage(image.get());
|
|
|
|
imageLayer->setLocator(locator.get());
|
2007-05-12 02:25:06 +08:00
|
|
|
imageLayer->setValidDataOperator(validDataOperator.get());
|
2008-10-20 16:43:25 +08:00
|
|
|
imageLayer->setMagFilter(filter);
|
2007-03-26 23:52:22 +08:00
|
|
|
|
2007-05-11 02:07:54 +08:00
|
|
|
if (offset!=0.0f || scale!=1.0f)
|
|
|
|
{
|
|
|
|
imageLayer->transform(offset,scale);
|
|
|
|
}
|
|
|
|
|
2008-03-27 18:55:39 +08:00
|
|
|
terrainTile->setElevationLayer(imageLayer.get());
|
2007-03-26 23:52:22 +08:00
|
|
|
|
2007-07-07 00:47:08 +08:00
|
|
|
lastAppliedLayer = imageLayer.get();
|
|
|
|
|
2007-03-26 23:52:22 +08:00
|
|
|
osg::notify(osg::NOTICE)<<"created Elevation osgTerrain::ImageLayer"<<std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"failed to create osgTerrain::ImageLayer"<<std::endl;
|
|
|
|
}
|
2007-05-11 02:07:54 +08:00
|
|
|
|
|
|
|
scale = 1.0f;
|
|
|
|
offset = 0.0f;
|
|
|
|
|
2007-03-26 23:52:22 +08:00
|
|
|
}
|
2007-04-11 19:20:42 +08:00
|
|
|
|
|
|
|
else if (arguments.read(pos, "-c",filename) || arguments.read(pos, "--image",filename))
|
2007-03-19 18:38:44 +08:00
|
|
|
{
|
|
|
|
readParameter = true;
|
|
|
|
osg::notify(osg::NOTICE)<<"--image "<<filename<<" x="<<x<<" y="<<y<<" w="<<w<<" h="<<h<<std::endl;
|
2007-03-26 23:52:22 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::Image> image = osgDB::readImageFile(filename);
|
|
|
|
if (image.valid())
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osgTerrain::ImageLayer> imageLayer = new osgTerrain::ImageLayer;
|
|
|
|
imageLayer->setImage(image.get());
|
|
|
|
imageLayer->setLocator(locator.get());
|
2007-05-12 02:25:06 +08:00
|
|
|
imageLayer->setValidDataOperator(validDataOperator.get());
|
2008-10-20 16:43:25 +08:00
|
|
|
imageLayer->setMagFilter(filter);
|
2007-03-26 23:52:22 +08:00
|
|
|
|
2007-05-11 02:07:54 +08:00
|
|
|
if (offset!=0.0f || scale!=1.0f)
|
|
|
|
{
|
|
|
|
imageLayer->transform(offset,scale);
|
|
|
|
}
|
|
|
|
|
2008-03-27 18:55:39 +08:00
|
|
|
terrainTile->setColorLayer(layerNum, imageLayer.get());
|
2007-07-07 00:47:08 +08:00
|
|
|
|
|
|
|
lastAppliedLayer = imageLayer.get();
|
|
|
|
|
2007-03-26 23:52:22 +08:00
|
|
|
osg::notify(osg::NOTICE)<<"created Color osgTerrain::ImageLayer"<<std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"failed to create osgTerrain::ImageLayer"<<std::endl;
|
|
|
|
}
|
2007-05-11 02:07:54 +08:00
|
|
|
|
|
|
|
scale = 1.0f;
|
|
|
|
offset = 0.0f;
|
|
|
|
|
2007-03-14 20:43:29 +08:00
|
|
|
}
|
|
|
|
|
2007-04-30 17:47:35 +08:00
|
|
|
else if (arguments.read(pos, "--filter",filterName))
|
|
|
|
{
|
|
|
|
readParameter = true;
|
|
|
|
|
|
|
|
if (filterName=="NEAREST")
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"--filter "<<filterName<<std::endl;
|
2008-10-20 16:43:25 +08:00
|
|
|
filter = osg::Texture::NEAREST;
|
2007-04-30 17:47:35 +08:00
|
|
|
}
|
2007-06-18 20:10:46 +08:00
|
|
|
else if (filterName=="LINEAR")
|
2007-04-30 17:47:35 +08:00
|
|
|
{
|
2008-10-20 16:43:25 +08:00
|
|
|
filter = osg::Texture::LINEAR;
|
2007-04-30 17:47:35 +08:00
|
|
|
osg::notify(osg::NOTICE)<<"--filter "<<filterName<<std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"--filter "<<filterName<<" unrecognized filter name, please use LINEAER or NEAREST."<<std::endl;
|
|
|
|
}
|
2008-02-22 19:52:23 +08:00
|
|
|
|
2008-03-27 18:55:39 +08:00
|
|
|
if (terrainTile->getColorLayer(layerNum))
|
2008-02-22 19:52:23 +08:00
|
|
|
{
|
2008-10-20 16:43:25 +08:00
|
|
|
terrainTile->getColorLayer(layerNum)->setMagFilter(filter);
|
2008-02-22 19:52:23 +08:00
|
|
|
}
|
2007-04-30 17:47:35 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-04-11 19:20:42 +08:00
|
|
|
else if (arguments.read(pos, "--tf",minValue, maxValue))
|
2007-04-02 21:06:59 +08:00
|
|
|
{
|
|
|
|
readParameter = true;
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::TransferFunction1D> tf = new osg::TransferFunction1D;
|
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
unsigned int numCells = 6;
|
|
|
|
float delta = (maxValue-minValue)/float(numCells-1);
|
|
|
|
float v = minValue;
|
2007-04-02 21:06:59 +08:00
|
|
|
|
|
|
|
tf->allocate(6);
|
2009-02-02 22:43:27 +08:00
|
|
|
tf->setColor(v, osg::Vec4(1.0,1.0,1.0,1.0)); v += delta;
|
|
|
|
tf->setColor(v, osg::Vec4(1.0,0.0,1.0,1.0)); v += delta;
|
|
|
|
tf->setColor(v, osg::Vec4(1.0,0.0,0.0,1.0)); v += delta;
|
|
|
|
tf->setColor(v, osg::Vec4(1.0,1.0,0.0,1.0)); v += delta;
|
|
|
|
tf->setColor(v, osg::Vec4(0.0,1.0,1.0,1.0)); v += delta;
|
|
|
|
tf->setColor(v, osg::Vec4(0.0,1.0,0.0,1.0));
|
2007-04-02 21:06:59 +08:00
|
|
|
|
|
|
|
osg::notify(osg::NOTICE)<<"--tf "<<minValue<<" "<<maxValue<<std::endl;
|
|
|
|
|
2008-03-27 18:55:39 +08:00
|
|
|
terrainTile->setColorLayer(layerNum, new osgTerrain::ContourLayer(tf.get()));
|
2007-04-11 19:20:42 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++pos;
|
2007-04-02 21:06:59 +08:00
|
|
|
}
|
|
|
|
|
2007-04-11 19:20:42 +08:00
|
|
|
}
|
2007-03-19 18:38:44 +08:00
|
|
|
|
2007-03-14 20:43:29 +08:00
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
osg::ref_ptr<osg::Group> scene = new osg::Group;
|
2007-06-18 20:10:46 +08:00
|
|
|
|
2008-03-27 18:55:39 +08:00
|
|
|
if (terrainTile.valid() && (terrainTile->getElevationLayer() || terrainTile->getColorLayer(0)))
|
2007-07-18 00:18:13 +08:00
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Terrain created"<<std::endl;
|
2007-03-26 23:52:22 +08:00
|
|
|
|
2008-03-27 18:55:39 +08:00
|
|
|
scene->addChild(terrainTile.get());
|
2007-07-11 01:36:33 +08:00
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
osg::ref_ptr<osgTerrain::GeometryTechnique> geometryTechnique = new osgTerrain::GeometryTechnique;
|
2008-03-27 18:55:39 +08:00
|
|
|
terrainTile->setTerrainTechnique(geometryTechnique.get());
|
2007-07-18 00:18:13 +08:00
|
|
|
viewer.addEventHandler(new FilterHandler(geometryTechnique.get()));
|
|
|
|
viewer.addEventHandler(new LayerHandler(lastAppliedLayer.get()));
|
|
|
|
}
|
2007-07-11 01:36:33 +08:00
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
if (masterOperation.valid())
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Master operation created"<<std::endl;
|
2007-07-11 01:36:33 +08:00
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
masterOperation->open(scene.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scene->getNumChildren()==0)
|
2007-07-11 01:36:33 +08:00
|
|
|
{
|
2007-07-18 00:18:13 +08:00
|
|
|
osg::notify(osg::NOTICE)<<"No model created, please specify terrain or master file on command line."<<std::endl;
|
|
|
|
return 0;
|
2007-07-11 01:36:33 +08:00
|
|
|
}
|
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
viewer.setSceneData(scene.get());
|
2007-07-11 01:36:33 +08:00
|
|
|
|
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
// start operation thread if a master file has been used.
|
2007-07-19 20:09:25 +08:00
|
|
|
osg::ref_ptr<osg::OperationThread> masterOperationThread;
|
|
|
|
|
|
|
|
typedef std::list< osg::ref_ptr<osg::OperationThread> > OperationThreadList;
|
|
|
|
OperationThreadList generalThreadList;
|
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
if (masterOperation.valid())
|
|
|
|
{
|
2007-07-19 20:09:25 +08:00
|
|
|
masterOperationThread = new osg::OperationThread;
|
|
|
|
masterOperationThread->startThread();
|
|
|
|
|
|
|
|
masterOperationThread->add(masterOperation.get());
|
|
|
|
|
|
|
|
// if (numLoadThreads>0)
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osg::OperationQueue> operationQueue = new osg::OperationQueue;
|
|
|
|
masterOperation->setOperationQueue(operationQueue.get());
|
|
|
|
|
|
|
|
for(unsigned int i=0; i<numLoadThreads; ++i)
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osg::OperationThread> thread = new osg::OperationThread;
|
|
|
|
thread->setOperationQueue(operationQueue.get());
|
|
|
|
thread->startThread();
|
|
|
|
generalThreadList.push_back(thread);
|
|
|
|
}
|
|
|
|
}
|
2007-08-11 20:43:31 +08:00
|
|
|
|
|
|
|
viewer.addUpdateOperation(masterOperation.get());
|
2007-07-18 00:18:13 +08:00
|
|
|
}
|
2007-07-19 00:17:06 +08:00
|
|
|
|
|
|
|
viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
|
2007-08-11 22:49:14 +08:00
|
|
|
|
|
|
|
// enable the use of compile contexts and associated threads.
|
|
|
|
osg::DisplaySettings::instance()->setCompileContextsHint(true);
|
2007-07-11 01:36:33 +08:00
|
|
|
|
2007-07-18 00:18:13 +08:00
|
|
|
// realize the graphics windows.
|
|
|
|
viewer.realize();
|
2007-07-11 01:36:33 +08:00
|
|
|
|
2007-07-19 00:17:06 +08:00
|
|
|
// run the viewers main loop
|
|
|
|
return viewer.run();
|
2007-07-11 01:36:33 +08:00
|
|
|
|
|
|
|
}
|