| 1 | #include <iostream> |
|---|
| 2 | |
|---|
| 3 | #include <osg/Notify> |
|---|
| 4 | #include <osg/MatrixTransform> |
|---|
| 5 | #include <osg/PositionAttitudeTransform> |
|---|
| 6 | #include <osg/Geometry> |
|---|
| 7 | #include <osg/Geode> |
|---|
| 8 | #include <osg/ShapeDrawable> |
|---|
| 9 | |
|---|
| 10 | #include <osgUtil/Optimizer> |
|---|
| 11 | |
|---|
| 12 | #include <osgDB/Registry> |
|---|
| 13 | #include <osgDB/ReadFile> |
|---|
| 14 | |
|---|
| 15 | #include <osgGA/TrackballManipulator> |
|---|
| 16 | #include <osgGA/FlightManipulator> |
|---|
| 17 | #include <osgGA/DriveManipulator> |
|---|
| 18 | |
|---|
| 19 | #include <osgProducer/Viewer> |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime) |
|---|
| 23 | { |
|---|
| 24 | |
|---|
| 25 | osg::AnimationPath* animationPath = new osg::AnimationPath; |
|---|
| 26 | animationPath->setLoopMode(osg::AnimationPath::LOOP); |
|---|
| 27 | |
|---|
| 28 | int numSamples = 40; |
|---|
| 29 | float yaw = 0.0f; |
|---|
| 30 | float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f); |
|---|
| 31 | float roll = osg::inDegrees(30.0f); |
|---|
| 32 | |
|---|
| 33 | double time=0.0f; |
|---|
| 34 | double time_delta = looptime/(double)numSamples; |
|---|
| 35 | for(int i=0;i<numSamples;++i) |
|---|
| 36 | { |
|---|
| 37 | osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f)); |
|---|
| 38 | osg::Quat rotation(osg::Quat(roll,osg::Vec3(0.0,1.0,0.0))*osg::Quat(-(yaw+osg::inDegrees(90.0f)),osg::Vec3(0.0,0.0,1.0))); |
|---|
| 39 | |
|---|
| 40 | animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation)); |
|---|
| 41 | |
|---|
| 42 | yaw += yaw_delta; |
|---|
| 43 | time += time_delta; |
|---|
| 44 | |
|---|
| 45 | } |
|---|
| 46 | return animationPath; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | osg::MatrixTransform* createEarthTranslationAndTilt( double RorbitEarth, double tiltEarth ) |
|---|
| 51 | { |
|---|
| 52 | osg::MatrixTransform* earthPositioned = new osg::MatrixTransform; |
|---|
| 53 | |
|---|
| 54 | earthPositioned->setMatrix(osg::Matrix::translate(osg::Vec3( 0.0, RorbitEarth, 0.0 ) )* |
|---|
| 55 | osg::Matrix::scale(1.0, 1.0, 1.0)* |
|---|
| 56 | osg::Matrix::rotate(osg::inDegrees( tiltEarth ),0.0f,0.0f,1.0f)); |
|---|
| 57 | |
|---|
| 58 | return earthPositioned; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | osg::MatrixTransform* createRotation( double orbit, double speed ) |
|---|
| 63 | { |
|---|
| 64 | osg::Vec3 center( 0.0, 0.0, 0.0 ); |
|---|
| 65 | float animationLength = 10.0f; |
|---|
| 66 | osg::AnimationPath* animationPath = createAnimationPath( center, orbit, animationLength ); |
|---|
| 67 | |
|---|
| 68 | osg::MatrixTransform* rotation = new osg::MatrixTransform; |
|---|
| 69 | rotation->setUpdateCallback( new osg::AnimationPathCallback( animationPath, 0.0f, speed ) ); |
|---|
| 70 | |
|---|
| 71 | return rotation; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | osg::MatrixTransform* createMoonTranslation( double RorbitMoon ) |
|---|
| 76 | { |
|---|
| 77 | osg::MatrixTransform* moonPositioned = new osg::MatrixTransform; |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | moonPositioned->setMatrix(osg::Matrix::translate(osg::Vec3( 0.0, RorbitMoon, 0.0 ) )* |
|---|
| 81 | |
|---|
| 82 | osg::Matrix::scale(1.0, 1.0, 1.0)* |
|---|
| 83 | osg::Matrix::rotate(osg::inDegrees(0.0f),0.0f,0.0f,1.0f)); |
|---|
| 84 | |
|---|
| 85 | return moonPositioned; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | osg::Geode* createPlanet( double radius, std::string name, osg::Vec4 color ) |
|---|
| 90 | { |
|---|
| 91 | |
|---|
| 92 | osg::Sphere *planetSphere = new osg::Sphere( osg::Vec3( 0.0, 0.0, 0.0 ), radius ); |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | osg::ShapeDrawable *sPlanetSphere = new osg::ShapeDrawable( planetSphere ); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | sPlanetSphere->setColor( color ); |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | osg::Geode* geodePlanet = new osg::Geode(); |
|---|
| 102 | geodePlanet->setName( name ); |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | geodePlanet->addDrawable( sPlanetSphere ); |
|---|
| 106 | |
|---|
| 107 | return( geodePlanet ); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | class SolarSystem |
|---|
| 112 | { |
|---|
| 113 | |
|---|
| 114 | public: |
|---|
| 115 | double _radiusSun; |
|---|
| 116 | double _radiusEarth; |
|---|
| 117 | double _RorbitEarth; |
|---|
| 118 | double _tiltEarth; |
|---|
| 119 | double _rotateSpeedEarthAndMoon; |
|---|
| 120 | double _rotateSpeedEarth; |
|---|
| 121 | double _radiusMoon; |
|---|
| 122 | double _RorbitMoon; |
|---|
| 123 | double _rotateSpeedMoon; |
|---|
| 124 | |
|---|
| 125 | SolarSystem( |
|---|
| 126 | double _radiusSun = 20.0, |
|---|
| 127 | double _radiusEarth = 10.0, |
|---|
| 128 | double _RorbitEarth = 100.0, |
|---|
| 129 | double _tiltEarth = 5.0, |
|---|
| 130 | double _rotateSpeedEarthAndMoon = 5.0, |
|---|
| 131 | double _rotateSpeedEarth = 5.0, |
|---|
| 132 | double _radiusMoon = 2.0, |
|---|
| 133 | double _RorbitMoon = 20.0, |
|---|
| 134 | double _rotateSpeedMoon = 5.0 ) |
|---|
| 135 | {} |
|---|
| 136 | |
|---|
| 137 | osg::Group* built() |
|---|
| 138 | { |
|---|
| 139 | osg::Group* thisSystem = new osg::Group; |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | osg::Node* sun = createPlanet( _radiusSun, "Sun", osg::Vec4( 1.0f, 1.0f, 0.5f, 1.0f) ); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | thisSystem->addChild( sun ); |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | osg::Node* earth = createPlanet( _radiusEarth, "Earth", osg::Vec4( 0.0f, 0.0f, 1.0f, 1.0f) ); |
|---|
| 154 | osg::Node* moon = createPlanet( _radiusMoon, "Moon", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f) ); |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | osg::MatrixTransform* aroundSunRotation = createRotation( _RorbitEarth, _rotateSpeedEarthAndMoon ); |
|---|
| 158 | osg::MatrixTransform* earthPosition = createEarthTranslationAndTilt( _RorbitEarth, _tiltEarth ); |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | osg::Group* earthMoonGroup = new osg::Group; |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | osg::MatrixTransform* earthRotationAroundItself = createRotation ( 0.0, _rotateSpeedEarth ); |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | osg::MatrixTransform* moonAroundEarthXform = createRotation( _RorbitMoon, _rotateSpeedMoon ); |
|---|
| 168 | osg::MatrixTransform* moonTranslation = createMoonTranslation( _RorbitMoon ); |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | moonTranslation->addChild( moon ); |
|---|
| 172 | moonAroundEarthXform->addChild( moonTranslation ); |
|---|
| 173 | earthMoonGroup->addChild( moonAroundEarthXform ); |
|---|
| 174 | |
|---|
| 175 | earthRotationAroundItself->addChild( earth ); |
|---|
| 176 | earthMoonGroup->addChild( earthRotationAroundItself ); |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | earthPosition->addChild( earthMoonGroup ); |
|---|
| 180 | aroundSunRotation->addChild( earthPosition ); |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | thisSystem->addChild( aroundSunRotation ); |
|---|
| 184 | |
|---|
| 185 | return( thisSystem ); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | void printParameters() |
|---|
| 189 | { |
|---|
| 190 | std::cout << "radiusSun\t= " << _radiusSun << std::endl; |
|---|
| 191 | std::cout << "radiusEarth\t= " << _radiusEarth << std::endl; |
|---|
| 192 | std::cout << "RorbitEarth\t= " << _RorbitEarth << std::endl; |
|---|
| 193 | std::cout << "tiltEarth\t= " << _tiltEarth << std::endl; |
|---|
| 194 | std::cout << "rotateSpeedEarthAndMoon= " << _rotateSpeedEarthAndMoon << std::endl; |
|---|
| 195 | std::cout << "rotateSpeedEarth= " << _rotateSpeedEarth << std::endl; |
|---|
| 196 | std::cout << "radiusMoon\t= " << _radiusMoon << std::endl; |
|---|
| 197 | std::cout << "RorbitMoon\t= " << _RorbitMoon << std::endl; |
|---|
| 198 | std::cout << "rotateSpeedMoon\t= " << _rotateSpeedMoon << std::endl; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | }; |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | int main( int argc, char **argv ) |
|---|
| 205 | { |
|---|
| 206 | |
|---|
| 207 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of osg::AnimationPath and UpdateCallbacks for adding animation to your scenes."); |
|---|
| 211 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 212 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | osgProducer::Viewer viewer(arguments); |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 222 | |
|---|
| 223 | SolarSystem solarSystem; |
|---|
| 224 | |
|---|
| 225 | while (arguments.read("--radiusSun",solarSystem._radiusSun)) { } |
|---|
| 226 | while (arguments.read("--radiusEarth",solarSystem._radiusEarth)) { } |
|---|
| 227 | while (arguments.read("--RorbitEarth",solarSystem._RorbitEarth)) { } |
|---|
| 228 | while (arguments.read("--tiltEarth",solarSystem._tiltEarth)) { } |
|---|
| 229 | while (arguments.read("--rotateSpeedEarthAndMoon",solarSystem._rotateSpeedEarthAndMoon)) { } |
|---|
| 230 | while (arguments.read("--rotateSpeedEarth",solarSystem._rotateSpeedEarth)) { } |
|---|
| 231 | while (arguments.read("--radiusMoon",solarSystem._radiusMoon)) { } |
|---|
| 232 | while (arguments.read("--RorbitMoon",solarSystem._RorbitMoon)) { } |
|---|
| 233 | while (arguments.read("--rotateSpeedMoon",solarSystem._rotateSpeedMoon)) { } |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 240 | { |
|---|
| 241 | std::cout << "setup the following arguments: " << std::endl; |
|---|
| 242 | std::cout << "--radiusSun: double" << std::endl; |
|---|
| 243 | std::cout << "--radiusEarth: double" << std::endl; |
|---|
| 244 | std::cout << "--RorbitEarth: double" << std::endl; |
|---|
| 245 | std::cout << "--tiltEarth: double" << std::endl; |
|---|
| 246 | std::cout << "--rotateSpeedEarthAndMoon: double" << std::endl; |
|---|
| 247 | std::cout << "--rotateSpeedEarth: double" << std::endl; |
|---|
| 248 | std::cout << "--radiusMoon: double" << std::endl; |
|---|
| 249 | std::cout << "--RorbitMoon: double" << std::endl; |
|---|
| 250 | std::cout << "--rotateSpeedMoon: double" << std::endl; |
|---|
| 251 | |
|---|
| 252 | return 1; |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | if (arguments.errors()) |
|---|
| 260 | { |
|---|
| 261 | arguments.writeErrorMessages(std::cout); |
|---|
| 262 | return 1; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | osg::Group* root = solarSystem.built(); |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | osgUtil::Optimizer optimzer; |
|---|
| 277 | optimzer.optimize( root ); |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | viewer.setSceneData( root ); |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | viewer.realize(); |
|---|
| 284 | |
|---|
| 285 | while( !viewer.done() ) |
|---|
| 286 | { |
|---|
| 287 | |
|---|
| 288 | viewer.sync(); |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | viewer.update(); |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | viewer.frame(); |
|---|
| 296 | |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | viewer.sync(); |
|---|
| 301 | |
|---|
| 302 | return 0; |
|---|
| 303 | } |
|---|