from PyOSG import * # details about distances and rotation on http://www.solarviews.com/eng/solarsys.htm defaultPos = osg.Vec3( 0.0, 0.0, 0.0 ) centerScope = osg.Vec3(0.0, 0.0, 0.0) # /** create quad at specified position. */ def createSquare(corner, width, height, image=None): # set up the Geometry. geom = osg.Geometry() coords = osg.Vec3Array(4) coords[0] = corner coords[1] = corner+width coords[2] = corner+width+height coords[3] = corner+height geom.setVertexArray(coords) norms = osg.Vec3Array(1) norms[0] = width^height norms[0].normalize() geom.setNormalArray(norms) geom.setNormalBinding(osg.Geometry.BIND_OVERALL) tcoords = osg.Vec2Array(4) tcoords[0].set(0.0,0.0) tcoords[1].set(1.0,0.0) tcoords[2].set(1.0,1.0) tcoords[3].set(0.0,1.0) geom.setTexCoordArray(0,tcoords) colours = osg.Vec4Array(1) colours[0].set(1.0,1.0,1.0,1.0) geom.setColorArray(colours) geom.setColorBinding(osg.Geometry.BIND_OVERALL) geom.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.QUADS,0,4)) if image: stateset = osg.StateSet() texture = 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) return geom def createBillboardImage(centerColour, size, power): backgroundColour = centerColour.copy() backgroundColour[3] = 0.0 image = osg.Image() image.allocateImage(size,size,1, GL_RGBA,GL_UNSIGNED_BYTE) mid = (size-1.0)*0.5 div = 2.0/size for r in range(size): ptr = image.data(0,r,0) for c in range(size): dx = (float(c) - mid)*div dy = (float(r) - mid)*div r = powf(1.0-sqrtf(dx*dx+dy*dy),power) if r<0.0: r=0.0 osg.Vec4 color = centerColour*r+backgroundColour*(1.0-r) # color.set(1.0,1.0,1.0,0.5) *ptr++ = (unsigned char)((color[0])*255.0) *ptr++ = (unsigned char)((color[1])*255.0) *ptr++ = (unsigned char)((color[2])*255.0) *ptr++ = (unsigned char)((color[3])*255.0) return image #return osgDB.readImageFile("spot.dds") def createAnimationPath(center, radius, looptime): # set up the animation path animationPath = osg.AnimationPath() animationPath.setLoopMode(osg.AnimationPath.LOOP) numSamples = 1000 yaw = 0.0 yaw_delta = -2.0*osg.PI/(numSamples-1.0) roll = osg.inDegrees(30.0) time=0.0 time_delta = looptime/(double)numSamples for i in range(numSamples): position = osg.Vec3(center+osg.Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0)) rotation = osg.Quat(osg.Quat(roll,osg.Vec3(0.0,1.0,0.0))*osg.Quat(-(yaw+osg.inDegrees(90.0)),osg.Vec3(0.0,0.0,1.0))) animationPath.insert(time,osg.AnimationPath.ControlPoint(position,rotation)) yaw += yaw_delta time += time_delta return animationPath class SolarSystem public: double _radiusSpace double _radiusSun double _radiusMercury double _radiusVenus double _radiusEarth double _radiusMoon double _radiusMars double _radiusJupiter double _RorbitMercury double _RorbitVenus double _RorbitEarth double _RorbitMoon double _RorbitMars double _RorbitJupiter double _rotateSpeedSun double _rotateSpeedMercury double _rotateSpeedVenus double _rotateSpeedEarthAndMoon double _rotateSpeedEarth double _rotateSpeedMoon double _rotateSpeedMars double _rotateSpeedJupiter double _tiltEarth std.string _mapSpace std.string _mapSun std.string _mapVenus; std.string _mapMercury; std.string _mapEarth std.string _mapEarthNight std.string _mapMoon std.string _mapMars std.string _mapJupiter double _rotateSpeedFactor double _RorbitFactor double _radiusFactor SolarSystem() { _radiusSpace = 500.0 _radiusSun = 109.0 _radiusMercury = 0.38 _radiusVenus = 0.95 _radiusEarth = 1.0 _radiusMoon = 0.1 _radiusMars = 0.53 _radiusJupiter = 5.0 _RorbitMercury = 11.7 _RorbitVenus = 21.6 _RorbitEarth = 30.0 _RorbitMoon = 1.0 _RorbitMars = 45.0 _RorbitJupiter = 156.0 # 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 _rotateSpeedEarth = 1.0; # _rotateSpeedMoon = 0.95; # _rotateSpeedMars = 0.53; # 686.98 _rotateSpeedJupiter = 0.08; # 4332.71 _tiltEarth = 23.45; # degrees _mapSpace = "Images/spacemap2.jpg" _mapSun = "SolarSystem/sun256128.jpg" _mapMercury = "SolarSystem/mercury256128.jpg" _mapVenus = "SolarSystem/venus256128.jpg" _mapEarth = "Images/land_shallow_topo_2048.jpg" _mapEarthNight = "Images/land_ocean_ice_lights_2048.jpg" _mapMoon = "SolarSystem/moon256128.jpg" _mapMars = "SolarSystem/mars256128.jpg" _mapJupiter = "SolarSystem/jupiter256128.jpg" _rotateSpeedFactor = 0.5 _RorbitFactor = 15.0 _radiusFactor = 10.0 } osg.MatrixTransform* createTranslationAndTilt( double translation, double tilt ) osg.MatrixTransform* createRotation( double orbit, double speed ) 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 ) osg.Geode* createPlanet( double radius, const std.string& name, const osg.Vec4& color , const std.string& textureName1, const std.string& textureName2) osg.Group* createSunLight() void rotateSpeedCorrection() { _rotateSpeedSun *= _rotateSpeedFactor _rotateSpeedMercury *= _rotateSpeedFactor _rotateSpeedVenus *= _rotateSpeedFactor _rotateSpeedEarthAndMoon *= _rotateSpeedFactor _rotateSpeedEarth *= _rotateSpeedFactor _rotateSpeedMoon *= _rotateSpeedFactor _rotateSpeedMars *= _rotateSpeedFactor _rotateSpeedJupiter *= _rotateSpeedFactor std.cout << "rotateSpeed corrected by factor " << _rotateSpeedFactor << std.endl } void RorbitCorrection() { _RorbitMercury *= _RorbitFactor _RorbitVenus *= _RorbitFactor _RorbitEarth *= _RorbitFactor _RorbitMoon *= _RorbitFactor _RorbitMars *= _RorbitFactor _RorbitJupiter *= _RorbitFactor std.cout << "Rorbits corrected by factor " << _RorbitFactor << std.endl } void radiusCorrection() { _radiusSpace *= _radiusFactor #_radiusSun *= _radiusFactor _radiusMercury *= _radiusFactor _radiusVenus *= _radiusFactor _radiusEarth *= _radiusFactor _radiusMoon *= _radiusFactor _radiusMars *= _radiusFactor _radiusJupiter *= _radiusFactor std.cout << "Radius corrected by factor " << _radiusFactor << std.endl } void printParameters() }; # end SolarSystem class FindNamedNodeVisitor : public osg.NodeVisitor { public: FindNamedNodeVisitor(const std.string& name): osg.NodeVisitor(osg.NodeVisitor.TRAVERSE_ALL_CHILDREN), _name(name) {} virtual void apply(osg.Node& node) { if (node.getName()==_name) { _foundNodes.push_back(&node) } traverse(node) } typedef std.vector< osg.ref_ptr > NodeList std.string _name NodeList _foundNodes } def SolarSystem.createRotation(orbit, speed ): center = osg.Vec3( 0.0, 0.0, 0.0 ) animationLength = 10.0 animationPath = createAnimationPath( center, orbit, animationLength ) rotation = osg.MatrixTransform() rotation.setUpdateCallback( osg.AnimationPathCallback( animationPath, 0.0, speed ) ) return rotation def SolarSystem.createTranslationAndTilt(translation, tilt ): moonPositioned = 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.0,0.0,1.0)) return moonPositioned def SolarSystem.createSpace(name,textureName ): spaceSphere = osg.Sphere( osg.Vec3( 0.0, 0.0, 0.0 ), _radiusSpace ) sSpaceSphere = osg.ShapeDrawable( spaceSphere ) if not textureName.empty(): image = osgDB.readImageFile( textureName ) if image: sSpaceSphere.getOrCreateStateSet().setTextureAttributeAndModes( 0, 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.0,1.0,1.0,1.0) ) } } geodeSpace = osg.Geode() geodeSpace.setName( name ) geodeSpace.addDrawable( sSpaceSphere ) return( geodeSpace ) def SolarSystem.createPlanet( double radius, const std.string& name, const osg.Vec4& color , const std.string& textureName) # create a container that makes the sphere drawable sPlanetSphere = osg.Geometry() # set the single colour so bind overall colours = osg.Vec4Array(1) colours[0] = color sPlanetSphere.setColorArray(colours) sPlanetSphere.setColorBinding(osg.Geometry.BIND_OVERALL) # now set up the coords, normals and texcoords for geometry numX = 100 numY = 50 numVertices = numX*numY coords = osg.Vec3Array(numVertices) sPlanetSphere.setVertexArray(coords) normals = osg.Vec3Array(numVertices) sPlanetSphere.setNormalArray(normals) sPlanetSphere.setNormalBinding(osg.Geometry.BIND_PER_VERTEX) texcoords = 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) delta_tx = 1.0 / (numX-1) delta_ty = 1.0 / (numY-1) elevation = -osg.PI*0.5 ty = 0.0 vert = 0 for (j=0 j","Write created model to file") # initialize the viewer. osgProducer.Viewer viewer(arguments) # set up the value with sensible default event handlers. viewer.setUpViewer(osgProducer.Viewer.ESCAPE_SETS_DONE | osgProducer.Viewer.VIEWER_MANIPULATOR | osgProducer.Viewer.STATE_MANIPULATOR) # get details on keyboard and mouse bindings used by the viewer. viewer.getUsage(*arguments.getApplicationUsage()) SolarSystem solarSystem while (arguments.read("--radiusSpace",solarSystem._radiusSpace)) { } while (arguments.read("--radiusSun",solarSystem._radiusSun)) { } while (arguments.read("--radiusMercury",solarSystem._radiusMercury)) { } while (arguments.read("--radiusVenus",solarSystem._radiusVenus)) { } while (arguments.read("--radiusEarth",solarSystem._radiusEarth)) { } while (arguments.read("--radiusMoon",solarSystem._radiusMoon)) { } while (arguments.read("--radiusMars",solarSystem._radiusMars)) { } while (arguments.read("--radiusJupiter",solarSystem._radiusJupiter)) { } while (arguments.read("--RorbitEarth",solarSystem._RorbitEarth)) { } while (arguments.read("--RorbitMoon",solarSystem._RorbitMoon)) { } while (arguments.read("--rotateSpeedEarthAndMoon",solarSystem._rotateSpeedEarthAndMoon)) { } while (arguments.read("--rotateSpeedEarth",solarSystem._rotateSpeedEarth)) { } while (arguments.read("--rotateSpeedMoon",solarSystem._rotateSpeedMoon)) { } while (arguments.read("--tiltEarth",solarSystem._tiltEarth)) { } while (arguments.read("--mapSpace",solarSystem._mapSpace)) { } while (arguments.read("--mapEarth",solarSystem._mapEarth)) { } while (arguments.read("--mapEarthNight",solarSystem._mapEarthNight)) { } while (arguments.read("--mapMoon",solarSystem._mapMoon)) { } while (arguments.read("--rotateSpeedFactor",solarSystem._rotateSpeedFactor)) { } while (arguments.read("--RorbitFactor",solarSystem._RorbitFactor)) { } while (arguments.read("--radiusFactor",solarSystem._radiusFactor)) { } solarSystem.rotateSpeedCorrection() solarSystem.RorbitCorrection() solarSystem.radiusCorrection() std.string writeFileName while (arguments.read("-o",writeFileName)) { } 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 "<