2007-06-12 22:20:16 +08:00
/* OpenSceneGraph example, osgplanets.
*
* Permission is hereby granted , free of charge , to any person obtaining a copy
* of this software and associated documentation files ( the " Software " ) , to deal
* in the Software without restriction , including without limitation the rights
* to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is
* furnished to do so , subject to the following conditions :
*
* THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE .
*/
2004-11-16 03:44:35 +08:00
/* details about distances and rotation on http://www.solarviews.com/eng/solarsys.htm */
2004-10-23 00:39:24 +08:00
# include <iostream>
# include <osg/Notify>
# include <osg/MatrixTransform>
# include <osg/PositionAttitudeTransform>
# include <osg/Geometry>
# include <osg/Geode>
2004-10-24 22:23:42 +08:00
# include <osg/ShapeDrawable>
2004-10-26 02:23:42 +08:00
# include <osg/Texture2D>
2004-10-28 17:38:03 +08:00
# include <osg/Material>
# include <osg/Light>
# include <osg/LightSource>
# include <osg/LightModel>
2004-11-09 23:01:39 +08:00
# include <osg/Billboard>
# include <osg/LineWidth>
2004-10-29 23:42:51 +08:00
# include <osg/TexEnv>
# include <osg/TexEnvCombine>
2004-11-18 23:10:24 +08:00
# include <osg/ClearNode>
2004-10-26 02:23:42 +08:00
2004-10-23 00:39:24 +08:00
# include <osgUtil/Optimizer>
# include <osgDB/Registry>
# include <osgDB/ReadFile>
2004-11-18 23:01:41 +08:00
# include <osgDB/WriteFile>
2004-10-23 00:39:24 +08:00
2004-10-29 03:09:32 +08:00
# include <osgGA/NodeTrackerManipulator>
2004-10-23 00:39:24 +08:00
# include <osgGA/TrackballManipulator>
# include <osgGA/FlightManipulator>
# include <osgGA/DriveManipulator>
2007-01-10 21:52:22 +08:00
# include <osgGA/KeySwitchMatrixManipulator>
2004-10-23 00:39:24 +08:00
2007-01-10 21:52:22 +08:00
# include <osgViewer/Viewer>
2004-10-23 00:39:24 +08:00
2004-10-28 17:38:03 +08:00
static osg : : Vec3 defaultPos ( 0.0f , 0.0f , 0.0f ) ;
static osg : : Vec3 centerScope ( 0.0f , 0.0f , 0.0f ) ;
2004-11-09 23:01:39 +08:00
2004-11-16 03:44:35 +08:00
2004-11-09 23:01:39 +08:00
/** create quad at specified position. */
osg : : Drawable * createSquare ( const osg : : Vec3 & corner , const osg : : Vec3 & width , const osg : : Vec3 & height , osg : : Image * image = NULL )
{
// set up the Geometry.
osg : : Geometry * geom = new osg : : Geometry ;
osg : : Vec3Array * coords = new osg : : Vec3Array ( 4 ) ;
( * coords ) [ 0 ] = corner ;
( * coords ) [ 1 ] = corner + width ;
( * coords ) [ 2 ] = corner + width + height ;
( * coords ) [ 3 ] = corner + height ;
geom - > setVertexArray ( coords ) ;
osg : : Vec3Array * norms = new osg : : Vec3Array ( 1 ) ;
( * norms ) [ 0 ] = width ^ height ;
( * norms ) [ 0 ] . normalize ( ) ;
2013-06-27 17:54:12 +08:00
geom - > setNormalArray ( norms , osg : : Array : : BIND_OVERALL ) ;
2004-11-09 23:01:39 +08:00
osg : : Vec2Array * tcoords = new osg : : Vec2Array ( 4 ) ;
( * tcoords ) [ 0 ] . set ( 0.0f , 0.0f ) ;
( * tcoords ) [ 1 ] . set ( 1.0f , 0.0f ) ;
( * tcoords ) [ 2 ] . set ( 1.0f , 1.0f ) ;
( * tcoords ) [ 3 ] . set ( 0.0f , 1.0f ) ;
geom - > setTexCoordArray ( 0 , tcoords ) ;
2013-06-27 17:54:12 +08:00
2004-11-09 23:01:39 +08:00
osg : : Vec4Array * colours = new osg : : Vec4Array ( 1 ) ;
( * colours ) [ 0 ] . set ( 1.0f , 1.0f , 1.0f , 1.0f ) ;
2013-06-27 17:54:12 +08:00
geom - > setColorArray ( colours , osg : : Array : : BIND_OVERALL ) ;
2004-11-09 23:01:39 +08:00
geom - > addPrimitiveSet ( new osg : : DrawArrays ( osg : : PrimitiveSet : : QUADS , 0 , 4 ) ) ;
2013-06-27 17:54:12 +08:00
2004-11-09 23:01:39 +08:00
if ( image )
{
osg : : StateSet * stateset = new osg : : StateSet ;
osg : : Texture2D * texture = new osg : : Texture2D ;
texture - > setImage ( image ) ;
stateset - > setTextureAttributeAndModes ( 0 , texture , osg : : StateAttribute : : ON ) ;
stateset - > setMode ( GL_LIGHTING , osg : : StateAttribute : : OFF ) ;
stateset - > setMode ( GL_BLEND , osg : : StateAttribute : : ON ) ;
stateset - > setRenderingHint ( osg : : StateSet : : TRANSPARENT_BIN ) ;
geom - > setStateSet ( stateset ) ;
}
2013-06-27 17:54:12 +08:00
2004-11-09 23:01:39 +08:00
return geom ;
}
2004-10-28 17:38:03 +08:00
2004-11-09 23:01:39 +08:00
osg : : Image * createBillboardImage ( const osg : : Vec4 & centerColour , unsigned int size , float power )
{
osg : : Vec4 backgroundColour = centerColour ;
backgroundColour [ 3 ] = 0.0f ;
2013-06-27 17:54:12 +08:00
2004-11-09 23:01:39 +08:00
osg : : Image * image = new osg : : Image ;
image - > allocateImage ( size , size , 1 ,
GL_RGBA , GL_UNSIGNED_BYTE ) ;
2013-06-27 17:54:12 +08:00
2004-11-09 23:01:39 +08:00
float mid = ( float ( size ) - 1 ) * 0.5f ;
float div = 2.0f / float ( size ) ;
for ( unsigned int r = 0 ; r < size ; + + r )
{
unsigned char * ptr = image - > data ( 0 , r , 0 ) ;
for ( unsigned int c = 0 ; c < size ; + + c )
{
float dx = ( float ( c ) - mid ) * div ;
float dy = ( float ( r ) - mid ) * div ;
float r = powf ( 1.0f - sqrtf ( dx * dx + dy * dy ) , power ) ;
if ( r < 0.0f ) r = 0.0f ;
osg : : Vec4 color = centerColour * r + backgroundColour * ( 1.0f - r ) ;
// color.set(1.0f,1.0f,1.0f,0.5f);
* ptr + + = ( unsigned char ) ( ( color [ 0 ] ) * 255.0f ) ;
* ptr + + = ( unsigned char ) ( ( color [ 1 ] ) * 255.0f ) ;
* ptr + + = ( unsigned char ) ( ( color [ 2 ] ) * 255.0f ) ;
* ptr + + = ( unsigned char ) ( ( color [ 3 ] ) * 255.0f ) ;
}
}
return image ;
2004-10-28 17:38:03 +08:00
2004-11-09 23:01:39 +08:00
//return osgDB::readImageFile("spot.dds");
}
2004-10-28 17:38:03 +08:00
2004-10-24 22:23:42 +08:00
osg : : AnimationPath * createAnimationPath ( const osg : : Vec3 & center , float radius , double looptime )
2004-10-23 00:39:24 +08:00
{
2013-06-27 17:54:12 +08:00
// set up the animation path
2004-10-24 22:23:42 +08:00
osg : : AnimationPath * animationPath = new osg : : AnimationPath ;
animationPath - > setLoopMode ( osg : : AnimationPath : : LOOP ) ;
2013-06-27 17:54:12 +08:00
2004-11-18 23:01:41 +08:00
int numSamples = 1000 ;
2004-10-24 22:23:42 +08:00
float yaw = 0.0f ;
2004-11-22 21:58:57 +08:00
float yaw_delta = - 2.0f * osg : : PI / ( ( float ) numSamples - 1.0f ) ;
2004-10-24 22:23:42 +08:00
float roll = osg : : inDegrees ( 30.0f ) ;
2013-06-27 17:54:12 +08:00
2004-10-24 22:23:42 +08:00
double time = 0.0f ;
double time_delta = looptime / ( double ) numSamples ;
for ( int i = 0 ; i < numSamples ; + + i )
{
osg : : Vec3 position ( center + osg : : Vec3 ( sinf ( yaw ) * radius , cosf ( yaw ) * radius , 0.0f ) ) ;
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 ) ) ) ;
2013-06-27 17:54:12 +08:00
2004-10-24 22:23:42 +08:00
animationPath - > insert ( time , osg : : AnimationPath : : ControlPoint ( position , rotation ) ) ;
yaw + = yaw_delta ;
time + = time_delta ;
}
2013-06-27 17:54:12 +08:00
return animationPath ;
2004-10-24 22:23:42 +08:00
} // end createAnimationPath
class SolarSystem
{
public :
2004-11-16 03:44:35 +08:00
double _radiusSpace ;
2004-10-24 22:23:42 +08:00
double _radiusSun ;
2004-11-16 03:44:35 +08:00
double _radiusMercury ;
double _radiusVenus ;
2004-10-24 22:23:42 +08:00
double _radiusEarth ;
2004-11-16 03:44:35 +08:00
double _radiusMoon ;
double _radiusMars ;
double _radiusJupiter ;
double _RorbitMercury ;
double _RorbitVenus ;
2004-10-24 22:23:42 +08:00
double _RorbitEarth ;
2004-11-16 03:44:35 +08:00
double _RorbitMoon ;
double _RorbitMars ;
double _RorbitJupiter ;
2004-11-22 21:58:57 +08:00
double _rotateSpeedSun ;
2004-11-16 03:44:35 +08:00
double _rotateSpeedMercury ;
double _rotateSpeedVenus ;
2004-10-24 22:23:42 +08:00
double _rotateSpeedEarthAndMoon ;
double _rotateSpeedEarth ;
double _rotateSpeedMoon ;
2004-11-16 03:44:35 +08:00
double _rotateSpeedMars ;
double _rotateSpeedJupiter ;
double _tiltEarth ;
std : : string _mapSpace ;
std : : string _mapSun ;
2013-06-27 17:54:12 +08:00
std : : string _mapVenus ;
std : : string _mapMercury ;
2004-11-16 03:44:35 +08:00
std : : string _mapEarth ;
std : : string _mapEarthNight ;
std : : string _mapMoon ;
std : : string _mapMars ;
std : : string _mapJupiter ;
2013-06-27 17:54:12 +08:00
2004-11-22 21:58:57 +08:00
double _rotateSpeedFactor ;
double _RorbitFactor ;
double _radiusFactor ;
2013-06-27 17:54:12 +08:00
2004-10-26 02:23:42 +08:00
SolarSystem ( )
{
2004-11-22 21:58:57 +08:00
_radiusSpace = 500.0 ;
_radiusSun = 109.0 ;
_radiusMercury = 0.38 ;
_radiusVenus = 0.95 ;
_radiusEarth = 1.0 ;
_radiusMoon = 0.1 ;
_radiusMars = 0.53 ;
_radiusJupiter = 5.0 ;
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
_RorbitMercury = 11.7 ;
_RorbitVenus = 21.6 ;
_RorbitEarth = 30.0 ;
2004-11-22 21:58:57 +08:00
_RorbitMoon = 1.0 ;
2004-11-16 03:44:35 +08:00
_RorbitMars = 45.0 ;
_RorbitJupiter = 156.0 ;
2013-06-27 17:54:12 +08:00
2004-11-22 21:58:57 +08:00
// orbital period in days
_rotateSpeedSun = 0.0 ; // should be 11.97; // 30.5 average
_rotateSpeedMercury = 4.15 ; // 87.96
_rotateSpeedVenus = 1.62 ; // 224.70
_rotateSpeedEarthAndMoon = 1.0 ; // 365.25
2013-06-27 17:54:12 +08:00
_rotateSpeedEarth = 1.0 ; //
2004-11-22 21:58:57 +08:00
_rotateSpeedMoon = 0.95 ; //
_rotateSpeedMars = 0.53 ; // 686.98
_rotateSpeedJupiter = 0.08 ; // 4332.71
_tiltEarth = 23.45 ; // degrees
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
_mapSpace = " Images/spacemap2.jpg " ;
2004-11-18 22:43:07 +08:00
_mapSun = " SolarSystem/sun256128.jpg " ;
_mapMercury = " SolarSystem/mercury256128.jpg " ;
_mapVenus = " SolarSystem/venus256128.jpg " ;
2004-11-16 03:44:35 +08:00
_mapEarth = " Images/land_shallow_topo_2048.jpg " ;
_mapEarthNight = " Images/land_ocean_ice_lights_2048.jpg " ;
2004-11-18 22:43:07 +08:00
_mapMoon = " SolarSystem/moon256128.jpg " ;
_mapMars = " SolarSystem/mars256128.jpg " ;
_mapJupiter = " SolarSystem/jupiter256128.jpg " ;
2004-11-22 21:58:57 +08:00
_rotateSpeedFactor = 0.5 ;
_RorbitFactor = 15.0 ;
_radiusFactor = 10.0 ;
2004-10-26 02:23:42 +08:00
}
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
osg : : MatrixTransform * createTranslationAndTilt ( double translation , double tilt ) ;
2004-10-29 03:09:32 +08:00
osg : : MatrixTransform * createRotation ( double orbit , double speed ) ;
2013-06-27 17:54:12 +08:00
2004-10-29 03:09:32 +08:00
osg : : Geode * createSpace ( const std : : string & name , const std : : string & textureName ) ;
osg : : Geode * createPlanet ( double radius , const std : : string & name , const osg : : Vec4 & color , const std : : string & textureName ) ;
2004-10-29 23:42:51 +08:00
osg : : Geode * createPlanet ( double radius , const std : : string & name , const osg : : Vec4 & color , const std : : string & textureName1 , const std : : string & textureName2 ) ;
2004-10-29 03:09:32 +08:00
osg : : Group * createSunLight ( ) ;
2013-06-27 17:54:12 +08:00
2004-11-22 21:58:57 +08:00
void rotateSpeedCorrection ( )
{
_rotateSpeedSun * = _rotateSpeedFactor ;
_rotateSpeedMercury * = _rotateSpeedFactor ;
_rotateSpeedVenus * = _rotateSpeedFactor ;
_rotateSpeedEarthAndMoon * = _rotateSpeedFactor ;
_rotateSpeedEarth * = _rotateSpeedFactor ;
_rotateSpeedMoon * = _rotateSpeedFactor ;
_rotateSpeedMars * = _rotateSpeedFactor ;
_rotateSpeedJupiter * = _rotateSpeedFactor ;
2013-06-27 17:54:12 +08:00
2004-11-22 21:58:57 +08:00
std : : cout < < " rotateSpeed corrected by factor " < < _rotateSpeedFactor < < std : : endl ;
}
2013-06-27 17:54:12 +08:00
2004-11-22 21:58:57 +08:00
void RorbitCorrection ( )
{
_RorbitMercury * = _RorbitFactor ;
_RorbitVenus * = _RorbitFactor ;
_RorbitEarth * = _RorbitFactor ;
_RorbitMoon * = _RorbitFactor ;
_RorbitMars * = _RorbitFactor ;
_RorbitJupiter * = _RorbitFactor ;
2013-06-27 17:54:12 +08:00
2004-11-22 21:58:57 +08:00
std : : cout < < " Rorbits corrected by factor " < < _RorbitFactor < < std : : endl ;
}
2013-06-27 17:54:12 +08:00
2004-11-22 21:58:57 +08:00
void radiusCorrection ( )
{
_radiusSpace * = _radiusFactor ;
//_radiusSun *= _radiusFactor;
_radiusMercury * = _radiusFactor ;
_radiusVenus * = _radiusFactor ;
_radiusEarth * = _radiusFactor ;
_radiusMoon * = _radiusFactor ;
_radiusMars * = _radiusFactor ;
_radiusJupiter * = _radiusFactor ;
2013-06-27 17:54:12 +08:00
2004-11-22 21:58:57 +08:00
std : : cout < < " Radius corrected by factor " < < _radiusFactor < < std : : endl ;
}
2004-11-16 03:44:35 +08:00
void printParameters ( ) ;
2013-06-27 17:54:12 +08:00
2004-10-24 22:23:42 +08:00
} ; // end SolarSystem
2004-10-29 03:09:32 +08:00
class FindNamedNodeVisitor : public osg : : NodeVisitor
{
public :
FindNamedNodeVisitor ( const std : : string & name ) :
osg : : NodeVisitor ( osg : : NodeVisitor : : TRAVERSE_ALL_CHILDREN ) ,
_name ( name ) { }
2013-06-27 17:54:12 +08:00
2004-10-29 03:09:32 +08:00
virtual void apply ( osg : : Node & node )
{
if ( node . getName ( ) = = _name )
{
_foundNodes . push_back ( & node ) ;
}
traverse ( node ) ;
}
2013-06-27 17:54:12 +08:00
2004-10-29 03:09:32 +08:00
typedef std : : vector < osg : : ref_ptr < osg : : Node > > NodeList ;
std : : string _name ;
NodeList _foundNodes ;
} ;
2004-10-23 00:39:24 +08:00
2004-11-18 23:01:41 +08:00
osg : : MatrixTransform * SolarSystem : : createRotation ( double orbit , double speed )
{
osg : : Vec3 center ( 0.0 , 0.0 , 0.0 ) ;
float animationLength = 10.0f ;
osg : : AnimationPath * animationPath = createAnimationPath ( center , orbit , animationLength ) ;
osg : : MatrixTransform * rotation = new osg : : MatrixTransform ;
rotation - > setUpdateCallback ( new osg : : AnimationPathCallback ( animationPath , 0.0f , speed ) ) ;
return rotation ;
} // end SolarSystem::createEarthRotation
osg : : MatrixTransform * SolarSystem : : createTranslationAndTilt ( double /*translation*/ , double tilt )
{
osg : : MatrixTransform * moonPositioned = new osg : : MatrixTransform ;
moonPositioned - > setMatrix ( osg : : Matrix : : translate ( osg : : Vec3 ( 0.0 , _RorbitMoon , 0.0 ) ) *
osg : : Matrix : : scale ( 1.0 , 1.0 , 1.0 ) *
osg : : Matrix : : rotate ( osg : : inDegrees ( tilt ) , 0.0f , 0.0f , 1.0f ) ) ;
return moonPositioned ;
} // end SolarSystem::createTranslationAndTilt
2013-06-27 17:54:12 +08:00
2004-11-18 23:01:41 +08:00
osg : : Geode * SolarSystem : : createSpace ( const std : : string & name , const std : : string & textureName )
{
osg : : Sphere * spaceSphere = new osg : : Sphere ( osg : : Vec3 ( 0.0 , 0.0 , 0.0 ) , _radiusSpace ) ;
osg : : ShapeDrawable * sSpaceSphere = new osg : : ShapeDrawable ( spaceSphere ) ;
if ( ! textureName . empty ( ) )
{
2015-10-22 22:14:53 +08:00
osg : : ref_ptr < osg : : Image > image = osgDB : : readRefImageFile ( textureName ) ;
2004-11-18 23:01:41 +08:00
if ( image )
{
sSpaceSphere - > getOrCreateStateSet ( ) - > setTextureAttributeAndModes ( 0 , new osg : : Texture2D ( image ) , osg : : StateAttribute : : ON ) ;
// reset the object color to white to allow the texture to set the colour.
sSpaceSphere - > setColor ( osg : : Vec4 ( 1.0f , 1.0f , 1.0f , 1.0f ) ) ;
}
}
osg : : Geode * geodeSpace = new osg : : Geode ( ) ;
geodeSpace - > setName ( name ) ;
geodeSpace - > addDrawable ( sSpaceSphere ) ;
return ( geodeSpace ) ;
} // end SolarSystem::createSpace
2013-06-27 17:54:12 +08:00
2004-11-18 23:01:41 +08:00
osg : : Geode * SolarSystem : : createPlanet ( double radius , const std : : string & name , const osg : : Vec4 & color , const std : : string & textureName )
{
// create a container that makes the sphere drawable
osg : : Geometry * sPlanetSphere = new osg : : Geometry ( ) ;
{
// set the single colour so bind overall
osg : : Vec4Array * colours = new osg : : Vec4Array ( 1 ) ;
( * colours ) [ 0 ] = color ;
2013-06-27 17:54:12 +08:00
sPlanetSphere - > setColorArray ( colours , osg : : Array : : BIND_OVERALL ) ;
2004-11-18 23:01:41 +08:00
2013-06-27 17:54:12 +08:00
// now set up the coords, normals and texcoords for geometry
2004-11-18 23:01:41 +08:00
unsigned int numX = 100 ;
unsigned int numY = 50 ;
unsigned int numVertices = numX * numY ;
osg : : Vec3Array * coords = new osg : : Vec3Array ( numVertices ) ;
sPlanetSphere - > setVertexArray ( coords ) ;
osg : : Vec3Array * normals = new osg : : Vec3Array ( numVertices ) ;
2013-06-27 17:54:12 +08:00
sPlanetSphere - > setNormalArray ( normals , osg : : Array : : BIND_PER_VERTEX ) ;
2004-11-18 23:01:41 +08:00
osg : : Vec2Array * texcoords = new osg : : Vec2Array ( numVertices ) ;
sPlanetSphere - > setTexCoordArray ( 0 , texcoords ) ;
sPlanetSphere - > setTexCoordArray ( 1 , texcoords ) ;
double delta_elevation = osg : : PI / ( double ) ( numY - 1 ) ;
double delta_azim = 2.0 * osg : : PI / ( double ) ( numX - 1 ) ;
float delta_tx = 1.0 / ( float ) ( numX - 1 ) ;
float delta_ty = 1.0 / ( float ) ( numY - 1 ) ;
double elevation = - osg : : PI * 0.5 ;
float ty = 0.0 ;
unsigned int vert = 0 ;
unsigned j ;
for ( j = 0 ;
j < numY ;
+ + j , elevation + = delta_elevation , ty + = delta_ty )
{
double azim = 0.0 ;
float tx = 0.0 ;
for ( unsigned int i = 0 ;
i < numX ;
+ + i , + + vert , azim + = delta_azim , tx + = delta_tx )
{
osg : : Vec3 direction ( cos ( azim ) * cos ( elevation ) , sin ( azim ) * cos ( elevation ) , sin ( elevation ) ) ;
( * coords ) [ vert ] . set ( direction * radius ) ;
( * normals ) [ vert ] . set ( direction ) ;
( * texcoords ) [ vert ] . set ( tx , ty ) ;
}
}
for ( j = 0 ;
j < numY - 1 ;
+ + j )
{
unsigned int curr_row = j * numX ;
unsigned int next_row = curr_row + numX ;
osg : : DrawElementsUShort * elements = new osg : : DrawElementsUShort ( GL_QUAD_STRIP ) ;
for ( unsigned int i = 0 ;
i < numX ;
+ + i )
{
elements - > push_back ( next_row + i ) ;
elements - > push_back ( curr_row + i ) ;
}
sPlanetSphere - > addPrimitiveSet ( elements ) ;
}
}
2013-06-27 17:54:12 +08:00
2004-11-18 23:01:41 +08:00
// set the object color
//sPlanetSphere->setColor( color );
// create a geode object to as a container for our drawable sphere object
osg : : Geode * geodePlanet = new osg : : Geode ( ) ;
geodePlanet - > setName ( name ) ;
if ( ! textureName . empty ( ) )
{
2015-10-22 22:14:53 +08:00
osg : : ref_ptr < osg : : Image > image = osgDB : : readRefImageFile ( textureName ) ;
2004-11-18 23:01:41 +08:00
if ( image )
{
2005-11-18 04:22:55 +08:00
osg : : Texture2D * tex2d = new osg : : Texture2D ( image ) ;
tex2d - > setWrap ( osg : : Texture : : WRAP_S , osg : : Texture : : REPEAT ) ;
tex2d - > setWrap ( osg : : Texture : : WRAP_T , osg : : Texture : : REPEAT ) ;
2004-11-25 03:10:44 +08:00
geodePlanet - > getOrCreateStateSet ( ) - > setTextureAttributeAndModes ( 0 , tex2d , osg : : StateAttribute : : ON ) ;
2004-11-18 23:01:41 +08:00
// reset the object color to white to allow the texture to set the colour.
//sPlanetSphere->setColor( osg::Vec4(1.0f,1.0f,1.0f,1.0f) );
}
}
// add our drawable sphere to the geode container
geodePlanet - > addDrawable ( sPlanetSphere ) ;
return ( geodePlanet ) ;
} // end SolarSystem::createPlanet
2013-06-27 17:54:12 +08:00
2004-11-18 23:01:41 +08:00
osg : : Geode * SolarSystem : : createPlanet ( double radius , const std : : string & name , const osg : : Vec4 & color , const std : : string & textureName1 , const std : : string & textureName2 )
{
osg : : Geode * geodePlanet = createPlanet ( radius , name , color , textureName1 ) ;
2013-06-27 17:54:12 +08:00
2004-11-18 23:01:41 +08:00
if ( ! textureName2 . empty ( ) )
{
2015-10-22 22:14:53 +08:00
osg : : ref_ptr < osg : : Image > image = osgDB : : readRefImageFile ( textureName2 ) ;
2004-11-18 23:01:41 +08:00
if ( image )
{
osg : : StateSet * stateset = geodePlanet - > getOrCreateStateSet ( ) ;
2013-06-27 17:54:12 +08:00
2004-11-18 23:01:41 +08:00
osg : : TexEnvCombine * texenv = new osg : : TexEnvCombine ;
2013-06-27 17:54:12 +08:00
2004-11-18 23:01:41 +08:00
texenv - > setCombine_RGB ( osg : : TexEnvCombine : : INTERPOLATE ) ;
texenv - > setSource0_RGB ( osg : : TexEnvCombine : : PREVIOUS ) ;
texenv - > setOperand0_RGB ( osg : : TexEnvCombine : : SRC_COLOR ) ;
texenv - > setSource1_RGB ( osg : : TexEnvCombine : : TEXTURE ) ;
texenv - > setOperand1_RGB ( osg : : TexEnvCombine : : SRC_COLOR ) ;
texenv - > setSource2_RGB ( osg : : TexEnvCombine : : PRIMARY_COLOR ) ;
texenv - > setOperand2_RGB ( osg : : TexEnvCombine : : SRC_COLOR ) ;
stateset - > setTextureAttribute ( 1 , texenv ) ;
2005-11-18 04:22:55 +08:00
osg : : Texture2D * tex2d = new osg : : Texture2D ( image ) ;
tex2d - > setWrap ( osg : : Texture : : WRAP_S , osg : : Texture : : REPEAT ) ;
tex2d - > setWrap ( osg : : Texture : : WRAP_T , osg : : Texture : : REPEAT ) ;
2004-11-25 03:10:44 +08:00
stateset - > setTextureAttributeAndModes ( 1 , tex2d , osg : : StateAttribute : : ON ) ;
2004-11-18 23:01:41 +08:00
}
}
return ( geodePlanet ) ;
} // end SolarSystem::createPlanet
osg : : Group * SolarSystem : : createSunLight ( )
{
osg : : LightSource * sunLightSource = new osg : : LightSource ;
osg : : Light * sunLight = sunLightSource - > getLight ( ) ;
sunLight - > setPosition ( osg : : Vec4 ( 0.0f , 0.0f , 0.0f , 1.0f ) ) ;
sunLight - > setAmbient ( osg : : Vec4 ( 0.0f , 0.0f , 0.0f , 1.0f ) ) ;
sunLightSource - > setLight ( sunLight ) ;
sunLightSource - > setLocalStateSetModes ( osg : : StateAttribute : : ON ) ;
sunLightSource - > getOrCreateStateSet ( ) - > setMode ( GL_LIGHTING , osg : : StateAttribute : : ON ) ;
osg : : LightModel * lightModel = new osg : : LightModel ;
lightModel - > setAmbientIntensity ( osg : : Vec4 ( 0.0f , 0.0f , 0.0f , 1.0f ) ) ;
sunLightSource - > getOrCreateStateSet ( ) - > setAttribute ( lightModel ) ;
return sunLightSource ;
} // end SolarSystem::createSunLight
2013-06-27 17:54:12 +08:00
2004-11-18 23:01:41 +08:00
void SolarSystem : : printParameters ( )
{
std : : cout < < " radiusSpace( " < < _radiusSpace < < " ) " < < std : : endl ;
std : : cout < < " radiusSun( " < < _radiusSun < < " ) " < < std : : endl ;
2004-11-22 21:58:57 +08:00
std : : cout < < " radiusMercury( " < < _radiusMercury < < " ) " < < std : : endl ;
std : : cout < < " radiusVenus( " < < _radiusVenus < < " ) " < < std : : endl ;
2004-11-18 23:01:41 +08:00
std : : cout < < " radiusEarth( " < < _radiusEarth < < " ) " < < std : : endl ;
std : : cout < < " radiusMoon( " < < _radiusMoon < < " ) " < < std : : endl ;
2004-11-22 21:58:57 +08:00
std : : cout < < " radiusMars( " < < _radiusMars < < " ) " < < std : : endl ;
std : : cout < < " radiusJupiter( " < < _radiusJupiter < < " ) " < < std : : endl ;
2004-11-18 23:01:41 +08:00
2004-11-22 21:58:57 +08:00
std : : cout < < " RorbitMercury( " < < _RorbitMercury < < " ) " < < std : : endl ;
std : : cout < < " RorbitVenus( " < < _RorbitVenus < < " ) " < < std : : endl ;
2004-11-18 23:01:41 +08:00
std : : cout < < " RorbitEarth( " < < _RorbitEarth < < " ) " < < std : : endl ;
std : : cout < < " RorbitMoon( " < < _RorbitMoon < < " ) " < < std : : endl ;
2004-11-22 21:58:57 +08:00
std : : cout < < " RorbitMars( " < < _RorbitMars < < " ) " < < std : : endl ;
std : : cout < < " RorbitJupiter( " < < _RorbitJupiter < < " ) " < < std : : endl ;
2004-11-18 23:01:41 +08:00
2004-11-22 21:58:57 +08:00
std : : cout < < " rotateSpeedMercury( " < < _rotateSpeedMercury < < " ) " < < std : : endl ;
std : : cout < < " rotateSpeedVenus( " < < _rotateSpeedVenus < < " ) " < < std : : endl ;
2004-11-18 23:01:41 +08:00
std : : cout < < " rotateSpeedEarthAndMoon( " < < _rotateSpeedEarthAndMoon < < " ) " < < std : : endl ;
std : : cout < < " rotateSpeedEarth( " < < _rotateSpeedEarth < < " ) " < < std : : endl ;
std : : cout < < " rotateSpeedMoon( " < < _rotateSpeedMoon < < " ) " < < std : : endl ;
2004-11-22 21:58:57 +08:00
std : : cout < < " rotateSpeedMars( " < < _rotateSpeedMars < < " ) " < < std : : endl ;
std : : cout < < " rotateSpeedJupiter( " < < _rotateSpeedJupiter < < " ) " < < std : : endl ;
2004-11-18 23:01:41 +08:00
std : : cout < < " tiltEarth( " < < _tiltEarth < < " ) " < < std : : endl ;
2004-11-22 21:58:57 +08:00
std : : cout < < " mapSpace( " < < _mapSpace < < " ) " < < std : : endl ;
std : : cout < < " mapSun( " < < _mapSun < < " ) " < < std : : endl ;
std : : cout < < " mapMercury( " < < _mapMercury < < " ) " < < std : : endl ;
std : : cout < < " mapVenus( " < < _mapVenus < < " ) " < < std : : endl ;
std : : cout < < " mapEarth( " < < _mapEarth < < " ) " < < std : : endl ;
std : : cout < < " mapEarthNight( " < < _mapEarthNight < < " ) " < < std : : endl ;
std : : cout < < " mapMoon( " < < _mapMoon < < " ) " < < std : : endl ;
std : : cout < < " mapMars( " < < _mapMars < < " ) " < < std : : endl ;
std : : cout < < " mapJupiter( " < < _mapJupiter < < " ) " < < std : : endl ;
2013-06-27 17:54:12 +08:00
2004-11-22 21:58:57 +08:00
std : : cout < < " rotateSpeedFactor( " < < _rotateSpeedFactor < < " ) " < < std : : endl ;
std : : cout < < " RorbitFactor( " < < _RorbitFactor < < " ) " < < std : : endl ;
std : : cout < < " radiusFactor( " < < _radiusFactor < < " ) " < < std : : endl ;
2004-11-18 23:01:41 +08:00
}
2004-10-23 00:39:24 +08:00
int main ( int argc , char * * argv )
{
2004-10-23 02:47:39 +08:00
// use an ArgumentParser object to manage the program arguments.
2004-10-23 00:39:24 +08:00
osg : : ArgumentParser arguments ( & argc , argv ) ;
// set up the usage document, in case we need to print out how to use this program.
arguments . getApplicationUsage ( ) - > setDescription ( arguments . getApplicationName ( ) + " is the example which demonstrates use of osg::AnimationPath and UpdateCallbacks for adding animation to your scenes. " ) ;
arguments . getApplicationUsage ( ) - > setCommandLineUsage ( arguments . getApplicationName ( ) + " [options] filename ... " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " -h or --help " , " Display this information " ) ;
2004-11-18 23:01:41 +08:00
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " -o <filename> " , " Write created model to file " ) ;
2004-10-23 00:39:24 +08:00
// initialize the viewer.
2007-01-10 21:52:22 +08:00
osgViewer : : Viewer viewer ;
2004-10-23 00:39:24 +08:00
2007-01-10 21:52:22 +08:00
osg : : ref_ptr < osgGA : : KeySwitchMatrixManipulator > keyswitchManipulator = new osgGA : : KeySwitchMatrixManipulator ;
viewer . setCameraManipulator ( keyswitchManipulator . get ( ) ) ;
2004-10-23 00:39:24 +08:00
2004-10-24 22:23:42 +08:00
SolarSystem solarSystem ;
2004-10-23 00:39:24 +08:00
2004-11-16 03:44:35 +08:00
while ( arguments . read ( " --radiusSpace " , solarSystem . _radiusSpace ) ) { }
2004-10-24 22:23:42 +08:00
while ( arguments . read ( " --radiusSun " , solarSystem . _radiusSun ) ) { }
2004-11-22 21:58:57 +08:00
while ( arguments . read ( " --radiusMercury " , solarSystem . _radiusMercury ) ) { }
while ( arguments . read ( " --radiusVenus " , solarSystem . _radiusVenus ) ) { }
2004-10-24 22:23:42 +08:00
while ( arguments . read ( " --radiusEarth " , solarSystem . _radiusEarth ) ) { }
2004-11-16 03:44:35 +08:00
while ( arguments . read ( " --radiusMoon " , solarSystem . _radiusMoon ) ) { }
2004-11-22 21:58:57 +08:00
while ( arguments . read ( " --radiusMars " , solarSystem . _radiusMars ) ) { }
while ( arguments . read ( " --radiusJupiter " , solarSystem . _radiusJupiter ) ) { }
2013-06-27 17:54:12 +08:00
2004-10-24 22:23:42 +08:00
while ( arguments . read ( " --RorbitEarth " , solarSystem . _RorbitEarth ) ) { }
2004-11-16 03:44:35 +08:00
while ( arguments . read ( " --RorbitMoon " , solarSystem . _RorbitMoon ) ) { }
2013-06-27 17:54:12 +08:00
2004-10-24 22:23:42 +08:00
while ( arguments . read ( " --rotateSpeedEarthAndMoon " , solarSystem . _rotateSpeedEarthAndMoon ) ) { }
while ( arguments . read ( " --rotateSpeedEarth " , solarSystem . _rotateSpeedEarth ) ) { }
while ( arguments . read ( " --rotateSpeedMoon " , solarSystem . _rotateSpeedMoon ) ) { }
2004-11-16 03:44:35 +08:00
while ( arguments . read ( " --tiltEarth " , solarSystem . _tiltEarth ) ) { }
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
while ( arguments . read ( " --mapSpace " , solarSystem . _mapSpace ) ) { }
while ( arguments . read ( " --mapEarth " , solarSystem . _mapEarth ) ) { }
while ( arguments . read ( " --mapEarthNight " , solarSystem . _mapEarthNight ) ) { }
while ( arguments . read ( " --mapMoon " , solarSystem . _mapMoon ) ) { }
2004-11-22 21:58:57 +08:00
while ( arguments . read ( " --rotateSpeedFactor " , solarSystem . _rotateSpeedFactor ) ) { }
while ( arguments . read ( " --RorbitFactor " , solarSystem . _RorbitFactor ) ) { }
while ( arguments . read ( " --radiusFactor " , solarSystem . _radiusFactor ) ) { }
2013-06-27 17:54:12 +08:00
2004-11-22 21:58:57 +08:00
solarSystem . rotateSpeedCorrection ( ) ;
solarSystem . RorbitCorrection ( ) ;
solarSystem . radiusCorrection ( ) ;
2013-06-27 17:54:12 +08:00
2004-11-18 23:01:41 +08:00
std : : string writeFileName ;
while ( arguments . read ( " -o " , writeFileName ) ) { }
2013-06-27 17:54:12 +08:00
2004-10-29 03:09:32 +08:00
osgGA : : NodeTrackerManipulator : : TrackerMode trackerMode = osgGA : : NodeTrackerManipulator : : NODE_CENTER_AND_ROTATION ;
std : : string mode ;
while ( arguments . read ( " --tracker-mode " , mode ) )
{
if ( mode = = " NODE_CENTER_AND_ROTATION " ) trackerMode = osgGA : : NodeTrackerManipulator : : NODE_CENTER_AND_ROTATION ;
else if ( mode = = " NODE_CENTER_AND_AZIM " ) trackerMode = osgGA : : NodeTrackerManipulator : : NODE_CENTER_AND_AZIM ;
else if ( mode = = " NODE_CENTER " ) trackerMode = osgGA : : NodeTrackerManipulator : : NODE_CENTER ;
else
{
std : : cout < < " Unrecognized --tracker-mode option " < < mode < < " , valid options are: " < < std : : endl ;
std : : cout < < " NODE_CENTER_AND_ROTATION " < < std : : endl ;
std : : cout < < " NODE_CENTER_AND_AZIM " < < std : : endl ;
std : : cout < < " NODE_CENTER " < < std : : endl ;
return 1 ;
}
}
2013-06-27 17:54:12 +08:00
2004-10-29 03:09:32 +08:00
osgGA : : NodeTrackerManipulator : : RotationMode rotationMode = osgGA : : NodeTrackerManipulator : : TRACKBALL ;
while ( arguments . read ( " --rotation-mode " , mode ) )
{
if ( mode = = " TRACKBALL " ) rotationMode = osgGA : : NodeTrackerManipulator : : TRACKBALL ;
else if ( mode = = " ELEVATION_AZIM " ) rotationMode = osgGA : : NodeTrackerManipulator : : ELEVATION_AZIM ;
else
{
std : : cout < < " Unrecognized --rotation-mode option " < < mode < < " , valid options are: " < < std : : endl ;
std : : cout < < " TRACKBALL " < < std : : endl ;
std : : cout < < " ELEVATION_AZIM " < < std : : endl ;
return 1 ;
}
}
2013-06-27 17:54:12 +08:00
2004-10-23 00:39:24 +08:00
2004-11-22 21:58:57 +08:00
// solarSystem.printParameters();
2004-10-23 00:39:24 +08:00
// if user request help write it out to cout.
if ( arguments . read ( " -h " ) | | arguments . read ( " --help " ) )
{
2004-10-24 22:23:42 +08:00
std : : cout < < " setup the following arguments: " < < std : : endl ;
2004-11-22 21:58:57 +08:00
std : : cout < < " \t --radiusSpace: double " < < std : : endl ;
std : : cout < < " \t --radiusSun: double " < < std : : endl ;
std : : cout < < " \t --radiusMercury: double " < < std : : endl ;
std : : cout < < " \t --radiusVenus: double " < < std : : endl ;
std : : cout < < " \t --radiusEarth: double " < < std : : endl ;
std : : cout < < " \t --radiusMoon: double " < < std : : endl ;
std : : cout < < " \t --radiusMars: double " < < std : : endl ;
std : : cout < < " \t --radiusJupiter: double " < < std : : endl ;
2013-06-27 17:54:12 +08:00
2004-11-22 21:58:57 +08:00
std : : cout < < " \t --RorbitMercury: double " < < std : : endl ;
std : : cout < < " \t --RorbitVenus: double " < < std : : endl ;
std : : cout < < " \t --RorbitEarth: double " < < std : : endl ;
std : : cout < < " \t --RorbitMoon: double " < < std : : endl ;
std : : cout < < " \t --RorbitMars: double " < < std : : endl ;
std : : cout < < " \t --RorbitJupiter: double " < < std : : endl ;
2013-06-27 17:54:12 +08:00
2004-11-22 21:58:57 +08:00
std : : cout < < " \t --rotateSpeedMercury: double " < < std : : endl ;
std : : cout < < " \t --rotateSpeedVenus: double " < < std : : endl ;
std : : cout < < " \t --rotateSpeedEarthAndMoon: double " < < std : : endl ;
std : : cout < < " \t --rotateSpeedEarth: double " < < std : : endl ;
std : : cout < < " \t --rotateSpeedMoon: double " < < std : : endl ;
std : : cout < < " \t --rotateSpeedMars: double " < < std : : endl ;
std : : cout < < " \t --rotateSpeedJupiter: double " < < std : : endl ;
std : : cout < < " \t --tiltEarth: double " < < std : : endl ;
2013-06-27 17:54:12 +08:00
2004-11-22 21:58:57 +08:00
std : : cout < < " \t --mapSpace: string " < < std : : endl ;
std : : cout < < " \t --mapSun: string " < < std : : endl ;
std : : cout < < " \t --mapMercury: string " < < std : : endl ;
std : : cout < < " \t --mapVenus: string " < < std : : endl ;
std : : cout < < " \t --mapEarth: string " < < std : : endl ;
std : : cout < < " \t --mapEarthNight: string " < < std : : endl ;
std : : cout < < " \t --mapMoon: string " < < std : : endl ;
std : : cout < < " \t --mapMars: string " < < std : : endl ;
std : : cout < < " \t --mapJupiter: string " < < std : : endl ;
2013-06-27 17:54:12 +08:00
2004-11-22 21:58:57 +08:00
std : : cout < < " \t --rotateSpeedFactor: string " < < std : : endl ;
std : : cout < < " \t --RorbitFactor: string " < < std : : endl ;
std : : cout < < " \t --radiusFactor: string " < < std : : endl ;
2013-06-27 17:54:12 +08:00
2004-10-23 00:39:24 +08:00
return 1 ;
}
// any option left unread are converted into errors to write out later.
arguments . reportRemainingOptionsAsUnrecognized ( ) ;
2007-12-11 01:30:18 +08:00
// report any errors if they have occurred when parsing the program arguments.
2004-10-23 00:39:24 +08:00
if ( arguments . errors ( ) )
{
arguments . writeErrorMessages ( std : : cout ) ;
return 1 ;
}
2013-06-27 17:54:12 +08:00
2004-10-29 03:09:32 +08:00
osg : : Group * root = new osg : : Group ;
2013-06-27 17:54:12 +08:00
2004-11-18 23:10:24 +08:00
osg : : ClearNode * clearNode = new osg : : ClearNode ;
clearNode - > setClearColor ( osg : : Vec4 ( 0.0f , 0.0f , 0.0f , 1.0f ) ) ;
root - > addChild ( clearNode ) ;
2004-10-29 03:09:32 +08:00
osg : : Group * sunLight = solarSystem . createSunLight ( ) ;
root - > addChild ( sunLight ) ;
// create the sun
2004-11-18 18:08:29 +08:00
osg : : Node * solarSun = solarSystem . createPlanet ( solarSystem . _radiusSun , " Sun " , osg : : Vec4 ( 1.0f , 1.0f , 1.0f , 1.0f ) , solarSystem . _mapSun ) ;
osg : : StateSet * sunStateSet = solarSun - > getOrCreateStateSet ( ) ;
2004-10-29 03:09:32 +08:00
osg : : Material * material = new osg : : Material ;
material - > setEmission ( osg : : Material : : FRONT_AND_BACK , osg : : Vec4 ( 1.0f , 1.0f , 0.0f , 0.0f ) ) ;
sunStateSet - > setAttributeAndModes ( material , osg : : StateAttribute : : ON ) ;
2004-11-16 03:44:35 +08:00
2004-11-09 23:01:39 +08:00
osg : : Billboard * sunBillboard = new osg : : Billboard ( ) ;
sunBillboard - > setMode ( osg : : Billboard : : POINT_ROT_EYE ) ;
sunBillboard - > addDrawable (
2004-11-22 21:58:57 +08:00
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 ) ) ,
2004-11-09 23:01:39 +08:00
osg : : Vec3 ( 0.0f , 0.0f , 0.0f ) ) ;
2013-06-27 17:54:12 +08:00
2004-11-09 23:01:39 +08:00
sunLight - > addChild ( sunBillboard ) ;
2004-11-16 03:44:35 +08:00
2004-11-22 21:58:57 +08:00
2004-10-29 03:09:32 +08:00
// stick sun right under root, no transformations for the sun
2004-11-18 18:08:29 +08:00
sunLight - > addChild ( solarSun ) ;
2004-10-29 03:09:32 +08:00
// create light source in the sun
2004-11-16 03:44:35 +08:00
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2013-06-27 17:54:12 +08:00
* * earthMoonGroup and Transformations
2004-11-16 03:44:35 +08:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
2004-10-29 03:09:32 +08:00
// create earth and moon
2004-11-16 03:44:35 +08:00
osg : : Node * earth = solarSystem . createPlanet ( solarSystem . _radiusEarth , " Earth " , osg : : Vec4 ( 1.0f , 1.0f , 1.0f , 1.0f ) , solarSystem . _mapEarth , solarSystem . _mapEarthNight ) ;
osg : : Node * moon = solarSystem . createPlanet ( solarSystem . _radiusMoon , " Moon " , osg : : Vec4 ( 1.0f , 1.0f , 1.0f , 1.0f ) , solarSystem . _mapMoon ) ;
2004-10-29 03:09:32 +08:00
// create transformations for the earthMoonGroup
2004-11-16 03:44:35 +08:00
osg : : MatrixTransform * aroundSunRotationEarthMoonGroup = solarSystem . createRotation ( solarSystem . _RorbitEarth , solarSystem . _rotateSpeedEarthAndMoon ) ;
2004-11-22 21:58:57 +08:00
// osg::MatrixTransform* earthMoonGroupPosition = solarSystem.createTranslationAndTilt( solarSystem._RorbitEarth, solarSystem._tiltEarth );
osg : : MatrixTransform * earthMoonGroupPosition = solarSystem . createTranslationAndTilt ( solarSystem . _RorbitEarth , 0.0 ) ;
2004-10-29 03:09:32 +08:00
//Group with earth and moon under it
osg : : Group * earthMoonGroup = new osg : : Group ;
2013-06-27 17:54:12 +08:00
2004-10-29 03:09:32 +08:00
//transformation to rotate the earth around itself
2004-11-16 03:44:35 +08:00
osg : : MatrixTransform * earthAroundItselfRotation = solarSystem . createRotation ( 0.0 , solarSystem . _rotateSpeedEarth ) ;
2004-10-29 03:09:32 +08:00
//transformations for the moon
2004-11-16 03:44:35 +08:00
osg : : MatrixTransform * moonAroundEarthRotation = solarSystem . createRotation ( solarSystem . _RorbitMoon , solarSystem . _rotateSpeedMoon ) ;
2004-11-22 21:58:57 +08:00
osg : : MatrixTransform * moonTranslation = solarSystem . createTranslationAndTilt ( solarSystem . _RorbitMoon , 0.0 ) ;
2004-10-29 03:09:32 +08:00
2004-10-23 00:39:24 +08:00
2004-10-29 03:09:32 +08:00
moonTranslation - > addChild ( moon ) ;
2004-11-16 03:44:35 +08:00
moonAroundEarthRotation - > addChild ( moonTranslation ) ;
earthMoonGroup - > addChild ( moonAroundEarthRotation ) ;
earthAroundItselfRotation - > addChild ( earth ) ;
earthMoonGroup - > addChild ( earthAroundItselfRotation ) ;
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
earthMoonGroupPosition - > addChild ( earthMoonGroup ) ;
2004-10-29 03:09:32 +08:00
2004-11-16 03:44:35 +08:00
aroundSunRotationEarthMoonGroup - > addChild ( earthMoonGroupPosition ) ;
2004-10-29 03:09:32 +08:00
2004-11-16 03:44:35 +08:00
sunLight - > addChild ( aroundSunRotationEarthMoonGroup ) ;
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2013-06-27 17:54:12 +08:00
* * end earthMoonGroup and Transformations
2004-11-16 03:44:35 +08:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2013-06-27 17:54:12 +08:00
* * Mercury and Transformations
2004-11-16 03:44:35 +08:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
osg : : Node * mercury = solarSystem . createPlanet ( solarSystem . _radiusMercury , " Mercury " , osg : : Vec4 ( 1.0f , 1.0f , 1.0f , 1.0f ) , solarSystem . _mapMercury , " " ) ;
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
osg : : MatrixTransform * aroundSunRotationMercury = solarSystem . createRotation ( solarSystem . _RorbitMercury , solarSystem . _rotateSpeedMercury ) ;
osg : : MatrixTransform * mercuryPosition = solarSystem . createTranslationAndTilt ( solarSystem . _RorbitMercury , 0.0f ) ;
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
mercuryPosition - > addChild ( mercury ) ;
aroundSunRotationMercury - > addChild ( mercuryPosition ) ;
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
sunLight - > addChild ( aroundSunRotationMercury ) ;
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2013-06-27 17:54:12 +08:00
* * end Mercury and Transformations
2004-11-16 03:44:35 +08:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
2004-10-29 03:09:32 +08:00
2004-11-16 03:44:35 +08:00
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2013-06-27 17:54:12 +08:00
* * Venus and Transformations
2004-11-16 03:44:35 +08:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
osg : : Node * venus = solarSystem . createPlanet ( solarSystem . _radiusVenus , " Venus " , osg : : Vec4 ( 1.0f , 1.0f , 1.0f , 1.0f ) , solarSystem . _mapVenus , " " ) ;
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
osg : : MatrixTransform * aroundSunRotationVenus = solarSystem . createRotation ( solarSystem . _RorbitVenus , solarSystem . _rotateSpeedVenus ) ;
osg : : MatrixTransform * venusPosition = solarSystem . createTranslationAndTilt ( solarSystem . _RorbitVenus , 0.0f ) ;
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
venusPosition - > addChild ( venus ) ;
aroundSunRotationVenus - > addChild ( venusPosition ) ;
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
sunLight - > addChild ( aroundSunRotationVenus ) ;
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2013-06-27 17:54:12 +08:00
* * end Venus and Transformations
2004-11-16 03:44:35 +08:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
2004-10-29 03:09:32 +08:00
2004-11-16 03:44:35 +08:00
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2013-06-27 17:54:12 +08:00
* * Mars and Transformations
2004-11-16 03:44:35 +08:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
osg : : Node * mars = solarSystem . createPlanet ( solarSystem . _radiusMars , " Mars " , osg : : Vec4 ( 1.0f , 1.0f , 1.0f , 1.0f ) , solarSystem . _mapMars , " " ) ;
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
osg : : MatrixTransform * aroundSunRotationMars = solarSystem . createRotation ( solarSystem . _RorbitMars , solarSystem . _rotateSpeedMars ) ;
osg : : MatrixTransform * marsPosition = solarSystem . createTranslationAndTilt ( solarSystem . _RorbitMars , 0.0f ) ;
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
marsPosition - > addChild ( mars ) ;
aroundSunRotationMars - > addChild ( marsPosition ) ;
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
sunLight - > addChild ( aroundSunRotationMars ) ;
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2013-06-27 17:54:12 +08:00
* * end Mars and Transformations
2004-11-16 03:44:35 +08:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
2004-10-29 03:09:32 +08:00
2004-11-16 03:44:35 +08:00
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2013-06-27 17:54:12 +08:00
* * Jupiter and Transformations
2004-11-16 03:44:35 +08:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
osg : : Node * jupiter = solarSystem . createPlanet ( solarSystem . _radiusJupiter , " Jupiter " , osg : : Vec4 ( 1.0f , 1.0f , 1.0f , 1.0f ) , solarSystem . _mapJupiter , " " ) ;
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
osg : : MatrixTransform * aroundSunRotationJupiter = solarSystem . createRotation ( solarSystem . _RorbitJupiter , solarSystem . _rotateSpeedJupiter ) ;
osg : : MatrixTransform * jupiterPosition = solarSystem . createTranslationAndTilt ( solarSystem . _RorbitJupiter , 0.0f ) ;
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
jupiterPosition - > addChild ( jupiter ) ;
aroundSunRotationJupiter - > addChild ( jupiterPosition ) ;
2013-06-27 17:54:12 +08:00
2004-11-16 03:44:35 +08:00
sunLight - > addChild ( aroundSunRotationJupiter ) ;
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2013-06-27 17:54:12 +08:00
* * end Jupiter and Transformations
2004-11-16 03:44:35 +08:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
2004-10-29 03:09:32 +08:00
2004-11-16 03:44:35 +08:00
/*
2004-10-29 03:09:32 +08:00
// add space, but don't light it, as its not illuminated by our sun
2004-11-16 03:44:35 +08:00
osg : : Node * space = solarSystem . createSpace ( " Space " , solarSystem . _mapSpace ) ;
2004-10-29 03:09:32 +08:00
space - > getOrCreateStateSet ( ) - > setMode ( GL_LIGHTING , osg : : StateAttribute : : OFF ) ;
root - > addChild ( space ) ;
2013-06-27 17:54:12 +08:00
*/
2004-11-18 23:01:41 +08:00
if ( ! writeFileName . empty ( ) )
{
osgDB : : writeNodeFile ( * root , writeFileName ) ;
std : : cout < < " Written solar system to \" " < < writeFileName < < " \" " < < std : : endl ;
2004-11-18 23:10:24 +08:00
return 0 ;
2004-11-18 23:01:41 +08:00
}
2004-10-23 00:39:24 +08:00
// run optimization over the scene graph
osgUtil : : Optimizer optimzer ;
2004-10-24 22:23:42 +08:00
optimzer . optimize ( root ) ;
2013-06-27 17:54:12 +08:00
2004-10-23 00:39:24 +08:00
// set the scene to render
2004-10-24 22:23:42 +08:00
viewer . setSceneData ( root ) ;
2004-10-23 00:39:24 +08:00
2004-10-29 03:09:32 +08:00
// set up tracker manipulators, once for each astral body
{
2004-11-16 03:44:35 +08:00
FindNamedNodeVisitor fnnv ( " Moon " ) ;
2004-10-29 03:09:32 +08:00
root - > accept ( fnnv ) ;
if ( ! fnnv . _foundNodes . empty ( ) )
{
// set up the node tracker.
osgGA : : NodeTrackerManipulator * tm = new osgGA : : NodeTrackerManipulator ;
tm - > setTrackerMode ( trackerMode ) ;
tm - > setRotationMode ( rotationMode ) ;
tm - > setTrackNode ( fnnv . _foundNodes . front ( ) . get ( ) ) ;
2007-01-10 21:52:22 +08:00
unsigned int num = keyswitchManipulator - > getNumMatrixManipulators ( ) ;
keyswitchManipulator - > addMatrixManipulator ( ' m ' , " moon " , tm ) ;
keyswitchManipulator - > selectMatrixManipulator ( num ) ;
2004-10-29 03:09:32 +08:00
}
2013-06-27 17:54:12 +08:00
}
2004-10-29 03:09:32 +08:00
{
2004-11-16 03:44:35 +08:00
FindNamedNodeVisitor fnnv ( " Earth " ) ;
2004-10-29 03:09:32 +08:00
root - > accept ( fnnv ) ;
if ( ! fnnv . _foundNodes . empty ( ) )
{
// set up the node tracker.
osgGA : : NodeTrackerManipulator * tm = new osgGA : : NodeTrackerManipulator ;
tm - > setTrackerMode ( trackerMode ) ;
tm - > setRotationMode ( rotationMode ) ;
tm - > setTrackNode ( fnnv . _foundNodes . front ( ) . get ( ) ) ;
2007-01-10 21:52:22 +08:00
unsigned int num = keyswitchManipulator - > getNumMatrixManipulators ( ) ;
keyswitchManipulator - > addMatrixManipulator ( ' e ' , " earth " , tm ) ;
keyswitchManipulator - > selectMatrixManipulator ( num ) ;
2004-10-29 03:09:32 +08:00
}
2013-06-27 17:54:12 +08:00
}
2004-10-29 03:09:32 +08:00
{
2004-11-16 03:44:35 +08:00
FindNamedNodeVisitor fnnv ( " Sun " ) ;
2004-10-29 03:09:32 +08:00
root - > accept ( fnnv ) ;
if ( ! fnnv . _foundNodes . empty ( ) )
{
// set up the node tracker.
osgGA : : NodeTrackerManipulator * tm = new osgGA : : NodeTrackerManipulator ;
tm - > setTrackerMode ( trackerMode ) ;
tm - > setRotationMode ( rotationMode ) ;
tm - > setTrackNode ( fnnv . _foundNodes . front ( ) . get ( ) ) ;
2007-01-10 21:52:22 +08:00
unsigned int num = keyswitchManipulator - > getNumMatrixManipulators ( ) ;
keyswitchManipulator - > addMatrixManipulator ( ' s ' , " sun " , tm ) ;
keyswitchManipulator - > selectMatrixManipulator ( num ) ;
2004-10-29 03:09:32 +08:00
}
2013-06-27 17:54:12 +08:00
}
2006-08-03 03:55:03 +08:00
2007-01-10 21:52:22 +08:00
return viewer . run ( ) ;
2004-10-23 00:39:24 +08:00
2004-10-29 03:09:32 +08:00
} // end main