def main(argv): # use an ArgumentParser object to manage the program arguments. arguments = osg.ArgumentParser(argv) # construct the viewer. viewer = osgViewer.Viewer() # load the nodes from the commandline arguments. rootnode = osgDB.readNodeFiles(arguments) # if not loaded assume no arguments passed in, try use default mode instead. if not rootnode : rootnode = osgDB.readNodeFile("cessnafire.osgt") if not rootnode : osg.notify(osg.NOTICE), "Please specify a model filename on the command line." return 1 image = osgDB.readImageFile("Images/reflect.rgb") if image : texture = osg.Texture2D() texture.setImage(image) texgen = osg.TexGen() texgen.setMode(osg.TexGen.SPHERE_MAP) texenv = osg.TexEnv() texenv.setMode(osg.TexEnv.BLEND) texenv.setColor(osg.Vec4(0.3,0.3,0.3,0.3)) stateset = osg.StateSet() stateset.setTextureAttributeAndModes(1,texture,osg.StateAttribute.ON) stateset.setTextureAttributeAndModes(1,texgen,osg.StateAttribute.ON) stateset.setTextureAttribute(1,texenv) rootnode.setStateSet(stateset) else: osg.notify(osg.NOTICE), "unable to load reflect map, model will not be mutlitextured" # run optimization over the scene graph optimzer = osgUtil.Optimizer() optimzer.optimize(rootnode) # add a viewport to the viewer and attach the scene graph. viewer.setSceneData( rootnode ) # create the windows and run the threads. viewer.realize() for(unsigned int contextID = 0 contextID<osg.DisplaySettings.instance().getMaxNumberOfGraphicsContexts() ++contextID) textExt = osg.Texture.getExtensions(contextID,False) if textExt : if not textExt.isMultiTexturingSupported() : print "Warning: multi-texturing not supported by OpenGL drivers, unable to run application." return 1
def createSkyBox(): stateset = osg.StateSet() te = osg.TexEnv() te.setMode(osg.TexEnv.REPLACE) stateset.setTextureAttributeAndModes(0, te, osg.StateAttribute.ON) tg = osg.TexGen() tg.setMode(osg.TexGen.NORMAL_MAP) stateset.setTextureAttributeAndModes(0, tg, osg.StateAttribute.ON) tm = osg.TexMat() stateset.setTextureAttribute(0, tm) skymap = readCubeMap() stateset.setTextureAttributeAndModes(0, skymap, osg.StateAttribute.ON) stateset.setMode( GL_LIGHTING, osg.StateAttribute.OFF ) stateset.setMode( GL_CULL_FACE, osg.StateAttribute.OFF ) # clear the depth to the far plane. depth = osg.Depth() depth.setFunction(osg.Depth.ALWAYS) depth.setRange(1.0,1.0) stateset.setAttributeAndModes(depth, osg.StateAttribute.ON ) stateset.setRenderBinDetails(-1,"RenderBin") drawable = osg.ShapeDrawable(osg.Sphere(osg.Vec3(0.0,0.0,0.0),1)) geode = osg.Geode() geode.setCullingActive(False) geode.setStateSet( stateset ) geode.addDrawable(drawable) transform = MoveEarthySkyWithEyePointTransform() transform.setCullingActive(False) transform.addChild(geode) clearNode = osg.ClearNode() # clearNode.setRequiresClear(False) clearNode.setCullCallback(TexMatCallback(*tm)) clearNode.addChild(transform) return clearNode
def main(): viewer = osgViewer.Viewer() # Declare a group to act as root node of a scene: root = osg.Group() # Declare a box class (derived from shape class) instance # This constructor takes an osg.Vec3 to define the center # and a float to define the height, width and depth. # (an overloaded constructor allows you to specify unique # height, width and height values.) unitCube = osg.Box( osg.Vec3(0,0,0), 1.0) unitCube.setDataVariance(osg.Object.DYNAMIC) # Declare an instance of the shape drawable class and initialize # it with the unitCube shape we created above. # This class is derived from 'drawable' so instances of this # class can be added to Geode instances. unitCubeDrawable = osg.ShapeDrawable(unitCube) # Declare a instance of the geode class: basicShapesGeode = osg.Geode() # Add the unit cube drawable to the geode: basicShapesGeode.addDrawable(unitCubeDrawable) # Add the goede to the scene: root.addChild(basicShapesGeode) unitSphere = osg.Sphere( osg.Vec3(0,0,0), 1.0) unitSphereDrawable = osg.ShapeDrawable(unitSphere) unitSphereDrawable.setColor( osg.Vec4(0.1, 0.1, 0.1, 0.1) ) unitSphereXForm = osg.PositionAttitudeTransform() unitSphereXForm.setPosition(osg.Vec3(3.0,0,0)) unitSphereGeode = osg.Geode() root.addChild(unitSphereXForm) unitSphereXForm.addChild(unitSphereGeode) unitSphereGeode.addDrawable(unitSphereDrawable) pyramidGeode = createPyramid() pyramidXForm = osg.PositionAttitudeTransform() pyramidXForm.setPosition(osg.Vec3(0,-3.0,0)) root.addChild(pyramidXForm) pyramidXForm.addChild(pyramidGeode) KLN89FaceTexture = osg.Texture2D() # protect from being optimized away as static state: KLN89FaceTexture.setDataVariance(osg.Object.DYNAMIC) klnFace = osgDB.readImageFile("../NPS_Data/Textures/KLN89FaceB.tga") if klnFace is None: print " couldn't find texture, quitting." sys.exit(-1) KLN89FaceTexture.setImage(klnFace) # Declare a state set for 'BLEND' texture mode blendStateSet = osg.StateSet() # Declare a TexEnv instance, set the mode to 'BLEND' blendTexEnv = osg.TexEnv() blendTexEnv.setMode(osg.TexEnv.BLEND) # Turn the attribute of texture 0 'ON' blendStateSet.setTextureAttributeAndModes(0,KLN89FaceTexture,osg.StateAttribute.ON) # Set the texture texture environment for texture 0 to the # texture envirnoment we declared above: blendStateSet.setTextureAttribute(0,blendTexEnv) decalStateSet = osg.StateSet() decalTexEnv = osg.TexEnv() decalTexEnv.setMode(osg.TexEnv.DECAL) decalStateSet.setTextureAttributeAndModes(0,KLN89FaceTexture,osg.StateAttribute.ON) decalStateSet.setTextureAttribute(0,decalTexEnv) root.setStateSet(blendStateSet) unitSphereGeode.setStateSet(decalStateSet) # OSG1 viewer.setUpViewer(osgProducer.Viewer.STANDARD_SETTINGS) viewer.setSceneData( root ) viewer.setCameraManipulator(osgGA.TrackballManipulator()) viewer.realize() while not viewer.done(): viewer.frame() return 0
def main(argv): # use an ArgumentParser object to manage the program arguments. arguments = osg.ArgumentParser(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 node tracker.") arguments.getApplicationUsage().setCommandLineUsage(arguments.getApplicationName()) arguments.getApplicationUsage().addCommandLineOption("-h or --help","Display this information") # construct the viewer. viewer = osgViewer.Viewer(arguments) # add the state manipulator viewer.addEventHandler( osgGA.StateSetManipulator(viewer.getCamera().getOrCreateStateSet()) ) # add the thread model handler viewer.addEventHandler(osgViewer.ThreadingHandler)() # add the window size toggle handler viewer.addEventHandler(osgViewer.WindowSizeHandler)() # add the stats handler viewer.addEventHandler(osgViewer.StatsHandler)() # add the record camera path handler viewer.addEventHandler(osgViewer.RecordCameraPathHandler)() # add the help handler viewer.addEventHandler(osgViewer.HelpHandler(arguments.getApplicationUsage())) # set the near far ration computation up. viewer.getCamera().setComputeNearFarMode(osg.CullSettings.COMPUTE_NEAR_FAR_USING_PRIMITIVES) viewer.getCamera().setNearFarRatio(0.000003) speed = 1.0 while arguments.read("-f") or arguments.read("--fixed") : speed = 0.0 rotation = osg.Quat() vec4 = osg.Vec4() while arguments.read("--rotate-model",vec4[0],vec4[1],vec4[2],vec4[3]) : local_rotate = osg.Quat() local_rotate.makeRotate(osg.DegreesToRadians(vec4[0]),vec4[1],vec4[2],vec4[3]) rotation = rotation * local_rotate nc = 0 flightpath_filename = str() while arguments.read("--flight-path",flightpath_filename) : fin = osgDB.ifstream(flightpath_filename.c_str()) if fin : path = osg.AnimationPath() path.read(fin) nc = osg.AnimationPathCallback(path) trackerMode = osgGA.NodeTrackerManipulator.NODE_CENTER_AND_ROTATION mode = str() while arguments.read("--tracker-mode",mode) : if mode=="NODE_CENTER_AND_ROTATION" : trackerMode = osgGA.NodeTrackerManipulator.NODE_CENTER_AND_ROTATION elif mode=="NODE_CENTER_AND_AZIM" : trackerMode = osgGA.NodeTrackerManipulator.NODE_CENTER_AND_AZIM elif mode=="NODE_CENTER" : trackerMode = osgGA.NodeTrackerManipulator.NODE_CENTER else: print "Unrecognized --tracker-mode option ", mode, ", valid options are:" print " NODE_CENTER_AND_ROTATION" print " NODE_CENTER_AND_AZIM" print " NODE_CENTER" return 1 rotationMode = osgGA.NodeTrackerManipulator.TRACKBALL while arguments.read("--rotation-mode",mode) : if mode=="TRACKBALL" : rotationMode = osgGA.NodeTrackerManipulator.TRACKBALL elif mode=="ELEVATION_AZIM" : rotationMode = osgGA.NodeTrackerManipulator.ELEVATION_AZIM else: print "Unrecognized --rotation-mode option ", mode, ", valid options are:" print " TRACKBALL" print " ELEVATION_AZIM" return 1 useOverlay = True while arguments.read("--no-overlay") or arguments.read("-n") : useOverlay = False technique = osgSim.OverlayNode.OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY while arguments.read("--object") : technique = osgSim.OverlayNode.OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY while arguments.read("--ortho") or arguments.read("--orthographic") : technique = osgSim.OverlayNode.VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY while arguments.read("--persp") or arguments.read("--perspective") : technique = osgSim.OverlayNode.VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY overlayTextureUnit = 1 while arguments.read("--unit", overlayTextureUnit) : pathfile = str() while arguments.read("-p",pathfile) : addFireEffect = arguments.read("--fire") # if user request help write it out to cout. if arguments.read("-h") or arguments.read("--help") : arguments.getApplicationUsage().write(std.cout) return 1 tm = osgGA.NodeTrackerManipulator() overlayFilename = str() while arguments.read("--overlay", overlayFilename) : # read the scene from the list of file specified commandline args. root = osgDB.readNodeFiles(arguments) if not root : root = createEarth() if not root : return 0 if not overlayFilename.empty() : #osg.Object *pObj = osgDB.readObjectFile("alaska_clean.shp") #osg.Geode shapefile = dynamic_cast<osg.Geode*> (pObj) # #ConvertLatLon2EllipsoidCoordinates latlon2em #shapefile.accept(latlon2em) shapefile = osgDB.readNodeFile(overlayFilename) if not shapefile : osg.notify(osg.NOTICE), "File `", overlayFilename, "` not found" return 1 csn = dynamic_cast<osg.CoordinateSystemNode*>(root) if csn : overlayNode = osgSim.OverlayNode(technique) overlayNode.getOrCreateStateSet().setTextureAttribute(1, osg.TexEnv(osg.TexEnv.DECAL)) overlayNode.setOverlaySubgraph(shapefile) overlayNode.setOverlayTextureSizeHint(1024) overlayNode.setOverlayTextureUnit(overlayTextureUnit) # insert the OverlayNode between the coordinate system node and its children. for(unsigned int i=0 i<csn.getNumChildren() ++i) overlayNode.addChild( csn.getChild(i) ) csn.removeChildren(0, csn.getNumChildren()) csn.addChild(overlayNode) viewer.setSceneData(csn) else: overlayNode = osgSim.OverlayNode(technique) overlayNode.getOrCreateStateSet().setTextureAttribute(1, osg.TexEnv(osg.TexEnv.DECAL)) overlayNode.setOverlaySubgraph(shapefile) overlayNode.setOverlayTextureSizeHint(1024) overlayNode.addChild(root) viewer.setSceneData(overlayNode) else: # add a viewport to the viewer and attach the scene graph. viewer.setSceneData(root) csn = dynamic_cast<osg.CoordinateSystemNode*>(root) if csn : overlayNode = osgSim.OverlayNode() if useOverlay : overlayNode = osgSim.OverlayNode(technique) # insert the OverlayNode between the coordinate system node and its children. for(unsigned int i=0 i<csn.getNumChildren() ++i) overlayNode.addChild( csn.getChild(i) ) csn.removeChildren(0, csn.getNumChildren()) csn.addChild(overlayNode) # tell the overlay node to continously update its overlay texture # as we know we'll be tracking a moving target. overlayNode.setContinuousUpdate(True) cessna = osgDB.readNodeFile("cessna.osgt") if cessna : s = 200000.0 / cessna.getBound().radius() scaler = osg.MatrixTransform() scaler.addChild(cessna) scaler.setMatrix(osg.Matrixd.scale(s,s,s)*osg.Matrixd.rotate(rotation)) scaler.getOrCreateStateSet().setMode(GL_RESCALE_NORMAL,osg.StateAttribute.ON) if addFireEffect : center = cessna.getBound().center() fire = osgParticle.FireEffect(center, 10.0) scaler.addChild(fire) if False : ss = osgSim.SphereSegment( osg.Vec3(0.0,0.0,0.0), # center 19.9, # radius osg.DegreesToRadians(135.0), osg.DegreesToRadians(240.0), osg.DegreesToRadians(-10.0), osg.DegreesToRadians(30.0), 60) scaler.addChild(ss) mt = osg.MatrixTransform() mt.addChild(scaler) if not nc : nc = ModelPositionCallback(speed) mt.setUpdateCallback(nc) csn.addChild(mt) # if we are using an overaly node, use the cessna subgraph as the overlay subgraph if overlayNode.valid() : overlayNode.setOverlaySubgraph(mt) tm = osgGA.NodeTrackerManipulator() tm.setTrackerMode(trackerMode) tm.setRotationMode(rotationMode) tm.setTrackNode(scaler) else: print "Failed to read cessna.osgt" # set up camera manipulators. keyswitchManipulator = osgGA.KeySwitchMatrixManipulator() if tm.valid() : keyswitchManipulator.addMatrixManipulator( ord("0"), "NodeTracker", tm ) keyswitchManipulator.addMatrixManipulator( ord("1"), "Trackball", osgGA.TrackballManipulator() ) keyswitchManipulator.addMatrixManipulator( ord("2"), "Flight", osgGA.FlightManipulator() ) keyswitchManipulator.addMatrixManipulator( ord("3"), "Drive", osgGA.DriveManipulator() ) keyswitchManipulator.addMatrixManipulator( ord("4"), "Terrain", osgGA.TerrainManipulator() ) if not pathfile.empty() : apm = osgGA.AnimationPathManipulator(pathfile) if apm or not apm.valid() : num = keyswitchManipulator.getNumMatrixManipulators() keyswitchManipulator.addMatrixManipulator( ord("5"), "Path", apm ) keyswitchManipulator.selectMatrixManipulator(num) viewer.setCameraManipulator( keyswitchManipulator ) # viewer.setThreadingModel(osgViewer.Viewer.SingleThreaded) return viewer.run() if __name__ == "__main__": main(sys.argv)
class IntersectionUpdateCallback (osg.NodeCallback) : virtual void operator()(osg.Node* #node, osg.NodeVisitor* nv) if not root_ or not terrain_ or not ss_ or not intersectionGroup_ : osg.notify(osg.NOTICE), "IntersectionUpdateCallback not set up correctly." return #traverse(node,nv) frameCount_++ if frameCount_ > 200 : # first we need find the transformation matrix that takes # the terrain into the coordinate frame of the sphere segment. terrainLocalToWorld = osg.Matrixd() terrain_worldMatrices = terrain_.getWorldMatrices(root_) if terrain_worldMatrices.empty() : terrainLocalToWorld.makeIdentity() elif terrain_worldMatrices.size()==1 : terrainLocalToWorld = terrain_worldMatrices.front() else: osg.notify(osg.NOTICE), "IntersectionUpdateCallback: warning cannot interestect with multiple terrain instances, just uses first one." terrainLocalToWorld = terrain_worldMatrices.front() # sphere segment is easier as this callback is attached to the node, so the node visitor has the unique path to it already. ssWorldToLocal = osg.computeWorldToLocal(nv.getNodePath()) # now we can compute the terrain to ss transform possie = terrainLocalToWorld*ssWorldToLocal lines = ss_.computeIntersection(possie, terrain_) if not lines.empty() : if intersectionGroup_.valid() : # now we need to place the intersections which are in the SphereSegmenet's coordinate frame into # to the final position. mt = osg.MatrixTransform() mt.setMatrix(osg.computeLocalToWorld(nv.getNodePath())) intersectionGroup_.addChild(mt) # print "matrix = ", mt.getMatrix() geode = osg.Geode() mt.addChild(geode) geode.getOrCreateStateSet().setMode(GL_LIGHTING,osg.StateAttribute.OFF) for(osgSim.SphereSegment.LineList.iterator itr=lines.begin() not = lines.end() ++itr) geom = osg.Geometry() geode.addDrawable(geom) vertices = itr geom.setVertexArray(vertices) geom.addPrimitiveSet(osg.DrawArrays(GL_LINE_STRIP, 0, vertices.getNumElements())) else: osg.notify(osg.NOTICE), "No intersections found" frameCount_ = 0 root_ = osg.observer_ptr<osg.Group>() terrain_ = osg.observer_ptr<osg.Geode>() ss_ = osg.observer_ptr<osgSim.SphereSegment>() intersectionGroup_ = osg.observer_ptr<osg.Group>() frameCount_ = unsigned() class RotateUpdateCallback (osg.NodeCallback) : RotateUpdateCallback() i=0 virtual void operator()(osg.Node* node, osg.NodeVisitor* nv) ss = dynamic_cast<osgSim.SphereSegment *>(node) if ss : ss.setArea(osg.Vec3(cos(i/(2*osg.PI)),sin(i/(2*osg.PI)),0), osg.PI_2, osg.PI_2) i += 0.1 i = float() def createMovingModel(center, radius, terrainGeode, root, createMovingRadar): animationLength = 10.0 animationPath = createAnimationPath(center,radius,animationLength) model = osg.Group() glider = osgDB.readNodeFile("glider.osgt") if glider : bs = glider.getBound() size = radius/bs.radius()*0.3 positioned = osg.MatrixTransform() positioned.setDataVariance(osg.Object.STATIC) positioned.setMatrix(osg.Matrix.translate(-bs.center())* osg.Matrix.scale(size,size,size)* osg.Matrix.rotate(osg.inDegrees(-90.0),0.0,0.0,1.0)) positioned.addChild(glider) xform = osg.PositionAttitudeTransform() xform.getOrCreateStateSet().setMode(GL_NORMALIZE, osg.StateAttribute.ON) xform.setUpdateCallback(osg.AnimationPathCallback(animationPath,0.0,1.0)) xform.addChild(positioned) model.addChild(xform) if createMovingRadar : # The IntersectionUpdateCallback has to have a safe place to put all its generated geometry into, # and this group can't be in the parental chain of the callback otherwise we will end up invalidating # traversal iterators. intersectionGroup = osg.Group() root.addChild(intersectionGroup) xform = osg.PositionAttitudeTransform() xform.setUpdateCallback(osg.AnimationPathCallback(animationPath,0.0,1.0)) ss = osgSim.SphereSegment(osg.Vec3d(0.0,0.0,0.0), 700.0, # radius osg.DegreesToRadians(135.0), osg.DegreesToRadians(240.0), osg.DegreesToRadians(-60.0), osg.DegreesToRadians(-40.0), 60) iuc = IntersectionUpdateCallback() iuc.frameCount_ = 0 iuc.root_ = root iuc.terrain_ = terrainGeode iuc.ss_ = ss iuc.intersectionGroup_ = intersectionGroup ss.setUpdateCallback(iuc) ss.setAllColors(osg.Vec4(1.0,1.0,1.0,0.5)) ss.setSideColor(osg.Vec4(0.5,1.0,1.0,0.1)) xform.addChild(ss) model.addChild(xform) cessna = osgDB.readNodeFile("cessna.osgt") if cessna : bs = cessna.getBound() text = osgText.Text() size = radius/bs.radius()*0.3 text.setPosition(bs.center()) text.setText("Cessna") text.setAlignment(osgText.Text.CENTER_CENTER) text.setAxisAlignment(osgText.Text.SCREEN) text.setCharacterSize(40.0) text.setCharacterSizeMode(osgText.Text.OBJECT_COORDS) geode = osg.Geode() geode.addDrawable(text) lod = osg.LOD() lod.setRangeMode(osg.LOD.PIXEL_SIZE_ON_SCREEN) lod.setRadius(cessna.getBound().radius()) lod.addChild(geode,0.0,100.0) lod.addChild(cessna,100.0,10000.0) positioned = osg.MatrixTransform() positioned.getOrCreateStateSet().setMode(GL_NORMALIZE, osg.StateAttribute.ON) positioned.setDataVariance(osg.Object.STATIC) positioned.setMatrix(osg.Matrix.translate(-bs.center())* osg.Matrix.scale(size,size,size)* osg.Matrix.rotate(osg.inDegrees(180.0),0.0,0.0,1.0)) #positioned.addChild(cessna) positioned.addChild(lod) xform = osg.MatrixTransform() xform.setUpdateCallback(osg.AnimationPathCallback(animationPath,0.0,2.0)) xform.addChild(positioned) model.addChild(xform) return model def createOverlay(center, radius): group = osg.Group() # create a grid of lines. geom = osg.Geometry() num_rows = 10 left = center+osg.Vec3(-radius,-radius,0.0) right = center+osg.Vec3(radius,-radius,0.0) delta_row = osg.Vec3(0.0,2.0*radius/float(num_rows-1),0.0) top = center+osg.Vec3(-radius,radius,0.0) bottom = center+osg.Vec3(-radius,-radius,0.0) delta_column = osg.Vec3(2.0*radius/float(num_rows-1),0.0,0.0) vertices = osg.Vec3Array() for(unsigned int i=0 i<num_rows ++i) vertices.push_back(left) vertices.push_back(right) left += delta_row right += delta_row vertices.push_back(top) vertices.push_back(bottom) top += delta_column bottom += delta_column geom.setVertexArray(vertices) color = *(osg.Vec4ubArray(1)) color[0].set(0,0,0,255) geom.setColorArray(color, osg.Array.BIND_OVERALL) geom.addPrimitiveSet(osg.DrawArrays(GL_LINES,0,vertices.getNumElements())) geom.getOrCreateStateSet().setMode(GL_LIGHTING,osg.StateAttribute.OFF) geode = osg.Geode() geode.addDrawable(geom) group.addChild(geode) return group def computeTerrainIntersection(subgraph, x, y): bs = subgraph.getBound() zMax = bs.center().z()+bs.radius() zMin = bs.center().z()-bs.radius() intersector = osgUtil.LineSegmentIntersector(osg.Vec3(x,y,zMin),osg.Vec3(x,y,zMax)) iv = osgUtil.IntersectionVisitor(intersector) subgraph.accept(iv) if intersector.containsIntersections() : return intersector.getFirstIntersection().getWorldIntersectPoint() return osg.Vec3(x,y,0.0) ####################################### # MAIN SCENE GRAPH BUILDING FUNCTION ####################################### def build_world(root, testCase, useOverlay, technique): # create terrain terrainGeode = 0 terrainGeode = osg.Geode() stateset = osg.StateSet() image = osgDB.readImageFile("Images/lz.rgb") if image : texture = osg.Texture2D() texture.setImage(image) stateset.setTextureAttributeAndModes(0,texture,osg.StateAttribute.ON) terrainGeode.setStateSet( stateset ) numColumns = 38 numRows = 39 unsigned int r, c origin = osg.Vec3(0.0,0.0,0.0) size = osg.Vec3(1000.0,1000.0,250.0) geometry = osg.Geometry() v = *(osg.Vec3Array(numColumns*numRows)) tc = *(osg.Vec2Array(numColumns*numRows)) color = *(osg.Vec4ubArray(1)) color[0].set(255,255,255,255) rowCoordDelta = size.y()/(float)(numRows-1) columnCoordDelta = size.x()/(float)(numColumns-1) rowTexDelta = 1.0/(float)(numRows-1) columnTexDelta = 1.0/(float)(numColumns-1) # compute z range of z values of grid data so we can scale it. min_z = FLT_MAX max_z = -FLT_MAX for(r=0r<numRows++r) for(c=0c<numColumns++c) min_z = osg.minimum(min_z,vertex[r+c*numRows][2]) max_z = osg.maximum(max_z,vertex[r+c*numRows][2]) scale_z = size.z()/(max_z-min_z) pos = origin tex = osg.Vec2(0.0,0.0) vi = 0 for(r=0r<numRows++r) pos.x() = origin.x() tex.x() = 0.0 for(c=0c<numColumns++c) v[vi].set(pos.x(),pos.y(),pos.z()+(vertex[r+c*numRows][2]-min_z)*scale_z) tc[vi] = tex pos.x()+=columnCoordDelta tex.x()+=columnTexDelta ++vi pos.y() += rowCoordDelta tex.y() += rowTexDelta geometry.setVertexArray(v) geometry.setTexCoordArray(0, tc) geometry.setColorArray(color, osg.Array.BIND_OVERALL) for(r=0r<numRows-1++r) drawElements = *(osg.DrawElementsUShort(GL_QUAD_STRIP,2*numColumns)) geometry.addPrimitiveSet(drawElements) ei = 0 for(c=0c<numColumns++c) drawElements[ei++] = (r+1)*numColumns+c drawElements[ei++] = (r)*numColumns+c smoother = osgUtil.SmoothingVisitor() smoother.smooth(*geometry) terrainGeode.addDrawable(geometry) # create sphere segment ss = 0 terrainToSS = osg.Matrix() switch(testCase) case(0): ss = osgSim.SphereSegment( computeTerrainIntersection(terrainGeode,550.0,780.0), # center 510.0, # radius osg.DegreesToRadians(135.0), osg.DegreesToRadians(240.0), osg.DegreesToRadians(-10.0), osg.DegreesToRadians(30.0), 60) root.addChild(ss) break case(1): ss = osgSim.SphereSegment( computeTerrainIntersection(terrainGeode,550.0,780.0), # center 510.0, # radius osg.DegreesToRadians(45.0), osg.DegreesToRadians(240.0), osg.DegreesToRadians(-10.0), osg.DegreesToRadians(30.0), 60) root.addChild(ss) break case(2): ss = osgSim.SphereSegment( computeTerrainIntersection(terrainGeode,550.0,780.0), # center 510.0, # radius osg.DegreesToRadians(5.0), osg.DegreesToRadians(355.0), osg.DegreesToRadians(-10.0), osg.DegreesToRadians(30.0), 60) root.addChild(ss) break case(3): ss = osgSim.SphereSegment( computeTerrainIntersection(terrainGeode,550.0,780.0), # center 510.0, # radius osg.DegreesToRadians(0.0), osg.DegreesToRadians(360.0), osg.DegreesToRadians(-10.0), osg.DegreesToRadians(30.0), 60) root.addChild(ss) break case(4): ss = osgSim.SphereSegment(osg.Vec3d(0.0,0.0,0.0), 700.0, # radius osg.DegreesToRadians(135.0), osg.DegreesToRadians(240.0), osg.DegreesToRadians(-60.0), osg.DegreesToRadians(-40.0), 60) mt = osg.MatrixTransform() mt.setMatrix(osg.Matrix(-0.851781, 0.156428, -0.5, 0, -0.180627, -0.983552, -6.93889e-18, 0, -0.491776, 0.0903136, 0.866025, 0, 598.217, 481.957, 100, 1)) mt.addChild(ss) terrainToSS.invert(mt.getMatrix()) root.addChild(mt) break case(5): ss = osgSim.SphereSegment(osg.Vec3d(0.0,0.0,0.0), 700.0, # radius osg.DegreesToRadians(35.0), osg.DegreesToRadians(135.0), osg.DegreesToRadians(-60.0), osg.DegreesToRadians(-40.0), 60) mt = osg.MatrixTransform() mt.setMatrix(osg.Matrix(-0.851781, 0.156428, -0.5, 0, -0.180627, -0.983552, -6.93889e-18, 0, -0.491776, 0.0903136, 0.866025, 0, 598.217, 481.957, 100, 1)) mt.addChild(ss) terrainToSS.invert(mt.getMatrix()) root.addChild(mt) break case(6): ss = osgSim.SphereSegment(osg.Vec3d(0.0,0.0,0.0), 700.0, # radius osg.DegreesToRadians(-45.0), osg.DegreesToRadians(45.0), osg.DegreesToRadians(-60.0), osg.DegreesToRadians(-40.0), 60) mt = osg.MatrixTransform() mt.setMatrix(osg.Matrix(-0.851781, 0.156428, -0.5, 0, -0.180627, -0.983552, -6.93889e-18, 0, -0.491776, 0.0903136, 0.866025, 0, 598.217, 481.957, 100, 1)) mt.addChild(ss) terrainToSS.invert(mt.getMatrix()) root.addChild(mt) break case(7): ss = osgSim.SphereSegment( computeTerrainIntersection(terrainGeode,550.0,780.0), # center 510.0, # radius osg.DegreesToRadians(-240.0), osg.DegreesToRadians(-135.0), osg.DegreesToRadians(-10.0), osg.DegreesToRadians(30.0), 60) ss.setUpdateCallback(RotateUpdateCallback()) root.addChild(ss) break if ss.valid() : ss.setAllColors(osg.Vec4(1.0,1.0,1.0,0.5)) ss.setSideColor(osg.Vec4(0.0,1.0,1.0,0.1)) if not ss.getParents().empty() : ss.getParent(0).addChild(ss.computeIntersectionSubgraph(terrainToSS, terrainGeode)) if useOverlay : overlayNode = osgSim.OverlayNode(technique) overlayNode.getOrCreateStateSet().setTextureAttribute(1, osg.TexEnv(osg.TexEnv.DECAL)) bs = terrainGeode.getBound() overlaySubgraph = createOverlay(bs.center(), bs.radius()*0.5) overlaySubgraph.addChild(ss) overlayNode.setOverlaySubgraph(overlaySubgraph) overlayNode.setOverlayTextureSizeHint(1024) overlayNode.setOverlayBaseHeight(0.0) overlayNode.addChild(terrainGeode) root.addChild(overlayNode) else: root.addChild(terrainGeode) # create particle effects position = computeTerrainIntersection(terrainGeode,100.0,100.0) explosion = osgParticle.ExplosionEffect(position, 10.0) smoke = osgParticle.SmokeEffect(position, 10.0) fire = osgParticle.FireEffect(position, 10.0) root.addChild(explosion) root.addChild(smoke) root.addChild(fire) # create particle effects position = computeTerrainIntersection(terrainGeode,200.0,100.0) explosion = osgParticle.ExplosionEffect(position, 1.0) smoke = osgParticle.SmokeEffect(position, 1.0) fire = osgParticle.FireEffect(position, 1.0) root.addChild(explosion) root.addChild(smoke) root.addChild(fire) createMovingRadar = True # create the moving models. root.addChild(createMovingModel(osg.Vec3(500.0,500.0,500.0),100.0, terrainGeode, root, createMovingRadar)) ####################################### # main() ####################################### def main(argv): # use an ArgumentParser object to manage the program arguments. arguments = osg.ArgumentParser(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 particle systems.") arguments.getApplicationUsage().setCommandLineUsage(arguments.getApplicationName()+" [options] image_file_left_eye image_file_right_eye") arguments.getApplicationUsage().addCommandLineOption("-h or --help","Display this information") # construct the viewer. viewer = osgViewer.Viewer(arguments) # if user request help write it out to cout. testCase = 0 while arguments.read("-t", testCase) : useOverlay = False technique = osgSim.OverlayNode.OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY while arguments.read("--object") : useOverlay = True technique = osgSim.OverlayNode.OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY while arguments.read("--ortho") or arguments.read("--orthographic") : useOverlay = True technique = osgSim.OverlayNode.VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY while arguments.read("--persp") or arguments.read("--perspective") : useOverlay = True technique = osgSim.OverlayNode.VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY # if user request help write it out to cout. if arguments.read("-h") or arguments.read("--help") : arguments.getApplicationUsage().write(std.cout) return 1 # any option left unread are converted into errors to write out later. arguments.reportRemainingOptionsAsUnrecognized() # report any errors if they have occurred when parsing the program arguments. if arguments.errors() : arguments.writeErrorMessages(std.cout) return 1 root = osg.Group() build_world(root, testCase, useOverlay, technique) # add a viewport to the viewer and attach the scene graph. viewer.setSceneData(root) return viewer.run() if __name__ == "__main__": main(sys.argv)