My DotSceneLoader code
1,595
3
Advertisement
Today I am sharing a bit of code. My reasons for doing this are to see if there are any glaring issues in the code that you all might find like "Hey! Don't do that stupid!" and also to help other people who might be looking for resources on a particular topic.
The following code was based on a dotscene loader for Python Ogre I found while browsing on the internet however I coded it differently and used what I hope are more descriptive names. If there are any issues please feel free to let me know because I am not perfect and I am not a guru at Python.
I hope that you find the following code useful!
Thank you.
The following code was based on a dotscene loader for Python Ogre I found while browsing on the internet however I coded it differently and used what I hope are more descriptive names. If there are any issues please feel free to let me know because I am not perfect and I am not a guru at Python.
import ogre.renderer.OGRE as ogreimport ogre.io.OIS as OISimport ogre.gui.CEGUI as CEGUIimport ogre.addons.caelum as caelumfrom xml.dom import minidom, Nodeclass DotScene(object): def __init__(self, scene_mgr): self.scene_mgr = scene_mgr self.nodes = {} def __setitem__(self, key, value): self.nodes[key] = value def __getitem__(self, key): return self.nodes[key] class DotSceneLoader(object): def __init__(self, sceneMgr): self.sceneMgr = sceneMgr def load_scene_from_file(self, file_name): rgm = ogre.ResourceGroupManager.getSingletonPtr() resource_group = "General" scene_file_list = rgm.findResourceNames(resource_group,"*.scene") scene_data = None for scene_file in scene_file_list: if scene_file == file_name: scene_data = rgm.openResource(scene_file, resource_group, searchGroupsIfNotFound=False) break assert isinstance(scene_data,ogre.DataStream) scene_string = scene_data.getAsString() document = minidom.parseString(scene_string) doc_elem = document.documentElement nodes = self.find_nodes(doc_elem, "nodes") return self.create_scene(nodes[0].childNodes) def find_nodes(self, root, name): node_list = minidom.NodeList() if root.hasChildNodes: nodes = root.childNodes for node in nodes: if node.nodeType == Node.ELEMENT_NODE and node.nodeName == name: node_list.append(node) return node_list; def parse_coord(self, attribList): return ogre.Vector3(float(attribList["x"].nodeValue), float(attribList["y"].nodeValue),float(attribList["z"].nodeValue)) def parse_quat(self, attribList): return ogre.Quaternion(float(attribList["qw"].nodeValue), float(attribList["qx"].nodeValue), float(attribList["qy"].nodeValue),float(attribList["qz"].nodeValue)) def parse_colour(self, attribList): return ogre.ColourValue(float(attribList["r"].nodeValue),float(attribList["g"].nodeValue), float(attribList["b"].nodeValue),float(attribList["a"].nodeValue)) def get_node_attrib_list(self, root, name): return self.find_nodes(root, name)[0].attributes def create_scene(self, root): dotscene = DotScene(self.sceneMgr) for node in root: if node.nodeType == Node.ELEMENT_NODE and node.nodeName == "node": #Let's create a scene node new_node = self.sceneMgr.getRootSceneNode().createChildSceneNode() #scale the node scale = self.get_node_attrib_list(node, "scale") new_node.setScale(self.parse_coord(scale)) #position the node pos = self.get_node_attrib_list(node,"position") new_node.setPosition(self.parse_coord(pos)) #rotate the node rot = self.get_node_attrib_list(node,"rotation") new_node.setOrientation(self.parse_quat(rot)) entity_nodes = self.find_nodes(node, "entity") #we have entities if len(entity_nodes) > 0: entity_attrib_list = entity_nodes[0].attributes name = str(entity_attrib_list["name"].nodeValue) mesh = str(entity_attrib_list["meshFile"].nodeValue) entity = self.sceneMgr.createEntity(name, mesh) dotscene[name] = entity new_node.attachObject(entity) if node.nodeType == Node.ELEMENT_NODE and node.nodeName == "light": light_attrib_list = node.attributes name = str(light_attrib_list["name"].nodeValue) light_type = str(light_attrib_list["type"].nodeValue) shadows = bool(light_attrib_list["castShadows"].nodeValue) #create a new scene node new_node = self.sceneMgr.getRootSceneNode().createChildSceneNode() #get the position of the node pos = self.get_node_attrib_list(node, "position") new_node.setPosition(self.parse_coord(pos)) light = self.sceneMgr.createLight(name) light.setPosition(self.parse_coord(pos)) #set type if light_type == "point": light.setType(ogre.Light.LightTypes.LT_POINT) #get the direction of the light direction = self.get_node_attrib_list(node, "normal") light.setDirection(self.parse_coord(direction)) #get the diffuse colour diffuse = self.get_node_attrib_list(node, "colourDiffuse") light.setDiffuseColour(self.parse_colour(diffuse)) #get the specular colour specular = self.get_node_attrib_list(node, "colourSpecular") light.setSpecularColour(self.parse_colour(specular)) range_nodes = self.find_nodes(node, "lightRange") if len(range_nodes) > 0 and light.getType() == ogre.Light.LightTypes.LT_SPOTLIGHT: range_attrib_list = rangeNodes[0].attributes inner = float(rangeAttribLst["inner"].nodeValue) outer = float(rangeAttribLst["outer"].nodeValue) falloff = float(rangeAttribLst["falloff"].nodeValue) light.setSpotlightRange(inner, outer, falloff) atten_nodes = self.find_nodes(node, "lightAttenuation") if len(atten_nodes) > 0 : atten_attrib_list = atten_nodes[0].attributes range = float(atten_attrib_list["range"].nodeValue) constant = float(atten_attrib_list["constant"].nodeValue) linear = float(atten_attrib_list["linear"].nodeValue) quadratic = float(atten_attrib_list["quadratic"].nodeValue) light.setAttenuation(range, constant, linear, quadratic) if node.nodeType == Node.ELEMENT_NODE and node.nodeName == "camera": camera_attrib_list = node.attributes name = str(camera_attrib_list["name"].nodeValue) fov = float(camera_attrib_list["fov"].nodeValue) projection_type = str(camera_attrib_list["projectionType"]) #create a new scene node new_node = self.sceneMgr.getRootSceneNode().createChildSceneNode() #rotate the node rot = self.get_node_attrib_list(node, "rotation") new_node.setOrientation(self.parse_quat(rot)) #position the node pos = self.get_node_attrib_list(node, "position") new_node.setPosition(self.parse_coord(pos)) clipping_nodes = self.find_nodes(node, "clipping") near = far = 0 #we have a clipping node if len(clipping_nodes) > 0: clip_attrib_list = clipping_nodes[0].attributes near = float(clip_attrib_list["near"].nodeValue) far = float(clip_attrib_list["far"].nodeValue) #create a camera camera = self.sceneMgr.createCamera(name) camera.setNearClipDistance(near) camera.setFarClipDistance(far) camera.setFOVy(fov) camera.setProjectionType(ogre.PT_PERSPECTIVE) dotscene[name] = camera new_node.attachObject(camera) return dotsceneI hope that you find the following code useful!
Thank you.
Advertisement
Advertisement
Advertisement
Discussion