#!/bin/env python # Copyright (C) 2002-2003 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++ 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. # A simple example demonstrating the use of the Producer.Camera # and rendering the scene with osgProducer.SceneHandler from PyOSG import * class KBMCB(Producer.KeyboardMouseCallback): def __init__(self): Producer.KeyboardMouseCallback.__init__(self) self._mx = 0.0 self._my = 0.0 self._mbutton = 0 # Trick to allow passing py reference of _toggleResetTrackball, # since it is modified in _checkToggle self._toggleResetTrackball = [False] self._quit = False def mouseMotion(self, mx, my ): self._mx = mx self._my = my def buttonPress(self, mx, my, button): self._mx = mx self._my = my self._mbutton |= (1<<(button-1)) def buttonRelease(self, mx, my, button ): self._mx = mx self._my = my self._mbutton &= ~(1<<(button-1)) def keyPress(self, key ): if key == ord(' '): self._toggleResetTrackball[0] = True def specialKeyPress(self, key ): if key == Producer.KeyChar_Escape : self._quit = True def mx(self): return self._mx def my(self): return self._my def mbutton(self): return self._mbutton def resetTrackball(self): return self._checkToggle(self._toggleResetTrackball) def quit(self): return self._quit def _checkToggle(self,toggle): if toggle[0]: toggle[0] = False return True return False def main(): # Set up the Producer Camera camera = Producer.Camera() camera.getRenderSurface().setWindowName("Simple example of OSG using Producer.Camera" ) camera.setRenderSurfaceWindowRectangle(0,0,800,600) # Load the database root = osgDB.readNodeFile( "cow.osg" ) # osgProducer glues Producer and Osg together through OsgSceneHandler # which inherits from both Producer.Camera.SceneHandler and # osgUtil.SceneView. sh = osgProducer.OsgSceneHandler() sh.setDefaults() sh.setSceneData(root) # Tell the Producer.Camera about its scene handler camera.setSceneHandler(sh) # Set up the Producer Trackball. Trackball is just one # example of a camera manipulator bs = root.getBound() tb = Producer.Trackball() tb.setDistance( bs.radius() * 3.4) tb.setReference() # Set up a KeyboardMouse handler. # This will run in a background thread after the call to # startThread(). kbm = Producer.KeyboardMouse(camera.getRenderSurface()) # Set up a keyboardmouse callback, which is our own implementation # of a Producer.KeyboardMouseCallback. See kbmcb.h kbmcb = KBMCB() kbm.setCallback(kbmcb ) kbm.startThread() # The keyboard mouse callback will indicate when its time # to quit (press ) while not kbmcb.quit(): # The KeyboardMouseCallback will tell us when to reset the # trackball (press ). if kbmcb.resetTrackball(): tb.reset() # The trackball manipulator takes normalized values for # x and y(ranging from -1 to 1), and a button state with # a bit set for each button pressed tb.input( kbmcb.mx(), kbmcb.my(), kbmcb.mbutton() ) # The Trackball manipulator outputs a matrix. Pass this # to the Producer;:Camera camera.setViewByMatrix(tb.getMatrix()) # Call the frame. camera.frame() if __name__ == "__main__": main()