dd996a3289
forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
196 lines
7.0 KiB
C++
196 lines
7.0 KiB
C++
/* -*-c++-*-
|
|
* Copyright (C) 2008 Cedric Pinson <mornifle@plopbyte.net>
|
|
*
|
|
* 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.
|
|
*
|
|
* This library 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
|
|
* OpenSceneGraph Public License for more details.
|
|
*
|
|
* Authors:
|
|
* Cedric Pinson <mornifle@plopbyte.net>
|
|
* jeremy Moles <jeremy@emperorlinux.com>
|
|
*/
|
|
|
|
#include "AnimtkViewerKeyHandler"
|
|
#include "AnimtkViewerGUI"
|
|
|
|
#include <iostream>
|
|
#include <osg/io_utils>
|
|
#include <osg/Geometry>
|
|
#include <osg/MatrixTransform>
|
|
#include <osg/Geode>
|
|
#include <osgDB/FileNameUtils>
|
|
#include <osgViewer/Viewer>
|
|
#include <osgViewer/ViewerEventHandlers>
|
|
#include <osgWidget/ViewerEventHandlers>
|
|
#include <osgGA/TrackballManipulator>
|
|
#include <osgGA/StateSetManipulator>
|
|
#include <osgDB/ReadFile>
|
|
#include <osgAnimation/AnimationManagerBase>
|
|
#include <osgAnimation/Bone>
|
|
|
|
const int WIDTH = 1440;
|
|
const int HEIGHT = 900;
|
|
|
|
|
|
osg::Geode* createAxis()
|
|
{
|
|
osg::Geode* geode = new osg::Geode();
|
|
osg::Geometry* geometry = new osg::Geometry();
|
|
osg::Vec3Array* vertices = new osg::Vec3Array();
|
|
osg::Vec4Array* colors = new osg::Vec4Array();
|
|
|
|
vertices->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));
|
|
vertices->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));
|
|
vertices->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));
|
|
vertices->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));
|
|
vertices->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));
|
|
vertices->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));
|
|
|
|
colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
|
|
colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
|
|
colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
|
|
colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
|
|
colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
|
|
colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
|
|
|
|
geometry->setVertexArray(vertices);
|
|
geometry->setColorArray(colors, osg::Array::BIND_PER_VERTEX);
|
|
geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 6));
|
|
geometry->getOrCreateStateSet()->setMode(GL_LIGHTING, false);
|
|
|
|
geode->addDrawable(geometry);
|
|
|
|
return geode;
|
|
}
|
|
|
|
|
|
struct AnimationManagerFinder : public osg::NodeVisitor
|
|
{
|
|
osg::ref_ptr<osgAnimation::BasicAnimationManager> _am;
|
|
AnimationManagerFinder() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
|
|
void apply(osg::Node& node) {
|
|
if (_am.valid())
|
|
return;
|
|
if (node.getUpdateCallback()) {
|
|
osgAnimation::AnimationManagerBase* b = dynamic_cast<osgAnimation::AnimationManagerBase*>(node.getUpdateCallback());
|
|
if (b) {
|
|
_am = new osgAnimation::BasicAnimationManager(*b);
|
|
return;
|
|
}
|
|
}
|
|
traverse(node);
|
|
}
|
|
};
|
|
|
|
|
|
struct AddHelperBone : public osg::NodeVisitor
|
|
{
|
|
AddHelperBone() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
|
|
void apply(osg::Transform& node) {
|
|
osgAnimation::Bone* bone = dynamic_cast<osgAnimation::Bone*>(&node);
|
|
if (bone)
|
|
bone->addChild(createAxis());
|
|
traverse(node);
|
|
}
|
|
};
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
osg::ArgumentParser arguments(&argc, argv);
|
|
arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
|
|
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is an example for viewing osgAnimation animations.");
|
|
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","List command line options.");
|
|
arguments.getApplicationUsage()->addCommandLineOption("--drawbone","draw helps to display bones.");
|
|
|
|
if (arguments.read("-h") || arguments.read("--help"))
|
|
{
|
|
arguments.getApplicationUsage()->write(std::cout, osg::ApplicationUsage::COMMAND_LINE_OPTION);
|
|
return 0;
|
|
}
|
|
|
|
if (arguments.argc()<=1)
|
|
{
|
|
arguments.getApplicationUsage()->write(std::cout, osg::ApplicationUsage::COMMAND_LINE_OPTION);
|
|
return 1;
|
|
}
|
|
|
|
bool drawBone = false;
|
|
if (arguments.read("--drawbone"))
|
|
drawBone = true;
|
|
|
|
osgViewer::Viewer viewer(arguments);
|
|
osg::ref_ptr<osg::Group> group = new osg::Group();
|
|
|
|
osg::ref_ptr<osg::Node> loadedmodel = osgDB::readRefNodeFiles(arguments);
|
|
osg::Group* node = dynamic_cast<osg::Group*>(loadedmodel.get());
|
|
if(!node)
|
|
{
|
|
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
// Set our Singleton's model.
|
|
AnimationManagerFinder finder;
|
|
node->accept(finder);
|
|
if (finder._am.valid())
|
|
{
|
|
|
|
std::string playModeOpt;
|
|
if (arguments.read("--play-mode", playModeOpt))
|
|
{
|
|
osgAnimation::Animation::PlayMode playMode = osgAnimation::Animation::LOOP;
|
|
if (osgDB::equalCaseInsensitive(playModeOpt, "ONCE")) playMode = osgAnimation::Animation::ONCE;
|
|
else if (osgDB::equalCaseInsensitive(playModeOpt, "STAY")) playMode = osgAnimation::Animation::STAY;
|
|
else if (osgDB::equalCaseInsensitive(playModeOpt, "LOOP")) playMode = osgAnimation::Animation::LOOP;
|
|
else if (osgDB::equalCaseInsensitive(playModeOpt, "PPONG")) playMode = osgAnimation::Animation::PPONG;
|
|
|
|
for (osgAnimation::AnimationList::const_iterator animIter = finder._am->getAnimationList().begin();
|
|
animIter != finder._am->getAnimationList().end(); ++animIter)
|
|
{
|
|
(*animIter)->setPlayMode(playMode);
|
|
}
|
|
}
|
|
|
|
node->setUpdateCallback(finder._am.get());
|
|
AnimtkViewerModelController::setModel(finder._am.get());
|
|
}
|
|
else
|
|
{
|
|
osg::notify(osg::WARN) << "no osgAnimation::AnimationManagerBase found in the subgraph, no animations available" << std::endl;
|
|
}
|
|
|
|
if (drawBone) {
|
|
osg::notify(osg::INFO) << "Add Bones Helper" << std::endl;
|
|
AddHelperBone addHelper;
|
|
node->accept(addHelper);
|
|
}
|
|
node->addChild(createAxis());
|
|
|
|
AnimtkViewerGUI* gui = new AnimtkViewerGUI(&viewer, WIDTH, HEIGHT, 0x1234);
|
|
osg::Camera* camera = gui->createParentOrthoCamera();
|
|
|
|
node->setNodeMask(0x0001);
|
|
|
|
group->addChild(node);
|
|
group->addChild(camera);
|
|
|
|
viewer.addEventHandler(new AnimtkKeyEventHandler());
|
|
viewer.addEventHandler(new osgViewer::StatsHandler());
|
|
viewer.addEventHandler(new osgViewer::WindowSizeHandler());
|
|
viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
|
|
viewer.addEventHandler(new osgWidget::MouseHandler(gui));
|
|
viewer.addEventHandler(new osgWidget::KeyboardHandler(gui));
|
|
viewer.addEventHandler(new osgWidget::ResizeHandler(gui, camera));
|
|
viewer.setSceneData(group.get());
|
|
|
|
viewer.setUpViewInWindow(40, 40, WIDTH, HEIGHT);
|
|
|
|
return viewer.run();
|
|
}
|