| 1 | #include <osg/Notify> |
|---|
| 2 | #include <osg/Geometry> |
|---|
| 3 | #include <osg/AnimationPath> |
|---|
| 4 | #include <osg/io_utils> |
|---|
| 5 | |
|---|
| 6 | #include <osgDB/Registry> |
|---|
| 7 | #include <osgDB/Input> |
|---|
| 8 | #include <osgDB/Output> |
|---|
| 9 | |
|---|
| 10 | using namespace osg; |
|---|
| 11 | using namespace osgDB; |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | bool AnimationPath_readLocalData(osg::Object &obj, osgDB::Input &fr); |
|---|
| 16 | bool AnimationPath_writeLocalData(const osg::Object &obj, osgDB::Output &fw); |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | REGISTER_DOTOSGWRAPPER(AnimationPath) |
|---|
| 21 | ( |
|---|
| 22 | new osg::AnimationPath, |
|---|
| 23 | "AnimationPath", |
|---|
| 24 | "Object AnimationPath", |
|---|
| 25 | AnimationPath_readLocalData, |
|---|
| 26 | AnimationPath_writeLocalData, |
|---|
| 27 | DotOsgWrapper::READ_AND_WRITE |
|---|
| 28 | ); |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | bool AnimationPath_readLocalData(osg::Object &obj, osgDB::Input &fr) |
|---|
| 32 | { |
|---|
| 33 | osg::AnimationPath *ap = dynamic_cast<osg::AnimationPath*>(&obj); |
|---|
| 34 | if (!ap) return false; |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | bool itAdvanced = false; |
|---|
| 38 | |
|---|
| 39 | if (fr[0].matchWord("LoopMode")) |
|---|
| 40 | { |
|---|
| 41 | if (fr[1].matchWord("SWING")) |
|---|
| 42 | { |
|---|
| 43 | ap->setLoopMode(AnimationPath::SWING); |
|---|
| 44 | fr += 2; |
|---|
| 45 | itAdvanced = true; |
|---|
| 46 | } |
|---|
| 47 | else if (fr[1].matchWord("LOOP")) |
|---|
| 48 | { |
|---|
| 49 | ap->setLoopMode(AnimationPath::LOOP); |
|---|
| 50 | fr += 2; |
|---|
| 51 | itAdvanced = true; |
|---|
| 52 | } |
|---|
| 53 | else if (fr[1].matchWord("NO_LOOPING")) |
|---|
| 54 | { |
|---|
| 55 | ap->setLoopMode(AnimationPath::NO_LOOPING); |
|---|
| 56 | fr += 2; |
|---|
| 57 | itAdvanced = true; |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | if (fr.matchSequence("ControlPoints {")) |
|---|
| 64 | { |
|---|
| 65 | int entry = fr[0].getNoNestedBrackets(); |
|---|
| 66 | |
|---|
| 67 | fr += 2; |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | double time; |
|---|
| 71 | Vec3d position,scale; |
|---|
| 72 | Quat rotation; |
|---|
| 73 | |
|---|
| 74 | while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) |
|---|
| 75 | { |
|---|
| 76 | if (fr[0].getFloat(time) && |
|---|
| 77 | fr[1].getFloat(position[0]) && |
|---|
| 78 | fr[2].getFloat(position[1]) && |
|---|
| 79 | fr[3].getFloat(position[2]) && |
|---|
| 80 | fr[4].getFloat(rotation[0]) && |
|---|
| 81 | fr[5].getFloat(rotation[1]) && |
|---|
| 82 | fr[6].getFloat(rotation[2]) && |
|---|
| 83 | fr[7].getFloat(rotation[3]) && |
|---|
| 84 | fr[8].getFloat(scale[0]) && |
|---|
| 85 | fr[9].getFloat(scale[1]) && |
|---|
| 86 | fr[10].getFloat(scale[2])) |
|---|
| 87 | { |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | osg::AnimationPath::ControlPoint ctrlPoint(position,rotation,scale); |
|---|
| 91 | ap->insert(time, ctrlPoint); |
|---|
| 92 | |
|---|
| 93 | fr+=11; |
|---|
| 94 | } |
|---|
| 95 | else fr.advanceOverCurrentFieldOrBlock(); |
|---|
| 96 | |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | itAdvanced = true; |
|---|
| 100 | |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | return itAdvanced; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | bool AnimationPath_writeLocalData(const osg::Object &obj, osgDB::Output &fw) |
|---|
| 108 | { |
|---|
| 109 | const osg::AnimationPath* ap = dynamic_cast<const osg::AnimationPath*>(&obj); |
|---|
| 110 | if (!ap) return false; |
|---|
| 111 | |
|---|
| 112 | fw.indent() << "LoopMode "; |
|---|
| 113 | switch(ap->getLoopMode()) |
|---|
| 114 | { |
|---|
| 115 | case AnimationPath::SWING: |
|---|
| 116 | fw << "SWING" <<std::endl; |
|---|
| 117 | break; |
|---|
| 118 | case AnimationPath::LOOP: |
|---|
| 119 | fw << "LOOP"<<std::endl; |
|---|
| 120 | break; |
|---|
| 121 | case AnimationPath::NO_LOOPING: |
|---|
| 122 | fw << "NO_LOOPING"<<std::endl; |
|---|
| 123 | break; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | const AnimationPath::TimeControlPointMap& tcpm = ap->getTimeControlPointMap(); |
|---|
| 127 | |
|---|
| 128 | fw.indent() << "ControlPoints {"<< std::endl; |
|---|
| 129 | fw.moveIn(); |
|---|
| 130 | |
|---|
| 131 | int prec = fw.precision(); |
|---|
| 132 | fw.precision(15); |
|---|
| 133 | |
|---|
| 134 | for (AnimationPath::TimeControlPointMap::const_iterator itr=tcpm.begin(); |
|---|
| 135 | itr!=tcpm.end(); |
|---|
| 136 | ++itr) |
|---|
| 137 | { |
|---|
| 138 | fw.indent() << itr->first << " " << itr->second.getPosition() << " " << itr->second.getRotation() << " " <<itr->second.getScale() << std::endl; |
|---|
| 139 | |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | fw.precision(prec); |
|---|
| 143 | |
|---|
| 144 | fw.moveOut(); |
|---|
| 145 | fw.indent() << "}"<< std::endl; |
|---|
| 146 | |
|---|
| 147 | return true; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | bool AnimationPathCallback_readLocalData(osg::Object &obj, osgDB::Input &fr); |
|---|
| 153 | bool AnimationPathCallback_writeLocalData(const osg::Object &obj, osgDB::Output &fw); |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | REGISTER_DOTOSGWRAPPER(AnimationPathCallback_Proxy) |
|---|
| 158 | ( |
|---|
| 159 | new osg::AnimationPathCallback, |
|---|
| 160 | "AnimationPathCallback", |
|---|
| 161 | "Object AnimationPathCallback", |
|---|
| 162 | AnimationPathCallback_readLocalData, |
|---|
| 163 | AnimationPathCallback_writeLocalData, |
|---|
| 164 | DotOsgWrapper::READ_AND_WRITE |
|---|
| 165 | ); |
|---|
| 166 | |
|---|
| 167 | bool AnimationPathCallback_readLocalData(osg::Object &obj, osgDB::Input &fr) |
|---|
| 168 | { |
|---|
| 169 | osg::AnimationPathCallback *apc = dynamic_cast<osg::AnimationPathCallback*>(&obj); |
|---|
| 170 | if (!apc) return false; |
|---|
| 171 | |
|---|
| 172 | bool iteratorAdvanced = false; |
|---|
| 173 | |
|---|
| 174 | if (fr.matchSequence("pivotPoint %f %f %f")) |
|---|
| 175 | { |
|---|
| 176 | osg::Vec3 pivot; |
|---|
| 177 | fr[1].getFloat(pivot[0]); |
|---|
| 178 | fr[2].getFloat(pivot[1]); |
|---|
| 179 | fr[3].getFloat(pivot[2]); |
|---|
| 180 | |
|---|
| 181 | apc->setPivotPoint(pivot); |
|---|
| 182 | |
|---|
| 183 | fr += 4; |
|---|
| 184 | iteratorAdvanced = true; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | if (fr.matchSequence("timeOffset %f")) |
|---|
| 188 | { |
|---|
| 189 | fr[1].getFloat(apc->_timeOffset); |
|---|
| 190 | fr+=2; |
|---|
| 191 | iteratorAdvanced = true; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | else if(fr.matchSequence("timeMultiplier %f")) |
|---|
| 195 | { |
|---|
| 196 | fr[1].getFloat(apc->_timeMultiplier); |
|---|
| 197 | fr+=2; |
|---|
| 198 | iteratorAdvanced = true; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | static osg::ref_ptr<osg::AnimationPath> s_path = new osg::AnimationPath; |
|---|
| 202 | ref_ptr<osg::Object> object = fr.readObjectOfType(*s_path); |
|---|
| 203 | if (object.valid()) |
|---|
| 204 | { |
|---|
| 205 | osg::AnimationPath* animpath = dynamic_cast<osg::AnimationPath*>(object.get()); |
|---|
| 206 | if (animpath) apc->setAnimationPath(animpath); |
|---|
| 207 | iteratorAdvanced = true; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | return iteratorAdvanced; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | bool AnimationPathCallback_writeLocalData(const osg::Object &obj, osgDB::Output &fw) |
|---|
| 215 | { |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | const osg::AnimationPathCallback* apc = dynamic_cast<const osg::AnimationPathCallback*>(&obj); |
|---|
| 219 | if (!apc) return false; |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | fw.indent() <<"pivotPoint " <<apc->getPivotPoint()<<std::endl; |
|---|
| 223 | fw.indent() <<"timeOffset " <<apc->_timeOffset<<std::endl; |
|---|
| 224 | fw.indent() <<"timeMultiplier " <<apc->_timeMultiplier << std::endl; |
|---|
| 225 | |
|---|
| 226 | if (apc->getAnimationPath()) |
|---|
| 227 | { |
|---|
| 228 | fw.writeObject(*(apc->getAnimationPath())); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | return true; |
|---|
| 232 | } |
|---|