| [9093] | 1 | |
|---|
| [10518] | 2 | |
|---|
| [9093] | 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| [10527] | 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| [9093] | 18 | |
|---|
| 19 | #include <osgAnimation/UpdateCallback> |
|---|
| 20 | #include <osg/MatrixTransform> |
|---|
| 21 | #include <osg/PositionAttitudeTransform> |
|---|
| 22 | |
|---|
| 23 | using namespace osgAnimation; |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | UpdateTransform::UpdateTransform(const UpdateTransform& apc,const osg::CopyOp& copyop) |
|---|
| [10386] | 27 | : osg::Object(apc, copyop), |
|---|
| [10518] | 28 | AnimationUpdateCallback<osg::NodeCallback>(apc, copyop) |
|---|
| [9093] | 29 | { |
|---|
| [10386] | 30 | _euler = new osgAnimation::Vec3Target(apc._euler->getValue()); |
|---|
| [10390] | 31 | _position = new osgAnimation::Vec3Target(apc._position->getValue()); |
|---|
| 32 | _scale = new osgAnimation::Vec3Target(apc._scale->getValue()); |
|---|
| [9093] | 33 | } |
|---|
| 34 | |
|---|
| [10518] | 35 | UpdateTransform::UpdateTransform(const std::string& name): |
|---|
| 36 | AnimationUpdateCallback<osg::NodeCallback>(name) |
|---|
| [9093] | 37 | { |
|---|
| 38 | _euler = new osgAnimation::Vec3Target; |
|---|
| 39 | _position = new osgAnimation::Vec3Target; |
|---|
| 40 | _scale = new osgAnimation::Vec3Target(osg::Vec3(1,1,1)); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | void UpdateTransform::operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 45 | { |
|---|
| 46 | if (nv && nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR) |
|---|
| 47 | { |
|---|
| 48 | osg::MatrixTransform* matrix = dynamic_cast<osg::MatrixTransform*>(node); |
|---|
| 49 | if (matrix) |
|---|
| 50 | { |
|---|
| 51 | update(*matrix); |
|---|
| 52 | } |
|---|
| 53 | else |
|---|
| 54 | { |
|---|
| 55 | osg::PositionAttitudeTransform* pat = dynamic_cast<osg::PositionAttitudeTransform*>(node); |
|---|
| 56 | if (pat) |
|---|
| 57 | update(*pat); |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | traverse(node,nv); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | void UpdateTransform::update(osg::MatrixTransform& mat) |
|---|
| 64 | { |
|---|
| 65 | float z = _euler->getValue()[2]; |
|---|
| 66 | float x = _euler->getValue()[0]; |
|---|
| 67 | float y = _euler->getValue()[1]; |
|---|
| 68 | osg::Matrix m = |
|---|
| 69 | osg::Matrix::rotate(x,1.0,0.0,0.0) * |
|---|
| 70 | osg::Matrix::rotate(y,0.0,1.0,0.0) * |
|---|
| 71 | osg::Matrix::rotate(z,0.0,0.0,1.0); |
|---|
| 72 | mat.setMatrix(osg::Matrix::scale(_scale->getValue()) * |
|---|
| 73 | m * |
|---|
| 74 | osg::Matrix::translate(_position->getValue())); |
|---|
| 75 | mat.dirtyBound(); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | void UpdateTransform::update(osg::PositionAttitudeTransform& pat) |
|---|
| 79 | { |
|---|
| 80 | float heading = _euler->getValue()[0]; |
|---|
| 81 | float pitch = _euler->getValue()[1]; |
|---|
| 82 | float roll = _euler->getValue()[2]; |
|---|
| 83 | osg::Matrix m = osg::Matrix::rotate(roll,0.0,1.0,0.0) * osg::Matrix::rotate(pitch,1.0,0.0,0.0) * osg::Matrix::rotate(-heading,0.0,0.0,1.0); |
|---|
| 84 | osg::Quat q = m.getRotate(); |
|---|
| 85 | |
|---|
| 86 | pat.setPosition(_position->getValue()); |
|---|
| 87 | pat.setScale(_scale->getValue()); |
|---|
| 88 | pat.setAttitude(q); |
|---|
| 89 | pat.dirtyBound(); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | bool UpdateTransform::needLink() const |
|---|
| 93 | { |
|---|
| 94 | |
|---|
| 95 | return !((_position->getCount() + _euler->getCount() + _scale->getCount()) > 3); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | bool UpdateTransform::link(osgAnimation::Channel* channel) |
|---|
| 99 | { |
|---|
| 100 | if (channel->getName().find("euler") != std::string::npos) |
|---|
| 101 | { |
|---|
| [10527] | 102 | return channel->setTarget(_euler.get()); |
|---|
| [9093] | 103 | } |
|---|
| 104 | else if (channel->getName().find("position") != std::string::npos) |
|---|
| 105 | { |
|---|
| [10527] | 106 | return channel->setTarget(_position.get()); |
|---|
| [9093] | 107 | } |
|---|
| 108 | else if (channel->getName().find("scale") != std::string::npos) |
|---|
| 109 | { |
|---|
| [10527] | 110 | return channel->setTarget(_scale.get()); |
|---|
| [9093] | 111 | } |
|---|
| 112 | else |
|---|
| 113 | { |
|---|
| [10518] | 114 | osg::notify(osg::WARN) << "Channel " << channel->getName() << " does not contain a valid symbolic name for this class" << className() << std::endl; |
|---|
| [9093] | 115 | } |
|---|
| 116 | return false; |
|---|
| 117 | } |
|---|
| [10518] | 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | UpdateMaterial::UpdateMaterial(const UpdateMaterial& apc,const osg::CopyOp& copyop) |
|---|
| 124 | : osg::Object(apc, copyop), |
|---|
| 125 | AnimationUpdateCallback<osg::StateAttribute::Callback>(apc, copyop) |
|---|
| 126 | { |
|---|
| 127 | _diffuse = new osgAnimation::Vec4Target(apc._diffuse->getValue()); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | UpdateMaterial::UpdateMaterial(const std::string& name): |
|---|
| 131 | AnimationUpdateCallback<osg::StateAttribute::Callback>(name) |
|---|
| 132 | { |
|---|
| [10561] | 133 | _diffuse = new osgAnimation::Vec4Target(osg::Vec4(1,0,1,1)); |
|---|
| [10518] | 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | void UpdateMaterial::operator()(osg::StateAttribute* sa, osg::NodeVisitor* nv) |
|---|
| 138 | { |
|---|
| 139 | if (nv && nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR) |
|---|
| 140 | { |
|---|
| 141 | osg::Material* material = dynamic_cast<osg::Material*>(sa); |
|---|
| 142 | if (material) |
|---|
| 143 | update(*material); |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | void UpdateMaterial::update(osg::Material& material) |
|---|
| 148 | { |
|---|
| 149 | osg::Vec4 diffuse = _diffuse->getValue(); |
|---|
| 150 | material.setDiffuse(osg::Material::FRONT_AND_BACK, diffuse); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | bool UpdateMaterial::needLink() const |
|---|
| 154 | { |
|---|
| 155 | |
|---|
| 156 | return (_diffuse->getCount() < 2); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | bool UpdateMaterial::link(osgAnimation::Channel* channel) |
|---|
| 160 | { |
|---|
| 161 | if (channel->getName().find("diffuse") != std::string::npos) |
|---|
| 162 | { |
|---|
| [10527] | 163 | return channel->setTarget(_diffuse.get()); |
|---|
| [10518] | 164 | } |
|---|
| 165 | else |
|---|
| 166 | { |
|---|
| 167 | osg::notify(osg::WARN) << "Channel " << channel->getName() << " does not contain a valid symbolic name for this class " << className() << std::endl; |
|---|
| 168 | } |
|---|
| 169 | return false; |
|---|
| 170 | } |
|---|