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