OSM Viewer in Python with PyOgre

mrkissinger

03-06-2006 23:55:58

Thanks for dermont's help for PyOgre 1.2 installation.

I am developing a simple OSM viewer in Python with PyOgre.
It is a part of a commercial project, so I just post the codes referred to OSM format.

It is not full functional, but it just works, and can deal with most OSM scenes. So far, my team is using it to show our own scenes with no problem.

I hope it could help some ones. And you and me could update the codes to be a full functional OSM viewer.


import xml.parsers.expat
...

OgreLight={
'spot': ogre.Light.LT_SPOTLIGHT ,
'omni': ogre.Light.LT_POINT ,
'directional': ogre.Light.LT_DIRECTIONAL
}

class EnvMapApplication(sf.Application):
node=None
nodeType=None
entity=None

# node types
NODETYPE_ENTITY =0
NODETYPE_LIGHT =1
NODETYPE_CAMERA =2

# These two functions are for OSM format.

def start_element(self,name, attrs):#{{{
print 'Start element:', name, attrs
if ('entity'==name):
self.node=self.sceneManager.rootSceneNode.createChildSceneNode()
self.nodeType=self.NODETYPE_ENTITY
#input (attrs['name']+"::"+ attrs['meshFile'])
name=str(attrs['name'])
mesh=str(attrs['filename'])
self.entity = self.sceneManager.createEntity(name, mesh)
elif ('camera'==name):
# Just for a single camera

name=str(attrs['name'])
self.node=self.camera
self.nodeType=self.NODETYPE_CAMERA
#self.node=self.sceneManager.createCamera(name)
#self.newCamera=self.node

elif ('light'==name):
name=str(attrs['name'])
type=str(attrs['type'])
self.node = self.sceneManager.createLight(name)
self.nodeType=self.NODETYPE_LIGHT
#light.type = ogre.Light.LT_POINT
self.node.type=OgreLight[type]
self.node.position = (0, 150, 250)
elif ('position'==name):
x=float(attrs['x'])
y=float(attrs['y'])
z=float(attrs['z'])

self.node.position= (x, y, z)
elif ('rotation'==name):
x=float(attrs['x'])
y=float(attrs['y'])
z=float(attrs['z'])
w=float(attrs['w'])

if self.nodeType==self.NODETYPE_ENTITY:
quaternion=Quaternion(w,x,y,z)
self.node.rotate(quaternion)
elif self.nodeType==self.NODETYPE_LIGHT:
self.node.direction=(w,x,y)
elif self.nodeType==self.NODETYPE_CAMERA:
self.node.lookAt(w,x,y)
elif ('specular'==name):
r=float(attrs['r'])
g=float(attrs['g'])
b=float(attrs['b'])
self.node.specularColour = (r, g, b)
#}}}

def end_element(self,name):#{{{
#print 'End element:', name
if ('entity'==name):
if (None != self.node):
self.node.attachObject(self.entity)
self.node=None
elif ('light'==name or 'camera'==name):
self.node=None
#self.light=None

#}}}

def _createScene(self):#{{{
sceneManager = self.sceneManager
self.sceneManager.ambientLight = 1,1,1

p = xml.parsers.expat.ParserCreate()
p.StartElementHandler = self.start_element
p.EndElementHandler = self.end_element

f=open('scene.osm','r')
p.ParseFile(f)

....