My DotSceneLoader code

Published January 28, 2010
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.

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 dotscene


I hope that you find the following code useful!

Thank you.
Previous Entry Just killing time
Next Entry Previously on...
0 likes 3 comments

Comments

zarfius
I just discovered your journal and read all the posts. I'd like to say that I'll be following it closely and you've inspired me to create my own journal just tonight :)

It sparked my interest because I'm also working on a project using many of the same API's and programs your using and I've come across many of the same problems. I figure we might be able to help each other out with our experiences.

The project I'm working on is a first person puzzle game. The API's and programs I use include Ogre, PhysX, OpenAL, DeleD, AC3D and I'm programming in C++ and C#.

Feel free to ask me any questions.
January 31, 2010 04:23 AM
shadowisadog
Quote:Original post by zarfius
I just discovered your journal and read all the posts. I'd like to say that I'll be following it closely and you've inspired me to create my own journal just tonight :)

It sparked my interest because I'm also working on a project using many of the same API's and programs your using and I've come across many of the same problems. I figure we might be able to help each other out with our experiences.

The project I'm working on is a first person puzzle game. The API's and programs I use include Ogre, PhysX, OpenAL, DeleD, AC3D and I'm programming in C++ and C#.

Feel free to ask me any questions.


Thank you Zarfius! I am glad that my journal has inspired you to create one of your own :) . I have read through your entries and your project is looking really good so far! I am sort of curious as to why you are using PhysX over say ODE or Bullet? Any particular reason to use PhysX over the competition or is it just a personal choice? Also are you planning to create an abstraction layer for your physics engine or are you going to go direct?

I am planning to write some plugins for Ogitor (eventually) to enable me to setup my physics right in Ogitor :) .
February 01, 2010 06:53 PM
zarfius
Quote:Original post by shadowisadog
Thank you Zarfius! I am glad that my journal has inspired you to create one of your own :) . I have read through your entries and your project is looking really good so far! I am sort of curious as to why you are using PhysX over say ODE or Bullet? Any particular reason to use PhysX over the competition or is it just a personal choice? Also are you planning to create an abstraction layer for your physics engine or are you going to go direct?

I am planning to write some plugins for Ogitor (eventually) to enable me to setup my physics right in Ogitor :) .


I really liked the look of Bullet until I tried to create a character controller, then I ran into trouble. I'm not saying it can't be done but I had a lot of trouble and the PhysX samples comes with one that works pretty well. If I was able to overcome this issue in the future I would happily use it in another project (or perhaps convert my current project if it was worth it)

I don't use a full physics abstraction layer but I have rolled one of my own to some degree, I find you have to be careful when adding more layers to your code because each one comes with it's own drawbacks. I plan on explaining my overall design in the journal sometime.

I did consider using ODE it along with a number of other physics engines. I can't remember the exact reasons why I chose not to use it but I did have them all layed out at one point with pro's and con's of each.

Ogitor has a lot of potential. I'd love to see it get to a point where it would be really useful. Unfortunately, the last time I checked it was unstable and wasn't much more than a scene editor.
February 03, 2010 01:37 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement

Latest Entries

Finished!

1609 views

Progress

1674 views

Resolve

1693 views

GUI

1852 views

Making a menu

2276 views

Teddystein!

1770 views

Doodles

1575 views

Tool overview

2153 views

Welcome!

1430 views
Advertisement