| 1 | |
|---|
| 2 | |
|---|
| 3 | #include <iostream> |
|---|
| 4 | |
|---|
| 5 | #include <osg/Notify> |
|---|
| 6 | #include <osg/MatrixTransform> |
|---|
| 7 | #include <osg/PositionAttitudeTransform> |
|---|
| 8 | #include <osg/Geometry> |
|---|
| 9 | #include <osg/Geode> |
|---|
| 10 | #include <osg/ShapeDrawable> |
|---|
| 11 | #include <osg/Texture2D> |
|---|
| 12 | #include <osg/Material> |
|---|
| 13 | #include <osg/Light> |
|---|
| 14 | #include <osg/LightSource> |
|---|
| 15 | #include <osg/LightModel> |
|---|
| 16 | #include <osg/Billboard> |
|---|
| 17 | #include <osg/LineWidth> |
|---|
| 18 | #include <osg/TexEnv> |
|---|
| 19 | #include <osg/TexEnvCombine> |
|---|
| 20 | #include <osg/ClearNode> |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | #include <osgUtil/Optimizer> |
|---|
| 24 | |
|---|
| 25 | #include <osgDB/Registry> |
|---|
| 26 | #include <osgDB/ReadFile> |
|---|
| 27 | #include <osgDB/WriteFile> |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | #include <osgGA/NodeTrackerManipulator> |
|---|
| 31 | #include <osgGA/TrackballManipulator> |
|---|
| 32 | #include <osgGA/FlightManipulator> |
|---|
| 33 | #include <osgGA/DriveManipulator> |
|---|
| 34 | #include <osgGA/KeySwitchMatrixManipulator> |
|---|
| 35 | |
|---|
| 36 | #include <osgViewer/Viewer> |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | static osg::Vec3 defaultPos( 0.0f, 0.0f, 0.0f ); |
|---|
| 40 | static osg::Vec3 centerScope(0.0f, 0.0f, 0.0f); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | osg::Drawable* createSquare(const osg::Vec3& corner,const osg::Vec3& width,const osg::Vec3& height, osg::Image* image=NULL) |
|---|
| 45 | { |
|---|
| 46 | |
|---|
| 47 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 48 | |
|---|
| 49 | osg::Vec3Array* coords = new osg::Vec3Array(4); |
|---|
| 50 | (*coords)[0] = corner; |
|---|
| 51 | (*coords)[1] = corner+width; |
|---|
| 52 | (*coords)[2] = corner+width+height; |
|---|
| 53 | (*coords)[3] = corner+height; |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | geom->setVertexArray(coords); |
|---|
| 57 | |
|---|
| 58 | osg::Vec3Array* norms = new osg::Vec3Array(1); |
|---|
| 59 | (*norms)[0] = width^height; |
|---|
| 60 | (*norms)[0].normalize(); |
|---|
| 61 | |
|---|
| 62 | geom->setNormalArray(norms); |
|---|
| 63 | geom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 64 | |
|---|
| 65 | osg::Vec2Array* tcoords = new osg::Vec2Array(4); |
|---|
| 66 | (*tcoords)[0].set(0.0f,0.0f); |
|---|
| 67 | (*tcoords)[1].set(1.0f,0.0f); |
|---|
| 68 | (*tcoords)[2].set(1.0f,1.0f); |
|---|
| 69 | (*tcoords)[3].set(0.0f,1.0f); |
|---|
| 70 | geom->setTexCoordArray(0,tcoords); |
|---|
| 71 | |
|---|
| 72 | osg::Vec4Array* colours = new osg::Vec4Array(1); |
|---|
| 73 | (*colours)[0].set(1.0f,1.0f,1.0f,1.0f); |
|---|
| 74 | geom->setColorArray(colours); |
|---|
| 75 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); |
|---|
| 79 | |
|---|
| 80 | if (image) |
|---|
| 81 | { |
|---|
| 82 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 83 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 84 | texture->setImage(image); |
|---|
| 85 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 86 | stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
|---|
| 87 | stateset->setMode(GL_BLEND, osg::StateAttribute::ON); |
|---|
| 88 | stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
|---|
| 89 | geom->setStateSet(stateset); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | return geom; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | osg::Image* createBillboardImage(const osg::Vec4& centerColour, unsigned int size, float power) |
|---|
| 96 | { |
|---|
| 97 | osg::Vec4 backgroundColour = centerColour; |
|---|
| 98 | backgroundColour[3] = 0.0f; |
|---|
| 99 | |
|---|
| 100 | osg::Image* image = new osg::Image; |
|---|
| 101 | image->allocateImage(size,size,1, |
|---|
| 102 | GL_RGBA,GL_UNSIGNED_BYTE); |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | float mid = (float(size)-1)*0.5f; |
|---|
| 106 | float div = 2.0f/float(size); |
|---|
| 107 | for(unsigned int r=0;r<size;++r) |
|---|
| 108 | { |
|---|
| 109 | unsigned char* ptr = image->data(0,r,0); |
|---|
| 110 | for(unsigned int c=0;c<size;++c) |
|---|
| 111 | { |
|---|
| 112 | float dx = (float(c) - mid)*div; |
|---|
| 113 | float dy = (float(r) - mid)*div; |
|---|
| 114 | float r = powf(1.0f-sqrtf(dx*dx+dy*dy),power); |
|---|
| 115 | if (r<0.0f) r=0.0f; |
|---|
| 116 | osg::Vec4 color = centerColour*r+backgroundColour*(1.0f-r); |
|---|
| 117 | |
|---|
| 118 | *ptr++ = (unsigned char)((color[0])*255.0f); |
|---|
| 119 | *ptr++ = (unsigned char)((color[1])*255.0f); |
|---|
| 120 | *ptr++ = (unsigned char)((color[2])*255.0f); |
|---|
| 121 | *ptr++ = (unsigned char)((color[3])*255.0f); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | return image; |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime) |
|---|
| 130 | { |
|---|
| 131 | |
|---|
| 132 | osg::AnimationPath* animationPath = new osg::AnimationPath; |
|---|
| 133 | animationPath->setLoopMode(osg::AnimationPath::LOOP); |
|---|
| 134 | |
|---|
| 135 | int numSamples = 1000; |
|---|
| 136 | float yaw = 0.0f; |
|---|
| 137 | float yaw_delta = -2.0f*osg::PI/((float)numSamples-1.0f); |
|---|
| 138 | float roll = osg::inDegrees(30.0f); |
|---|
| 139 | |
|---|
| 140 | double time=0.0f; |
|---|
| 141 | double time_delta = looptime/(double)numSamples; |
|---|
| 142 | for(int i=0;i<numSamples;++i) |
|---|
| 143 | { |
|---|
| 144 | osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f)); |
|---|
| 145 | 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))); |
|---|
| 146 | |
|---|
| 147 | animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation)); |
|---|
| 148 | |
|---|
| 149 | yaw += yaw_delta; |
|---|
| 150 | time += time_delta; |
|---|
| 151 | |
|---|
| 152 | } |
|---|
| 153 | return animationPath; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | class SolarSystem |
|---|
| 158 | { |
|---|
| 159 | |
|---|
| 160 | public: |
|---|
| 161 | double _radiusSpace; |
|---|
| 162 | double _radiusSun; |
|---|
| 163 | double _radiusMercury; |
|---|
| 164 | double _radiusVenus; |
|---|
| 165 | double _radiusEarth; |
|---|
| 166 | double _radiusMoon; |
|---|
| 167 | double _radiusMars; |
|---|
| 168 | double _radiusJupiter; |
|---|
| 169 | |
|---|
| 170 | double _RorbitMercury; |
|---|
| 171 | double _RorbitVenus; |
|---|
| 172 | double _RorbitEarth; |
|---|
| 173 | double _RorbitMoon; |
|---|
| 174 | double _RorbitMars; |
|---|
| 175 | double _RorbitJupiter; |
|---|
| 176 | |
|---|
| 177 | double _rotateSpeedSun; |
|---|
| 178 | double _rotateSpeedMercury; |
|---|
| 179 | double _rotateSpeedVenus; |
|---|
| 180 | double _rotateSpeedEarthAndMoon; |
|---|
| 181 | double _rotateSpeedEarth; |
|---|
| 182 | double _rotateSpeedMoon; |
|---|
| 183 | double _rotateSpeedMars; |
|---|
| 184 | double _rotateSpeedJupiter; |
|---|
| 185 | |
|---|
| 186 | double _tiltEarth; |
|---|
| 187 | |
|---|
| 188 | std::string _mapSpace; |
|---|
| 189 | std::string _mapSun; |
|---|
| 190 | std::string _mapVenus; |
|---|
| 191 | std::string _mapMercury; |
|---|
| 192 | std::string _mapEarth; |
|---|
| 193 | std::string _mapEarthNight; |
|---|
| 194 | std::string _mapMoon; |
|---|
| 195 | std::string _mapMars; |
|---|
| 196 | std::string _mapJupiter; |
|---|
| 197 | |
|---|
| 198 | double _rotateSpeedFactor; |
|---|
| 199 | double _RorbitFactor; |
|---|
| 200 | double _radiusFactor; |
|---|
| 201 | |
|---|
| 202 | SolarSystem() |
|---|
| 203 | { |
|---|
| 204 | _radiusSpace = 500.0; |
|---|
| 205 | _radiusSun = 109.0; |
|---|
| 206 | _radiusMercury = 0.38; |
|---|
| 207 | _radiusVenus = 0.95; |
|---|
| 208 | _radiusEarth = 1.0; |
|---|
| 209 | _radiusMoon = 0.1; |
|---|
| 210 | _radiusMars = 0.53; |
|---|
| 211 | _radiusJupiter = 5.0; |
|---|
| 212 | |
|---|
| 213 | _RorbitMercury = 11.7; |
|---|
| 214 | _RorbitVenus = 21.6; |
|---|
| 215 | _RorbitEarth = 30.0; |
|---|
| 216 | _RorbitMoon = 1.0; |
|---|
| 217 | _RorbitMars = 45.0; |
|---|
| 218 | _RorbitJupiter = 156.0; |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | _rotateSpeedSun = 0.0; |
|---|
| 222 | _rotateSpeedMercury = 4.15; |
|---|
| 223 | _rotateSpeedVenus = 1.62; |
|---|
| 224 | _rotateSpeedEarthAndMoon = 1.0; |
|---|
| 225 | _rotateSpeedEarth = 1.0; |
|---|
| 226 | _rotateSpeedMoon = 0.95; |
|---|
| 227 | _rotateSpeedMars = 0.53; |
|---|
| 228 | _rotateSpeedJupiter = 0.08; |
|---|
| 229 | |
|---|
| 230 | _tiltEarth = 23.45; |
|---|
| 231 | |
|---|
| 232 | _mapSpace = "Images/spacemap2.jpg"; |
|---|
| 233 | _mapSun = "SolarSystem/sun256128.jpg"; |
|---|
| 234 | _mapMercury = "SolarSystem/mercury256128.jpg"; |
|---|
| 235 | _mapVenus = "SolarSystem/venus256128.jpg"; |
|---|
| 236 | _mapEarth = "Images/land_shallow_topo_2048.jpg"; |
|---|
| 237 | _mapEarthNight = "Images/land_ocean_ice_lights_2048.jpg"; |
|---|
| 238 | _mapMoon = "SolarSystem/moon256128.jpg"; |
|---|
| 239 | _mapMars = "SolarSystem/mars256128.jpg"; |
|---|
| 240 | _mapJupiter = "SolarSystem/jupiter256128.jpg"; |
|---|
| 241 | |
|---|
| 242 | _rotateSpeedFactor = 0.5; |
|---|
| 243 | _RorbitFactor = 15.0; |
|---|
| 244 | _radiusFactor = 10.0; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | osg::MatrixTransform* createTranslationAndTilt( double translation, double tilt ); |
|---|
| 248 | osg::MatrixTransform* createRotation( double orbit, double speed ); |
|---|
| 249 | |
|---|
| 250 | osg::Geode* createSpace( const std::string& name, const std::string& textureName ); |
|---|
| 251 | osg::Geode* createPlanet( double radius, const std::string& name, const osg::Vec4& color , const std::string& textureName ); |
|---|
| 252 | osg::Geode* createPlanet( double radius, const std::string& name, const osg::Vec4& color , const std::string& textureName1, const std::string& textureName2); |
|---|
| 253 | osg::Group* createSunLight(); |
|---|
| 254 | |
|---|
| 255 | void rotateSpeedCorrection() |
|---|
| 256 | { |
|---|
| 257 | _rotateSpeedSun *= _rotateSpeedFactor; |
|---|
| 258 | _rotateSpeedMercury *= _rotateSpeedFactor; |
|---|
| 259 | _rotateSpeedVenus *= _rotateSpeedFactor; |
|---|
| 260 | _rotateSpeedEarthAndMoon *= _rotateSpeedFactor; |
|---|
| 261 | _rotateSpeedEarth *= _rotateSpeedFactor; |
|---|
| 262 | _rotateSpeedMoon *= _rotateSpeedFactor; |
|---|
| 263 | _rotateSpeedMars *= _rotateSpeedFactor; |
|---|
| 264 | _rotateSpeedJupiter *= _rotateSpeedFactor; |
|---|
| 265 | |
|---|
| 266 | std::cout << "rotateSpeed corrected by factor " << _rotateSpeedFactor << std::endl; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | void RorbitCorrection() |
|---|
| 270 | { |
|---|
| 271 | _RorbitMercury *= _RorbitFactor; |
|---|
| 272 | _RorbitVenus *= _RorbitFactor; |
|---|
| 273 | _RorbitEarth *= _RorbitFactor; |
|---|
| 274 | _RorbitMoon *= _RorbitFactor; |
|---|
| 275 | _RorbitMars *= _RorbitFactor; |
|---|
| 276 | _RorbitJupiter *= _RorbitFactor; |
|---|
| 277 | |
|---|
| 278 | std::cout << "Rorbits corrected by factor " << _RorbitFactor << std::endl; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | void radiusCorrection() |
|---|
| 282 | { |
|---|
| 283 | _radiusSpace *= _radiusFactor; |
|---|
| 284 | |
|---|
| 285 | _radiusMercury *= _radiusFactor; |
|---|
| 286 | _radiusVenus *= _radiusFactor; |
|---|
| 287 | _radiusEarth *= _radiusFactor; |
|---|
| 288 | _radiusMoon *= _radiusFactor; |
|---|
| 289 | _radiusMars *= _radiusFactor; |
|---|
| 290 | _radiusJupiter *= _radiusFactor; |
|---|
| 291 | |
|---|
| 292 | std::cout << "Radius corrected by factor " << _radiusFactor << std::endl; |
|---|
| 293 | } |
|---|
| 294 | void printParameters(); |
|---|
| 295 | |
|---|
| 296 | }; |
|---|
| 297 | |
|---|
| 298 | class FindNamedNodeVisitor : public osg::NodeVisitor |
|---|
| 299 | { |
|---|
| 300 | public: |
|---|
| 301 | FindNamedNodeVisitor(const std::string& name): |
|---|
| 302 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
|---|
| 303 | _name(name) {} |
|---|
| 304 | |
|---|
| 305 | virtual void apply(osg::Node& node) |
|---|
| 306 | { |
|---|
| 307 | if (node.getName()==_name) |
|---|
| 308 | { |
|---|
| 309 | _foundNodes.push_back(&node); |
|---|
| 310 | } |
|---|
| 311 | traverse(node); |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | typedef std::vector< osg::ref_ptr<osg::Node> > NodeList; |
|---|
| 315 | |
|---|
| 316 | std::string _name; |
|---|
| 317 | NodeList _foundNodes; |
|---|
| 318 | }; |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | osg::MatrixTransform* SolarSystem::createRotation( double orbit, double speed ) |
|---|
| 322 | { |
|---|
| 323 | osg::Vec3 center( 0.0, 0.0, 0.0 ); |
|---|
| 324 | float animationLength = 10.0f; |
|---|
| 325 | osg::AnimationPath* animationPath = createAnimationPath( center, orbit, animationLength ); |
|---|
| 326 | |
|---|
| 327 | osg::MatrixTransform* rotation = new osg::MatrixTransform; |
|---|
| 328 | rotation->setUpdateCallback( new osg::AnimationPathCallback( animationPath, 0.0f, speed ) ); |
|---|
| 329 | |
|---|
| 330 | return rotation; |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | osg::MatrixTransform* SolarSystem::createTranslationAndTilt( double , double tilt ) |
|---|
| 335 | { |
|---|
| 336 | osg::MatrixTransform* moonPositioned = new osg::MatrixTransform; |
|---|
| 337 | moonPositioned->setMatrix(osg::Matrix::translate(osg::Vec3( 0.0, _RorbitMoon, 0.0 ) )* |
|---|
| 338 | osg::Matrix::scale(1.0, 1.0, 1.0)* |
|---|
| 339 | osg::Matrix::rotate(osg::inDegrees( tilt ),0.0f,0.0f,1.0f)); |
|---|
| 340 | |
|---|
| 341 | return moonPositioned; |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | osg::Geode* SolarSystem::createSpace( const std::string& name, const std::string& textureName ) |
|---|
| 346 | { |
|---|
| 347 | osg::Sphere *spaceSphere = new osg::Sphere( osg::Vec3( 0.0, 0.0, 0.0 ), _radiusSpace ); |
|---|
| 348 | |
|---|
| 349 | osg::ShapeDrawable *sSpaceSphere = new osg::ShapeDrawable( spaceSphere ); |
|---|
| 350 | |
|---|
| 351 | if( !textureName.empty() ) |
|---|
| 352 | { |
|---|
| 353 | osg::Image* image = osgDB::readImageFile( textureName ); |
|---|
| 354 | if ( image ) |
|---|
| 355 | { |
|---|
| 356 | sSpaceSphere->getOrCreateStateSet()->setTextureAttributeAndModes( 0, new osg::Texture2D( image ), osg::StateAttribute::ON ); |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | sSpaceSphere->setColor( osg::Vec4(1.0f,1.0f,1.0f,1.0f) ); |
|---|
| 360 | } |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | osg::Geode* geodeSpace = new osg::Geode(); |
|---|
| 364 | geodeSpace->setName( name ); |
|---|
| 365 | |
|---|
| 366 | geodeSpace->addDrawable( sSpaceSphere ); |
|---|
| 367 | |
|---|
| 368 | return( geodeSpace ); |
|---|
| 369 | |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | osg::Geode* SolarSystem::createPlanet( double radius, const std::string& name, const osg::Vec4& color , const std::string& textureName) |
|---|
| 374 | { |
|---|
| 375 | |
|---|
| 376 | osg::Geometry *sPlanetSphere = new osg::Geometry(); |
|---|
| 377 | |
|---|
| 378 | { |
|---|
| 379 | |
|---|
| 380 | osg::Vec4Array* colours = new osg::Vec4Array(1); |
|---|
| 381 | (*colours)[0] = color; |
|---|
| 382 | sPlanetSphere->setColorArray(colours); |
|---|
| 383 | sPlanetSphere->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | unsigned int numX = 100; |
|---|
| 388 | unsigned int numY = 50; |
|---|
| 389 | unsigned int numVertices = numX*numY; |
|---|
| 390 | |
|---|
| 391 | osg::Vec3Array* coords = new osg::Vec3Array(numVertices); |
|---|
| 392 | sPlanetSphere->setVertexArray(coords); |
|---|
| 393 | |
|---|
| 394 | osg::Vec3Array* normals = new osg::Vec3Array(numVertices); |
|---|
| 395 | sPlanetSphere->setNormalArray(normals); |
|---|
| 396 | sPlanetSphere->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 397 | |
|---|
| 398 | osg::Vec2Array* texcoords = new osg::Vec2Array(numVertices); |
|---|
| 399 | sPlanetSphere->setTexCoordArray(0,texcoords); |
|---|
| 400 | sPlanetSphere->setTexCoordArray(1,texcoords); |
|---|
| 401 | |
|---|
| 402 | double delta_elevation = osg::PI / (double)(numY-1); |
|---|
| 403 | double delta_azim = 2.0*osg::PI / (double)(numX-1); |
|---|
| 404 | float delta_tx = 1.0 / (float)(numX-1); |
|---|
| 405 | float delta_ty = 1.0 / (float)(numY-1); |
|---|
| 406 | |
|---|
| 407 | double elevation = -osg::PI*0.5; |
|---|
| 408 | float ty = 0.0; |
|---|
| 409 | unsigned int vert = 0; |
|---|
| 410 | unsigned j; |
|---|
| 411 | for(j=0; |
|---|
| 412 | j<numY; |
|---|
| 413 | ++j, elevation+=delta_elevation, ty+=delta_ty ) |
|---|
| 414 | { |
|---|
| 415 | double azim = 0.0; |
|---|
| 416 | float tx = 0.0; |
|---|
| 417 | for(unsigned int i=0; |
|---|
| 418 | i<numX; |
|---|
| 419 | ++i, ++vert, azim+=delta_azim, tx+=delta_tx) |
|---|
| 420 | { |
|---|
| 421 | osg::Vec3 direction(cos(azim)*cos(elevation), sin(azim)*cos(elevation), sin(elevation)); |
|---|
| 422 | (*coords)[vert].set(direction*radius); |
|---|
| 423 | (*normals)[vert].set(direction); |
|---|
| 424 | (*texcoords)[vert].set(tx,ty); |
|---|
| 425 | } |
|---|
| 426 | } |
|---|
| 427 | |
|---|
| 428 | for(j=0; |
|---|
| 429 | j<numY-1; |
|---|
| 430 | ++j) |
|---|
| 431 | { |
|---|
| 432 | unsigned int curr_row = j*numX; |
|---|
| 433 | unsigned int next_row = curr_row+numX; |
|---|
| 434 | osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(GL_QUAD_STRIP); |
|---|
| 435 | for(unsigned int i=0; |
|---|
| 436 | i<numX; |
|---|
| 437 | ++i) |
|---|
| 438 | { |
|---|
| 439 | elements->push_back(next_row + i); |
|---|
| 440 | elements->push_back(curr_row + i); |
|---|
| 441 | } |
|---|
| 442 | sPlanetSphere->addPrimitiveSet(elements); |
|---|
| 443 | } |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | osg::Geode* geodePlanet = new osg::Geode(); |
|---|
| 452 | geodePlanet->setName( name ); |
|---|
| 453 | |
|---|
| 454 | if( !textureName.empty() ) |
|---|
| 455 | { |
|---|
| 456 | osg::Image* image = osgDB::readImageFile( textureName ); |
|---|
| 457 | if ( image ) |
|---|
| 458 | { |
|---|
| 459 | osg::Texture2D* tex2d = new osg::Texture2D( image ); |
|---|
| 460 | tex2d->setWrap( osg::Texture::WRAP_S, osg::Texture::REPEAT ); |
|---|
| 461 | tex2d->setWrap( osg::Texture::WRAP_T, osg::Texture::REPEAT ); |
|---|
| 462 | geodePlanet->getOrCreateStateSet()->setTextureAttributeAndModes( 0, tex2d, osg::StateAttribute::ON ); |
|---|
| 463 | |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | } |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | |
|---|
| 470 | geodePlanet->addDrawable( sPlanetSphere ); |
|---|
| 471 | |
|---|
| 472 | return( geodePlanet ); |
|---|
| 473 | |
|---|
| 474 | } |
|---|
| 475 | |
|---|
| 476 | osg::Geode* SolarSystem::createPlanet( double radius, const std::string& name, const osg::Vec4& color , const std::string& textureName1, const std::string& textureName2) |
|---|
| 477 | { |
|---|
| 478 | osg::Geode* geodePlanet = createPlanet( radius, name, color , textureName1); |
|---|
| 479 | |
|---|
| 480 | if( !textureName2.empty() ) |
|---|
| 481 | { |
|---|
| 482 | osg::Image* image = osgDB::readImageFile( textureName2 ); |
|---|
| 483 | if ( image ) |
|---|
| 484 | { |
|---|
| 485 | osg::StateSet* stateset = geodePlanet->getOrCreateStateSet(); |
|---|
| 486 | |
|---|
| 487 | osg::TexEnvCombine* texenv = new osg::TexEnvCombine; |
|---|
| 488 | |
|---|
| 489 | texenv->setCombine_RGB(osg::TexEnvCombine::INTERPOLATE); |
|---|
| 490 | texenv->setSource0_RGB(osg::TexEnvCombine::PREVIOUS); |
|---|
| 491 | texenv->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 492 | texenv->setSource1_RGB(osg::TexEnvCombine::TEXTURE); |
|---|
| 493 | texenv->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 494 | texenv->setSource2_RGB(osg::TexEnvCombine::PRIMARY_COLOR); |
|---|
| 495 | texenv->setOperand2_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 496 | |
|---|
| 497 | stateset->setTextureAttribute( 1, texenv ); |
|---|
| 498 | osg::Texture2D* tex2d = new osg::Texture2D( image ); |
|---|
| 499 | tex2d->setWrap( osg::Texture::WRAP_S, osg::Texture::REPEAT ); |
|---|
| 500 | tex2d->setWrap( osg::Texture::WRAP_T, osg::Texture::REPEAT ); |
|---|
| 501 | stateset->setTextureAttributeAndModes( 1, tex2d, osg::StateAttribute::ON ); |
|---|
| 502 | } |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | return( geodePlanet ); |
|---|
| 506 | |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | osg::Group* SolarSystem::createSunLight() |
|---|
| 510 | { |
|---|
| 511 | |
|---|
| 512 | osg::LightSource* sunLightSource = new osg::LightSource; |
|---|
| 513 | |
|---|
| 514 | osg::Light* sunLight = sunLightSource->getLight(); |
|---|
| 515 | sunLight->setPosition( osg::Vec4( 0.0f, 0.0f, 0.0f, 1.0f ) ); |
|---|
| 516 | sunLight->setAmbient( osg::Vec4( 0.0f, 0.0f, 0.0f, 1.0f ) ); |
|---|
| 517 | |
|---|
| 518 | sunLightSource->setLight( sunLight ); |
|---|
| 519 | sunLightSource->setLocalStateSetModes( osg::StateAttribute::ON ); |
|---|
| 520 | sunLightSource->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::ON); |
|---|
| 521 | |
|---|
| 522 | osg::LightModel* lightModel = new osg::LightModel; |
|---|
| 523 | lightModel->setAmbientIntensity(osg::Vec4(0.0f,0.0f,0.0f,1.0f)); |
|---|
| 524 | sunLightSource->getOrCreateStateSet()->setAttribute(lightModel); |
|---|
| 525 | |
|---|
| 526 | |
|---|
| 527 | return sunLightSource; |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | void SolarSystem::printParameters() |
|---|
| 531 | { |
|---|
| 532 | std::cout << "radiusSpace(" << _radiusSpace << ")" << std::endl; |
|---|
| 533 | std::cout << "radiusSun(" << _radiusSun << ")" << std::endl; |
|---|
| 534 | std::cout << "radiusMercury(" << _radiusMercury << ")" << std::endl; |
|---|
| 535 | std::cout << "radiusVenus(" << _radiusVenus << ")" << std::endl; |
|---|
| 536 | std::cout << "radiusEarth(" << _radiusEarth << ")" << std::endl; |
|---|
| 537 | std::cout << "radiusMoon(" << _radiusMoon << ")" << std::endl; |
|---|
| 538 | std::cout << "radiusMars(" << _radiusMars << ")" << std::endl; |
|---|
| 539 | std::cout << "radiusJupiter(" << _radiusJupiter << ")" << std::endl; |
|---|
| 540 | |
|---|
| 541 | std::cout << "RorbitMercury(" << _RorbitMercury << ")" << std::endl; |
|---|
| 542 | std::cout << "RorbitVenus(" << _RorbitVenus << ")" << std::endl; |
|---|
| 543 | std::cout << "RorbitEarth(" << _RorbitEarth << ")" << std::endl; |
|---|
| 544 | std::cout << "RorbitMoon(" << _RorbitMoon << ")" << std::endl; |
|---|
| 545 | std::cout << "RorbitMars(" << _RorbitMars << ")" << std::endl; |
|---|
| 546 | std::cout << "RorbitJupiter(" << _RorbitJupiter << ")" << std::endl; |
|---|
| 547 | |
|---|
| 548 | std::cout << "rotateSpeedMercury(" << _rotateSpeedMercury << ")" << std::endl; |
|---|
| 549 | std::cout << "rotateSpeedVenus(" << _rotateSpeedVenus << ")" << std::endl; |
|---|
| 550 | std::cout << "rotateSpeedEarthAndMoon(" << _rotateSpeedEarthAndMoon << ")" << std::endl; |
|---|
| 551 | std::cout << "rotateSpeedEarth(" << _rotateSpeedEarth << ")" << std::endl; |
|---|
| 552 | std::cout << "rotateSpeedMoon(" << _rotateSpeedMoon << ")" << std::endl; |
|---|
| 553 | std::cout << "rotateSpeedMars(" << _rotateSpeedMars << ")" << std::endl; |
|---|
| 554 | std::cout << "rotateSpeedJupiter(" << _rotateSpeedJupiter << ")" << std::endl; |
|---|
| 555 | |
|---|
| 556 | std::cout << "tiltEarth(" << _tiltEarth << ")" << std::endl; |
|---|
| 557 | |
|---|
| 558 | std::cout << "mapSpace(" << _mapSpace << ")" << std::endl; |
|---|
| 559 | std::cout << "mapSun(" << _mapSun << ")" << std::endl; |
|---|
| 560 | std::cout << "mapMercury(" << _mapMercury << ")" << std::endl; |
|---|
| 561 | std::cout << "mapVenus(" << _mapVenus << ")" << std::endl; |
|---|
| 562 | std::cout << "mapEarth(" << _mapEarth << ")" << std::endl; |
|---|
| 563 | std::cout << "mapEarthNight(" << _mapEarthNight << ")" << std::endl; |
|---|
| 564 | std::cout << "mapMoon(" << _mapMoon << ")" << std::endl; |
|---|
| 565 | std::cout << "mapMars(" << _mapMars << ")" << std::endl; |
|---|
| 566 | std::cout << "mapJupiter(" << _mapJupiter << ")" << std::endl; |
|---|
| 567 | |
|---|
| 568 | std::cout << "rotateSpeedFactor(" << _rotateSpeedFactor << ")" << std::endl; |
|---|
| 569 | std::cout << "RorbitFactor(" << _RorbitFactor << ")" << std::endl; |
|---|
| 570 | std::cout << "radiusFactor(" << _radiusFactor << ")" << std::endl; |
|---|
| 571 | } |
|---|
| 572 | |
|---|
| 573 | |
|---|
| 574 | int main( int argc, char **argv ) |
|---|
| 575 | { |
|---|
| 576 | |
|---|
| 577 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 578 | |
|---|
| 579 | |
|---|
| 580 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of osg::AnimationPath and UpdateCallbacks for adding animation to your scenes."); |
|---|
| 581 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 582 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 583 | arguments.getApplicationUsage()->addCommandLineOption("-o <filename>","Write created model to file"); |
|---|
| 584 | |
|---|
| 585 | |
|---|
| 586 | osgViewer::Viewer viewer; |
|---|
| 587 | |
|---|
| 588 | osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; |
|---|
| 589 | viewer.setCameraManipulator( keyswitchManipulator.get() ); |
|---|
| 590 | |
|---|
| 591 | SolarSystem solarSystem; |
|---|
| 592 | |
|---|
| 593 | while (arguments.read("--radiusSpace",solarSystem._radiusSpace)) { } |
|---|
| 594 | while (arguments.read("--radiusSun",solarSystem._radiusSun)) { } |
|---|
| 595 | while (arguments.read("--radiusMercury",solarSystem._radiusMercury)) { } |
|---|
| 596 | while (arguments.read("--radiusVenus",solarSystem._radiusVenus)) { } |
|---|
| 597 | while (arguments.read("--radiusEarth",solarSystem._radiusEarth)) { } |
|---|
| 598 | while (arguments.read("--radiusMoon",solarSystem._radiusMoon)) { } |
|---|
| 599 | while (arguments.read("--radiusMars",solarSystem._radiusMars)) { } |
|---|
| 600 | while (arguments.read("--radiusJupiter",solarSystem._radiusJupiter)) { } |
|---|
| 601 | |
|---|
| 602 | while (arguments.read("--RorbitEarth",solarSystem._RorbitEarth)) { } |
|---|
| 603 | while (arguments.read("--RorbitMoon",solarSystem._RorbitMoon)) { } |
|---|
| 604 | |
|---|
| 605 | while (arguments.read("--rotateSpeedEarthAndMoon",solarSystem._rotateSpeedEarthAndMoon)) { } |
|---|
| 606 | while (arguments.read("--rotateSpeedEarth",solarSystem._rotateSpeedEarth)) { } |
|---|
| 607 | while (arguments.read("--rotateSpeedMoon",solarSystem._rotateSpeedMoon)) { } |
|---|
| 608 | while (arguments.read("--tiltEarth",solarSystem._tiltEarth)) { } |
|---|
| 609 | |
|---|
| 610 | while (arguments.read("--mapSpace",solarSystem._mapSpace)) { } |
|---|
| 611 | while (arguments.read("--mapEarth",solarSystem._mapEarth)) { } |
|---|
| 612 | while (arguments.read("--mapEarthNight",solarSystem._mapEarthNight)) { } |
|---|
| 613 | while (arguments.read("--mapMoon",solarSystem._mapMoon)) { } |
|---|
| 614 | |
|---|
| 615 | while (arguments.read("--rotateSpeedFactor",solarSystem._rotateSpeedFactor)) { } |
|---|
| 616 | while (arguments.read("--RorbitFactor",solarSystem._RorbitFactor)) { } |
|---|
| 617 | while (arguments.read("--radiusFactor",solarSystem._radiusFactor)) { } |
|---|
| 618 | |
|---|
| 619 | solarSystem.rotateSpeedCorrection(); |
|---|
| 620 | solarSystem.RorbitCorrection(); |
|---|
| 621 | solarSystem.radiusCorrection(); |
|---|
| 622 | |
|---|
| 623 | std::string writeFileName; |
|---|
| 624 | while (arguments.read("-o",writeFileName)) { } |
|---|
| 625 | |
|---|
| 626 | |
|---|
| 627 | osgGA::NodeTrackerManipulator::TrackerMode trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_ROTATION; |
|---|
| 628 | std::string mode; |
|---|
| 629 | while (arguments.read("--tracker-mode",mode)) |
|---|
| 630 | { |
|---|
| 631 | if (mode=="NODE_CENTER_AND_ROTATION") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_ROTATION; |
|---|
| 632 | else if (mode=="NODE_CENTER_AND_AZIM") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_AZIM; |
|---|
| 633 | else if (mode=="NODE_CENTER") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER; |
|---|
| 634 | else |
|---|
| 635 | { |
|---|
| 636 | std::cout<<"Unrecognized --tracker-mode option "<<mode<<", valid options are:"<<std::endl; |
|---|
| 637 | std::cout<<" NODE_CENTER_AND_ROTATION"<<std::endl; |
|---|
| 638 | std::cout<<" NODE_CENTER_AND_AZIM"<<std::endl; |
|---|
| 639 | std::cout<<" NODE_CENTER"<<std::endl; |
|---|
| 640 | return 1; |
|---|
| 641 | } |
|---|
| 642 | } |
|---|
| 643 | |
|---|
| 644 | |
|---|
| 645 | osgGA::NodeTrackerManipulator::RotationMode rotationMode = osgGA::NodeTrackerManipulator::TRACKBALL; |
|---|
| 646 | while (arguments.read("--rotation-mode",mode)) |
|---|
| 647 | { |
|---|
| 648 | if (mode=="TRACKBALL") rotationMode = osgGA::NodeTrackerManipulator::TRACKBALL; |
|---|
| 649 | else if (mode=="ELEVATION_AZIM") rotationMode = osgGA::NodeTrackerManipulator::ELEVATION_AZIM; |
|---|
| 650 | else |
|---|
| 651 | { |
|---|
| 652 | std::cout<<"Unrecognized --rotation-mode option "<<mode<<", valid options are:"<<std::endl; |
|---|
| 653 | std::cout<<" TRACKBALL"<<std::endl; |
|---|
| 654 | std::cout<<" ELEVATION_AZIM"<<std::endl; |
|---|
| 655 | return 1; |
|---|
| 656 | } |
|---|
| 657 | } |
|---|
| 658 | |
|---|
| 659 | |
|---|
| 660 | |
|---|
| 661 | |
|---|
| 662 | |
|---|
| 663 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 664 | { |
|---|
| 665 | std::cout << "setup the following arguments: " << std::endl; |
|---|
| 666 | std::cout << "\t--radiusSpace: double" << std::endl; |
|---|
| 667 | std::cout << "\t--radiusSun: double" << std::endl; |
|---|
| 668 | std::cout << "\t--radiusMercury: double" << std::endl; |
|---|
| 669 | std::cout << "\t--radiusVenus: double" << std::endl; |
|---|
| 670 | std::cout << "\t--radiusEarth: double" << std::endl; |
|---|
| 671 | std::cout << "\t--radiusMoon: double" << std::endl; |
|---|
| 672 | std::cout << "\t--radiusMars: double" << std::endl; |
|---|
| 673 | std::cout << "\t--radiusJupiter: double" << std::endl; |
|---|
| 674 | |
|---|
| 675 | std::cout << "\t--RorbitMercury: double" << std::endl; |
|---|
| 676 | std::cout << "\t--RorbitVenus: double" << std::endl; |
|---|
| 677 | std::cout << "\t--RorbitEarth: double" << std::endl; |
|---|
| 678 | std::cout << "\t--RorbitMoon: double" << std::endl; |
|---|
| 679 | std::cout << "\t--RorbitMars: double" << std::endl; |
|---|
| 680 | std::cout << "\t--RorbitJupiter: double" << std::endl; |
|---|
| 681 | |
|---|
| 682 | std::cout << "\t--rotateSpeedMercury: double" << std::endl; |
|---|
| 683 | std::cout << "\t--rotateSpeedVenus: double" << std::endl; |
|---|
| 684 | std::cout << "\t--rotateSpeedEarthAndMoon: double" << std::endl; |
|---|
| 685 | std::cout << "\t--rotateSpeedEarth: double" << std::endl; |
|---|
| 686 | std::cout << "\t--rotateSpeedMoon: double" << std::endl; |
|---|
| 687 | std::cout << "\t--rotateSpeedMars: double" << std::endl; |
|---|
| 688 | std::cout << "\t--rotateSpeedJupiter: double" << std::endl; |
|---|
| 689 | |
|---|
| 690 | std::cout << "\t--tiltEarth: double" << std::endl; |
|---|
| 691 | |
|---|
| 692 | std::cout << "\t--mapSpace: string" << std::endl; |
|---|
| 693 | std::cout << "\t--mapSun: string" << std::endl; |
|---|
| 694 | std::cout << "\t--mapMercury: string" << std::endl; |
|---|
| 695 | std::cout << "\t--mapVenus: string" << std::endl; |
|---|
| 696 | std::cout << "\t--mapEarth: string" << std::endl; |
|---|
| 697 | std::cout << "\t--mapEarthNight: string" << std::endl; |
|---|
| 698 | std::cout << "\t--mapMoon: string" << std::endl; |
|---|
| 699 | std::cout << "\t--mapMars: string" << std::endl; |
|---|
| 700 | std::cout << "\t--mapJupiter: string" << std::endl; |
|---|
| 701 | |
|---|
| 702 | std::cout << "\t--rotateSpeedFactor: string" << std::endl; |
|---|
| 703 | std::cout << "\t--RorbitFactor: string" << std::endl; |
|---|
| 704 | std::cout << "\t--radiusFactor: string" << std::endl; |
|---|
| 705 | |
|---|
| 706 | return 1; |
|---|
| 707 | } |
|---|
| 708 | |
|---|
| 709 | |
|---|
| 710 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 711 | |
|---|
| 712 | |
|---|
| 713 | if (arguments.errors()) |
|---|
| 714 | { |
|---|
| 715 | arguments.writeErrorMessages(std::cout); |
|---|
| 716 | return 1; |
|---|
| 717 | } |
|---|
| 718 | |
|---|
| 719 | |
|---|
| 720 | osg::Group* root = new osg::Group; |
|---|
| 721 | |
|---|
| 722 | osg::ClearNode* clearNode = new osg::ClearNode; |
|---|
| 723 | clearNode->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f)); |
|---|
| 724 | root->addChild(clearNode); |
|---|
| 725 | |
|---|
| 726 | osg::Group* sunLight = solarSystem.createSunLight(); |
|---|
| 727 | root->addChild(sunLight); |
|---|
| 728 | |
|---|
| 729 | |
|---|
| 730 | osg::Node* solarSun = solarSystem.createPlanet( solarSystem._radiusSun, "Sun", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f), solarSystem._mapSun ); |
|---|
| 731 | osg::StateSet* sunStateSet = solarSun->getOrCreateStateSet(); |
|---|
| 732 | osg::Material* material = new osg::Material; |
|---|
| 733 | material->setEmission( osg::Material::FRONT_AND_BACK, osg::Vec4( 1.0f, 1.0f, 0.0f, 0.0f ) ); |
|---|
| 734 | sunStateSet->setAttributeAndModes( material, osg::StateAttribute::ON ); |
|---|
| 735 | |
|---|
| 736 | osg::Billboard* sunBillboard = new osg::Billboard(); |
|---|
| 737 | sunBillboard->setMode(osg::Billboard::POINT_ROT_EYE); |
|---|
| 738 | sunBillboard->addDrawable( |
|---|
| 739 | createSquare(osg::Vec3(-150.0f,0.0f,-150.0f),osg::Vec3(300.0f,0.0f,0.0f),osg::Vec3(0.0f,0.0f,300.0f),createBillboardImage( osg::Vec4( 1.0, 1.0, 0, 1.0f), 64, 1.0) ), |
|---|
| 740 | osg::Vec3(0.0f,0.0f,0.0f)); |
|---|
| 741 | |
|---|
| 742 | sunLight->addChild( sunBillboard ); |
|---|
| 743 | |
|---|
| 744 | |
|---|
| 745 | |
|---|
| 746 | sunLight->addChild( solarSun ); |
|---|
| 747 | |
|---|
| 748 | |
|---|
| 749 | |
|---|
| 750 | |
|---|
| 751 | |
|---|
| 752 | |
|---|
| 753 | |
|---|
| 754 | |
|---|
| 755 | |
|---|
| 756 | osg::Node* earth = solarSystem.createPlanet( solarSystem._radiusEarth, "Earth", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f), solarSystem._mapEarth, solarSystem._mapEarthNight ); |
|---|
| 757 | osg::Node* moon = solarSystem.createPlanet( solarSystem._radiusMoon, "Moon", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f), solarSystem._mapMoon ); |
|---|
| 758 | |
|---|
| 759 | |
|---|
| 760 | osg::MatrixTransform* aroundSunRotationEarthMoonGroup = solarSystem.createRotation( solarSystem._RorbitEarth, solarSystem._rotateSpeedEarthAndMoon ); |
|---|
| 761 | |
|---|
| 762 | osg::MatrixTransform* earthMoonGroupPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitEarth, 0.0 ); |
|---|
| 763 | |
|---|
| 764 | |
|---|
| 765 | |
|---|
| 766 | osg::Group* earthMoonGroup = new osg::Group; |
|---|
| 767 | |
|---|
| 768 | |
|---|
| 769 | osg::MatrixTransform* earthAroundItselfRotation = solarSystem.createRotation ( 0.0, solarSystem._rotateSpeedEarth ); |
|---|
| 770 | |
|---|
| 771 | |
|---|
| 772 | osg::MatrixTransform* moonAroundEarthRotation = solarSystem.createRotation( solarSystem._RorbitMoon, solarSystem._rotateSpeedMoon ); |
|---|
| 773 | osg::MatrixTransform* moonTranslation = solarSystem.createTranslationAndTilt( solarSystem._RorbitMoon, 0.0 ); |
|---|
| 774 | |
|---|
| 775 | |
|---|
| 776 | moonTranslation->addChild( moon ); |
|---|
| 777 | moonAroundEarthRotation->addChild( moonTranslation ); |
|---|
| 778 | earthMoonGroup->addChild( moonAroundEarthRotation ); |
|---|
| 779 | |
|---|
| 780 | earthAroundItselfRotation->addChild( earth ); |
|---|
| 781 | earthMoonGroup->addChild( earthAroundItselfRotation ); |
|---|
| 782 | |
|---|
| 783 | earthMoonGroupPosition->addChild( earthMoonGroup ); |
|---|
| 784 | |
|---|
| 785 | aroundSunRotationEarthMoonGroup->addChild( earthMoonGroupPosition ); |
|---|
| 786 | |
|---|
| 787 | sunLight->addChild( aroundSunRotationEarthMoonGroup ); |
|---|
| 788 | |
|---|
| 789 | |
|---|
| 790 | |
|---|
| 791 | |
|---|
| 792 | |
|---|
| 793 | |
|---|
| 794 | |
|---|
| 795 | |
|---|
| 796 | |
|---|
| 797 | |
|---|
| 798 | |
|---|
| 799 | osg::Node* mercury = solarSystem.createPlanet( solarSystem._radiusMercury, "Mercury", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ), solarSystem._mapMercury, "" ); |
|---|
| 800 | |
|---|
| 801 | osg::MatrixTransform* aroundSunRotationMercury = solarSystem.createRotation( solarSystem._RorbitMercury, solarSystem._rotateSpeedMercury ); |
|---|
| 802 | osg::MatrixTransform* mercuryPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitMercury, 0.0f ); |
|---|
| 803 | |
|---|
| 804 | mercuryPosition->addChild( mercury ); |
|---|
| 805 | aroundSunRotationMercury->addChild( mercuryPosition ); |
|---|
| 806 | |
|---|
| 807 | sunLight->addChild( aroundSunRotationMercury ); |
|---|
| 808 | |
|---|
| 809 | |
|---|
| 810 | |
|---|
| 811 | |
|---|
| 812 | |
|---|
| 813 | |
|---|
| 814 | |
|---|
| 815 | |
|---|
| 816 | |
|---|
| 817 | |
|---|
| 818 | |
|---|
| 819 | osg::Node* venus = solarSystem.createPlanet( solarSystem._radiusVenus, "Venus", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ), solarSystem._mapVenus, "" ); |
|---|
| 820 | |
|---|
| 821 | osg::MatrixTransform* aroundSunRotationVenus = solarSystem.createRotation( solarSystem._RorbitVenus, solarSystem._rotateSpeedVenus ); |
|---|
| 822 | osg::MatrixTransform* venusPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitVenus, 0.0f ); |
|---|
| 823 | |
|---|
| 824 | venusPosition->addChild( venus ); |
|---|
| 825 | aroundSunRotationVenus->addChild( venusPosition ); |
|---|
| 826 | |
|---|
| 827 | sunLight->addChild( aroundSunRotationVenus ); |
|---|
| 828 | |
|---|
| 829 | |
|---|
| 830 | |
|---|
| 831 | |
|---|
| 832 | |
|---|
| 833 | |
|---|
| 834 | |
|---|
| 835 | |
|---|
| 836 | |
|---|
| 837 | |
|---|
| 838 | |
|---|
| 839 | osg::Node* mars = solarSystem.createPlanet( solarSystem._radiusMars, "Mars", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ), solarSystem._mapMars, "" ); |
|---|
| 840 | |
|---|
| 841 | osg::MatrixTransform* aroundSunRotationMars = solarSystem.createRotation( solarSystem._RorbitMars, solarSystem._rotateSpeedMars ); |
|---|
| 842 | osg::MatrixTransform* marsPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitMars, 0.0f ); |
|---|
| 843 | |
|---|
| 844 | marsPosition->addChild( mars ); |
|---|
| 845 | aroundSunRotationMars->addChild( marsPosition ); |
|---|
| 846 | |
|---|
| 847 | sunLight->addChild( aroundSunRotationMars ); |
|---|
| 848 | |
|---|
| 849 | |
|---|
| 850 | |
|---|
| 851 | |
|---|
| 852 | |
|---|
| 853 | |
|---|
| 854 | |
|---|
| 855 | |
|---|
| 856 | |
|---|
| 857 | |
|---|
| 858 | |
|---|
| 859 | osg::Node* jupiter = solarSystem.createPlanet( solarSystem._radiusJupiter, "Jupiter", osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ), solarSystem._mapJupiter, "" ); |
|---|
| 860 | |
|---|
| 861 | osg::MatrixTransform* aroundSunRotationJupiter = solarSystem.createRotation( solarSystem._RorbitJupiter, solarSystem._rotateSpeedJupiter ); |
|---|
| 862 | osg::MatrixTransform* jupiterPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitJupiter, 0.0f ); |
|---|
| 863 | |
|---|
| 864 | jupiterPosition->addChild( jupiter ); |
|---|
| 865 | aroundSunRotationJupiter->addChild( jupiterPosition ); |
|---|
| 866 | |
|---|
| 867 | sunLight->addChild( aroundSunRotationJupiter ); |
|---|
| 868 | |
|---|
| 869 | |
|---|
| 870 | |
|---|
| 871 | |
|---|
| 872 | |
|---|
| 873 | |
|---|
| 874 | |
|---|
| 875 | |
|---|
| 876 | |
|---|
| 877 | |
|---|
| 878 | |
|---|
| 879 | |
|---|
| 880 | |
|---|
| 881 | if (!writeFileName.empty()) |
|---|
| 882 | { |
|---|
| 883 | osgDB::writeNodeFile(*root, writeFileName); |
|---|
| 884 | std::cout<<"Written solar system to \""<<writeFileName<<"\""<<std::endl; |
|---|
| 885 | return 0; |
|---|
| 886 | } |
|---|
| 887 | |
|---|
| 888 | |
|---|
| 889 | |
|---|
| 890 | osgUtil::Optimizer optimzer; |
|---|
| 891 | optimzer.optimize( root ); |
|---|
| 892 | |
|---|
| 893 | |
|---|
| 894 | viewer.setSceneData( root ); |
|---|
| 895 | |
|---|
| 896 | |
|---|
| 897 | |
|---|
| 898 | { |
|---|
| 899 | FindNamedNodeVisitor fnnv("Moon"); |
|---|
| 900 | root->accept(fnnv); |
|---|
| 901 | |
|---|
| 902 | if (!fnnv._foundNodes.empty()) |
|---|
| 903 | { |
|---|
| 904 | |
|---|
| 905 | osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator; |
|---|
| 906 | tm->setTrackerMode( trackerMode ); |
|---|
| 907 | tm->setRotationMode( rotationMode ); |
|---|
| 908 | tm->setTrackNode( fnnv._foundNodes.front().get() ); |
|---|
| 909 | |
|---|
| 910 | unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 911 | keyswitchManipulator->addMatrixManipulator( 'm', "moon", tm ); |
|---|
| 912 | keyswitchManipulator->selectMatrixManipulator( num ); |
|---|
| 913 | } |
|---|
| 914 | } |
|---|
| 915 | |
|---|
| 916 | { |
|---|
| 917 | FindNamedNodeVisitor fnnv("Earth"); |
|---|
| 918 | root->accept(fnnv); |
|---|
| 919 | |
|---|
| 920 | if (!fnnv._foundNodes.empty()) |
|---|
| 921 | { |
|---|
| 922 | |
|---|
| 923 | osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator; |
|---|
| 924 | tm->setTrackerMode( trackerMode ); |
|---|
| 925 | tm->setRotationMode( rotationMode ); |
|---|
| 926 | tm->setTrackNode( fnnv._foundNodes.front().get() ); |
|---|
| 927 | |
|---|
| 928 | unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 929 | keyswitchManipulator->addMatrixManipulator( 'e', "earth", tm); |
|---|
| 930 | keyswitchManipulator->selectMatrixManipulator( num ); |
|---|
| 931 | } |
|---|
| 932 | } |
|---|
| 933 | |
|---|
| 934 | { |
|---|
| 935 | FindNamedNodeVisitor fnnv("Sun"); |
|---|
| 936 | root->accept(fnnv); |
|---|
| 937 | |
|---|
| 938 | if (!fnnv._foundNodes.empty()) |
|---|
| 939 | { |
|---|
| 940 | |
|---|
| 941 | osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator; |
|---|
| 942 | tm->setTrackerMode( trackerMode ); |
|---|
| 943 | tm->setRotationMode( rotationMode ); |
|---|
| 944 | tm->setTrackNode( fnnv._foundNodes.front().get() ); |
|---|
| 945 | |
|---|
| 946 | unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 947 | keyswitchManipulator->addMatrixManipulator( 's', "sun", tm); |
|---|
| 948 | keyswitchManipulator->selectMatrixManipulator( num ); |
|---|
| 949 | } |
|---|
| 950 | } |
|---|
| 951 | |
|---|
| 952 | return viewer.run(); |
|---|
| 953 | |
|---|
| 954 | } |
|---|