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