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

Revision 13576, 6.1 kB (checked in by robert, 4 days ago)

Moved widgets from VolumeEditorWidget? to TransferFunctionWidget?, and widget utilities into WidgetUtils?.

  • 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, osg::Array::BIND_PER_VERTEX);
63    geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 6));
64    geometry->getOrCreateStateSet()->setMode(GL_LIGHTING, false);
65
66    geode->addDrawable(geometry);
67
68    return geode;
69}
70
71
72struct AnimationManagerFinder : public osg::NodeVisitor
73{
74    osg::ref_ptr<osgAnimation::BasicAnimationManager> _am;
75    AnimationManagerFinder() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
76    void apply(osg::Node& node) {
77        if (_am.valid())
78            return;
79        if (node.getUpdateCallback()) {
80            osgAnimation::AnimationManagerBase* b = dynamic_cast<osgAnimation::AnimationManagerBase*>(node.getUpdateCallback());
81            if (b) {
82                _am = new osgAnimation::BasicAnimationManager(*b);
83                return;
84            }
85        }
86        traverse(node);
87    }
88};
89
90
91struct AddHelperBone : public osg::NodeVisitor
92{
93    AddHelperBone() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
94    void apply(osg::Transform& node) {
95        osgAnimation::Bone* bone = dynamic_cast<osgAnimation::Bone*>(&node);
96        if (bone)
97            bone->addChild(createAxis());
98        traverse(node);
99    }
100};
101
102int main(int argc, char** argv)
103{
104    osg::ArgumentParser arguments(&argc, argv);
105    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
106    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is an example for viewing osgAnimation animations.");
107    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","List command line options.");
108    arguments.getApplicationUsage()->addCommandLineOption("--drawbone","draw helps to display bones.");
109
110    if (arguments.read("-h") || arguments.read("--help"))
111    {
112        arguments.getApplicationUsage()->write(std::cout, osg::ApplicationUsage::COMMAND_LINE_OPTION);
113        return 0;
114    }
115
116    if (arguments.argc()<=1)
117    {
118        arguments.getApplicationUsage()->write(std::cout, osg::ApplicationUsage::COMMAND_LINE_OPTION);
119        return 1;
120    }
121
122    bool drawBone = false;
123    if (arguments.read("--drawbone"))
124        drawBone = true;
125
126    osgViewer::Viewer viewer(arguments);
127    osg::ref_ptr<osg::Group> group = new osg::Group();
128
129    osg::Group* node = dynamic_cast<osg::Group*>(osgDB::readNodeFiles(arguments)); //dynamic_cast<osgAnimation::AnimationManager*>(osgDB::readNodeFile(psr[1]));
130    if(!node)
131    {
132        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
133        return 1;
134    }
135
136    // Set our Singleton's model.
137    AnimationManagerFinder finder;
138    node->accept(finder);
139    if (finder._am.valid()) {
140        node->setUpdateCallback(finder._am.get());
141        AnimtkViewerModelController::setModel(finder._am.get());
142    } else {
143        osg::notify(osg::WARN) << "no osgAnimation::AnimationManagerBase found in the subgraph, no animations available" << std::endl;
144    }
145
146    if (drawBone) {
147        osg::notify(osg::INFO) << "Add Bones Helper" << std::endl;
148        AddHelperBone addHelper;
149        node->accept(addHelper);
150    }
151    node->addChild(createAxis());
152
153    AnimtkViewerGUI* gui    = new AnimtkViewerGUI(&viewer, WIDTH, HEIGHT, 0x1234);
154    osg::Camera*     camera = gui->createParentOrthoCamera();
155
156    node->setNodeMask(0x0001);
157
158    group->addChild(node);
159    group->addChild(camera);
160
161    viewer.addEventHandler(new AnimtkKeyEventHandler());
162    viewer.addEventHandler(new osgViewer::StatsHandler());
163    viewer.addEventHandler(new osgViewer::WindowSizeHandler());
164    viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
165    viewer.addEventHandler(new osgWidget::MouseHandler(gui));
166    viewer.addEventHandler(new osgWidget::KeyboardHandler(gui));
167    viewer.addEventHandler(new osgWidget::ResizeHandler(gui, camera));
168    viewer.setSceneData(group.get());
169
170    viewer.setUpViewInWindow(40, 40, WIDTH, HEIGHT);
171
172    return viewer.run();
173}
Note: See TracBrowser for help on using the browser.