2008-11-22 20:14:19 +08:00
|
|
|
/* -*-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 <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;
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
2008-12-17 04:29:00 +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));
|
2008-12-17 04:29:00 +08:00
|
|
|
|
2008-11-22 20:14:19 +08:00
|
|
|
geometry->setVertexArray(vertices);
|
|
|
|
geometry->setColorArray(colors);
|
|
|
|
geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-11-22 20:14:19 +08:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
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();
|
|
|
|
|
2009-01-22 03:02:54 +08:00
|
|
|
osg::Group* node = dynamic_cast<osg::Group*>(osgDB::readNodeFiles(arguments)); //dynamic_cast<osgAnimation::AnimationManager*>(osgDB::readNodeFile(psr[1]));
|
|
|
|
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);
|
|
|
|
if (finder._am.valid()) {
|
|
|
|
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;
|
|
|
|
}
|
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();
|
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();
|
|
|
|
}
|