2013-06-27 17:59:33 +08:00
|
|
|
/* -*-c++-*-
|
2008-11-22 20:14:19 +08:00
|
|
|
* Copyright (C) 2008 Cedric Pinson <mornifle@plopbyte.net>
|
|
|
|
*
|
2013-06-27 17:59:33 +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
|
2008-11-22 20:14:19 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2013-06-27 17:59:33 +08:00
|
|
|
*
|
2008-11-22 20:14:19 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2013-06-27 17:59:33 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2008-11-22 20:14:19 +08:00
|
|
|
* 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>
|
2015-03-16 18:07:13 +08:00
|
|
|
#include <osgDB/FileNameUtils>
|
2008-11-22 20:14:19 +08:00
|
|
|
#include <osgViewer/Viewer>
|
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
|
|
|
#include <osgWidget/ViewerEventHandlers>
|
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
#include <osgGA/StateSetManipulator>
|
|
|
|
#include <osgDB/ReadFile>
|
2008-12-17 04:29:00 +08:00
|
|
|
#include <osgAnimation/AnimationManagerBase>
|
2009-01-22 03:02:54 +08:00
|
|
|
#include <osgAnimation/Bone>
|
2008-11-22 20:14:19 +08:00
|
|
|
|
|
|
|
const int WIDTH = 1440;
|
|
|
|
const int HEIGHT = 900;
|
|
|
|
|
|
|
|
|
2013-06-27 17:59:33 +08:00
|
|
|
osg::Geode* createAxis()
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
2013-06-27 17:59:33 +08:00
|
|
|
osg::Geode* geode = new osg::Geode();
|
2008-11-22 20:14:19 +08:00
|
|
|
osg::Geometry* geometry = new osg::Geometry();
|
|
|
|
osg::Vec3Array* vertices = new osg::Vec3Array();
|
|
|
|
osg::Vec4Array* colors = new osg::Vec4Array();
|
2013-06-27 17:59:33 +08:00
|
|
|
|
2008-11-22 20:14:19 +08:00
|
|
|
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));
|
2013-06-27 17:59:33 +08:00
|
|
|
|
2008-11-22 20:14:19 +08:00
|
|
|
geometry->setVertexArray(vertices);
|
2013-06-27 17:59:33 +08:00
|
|
|
geometry->setColorArray(colors, osg::Array::BIND_PER_VERTEX);
|
2008-11-22 20:14:19 +08:00
|
|
|
geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 6));
|
|
|
|
geometry->getOrCreateStateSet()->setMode(GL_LIGHTING, false);
|
|
|
|
|
|
|
|
geode->addDrawable(geometry);
|
|
|
|
|
|
|
|
return geode;
|
|
|
|
}
|
|
|
|
|
2009-01-22 03:02:54 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-27 17:59:33 +08:00
|
|
|
int main(int argc, char** argv)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
2009-01-22 03:02:54 +08:00
|
|
|
osg::ArgumentParser arguments(&argc, argv);
|
|
|
|
arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
|
2009-02-20 19:53:49 +08:00
|
|
|
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is an example for viewing osgAnimation animations.");
|
2009-01-22 03:02:54 +08:00
|
|
|
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;
|
|
|
|
}
|
2008-11-22 20:14:19 +08:00
|
|
|
|
2009-01-22 03:02:54 +08:00
|
|
|
if (arguments.argc()<=1)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
2009-01-22 03:02:54 +08:00
|
|
|
arguments.getApplicationUsage()->write(std::cout, osg::ApplicationUsage::COMMAND_LINE_OPTION);
|
2008-11-22 20:14:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-01-22 03:02:54 +08:00
|
|
|
bool drawBone = false;
|
|
|
|
if (arguments.read("--drawbone"))
|
|
|
|
drawBone = true;
|
|
|
|
|
|
|
|
osgViewer::Viewer viewer(arguments);
|
2008-11-22 20:14:19 +08:00
|
|
|
osg::ref_ptr<osg::Group> group = new osg::Group();
|
|
|
|
|
2015-10-22 22:14:53 +08:00
|
|
|
osg::ref_ptr<osg::Node> loadedmodel = osgDB::readRefNodeFiles(arguments);
|
|
|
|
osg::Group* node = dynamic_cast<osg::Group*>(loadedmodel.get());
|
2009-01-22 03:02:54 +08:00
|
|
|
if(!node)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
2009-01-22 03:02:54 +08:00
|
|
|
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
|
2008-11-22 20:14:19 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set our Singleton's model.
|
2009-01-22 03:02:54 +08:00
|
|
|
AnimationManagerFinder finder;
|
|
|
|
node->accept(finder);
|
2015-03-16 18:07:13 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-22 03:02:54 +08:00
|
|
|
node->setUpdateCallback(finder._am.get());
|
|
|
|
AnimtkViewerModelController::setModel(finder._am.get());
|
2015-03-16 18:07:13 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-22 03:02:54 +08:00
|
|
|
osg::notify(osg::WARN) << "no osgAnimation::AnimationManagerBase found in the subgraph, no animations available" << std::endl;
|
|
|
|
}
|
2008-11-22 20:14:19 +08:00
|
|
|
|
2009-01-22 03:02:54 +08:00
|
|
|
if (drawBone) {
|
|
|
|
osg::notify(osg::INFO) << "Add Bones Helper" << std::endl;
|
|
|
|
AddHelperBone addHelper;
|
|
|
|
node->accept(addHelper);
|
|
|
|
}
|
2008-12-17 04:29:00 +08:00
|
|
|
node->addChild(createAxis());
|
2008-11-22 20:14:19 +08:00
|
|
|
|
|
|
|
AnimtkViewerGUI* gui = new AnimtkViewerGUI(&viewer, WIDTH, HEIGHT, 0x1234);
|
|
|
|
osg::Camera* camera = gui->createParentOrthoCamera();
|
2013-06-27 17:59:33 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
node->setNodeMask(0x0001);
|
2008-11-22 20:14:19 +08:00
|
|
|
|
2008-12-17 04:29:00 +08:00
|
|
|
group->addChild(node);
|
2008-11-22 20:14:19 +08:00
|
|
|
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();
|
|
|
|
}
|