#! /usr/bin/env python # Copyright (C) 2002-2005 Gideon May (gideon@computer.org) # # Permission to copy, use, sell and distribute this software is granted # provided this copyright notice appears in all copies. # Permission to modify the code and to distribute modified code is granted # provided this copyright notice appears in all copies, and a notice # that the code was modified is included with the copyright notice. # # This software is provided "as is" without express or implied warranty, # and with no claim as to its suitability for any purpose. # -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield # # This application is open source and may be redistributed and/or modified # freely and without restriction, both in commericial and non commericial applications, # as long as this copyright notice is maintained. # # This application is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 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)