Beispiel #1
0
        public Scene LoadScene(string fileName)
        {
            _scene = new SimpleScene();
            _scene.Camera = new TargetedCamera();

            _material = new Material();

            _transforms = new Stack<Matrix>();
            _transforms.Push(Matrix.Identity);

            StreamReader reader = new StreamReader(fileName);

            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine().Trim();

                if (string.IsNullOrEmpty(line) || line.StartsWith("#"))
                {
                    continue;
                }

                ProcessLine(line.Split(' ').Where(t => !string.IsNullOrEmpty(t)).ToArray());
            }

            return _scene;
        }
        public void BuildMesh(int[] vertexCounts, int[] poligonIndexes, float[] vertices, float[] normals, float[] texcoord, Material material)
        {
            //Only tiangles supported for now
            if (!vertexCounts.All(c => c == 3))
                throw new Exception("Only tiangles supported for now");

            int poligonCount = vertexCounts.Count();
            Triangles = new List<Triangle>(poligonCount);

            int poligonComponentsCount = texcoord != null ? 3 : 2;

            for (int i = 0; i < poligonCount; i++)
            {
                Triangle triangle = new Triangle();
                triangle.Material = material;

                int polygonOffset = i*vertexCounts[i]*poligonComponentsCount;

                int vertexBOffset = 1*poligonComponentsCount;
                int vertexCOffset = 2*poligonComponentsCount;

                triangle.A = vertices.ToVec3(poligonIndexes[polygonOffset]);
                triangle.B = vertices.ToVec3(poligonIndexes[polygonOffset + vertexBOffset]);
                triangle.C = vertices.ToVec3(poligonIndexes[polygonOffset + vertexCOffset]);

                int normalOffset = polygonOffset + 1;

                triangle.Na = normals.ToVec3(poligonIndexes[normalOffset]);
                triangle.Nb = normals.ToVec3(poligonIndexes[normalOffset + vertexBOffset]);
                triangle.Nc = normals.ToVec3(poligonIndexes[normalOffset + vertexCOffset]);

                if (texcoord != null)
                {
                    int texcoordOffset = polygonOffset + 2;

                    triangle.Ta = texcoord.ToVec2(poligonIndexes[texcoordOffset]);
                    triangle.Tb = texcoord.ToVec2(poligonIndexes[texcoordOffset + vertexBOffset]);
                    triangle.Tc = texcoord.ToVec2(poligonIndexes[texcoordOffset + vertexCOffset]);
                }

                Triangles.Add(triangle);
            }
        }
        private Material ReadMaterial(string id)
        {
            XElement instanceElement = _document.XPathSelectElement(string.Format("//c:library_materials/c:material[@id='{0}']/c:instance_effect", id), _nsMgr);
            string effectId = instanceElement.Attribute("url").Value.Substring(1);

            XElement effectInstanceElement = _document.XPathSelectElement(string.Format("//c:library_effects/c:effect[@id='{0}']", effectId), _nsMgr);
            XElement profileElement = effectInstanceElement.XPathSelectElement("c:profile_COMMON", _nsMgr);
            XElement effectElement = profileElement.XPathSelectElement("c:technique[@sid='common']/c:phong", _nsMgr);

            Material material = new Material();

            material.EmissionColor = ReadColor(effectElement, "c:emission/c:color");
            material.AmbientColor = ReadColor(effectElement, "c:ambient/c:color");
            material.DiffuseColor = ReadColorSampler(profileElement, effectElement, "c:diffuse/node()");
            material.SpecularColor = ReadColor(effectElement, "c:specular/c:color");
            material.ReflectiveColor = ReadColor(effectElement, "c:reflective/c:color");

            material.Shininess = ReadFloat(effectElement, "c:shininess/c:float");
            material.Reflectivity = ReadFloat(effectElement, "c:reflectivity/c:float");

            return material;
        }