#!/usr/bin/env python # C++ source file - Open Producer - Copyright (C) 2002 Don Burns # Distributed under the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE (LGPL) # as published by the Free Software Foundation. import sys from PyOSG import Producer from PyOSG import osg from PyOSG import osgGA from PyOSG import osgDB from PyOSG import osgUtil from PyOSG import osgProducer # Simple example of use of Producer.RenderSurface # The myGraphics class is a simple sample of how one would implement # graphics drawing with Producer.RenderSurface class MyKeyboardMouseCallback(Producer.KeyboardMouseCallback): def __init__(self): Producer.KeyboardMouseCallback.__init__(self) self._mx = 0.0 self._my = 0.0 self._mbutton = 0 self._done = False def specialKeyPress(self, key ): if key==Producer.KeyChar_Escape: self.shutdown() def shutdown(self): self._done = True def keyPress(keychar): pass def mouseMotion(self, mx, my): self._mx = mx self._my = my def buttonPress(self, mx, my, mbutton ): self._mx = mx self._my = my self._mbutton |= (1<<(mbutton-1)) def buttonRelease(self, mx, my, mbutton ): self._mx = mx self._my = my self._mbutton &= ~(1<<(mbutton-1)) def done(self): return self._done def mx(self): return self._mx def my(self): return self._my def mbutton(self): return self._mbutton def idle(self): return True 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().setApplicationName(arguments.getApplicationName()) arguments.getApplicationUsage().setDescription(arguments.getApplicationName()+" is the standard example of using osgProducer.CameraGroup.") arguments.getApplicationUsage().setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...") arguments.getApplicationUsage().addCommandLineOption("-h or --help","Display this information") # create the camera group. cg = osgProducer.OsgCameraGroup(arguments) # 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 if arguments.argc()<=1: arguments.getApplicationUsage().write(osg.ApplicationUsage.COMMAND_LINE_OPTION) return 1 # read the scene. loadedModel = osgDB.readNodeFiles(arguments) if not loadedModel: print arguments.getApplicationName() + ": No data loaded" return 1 optimizer = osgUtil.Optimizer() optimizer.optimize(loadedModel) # set up the keyboard and mouse handling. ia = cg.getCameraConfig().getInputArea() if ia: kbm = Producer.KeyboardMouse(ia) else: kbm = Producer.KeyboardMouse(cg.getCamera(0).getRenderSurface()) kbmcb = MyKeyboardMouseCallback() # register the callback with the keyboard mouse manger. kbm.setCallback(kbmcb) kbm.startThread() # set the scene to render cg.setSceneData(loadedModel) bs = loadedModel.getBound() tb = Producer.Trackball() tb.setOrientation( Producer.Trackball.Z_UP ) tb.setDistance(bs.radius()*3.0) tb.translate(-bs.center()._x,-bs.center()._y,-bs.center()._z) # create the windows and run the threads. #cg.realize() cg.realize(Producer.CameraGroup.SingleThreaded) update = osgUtil.UpdateVisitor() while not kbmcb.done(): # syncronize to the when cull and draw threads have completed. cg.sync() # update the scene by traversing it with the the update visitor which will # call all node update callbacks and animations. cg.getSceneData().accept(update) tb.input( kbmcb.mx(), kbmcb.my(), kbmcb.mbutton() ) # update the main producer camera cg.setViewByMatrix(tb.getMatrix()) # fire off the cull and draw traversals of the scene. cg.frame() # syncronize to the when cull and draw threads have completed. cg.sync() return 0 if __name__ == "__main__": # raw_input("press enter") main(sys.argv)