#!/usr/bin/env python import sys import os import math from PyOSG import Producer from PyOSG import osg from PyOSG import osgGA from PyOSG import osgUtil from PyOSG import osgProducer from PyOSG import osgDB from OpenGL.GL import * import random as rand import time def random(min, max): return min + (max-min)*rand.random() class DrawCallback( osg.Drawable.DrawCallback): def __init__(self): osg.Drawable.DrawCallback.__init__(self) self._firstTime = True self._currentModelViewMatrix = osg.Matrix() self._inverseModelViewMatrix = osg.Matrix() self._previousModelViewMatrix = osg.Matrix() def drawImplementation(self, state, drawable): if not self._firstTime: t1 = time.time() self._previousModelViewMatrix = self._currentModelViewMatrix self._currentModelViewMatrix = state.getModelViewMatrix() self._inverseModelViewMatrix.invert(self._currentModelViewMatrix) t2 = time.time() print "delta : ", t2 - t1 T = osg.Matrix(self._previousModelViewMatrix*self._inverseModelViewMatrix) geometry = drawable.asGeometry() vertices = geometry.getVertexArray() for i in range(0, vertices.size(), 2): v = vertices[i] * T vertices[i+1] = v else: self._currentModelViewMatrix = state.getModelViewMatrix() self._firstTime = False drawable.drawImplementation(state) def createScene(noStars): geometry = osg.Geometry() # set up vertices vertices = osg.Vec3Array(noStars*2) geometry.setVertexArray(vertices) min = -1.0 max = 1.0 j = 0 i = 0 for i in range(noStars): vertices[j].set(random(min,max),random(min,max),random(min,max)) vertices[j+1] = vertices[j]+osg.Vec3(0.0,0.0,0.001) j += 2 # set up colours colours = osg.Vec4Array(1) geometry.setColorArray(colours) geometry.setColorBinding(osg.Geometry.BIND_OVERALL) colours[0].set(1.0,1.0,1.0,1.0) # set up the primitive set to draw lines geometry.addPrimitiveSet(osg.DrawArrays(GL_LINES,0,noStars*2)) # set up the points for the stars. points = osg.DrawElementsUShort(GL_POINTS,noStars) geometry.addPrimitiveSet(points) for i in range(noStars): try: points[i] = i*2 except: print i geometry.setUseDisplayList(False) geometry.setDrawCallback(DrawCallback()) geode = osg.Geode() geode.addDrawable(geometry) group = osg.Group() group.addChild(geode) 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().setApplicationName(arguments.getApplicationName()) 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) # 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(sys.stdout) return 1 # report any errors if they have occured when parsing the program aguments. if arguments.errors(): arguments.writeErrorMessages(sys.stdout) return 1 # read the scene from the list of file specified commandline args. loadedModel = createScene(5000) # if no model has been successfully loaded report failure. if not loadedModel: print arguments.getApplicationName()," No data loaded" 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(sys.stdout) # set the scene to render viewer.setSceneData(loadedModel) # 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)