from OpenGL.GLUT import * from PyOSG import osg from PyOSG import osgUtil from PyOSG import osgDB from math import sin, cos import sys sceneView = osgUtil.SceneView() start_tick = 0 frameNum = 0 lastx = 0 lasty = 0 centerPos = osg.Vec3() viewRot = 0 viewElev = 0 viewRadius = 0 def display(): global frameNum, viewRadius, viewElev, viewRot, centerPos, lastx, lasty # set up the frame stamp for current frame to record the current time and frame number so that animtion code can advance correctly frameStamp = osg.FrameStamp() frameStamp.setReferenceTime(osg.Timer.instance().delta_s(start_tick,osg.Timer.instance().tick())) frameStamp.setFrameNumber( frameNum ) frameNum += 1 # pass frame stamp to the SceneView so that the update, cull and draw traversals all use the same FrameStamp sceneView.setFrameStamp( frameStamp ) # set the view viewPos = osg.Vec3(\ viewRadius * cos(viewElev) * sin(viewRot),\ viewRadius * cos(viewElev) * cos(viewRot),\ viewRadius * sin(viewElev)) sceneView.setViewMatrixAsLookAt( centerPos-viewPos, centerPos, osg.Vec3(0.0,0.0,1.0) ) # do the update traversal the scene graph - such as updating animations sceneView.update() # do the cull traversal, collect all objects in the view frustum into a sorted set of rendering bins sceneView.cull() # draw the rendering bins. sceneView.draw() # Swap Buffers glutSwapBuffers() glutPostRedisplay() def reshape( w, h ): # update the viewport dimensions, in case the window has been resized. print "Reshaping viewport: ", w, h sceneView.setViewport( 0, 0, w, h ) def mousedown( button, state, x, y ): global lastx, lasty lastx = x lasty = y def mousemove(x, y ): global viewRot, viewElev, lastx, lasty viewRot += (x - lastx) / 100.0 viewElev -= (y - lasty) / 100.0 if( viewElev < -1.5 ): viewElev = -1.5 if( viewElev > 1.5 ): viewElev = 1.5 lastx = x lasty = y def keyboard(key, x, y ): global viewRot, viewElev if key == chr(27): sys.exit(0) elif key == ' ': viewRot = 0.0 viewElev = 0.0 def menu(selection ): print "menu selection = ", selection def main( argv ): global frameNum, viewRadius, viewElev, viewRot, centerPos, lastx, lasty, start_tick, sceneView glutInit(argv) if len(argv)<2: print argv[0], ": requires filename argument.\n" return 1 # load the scene. loadedModel = osgDB.readNodeFile(argv[1]) if not loadedModel: print argv[0], ": No data loaded.\n" return 1 glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA ) glutInitWindowPosition( 100, 100 ) glutInitWindowSize( 800, 600 ) glutCreateWindow( argv[0] ) glutDisplayFunc( display ) glutReshapeFunc( reshape ) glutMouseFunc( mousedown ) glutMotionFunc( mousemove ) glutKeyboardFunc( keyboard ) glutCreateMenu( menu ) glutAddMenuEntry( "item 0", 0 ) glutAddMenuEntry( "item 1", 1 ) glutAttachMenu( GLUT_RIGHT_BUTTON ) # create the view of the scene. sceneView.setDefaults() sceneView.setSceneData(loadedModel) # initialize the view to look at the center of the scene graph bs = loadedModel.getBound() centerPos = bs.center() viewRot = 0.0 viewElev = 0.0 viewRadius = 2.0 * bs.radius() # record the timer tick at the start of rendering. start_tick = osg.Timer.instance().tick() frameNum = 0 glutMainLoop() return 0 if __name__ == "__main__": main(sys.argv)