#!/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++-*- 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 from PyOSG import osg from PyOSG import osgGLUT from PyOSG import osgDB from PyOSG import osgGA import OpenGL.GLUT class MyTransformCallback(osg.NodeCallback): def __init__(self, node, angularVelocity): self._nodeToOperateOn = node self._angular_velocity = angularVelocity self._previousTraversalNumber = -1 self._timer = osg.Timer() self._orig_t = self._timer.tick() def apply(self, node, nv): if (nv): if self._nodeToOperateOn: # ensure that we do not operate on this node more than # once during this traversal. This is an issue since node # can be shared between multiple parents. if nv.getTraversalNumber() != self._previousTraversalNumber: new_t = self._timer.tick() delta_angle = self._angular_velocity * self._timer.delta_s(self._orig_t, new_t) matrix = osg.Matrix() matrix.makeRotate(delta_angle, 1.0, 1.0, 1.0) matrix *= osg.Matrix.translate(1.0, 0.0, 0.0) matrix *= osg.Matrix.rotate(delta_angle, 0.0, 0.0, 1.0) self._nodeToOperateOn.setMatrix(matrix) self._previousTraversalNumber = nv.getTraversalNumber() pass def createGeometryCube(): geode = osg.Geode() # ------------------------------------------- # Set up a new Geometry which will be our cube # ------------------------------------------- cube = osg.Geometry() # set up the primitives cube.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.POLYGON,0,4)) cube.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.POLYGON,4,4)) cube.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.POLYGON,8,4)) cube.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.POLYGON,12,4)) cube.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.POLYGON,16,4)) cube.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.POLYGON,20,4)) # set up coords. coords = osg.Vec3Array() coords.resize(24) coords[0].set( -1.0000, 1.0000, -1.000 ); coords[1].set( 1.0000, 1.0000, -1.0000 ); coords[2].set( 1.0000, -1.0000, -1.0000 ); coords[3].set( -1.0000, -1.0000, -1.000 ); coords[4].set( 1.0000, 1.0000, -1.0000 ); coords[5].set( 1.0000, 1.0000, 1.0000 ); coords[6].set( 1.0000, -1.0000, 1.0000 ); coords[7].set( 1.0000, -1.0000, -1.0000 ); coords[8].set( 1.0000, 1.0000, 1.0000 ); coords[9].set( -1.0000, 1.0000, 1.000 ); coords[10].set( -1.0000, -1.0000, 1.000 ); coords[11].set( 1.0000, -1.0000, 1.0000 ); coords[12].set( -1.0000, 1.0000, 1.000 ); coords[13].set( -1.0000, 1.0000, -1.000 ); coords[14].set( -1.0000, -1.0000, -1.000 ); coords[15].set( -1.0000, -1.0000, 1.000 ); coords[16].set( -1.0000, 1.0000, 1.000 ); coords[17].set( 1.0000, 1.0000, 1.0000 ); coords[18].set( 1.0000, 1.0000, -1.0000 ); coords[19].set( -1.0000, 1.0000, -1.000 ); coords[20].set( -1.0000, -1.0000, 1.000 ); coords[21].set( -1.0000, -1.0000, -1.000 ); coords[22].set( 1.0000, -1.0000, -1.0000 ); coords[23].set( 1.0000, -1.0000, 1.0000 ); cube.setVertexArray(coords) # set up the normals. cubeNormals = osg.Vec3Array() cubeNormals.resize(6) cubeNormals[0].set(0.0,0.0,-1.0) cubeNormals[1].set(1.0,0.0,0.0) cubeNormals[2].set(0.0,0.0,1.0) cubeNormals[3].set(-1.0,0.0,0.0) cubeNormals[4].set(0.0,1.0,0.0) cubeNormals[5].set(0.0,-1.0,0.0) cube.setNormalArray( cubeNormals ) cube.setNormalBinding(osg.Geometry.BIND_PER_PRIMITIVE ) # --------------------------------------- # Set up a StateSet to make the cube red # --------------------------------------- cubeState = osg.StateSet() redMaterial = osg.Material() red = osg.Vec4(1.0, 0.0, 0.0, 1.0) redMaterial.setDiffuse(osg.Material.FRONT_AND_BACK, red) cubeState.setAttribute(redMaterial) cube.setStateSet(cubeState) geode.addDrawable(cube) return geode def main(): OpenGL.GLUT.glutInit(sys.argv) # create the commandline args. commandLine = [] for x in range(1, len(sys.argv)): commandLine.append(sys.argv[x]) viewer = osgGLUT.Viewer() viewer.setWindowTitle(sys.argv[0]) # configure the viewer from the commandline arguments, and eat any # parameters that have been matched. viewer.readCommandLine(commandLine) myTransform = osg.MatrixTransform() myTransform.addChild(createGeometryCube()) # move node in a circle at 90 degrees a sec. myTransform.setUpdateCallback(MyTransformCallback(myTransform, osg.inDegrees(90.0))) # add model to viewer. viewer.addViewport(myTransform) # register trackball maniupulators. viewer.registerCameraManipulator(osgGA.TrackballManipulator()) viewer.open() viewer.run() if __name__ == "__main__": main()