#!/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. # from PyOSG import Producer from PyOSG import osg from PyOSG import osgGA from PyOSG import osgDB from PyOSG import osgUtil from PyOSG import osgProducer from PyOSG import osgText import OpenGL import sys import math # class to handle events with a pick class PickHandler( osgGA.GUIEventHandler): def __init__(self, viewer, text): osgGA.GUIEventHandler.__init__(self) self._viewer = viewer self._updateText = text # XXX Create a circular reference to ourself, to prevent the destruction # of the PickHandler when it goes out of scope. self._self = self def setLabel(self, name): if self._updateText: self._updateText.setText(name) def handle(self, ea, aa): if ea.getEventType() == osgGA.GUIEventAdapter.FRAME: self.pick(ea) return False return False def pick(self, ea): hlist = osgUtil.IntersectVisitor.HitList() gdlist = "" if self._viewer.computeIntersections(ea.getX(),ea.getY(),hlist): for hitr in hlist: if hitr.geode() and len(hitr.geode().getName()): gdlist += 'Object "' + hitr.geode().getName() + '"\n' elif hitr._drawable(): gdlist += 'Object "' + hitr.drawable().className() + '"\n' gdlist += " local coords vertex(" + str(hitr.getLocalIntersectPoint()) + ") normal(" + str(hitr.getLocalIntersectNormal()) + ")\n" gdlist += " world coords vertex(" + str(hitr.getWorldIntersectPoint()) + ") normal(" + str(hitr.getWorldIntersectNormal()) + ")\n" vil = hitr.vecIndexList() for i in range(vil.size()): gdlist += " vertex indices [" + str(i) + "] = " + str(vil[i]) + "\n" self.setLabel(gdlist) def createHUD(updateText): # create the hud. derived from osgHud.cpp # adds a set of quads, each in a separate Geode - which can be picked individually # eg to be used as a menuing/help system! # Can pick texts too! modelview_abs = osg.MatrixTransform() modelview_abs.setReferenceFrame(osg.Transform.ABSOLUTE_RF) modelview_abs.setMatrix(osg.Matrix.identity()) projection = osg.Projection() projection.setMatrix(osg.Matrix.ortho2D(0,1280,0,1024)) projection.addChild(modelview_abs) timesFont = "fonts/times.ttf" # turn lighting off for the text and disable depth test to ensure its always ontop. position = osg.Vec3(150.0,800.0,0.0) delta = osg.Vec3(0.0,-60.0,0.0) if 1: geode = osg.Geode() stateset = geode.getOrCreateStateSet() stateset.setMode(OpenGL.GL.GL_LIGHTING,osg.StateAttribute.OFF) stateset.setMode(OpenGL.GL.GL_DEPTH_TEST,osg.StateAttribute.OFF) geode.setName("simple") modelview_abs.addChild(geode) text = osgText.Text() geode.addDrawable( text ) text.setFont(timesFont) text.setText("Picking in Head Up Displays is simple !=]") text.setPosition(position) position += delta for i in range(5): dy = osg.Vec3(0.0,-30.0,0.0) dx = osg.Vec3(120.0,0.0,0.0) geode = osg.Geode() stateset = geode.getOrCreateStateSet() opts=["One", "Two", "Three", "January", "Feb", "2003"] quad=osg.Geometry() stateset.setMode(OpenGL.GL.GL_LIGHTING,osg.StateAttribute.OFF) stateset.setMode(OpenGL.GL.GL_DEPTH_TEST,osg.StateAttribute.OFF) name="subOption" name += " " name += opts[i] geode.setName(name) vertices = osg.Vec3Array(4); # 1 quad colors = osg.Vec4Array() colors = osg.Vec4Array() colors.push_back(osg.Vec4(0.8-0.1*i,0.1*i,0.2*i, 1.0)) quad.setColorArray(colors) quad.setColorBinding(osg.Geometry.BIND_PER_PRIMITIVE) vertices[0]=position vertices[1]=position+dx vertices[2]=position+dx+dy vertices[3]=position+dy quad.setVertexArray(vertices) quad.addPrimitiveSet(osg.DrawArrays(osg.PrimitiveSet.QUADS,0,4)) geode.addDrawable(quad) modelview_abs.addChild(geode) position += delta if 1: # this displays what has been selected geode = osg.Geode() stateset = geode.getOrCreateStateSet() stateset.setMode(OpenGL.GL.GL_LIGHTING,osg.StateAttribute.OFF) stateset.setMode(OpenGL.GL.GL_DEPTH_TEST,osg.StateAttribute.OFF) geode.setName("The text label") geode.addDrawable( updateText ) modelview_abs.addChild(geode) updateText.setCharacterSize(20.0) updateText.setFont(timesFont) updateText.setColor(osg.Vec4(1.0,1.0,0.0,1.0)) updateText.setText("") updateText.setPosition(position) position += delta return projection 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().setDescription(arguments.getApplicationName()+" is the example which demonstrates how to do Head Up Displays.") 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() 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(std.cout) return 1 # read the scene from the list of file specified commandline args. scene = osgDB.readNodeFiles(arguments) group = scene.asGroup() if group == None: group = osg.Group() group.addChild(scene) updateText = osgText.Text() # add the HUD subgraph. group.addChild(createHUD(updateText)) # add the handler for doing the picking # XXX Solved the following code by creating a circular reference in # the PickHandler __init__ function #-- ph = PickHandler(viewer, updateText) #-- viewer.eventHandler().push_front(ph) viewer.getEventHandlerList().push_front(PickHandler(viewer,updateText)) # set the scene to render viewer.setSceneData(group) # 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 main(sys.argv)