#!/usr/bin/env python import sys import math from PyOSG import Producer from PyOSG import osg from PyOSG import osgDB from PyOSG import osgUtil from PyOSG import osgGA from PyOSG import osgProducer import OpenGL from terrain import makeTerrain from trees import makeTrees from tank import makeTank from sky import makeSky from base import makeBase #from clouds import makeClouds from GliderManipulator import GliderManipulator #extern osg.Node *makeWindsocks( void ) #extern osg.Node *makeGliders( void ) #extern osg.Node *makeGlider( void ) keep = [] class MoveEarthySkyWithEyePointTransform(osg.Transform_base): # Important to have the __init__ function, otherwise this is a virtual # class. def __init__(self): osg.Transform_base.__init__(self) # Get the transformation matrix which moves from local coords to world coords. def computeLocalToWorldMatrix(self, matrix, nv): cv = osgUtil.asCullVisitor(nv) # try to cast the nodevisitor to cull visitor, returns None if no match if cv : eyePointLocal = cv.getEyeLocal() matrix.preMult(osg.Matrix.translate(eyePointLocal._x,eyePointLocal._y,0.0)) return 1 # Get the transformation matrix which moves from world coords to local coords. def computeWorldToLocalMatrix(self, matrix, nv): cv = osgUtil.asCullVisitor(nv) if cv: eyePointLocal = cv.getEyeLocal() matrix.postMult(osg.Matrix.translate(-eyePointLocal._x,-eyePointLocal._y,0.0)) return 1 def createModel(): # no database loaded so automatically create Ed Levin Park.. group = osg.Group() # the base and sky subgraphs go to set the earth sky of the # model and clear the color and depth buffer for us, by using # osg.Depth, and setting their bin numbers to less than 0, # to force them to draw before the rest of the scene. clearNode = osg.ClearNode() clearNode.setRequiresClear(False) # we've got base and sky to do it. # use a transform to make the sky and base around with the eye point. transform = MoveEarthySkyWithEyePointTransform() # transform's value isn't knowm until in the cull traversal so its bounding # volume is can't be determined, therefore culling will be invalid, # so switch it off, this cause all our paresnts to switch culling # off as well. But don't worry culling will be back on once underneath # this node or any other branch above this transform. transform.setCullingActive(0) # add the sky and base layer. transform.addChild(makeSky()) # bin number -2 so drawn first. transform.addChild(makeBase()) # bin number -1 so draw second. # add the transform to the earth sky. clearNode.addChild(transform) # add to earth sky to the scene. group.addChild(clearNode) # the rest of the scene drawn after the base and sky above. group.addChild(makeTrees()); # will drop into a transparent, depth sorted bin (1) group.addChild(makeTerrain()); # will drop into default bin - state sorted 0 group.addChild(makeTank()); # will drop into default bin - state sorted 0 # add the following in the future... # makeGliders # makeClouds return group 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 standard OpenSceneGraph example which loads and visualises 3d models.") arguments.getApplicationUsage().setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...") arguments.getApplicationUsage().addCommandLineOption("-h or --help","Display this information") # construct the viewer. viewer = osgProducer.Viewer(arguments) # set up the value with sensible default event handlers. viewer.setUpViewer(osgProducer.Viewer.STANDARD_SETTINGS) pos = viewer.addCameraManipulator(GliderManipulator()) viewer.selectCameraManipulator(pos) # get details on keyboard and mouse bindings used by the viewer. viewer.getUsage(arguments.getApplicationUsage()) # if user request help write it out to cout. if arguments.read("-h") or arguments.read("--help"): arguments.getApplicationUsage().write() return 1 # any option left unread are converted into errors to write out later. arguments.reportRemainingOptionsAsUnrecognized() # report any errors if they have occured when parsing the program aguments. if arguments.errors(): arguments.writeErrorMessages() return 1 # load the nodes from the commandline arguments. rootnode = osgDB.readNodeFiles(arguments) if not rootnode: rootnode = createModel() viewer.setSceneData(rootnode) # create the windows and run the threads. viewer.realize() while not viewer.done(): # wait for all cull and draw threads to complete. viewer.sync() # update the scene by traversing it with the the update visitor which will # call all node update callbacks and animations. viewer.update() # fire off the cull and draw traversals of the scene. viewer.frame() # wait for all cull and draw threads to complete before exit. viewer.sync() return 0 if __name__ == "__main__": main(sys.argv)