root/OpenSceneGraph/trunk/examples/osganimationviewer/AnimtkViewer.cpp @ 12292

Revision 12292, 6.1 kB (checked in by robert, 4 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

  • Property svn:eol-style set to native
Line 
1/*  -*-c++-*-
2 *  Copyright (C) 2008 Cedric Pinson <mornifle@plopbyte.net>
3 *
4 * This library is open source and may be redistributed and/or modified under 
5 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
6 * (at your option) any later version.  The full license is in LICENSE file
7 * included with this distribution, and on the openscenegraph.org website.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * OpenSceneGraph Public License for more details.
13 *
14 * Authors:
15 * Cedric Pinson <mornifle@plopbyte.net>
16 * jeremy Moles <jeremy@emperorlinux.com>
17*/
18
19#include "AnimtkViewerKeyHandler"
20#include "AnimtkViewerGUI"
21
22#include <iostream>
23#include <osg/io_utils>
24#include <osg/Geometry>
25#include <osg/MatrixTransform>
26#include <osg/Geode>
27#include <osgViewer/Viewer>
28#include <osgViewer/ViewerEventHandlers>
29#include <osgWidget/ViewerEventHandlers>
30#include <osgGA/TrackballManipulator>
31#include <osgGA/StateSetManipulator>
32#include <osgDB/ReadFile>
33#include <osgAnimation/AnimationManagerBase>
34#include <osgAnimation/Bone>
35
36const int WIDTH  = 1440;
37const int HEIGHT = 900;
38
39
40osg::Geode* createAxis()
41{
42    osg::Geode*     geode    = new osg::Geode(); 
43    osg::Geometry*  geometry = new osg::Geometry();
44    osg::Vec3Array* vertices = new osg::Vec3Array();
45    osg::Vec4Array* colors   = new osg::Vec4Array();
46   
47    vertices->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));
48    vertices->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));
49    vertices->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));
50    vertices->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));
51    vertices->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));
52    vertices->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));
53
54    colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
55    colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
56    colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
57    colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
58    colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
59    colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
60   
61    geometry->setVertexArray(vertices);
62    geometry->setColorArray(colors);
63    geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);   
64    geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 6));
65    geometry->getOrCreateStateSet()->setMode(GL_LIGHTING, false);
66
67    geode->addDrawable(geometry);
68
69    return geode;
70}
71
72
73struct AnimationManagerFinder : public osg::NodeVisitor
74{
75    osg::ref_ptr<osgAnimation::BasicAnimationManager> _am;
76    AnimationManagerFinder() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
77    void apply(osg::Node& node) {
78        if (_am.valid())
79            return;
80        if (node.getUpdateCallback()) {
81            osgAnimation::AnimationManagerBase* b = dynamic_cast<osgAnimation::AnimationManagerBase*>(node.getUpdateCallback());
82            if (b) {
83                _am = new osgAnimation::BasicAnimationManager(*b);
84                return;
85            }
86        }
87        traverse(node);
88    }
89};
90
91
92struct AddHelperBone : public osg::NodeVisitor
93{
94    AddHelperBone() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
95    void apply(osg::Transform& node) {
96        osgAnimation::Bone* bone = dynamic_cast<osgAnimation::Bone*>(&node);
97        if (bone)
98            bone->addChild(createAxis());
99        traverse(node);
100    }
101};
102
103int main(int argc, char** argv)
104{
105    osg::ArgumentParser arguments(&argc, argv);
106    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
107    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is an example for viewing osgAnimation animations.");
108    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","List command line options.");
109    arguments.getApplicationUsage()->addCommandLineOption("--drawbone","draw helps to display bones.");
110
111    if (arguments.read("-h") || arguments.read("--help"))
112    {
113        arguments.getApplicationUsage()->write(std::cout, osg::ApplicationUsage::COMMAND_LINE_OPTION);
114        return 0;
115    }
116
117    if (arguments.argc()<=1)
118    {
119        arguments.getApplicationUsage()->write(std::cout, osg::ApplicationUsage::COMMAND_LINE_OPTION);
120        return 1;
121    }
122
123    bool drawBone = false;
124    if (arguments.read("--drawbone"))
125        drawBone = true;
126
127    osgViewer::Viewer viewer(arguments);
128    osg::ref_ptr<osg::Group> group = new osg::Group();
129
130    osg::Group* node = dynamic_cast<osg::Group*>(osgDB::readNodeFiles(arguments)); //dynamic_cast<osgAnimation::AnimationManager*>(osgDB::readNodeFile(psr[1]));
131    if(!node)
132    {
133        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
134        return 1;
135    }
136
137    // Set our Singleton's model.
138    AnimationManagerFinder finder;
139    node->accept(finder);
140    if (finder._am.valid()) {
141        node->setUpdateCallback(finder._am.get());
142        AnimtkViewerModelController::setModel(finder._am.get());
143    } else {
144        osg::notify(osg::WARN) << "no osgAnimation::AnimationManagerBase found in the subgraph, no animations available" << std::endl;
145    }
146
147    if (drawBone) {
148        osg::notify(osg::INFO) << "Add Bones Helper" << std::endl;
149        AddHelperBone addHelper;
150        node->accept(addHelper);
151    }
152    node->addChild(createAxis());
153
154    AnimtkViewerGUI* gui    = new AnimtkViewerGUI(&viewer, WIDTH, HEIGHT, 0x1234);
155    osg::Camera*     camera = gui->createParentOrthoCamera();
156   
157    node->setNodeMask(0x0001);
158
159    group->addChild(node);
160    group->addChild(camera);
161
162    viewer.addEventHandler(new AnimtkKeyEventHandler());
163    viewer.addEventHandler(new osgViewer::StatsHandler());
164    viewer.addEventHandler(new osgViewer::WindowSizeHandler());
165    viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
166    viewer.addEventHandler(new osgWidget::MouseHandler(gui));
167    viewer.addEventHandler(new osgWidget::KeyboardHandler(gui));
168    viewer.addEventHandler(new osgWidget::ResizeHandler(gui, camera));
169    viewer.setSceneData(group.get());
170
171    viewer.setUpViewInWindow(40, 40, WIDTH, HEIGHT);
172
173    return viewer.run();
174}
Note: See TracBrowser for help on using the browser.