Example #1
0
        public Mesh GetMesh(string name, string src)
        {
            for( var i = 0; i < meshes.Count; i++ )
            {
                if( meshes[i].name == name )
                    return meshes[i];
            }

            Mesh mesh = new Mesh(this, name, src);
            meshes.Add(mesh);
            return mesh;
        }
        public RenderObject(Scene scene, string name, string src)
            : base(scene, name, src)
        {
            textures = new List<Texture>();

            pos = new Vector3(0, 0, 0);
            rot = new Quaternion(0, 0, 0, 1.0f);
            scale = new Vector3(1.0f, 1.0f, 1.0f);
            worldMatrix = new Matrix4();

            try
            {
                XmlDocument roXML = new XmlDocument();
                roXML.Load(src);

                foreach( XmlAttribute attrib in roXML.DocumentElement.Attributes )
                {
                    if( attrib.Name == "pos")
                    {
                        string[] values = attrib.Value.Split(',');
                        pos = new Vector3(Convert.ToSingle(values[0]), Convert.ToSingle(values[1]), Convert.ToSingle(values[2]));
                    }
                    else if( attrib.Name == "rot")
                    {
                        string[] values = attrib.Value.Split(',');
                        rot = new Quaternion(Convert.ToSingle(values[0]), Convert.ToSingle(values[1]), Convert.ToSingle(values[2]), Convert.ToSingle(values[3]));
                    }
                    else if( attrib.Name == "scale" )
                    {
                        string[] values = attrib.Value.Split(',');
                        scale = new Vector3(Convert.ToSingle(values[0]), Convert.ToSingle(values[1]), Convert.ToSingle(values[2]));
                    }
                    else if( attrib.Name == "update" )
                    {
                        updateCallback = Program.TheForm.FindUpdateFunction(attrib.Value);
                    }
                }

                foreach( XmlNode child in roXML.DocumentElement.ChildNodes )
                {
                    if( child.NodeType == XmlNodeType.Element )
                    {
                        string nodeName = child.Attributes.GetNamedItem("name").Value;
                        string nodeSrc = child.Attributes.GetNamedItem("src").Value;
                        if (child.Name == "mesh")
                        {
                            mesh = scene.GetMesh(nodeName, nodeSrc);
                        }
                        else if (child.Name == "shader")
                        {
                            shader = scene.GetShader(nodeName, nodeSrc);
                        }
                        else if (child.Name == "texture")
                        {
                            Texture tex = null;
                            if( nodeSrc == "frameBuffer" )
                            {
                                var depthTexture = false;
                                XmlAttribute dtAttr = (XmlAttribute)child.Attributes.GetNamedItem("depthTexture");
                                if( dtAttr != null )
                                    depthTexture = (dtAttr.Value == "true");

                                FrameBuffer fb = scene.GetFrameBuffer(nodeName, null);
                                if( fb != null )
                                {
                                    if( depthTexture )
                                        tex = fb.depthTexture;
                                    else
                                        tex = fb.colorTexture;
                                }
                            }
                            else
                            {
                                // Normal texture
                                tex = scene.GetTexture(nodeName, nodeSrc);
                            }

                            if( tex != null )
                            {
                                var texIndex = 0;
                                XmlAttribute index = (XmlAttribute)child.Attributes.GetNamedItem("texIndex");
                                if( index != null)
                                    texIndex = Convert.ToInt32(index.Value);

                                while (textures.Count <= texIndex)
                                    textures.Add(null);
                                textures[texIndex] = tex;
                            }
                        }
                        else if (child.Name == "shadowCamera")
                        {
                            shadowCamera = scene.GetCamera(nodeName, nodeSrc);
                        }
                    }
                }
            }
            catch(Exception e)
            {
                System.Windows.Forms.MessageBox.Show("Failed to load render object: " + src + "\r\n" + e.Message);
            }

            UpdateWorldMatrix();
        }