root/OpenSceneGraph/trunk/examples/osganimationskinning/osganimationskinning.cpp @ 9212

Revision 9104, 9.8 kB (checked in by cedricpinson, 5 years ago)

remove old timer
update example without initTimer
update the UpdateCallback? of AnimationManager? to use the FrameStamp?
split osganimationnode example in two piece
fix the -DOSGANIMATION_LIBRARY in the CMakeLists.txt of osgAnimation

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
15#include <iostream>
16#include <osg/Geometry>
17#include <osg/MatrixTransform>
18#include <osg/Geode>
19#include <osgViewer/Viewer>
20#include <osgGA/TrackballManipulator>
21#include <osgUtil/SmoothingVisitor>
22#include <osg/io_utils>
23
24#include <osgAnimation/Bone>
25#include <osgAnimation/Skeleton>
26#include <osgAnimation/RigGeometry>
27#include <osgAnimation/Skinning>
28
29osg::Geode* createAxis()
30{
31    osg::Geode* geode (new osg::Geode()); 
32    osg::Geometry* geometry (new osg::Geometry());
33
34    osg::Vec3Array* vertices (new osg::Vec3Array());
35    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
36    vertices->push_back (osg::Vec3 ( 1.0, 0.0, 0.0));
37    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
38    vertices->push_back (osg::Vec3 ( 0.0, 1.0, 0.0));
39    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
40    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 1.0));
41    geometry->setVertexArray (vertices);
42
43    osg::Vec4Array* colors (new osg::Vec4Array());
44    colors->push_back (osg::Vec4 (1.0f, 0.0f, 0.0f, 1.0f));
45    colors->push_back (osg::Vec4 (1.0f, 0.0f, 0.0f, 1.0f));
46    colors->push_back (osg::Vec4 (0.0f, 1.0f, 0.0f, 1.0f));
47    colors->push_back (osg::Vec4 (0.0f, 1.0f, 0.0f, 1.0f));
48    colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f));
49    colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f));
50    geometry->setColorArray (colors);
51
52    geometry->setColorBinding (osg::Geometry::BIND_PER_VERTEX);   
53    geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,6));
54
55    geode->addDrawable( geometry );
56    return geode;
57}
58
59osgAnimation::RigGeometry* createTesselatedBox(int nsplit, float size)
60{
61    osgAnimation::RigGeometry* geometry = new osgAnimation::RigGeometry;
62
63    osg::ref_ptr<osg::Vec3Array> vertices (new osg::Vec3Array());
64    osg::ref_ptr<osg::Vec3Array> colors (new osg::Vec3Array());
65    geometry->setVertexArray (vertices.get());
66    geometry->setColorArray (colors.get());
67    geometry->setColorBinding (osg::Geometry::BIND_PER_VERTEX);   
68 
69    float step = size / nsplit;
70    float s = 0.5/4.0;
71    for (int i = 0; i < nsplit; i++)
72    {
73        float x = -1 + i * step;
74        std::cout << x << std::endl;
75        vertices->push_back (osg::Vec3 ( x, s, s));
76        vertices->push_back (osg::Vec3 ( x, -s, s));
77        vertices->push_back (osg::Vec3 ( x, -s, -s));
78        vertices->push_back (osg::Vec3 ( x, s, -s));
79        osg::Vec3 c (0,0,0);
80        c[i%3] = 1;
81        colors->push_back (c);
82        colors->push_back (c);
83        colors->push_back (c);
84        colors->push_back (c);
85    }
86
87    osg::ref_ptr<osg::UIntArray> array = new osg::UIntArray;
88    for (int i = 0; i < nsplit - 1; i++)
89    {
90        int base = i * 4;
91        array->push_back(base);
92        array->push_back(base+1);
93        array->push_back(base+4);
94        array->push_back(base+1);
95        array->push_back(base+5);
96        array->push_back(base+4);
97
98        array->push_back(base+3);
99        array->push_back(base);
100        array->push_back(base+4);
101        array->push_back(base+7);
102        array->push_back(base+3);
103        array->push_back(base+4);
104
105        array->push_back(base+5);
106        array->push_back(base+1);
107        array->push_back(base+2);
108        array->push_back(base+2);
109        array->push_back(base+6);
110        array->push_back(base+5);
111
112        array->push_back(base+2);
113        array->push_back(base+3);
114        array->push_back(base+7);
115        array->push_back(base+6);
116        array->push_back(base+2);
117        array->push_back(base+7);
118    }
119 
120    geometry->addPrimitiveSet(new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, array->size(), &array->front()));
121    geometry->setUseDisplayList( false );
122    return geometry;
123}
124
125
126void initVertexMap(osgAnimation::Bone* b0,
127                   osgAnimation::Bone* b1,
128                   osgAnimation::Bone* b2,
129                   osgAnimation::RigGeometry* geom,
130                   osg::Vec3Array* array)
131{
132    osgAnimation::VertexInfluenceSet vertexesInfluences;
133    osgAnimation::VertexInfluenceMap* vim = new osgAnimation::VertexInfluenceMap;
134
135    (*vim)[b0->getName()].setName(b0->getName());
136    (*vim)[b1->getName()].setName(b1->getName());
137    (*vim)[b2->getName()].setName(b2->getName());
138
139    for (int i = 0; i < (int)array->size(); i++)
140    {
141        float val = (*array)[i][0];
142        std::cout << val << std::endl;
143        if (val >= -1 && val <= 0)
144            (*vim)[b0->getName()].push_back(osgAnimation::VertexIndexWeight(i,1));
145        else if ( val > 0 && val <= 1)
146            (*vim)[b1->getName()].push_back(osgAnimation::VertexIndexWeight(i,1));
147        else if ( val > 1)
148            (*vim)[b2->getName()].push_back(osgAnimation::VertexIndexWeight(i,1));
149    }
150
151    geom->setInfluenceMap(vim);
152}
153
154
155
156int main (int argc, char* argv[])
157{
158    osgViewer::Viewer viewer;
159    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
160
161    osg::ref_ptr<osgAnimation::Skeleton> skelroot = new osgAnimation::Skeleton;
162    osg::ref_ptr<osgAnimation::Bone> root = new osgAnimation::Bone;
163    {
164        root->setBindMatrixInBoneSpace(osg::Matrix::identity());
165        root->setBindMatrixInBoneSpace(osg::Matrix::translate(-1,0,0));
166        root->setName("root");
167    }
168
169    osg::ref_ptr<osgAnimation::Bone> right0 = new osgAnimation::Bone;
170    right0->setBindMatrixInBoneSpace(osg::Matrix::translate(1,0,0));
171    right0->setName("right0");
172
173    osg::ref_ptr<osgAnimation::Bone> right1 = new osgAnimation::Bone;
174    right1->setBindMatrixInBoneSpace(osg::Matrix::translate(1,0,0));
175    right1->setName("right1");
176
177    root->addChild(right0.get());
178    right0->addChild(right1.get());
179    skelroot->addChild(root.get());
180
181    osg::ref_ptr<osgAnimation::AnimationManager> manager = new osgAnimation::AnimationManager;
182
183    osgAnimation::Animation* anim = new osgAnimation::Animation;
184    {
185        osgAnimation::QuatKeyframeContainer* keys0 = new osgAnimation::QuatKeyframeContainer;
186        osg::Quat rotate;
187        rotate.makeRotate(osg::PI_2, osg::Vec3(0,0,1));
188        keys0->push_back(osgAnimation::QuatKeyframe(0,osg::Quat(0,0,0,1)));
189        keys0->push_back(osgAnimation::QuatKeyframe(3,rotate));
190        keys0->push_back(osgAnimation::QuatKeyframe(6,rotate));
191        osgAnimation::QuatSphericalLinearSampler* sampler = new osgAnimation::QuatSphericalLinearSampler;
192        sampler->setKeyframeContainer(keys0);
193        osgAnimation::AnimationUpdateCallback* cb = dynamic_cast<osgAnimation::AnimationUpdateCallback*>(right0->getUpdateCallback());
194        cb->setName("right0");
195        osgAnimation::QuatSphericalLinearChannel* channel = new osgAnimation::QuatSphericalLinearChannel(sampler);
196        channel->setName("quaternion");
197        channel->setTargetName("right0");
198        //cb->link(channel);
199        anim->addChannel(channel);
200    }
201
202    {
203        osgAnimation::QuatKeyframeContainer* keys1 = new osgAnimation::QuatKeyframeContainer;
204        osg::Quat rotate;
205        rotate.makeRotate(osg::PI_2, osg::Vec3(0,0,1));
206        keys1->push_back(osgAnimation::QuatKeyframe(0,osg::Quat(0,0,0,1)));
207        keys1->push_back(osgAnimation::QuatKeyframe(3,osg::Quat(0,0,0,1)));
208        keys1->push_back(osgAnimation::QuatKeyframe(6,rotate));
209        osgAnimation::QuatSphericalLinearSampler* sampler = new osgAnimation::QuatSphericalLinearSampler;
210        sampler->setKeyframeContainer(keys1);
211        osgAnimation::QuatSphericalLinearChannel* channel = new osgAnimation::QuatSphericalLinearChannel(sampler);
212        osgAnimation::AnimationUpdateCallback* cb = dynamic_cast<osgAnimation::AnimationUpdateCallback*>(right1->getUpdateCallback());
213        cb->setName("right1");
214        channel->setName("quaternion");
215        channel->setTargetName("right1");
216        //cb->link(channel);
217        anim->addChannel(channel);
218    }
219    manager->registerAnimation(anim);
220    manager->buildTargetReference();
221 
222    // let's start !
223    manager->playAnimation(anim);
224
225    // we will use local data from the skeleton
226    osg::Group* scene = new osg::Group;
227    osg::MatrixTransform* rootTransform = new osg::MatrixTransform;
228    rootTransform->setMatrix(osg::Matrix::rotate(osg::PI_2,osg::Vec3(1,0,0)));
229    right0->addChild(createAxis());
230    right0->setDataVariance(osg::Object::DYNAMIC);
231    right1->addChild(createAxis());
232    right1->setDataVariance(osg::Object::DYNAMIC);
233    osg::MatrixTransform* trueroot = new osg::MatrixTransform;
234    trueroot->setMatrix(osg::Matrix(root->getMatrixInBoneSpace().ptr()));
235    trueroot->addChild(createAxis());
236    trueroot->setDataVariance(osg::Object::DYNAMIC);
237    rootTransform->addChild(manager.get());
238    scene->addChild(rootTransform);
239    manager->addChild(skelroot.get());
240 
241    osgAnimation::RigGeometry* geom = createTesselatedBox(4, 4.0);
242    osg::Geode* geode = new osg::Geode;
243    geode->addDrawable(geom);
244    skelroot->addChild(geode);
245    osg::ref_ptr<osg::Vec3Array> src = dynamic_cast<osg::Vec3Array*>(geom->getVertexArray());
246    geom->getOrCreateStateSet()->setMode(GL_LIGHTING, false);
247    geom->setDataVariance(osg::Object::DYNAMIC);
248    OSGANIMATION_ASSERT(src);
249
250    initVertexMap(root.get(), right0.get(), right1.get(), geom, src.get());
251
252    geom->buildVertexSet();
253    geom->buildTransformer(skelroot.get());
254
255    // let's run !
256    viewer.setSceneData( scene );
257    viewer.realize();
258
259    while (!viewer.done())
260    {
261        viewer.frame();
262    }
263
264    return 0;
265}
266
Note: See TracBrowser for help on using the browser.