Exemplo n.º 1
0
        private static void AddMaterials(string fullFileName, ISceneEngine sceneEngine, string line, ref string mtlName, ActionResult <SceneElement> actionResult)
        {
            var currentDirectory = Environment.CurrentDirectory;

            try
            {
                var fileInfo = new FileInfo(fullFileName);
                if (line.StartsWith("mtllib") && fullFileName != null && fileInfo.Exists && fileInfo.Directory != null)
                {
                    // Set current directory in case a relative path to material file is used.
                    // ReSharper disable once AssignNullToNotNullAttribute
                    Environment.CurrentDirectory = fileInfo.Directory.FullName;

                    // Load materials file.
                    var mtlPath = ReadMaterialValue(line);
                    if (!File.Exists(mtlPath))
                    {
                        actionResult.AddWarning(Resources.Message_AddMaterials_File_with_material_N_not_found, mtlPath);
                        return;
                    }
                    LoadMaterials(mtlPath, sceneEngine);
                }

                if (line.StartsWith("usemtl"))
                {
                    mtlName = ReadMaterialValue(line);
                }
            }
            finally
            {
                Environment.CurrentDirectory = currentDirectory;
            }
        }
Exemplo n.º 2
0
        private static void AddFace(ISceneEngine sceneEngine, Polygon polygon, string mtlName, string line, char[] split)
        {
            var face = new Face();

            if (!string.IsNullOrWhiteSpace(mtlName))
            {
                face.Material = sceneEngine.GetAssets <Material>().FirstOrDefault(t => t.Name == mtlName);
            }

            //  Get the face indices
            string[] indices = line.Substring(2).Split(split, StringSplitOptions.RemoveEmptyEntries);

            //  Add each index.
            AddIndexesToFace(indices, face);

            //  Add the face.
            polygon.Faces.Add(face);
        }
Exemplo n.º 3
0
        private static void LoadGeometryFromFile(string fullFileName, ISceneEngine sceneEngine, Polygon polygon, ActionResult <SceneElement> actionResult)
        {
            var    split   = new[] { ' ' };
            string mtlName = null;

            using (var reader = new StreamReader(fullFileName))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    //  Skip any comments (lines that start with '#').
                    if (line.StartsWith("#"))
                    {
                        continue;
                    }

                    if (line.StartsWith("vt")) //texture coordinate
                    {
                        AddTextureCoordinate(polygon, line, split);
                        continue;
                    }

                    if (line.StartsWith("vn")) //normal coordinate
                    {
                        AddNormal(polygon, line, split);
                        continue;
                    }

                    if (line.StartsWith("v")) //vertex
                    {
                        AddVertices(polygon, line, split);
                        continue;
                    }

                    if (line.StartsWith("f")) //Face
                    {
                        AddFace(sceneEngine, polygon, mtlName, line, split);
                        continue;
                    }

                    AddMaterials(fullFileName, sceneEngine, line, ref mtlName, actionResult);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Load of 3D geometry from the file
        /// </summary>
        /// <param name="fullFileName">The path to the file with 3D geometry</param>
        /// <param name="sceneEngine">The engine of the scene</param>
        /// <returns></returns>
        public ActionResult <SceneElement> LoadGeometry(string fullFileName, ISceneEngine sceneEngine)
        {
            var polygon = new Polygon();

            var actionResult = new ActionResult <SceneElement>("Import Wavefront format geometry")
            {
                Value = polygon
            };

            if (!File.Exists(fullFileName))
            {
                actionResult.AddError(Resources.Message_LoadGeometry_File_N_not_found, fullFileName);
                return(actionResult);
            }
            try
            {
                LoadGeometryFromFile(fullFileName, sceneEngine, polygon, actionResult);
            }
            catch (Exception e)
            {
                actionResult.AddError(e);
            }
            return(actionResult);
        }
        private static void LoadMaterials(string path, ISceneEngine sceneEngine)
        {
            //  Create a stream reader.
            using (var reader = new StreamReader(path))
            {
                Material mtl = null;
                float alpha = 1;

                //  Read line by line.
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    line = line.Trim();

                    //  Skip any comments (lines that start with '#').
                    if (line.StartsWith("#"))
                        continue;

                    // newmatl indicates start of material definition.
                    if (line.StartsWith("newmtl"))
                    {
                        // Add new material to scene's assets.
                        mtl = new Material();
                        sceneEngine.AddAsset(mtl);

                        // Name of material is on same line, immediately follows newmatl.
                        mtl.Name = ReadMaterialValue(line);

                        // Reset assumed alpha.
                        alpha = 1;
                    }

                    // Read properties of material.
                    if (mtl != null)
                    {
                        if (line.StartsWith("Ka"))
                            mtl.Ambient = ReadMaterialColor(line, alpha);
                        else if (line.StartsWith("Kd"))
                            mtl.Diffuse = ReadMaterialColor(line, alpha);
                        else if (line.StartsWith("Ks"))
                            mtl.Specular = ReadMaterialColor(line, alpha);
                        else if (line.StartsWith("Ns"))
                            mtl.Shininess = Convert.ToSingle(ReadMaterialValue(line));
                        else if (line.StartsWith("map_Ka") ||
                                 line.StartsWith("map_Kd") ||
                                 line.StartsWith("map_Ks"))
                        {
                            // Get texture map.                    		
                            string textureFile = ReadMaterialValue(line);
                            // Set texture for material.
                            mtl.Texture = sceneEngine.LoadOrCreateTexture(path, textureFile);
                        }
                        else if (line.StartsWith("d") || line.StartsWith("Tr"))
                        {
                            alpha = Convert.ToSingle(ReadMaterialValue(line));
                            SetAlphaForMaterial(mtl, alpha);
                        }
                        // TODO: Handle illumination mode (illum)                    	                    
                    }
                }
            }
        }
        private static void AddFace(ISceneEngine sceneEngine, Polygon polygon, string mtlName, string line, char[] split)
        {
            var face = new Face();

            if (!string.IsNullOrWhiteSpace(mtlName))
                face.Material = sceneEngine.GetAssets<Material>().FirstOrDefault(t => t.Name == mtlName);

            //  Get the face indices
            string[] indices = line.Substring(2).Split(split, StringSplitOptions.RemoveEmptyEntries);

            //  Add each index.
            AddIndexesToFace(indices, face);

            //  Add the face.
            polygon.Faces.Add(face);
        }
        private static void AddMaterials(string fullFileName, ISceneEngine sceneEngine, string line, ref string mtlName, ActionResult<SceneElement> actionResult)
        {
            var currentDirectory = Environment.CurrentDirectory;
            try
            {
                var fileInfo = new FileInfo(fullFileName);
                if (line.StartsWith("mtllib") && fullFileName != null && fileInfo.Exists && fileInfo.Directory != null)
                {
                    // Set current directory in case a relative path to material file is used.
                    // ReSharper disable once AssignNullToNotNullAttribute
                    Environment.CurrentDirectory = fileInfo.Directory.FullName;

                    // Load materials file.
                    var mtlPath = ReadMaterialValue(line);
                    if (!File.Exists(mtlPath))
                    {
                        actionResult.AddWarning(Resources.Message_AddMaterials_File_with_material_N_not_found, mtlPath);
                        return;
                    }
                    LoadMaterials(mtlPath, sceneEngine);
                }

                if (line.StartsWith("usemtl"))
                    mtlName = ReadMaterialValue(line);
            }
            finally
            {
                Environment.CurrentDirectory = currentDirectory;
            }
        }
        private static void LoadGeometryFromFile(string fullFileName, ISceneEngine sceneEngine, Polygon polygon, ActionResult<SceneElement> actionResult)
        {
            var split = new[] {' '};
            string mtlName = null;
            using (var reader = new StreamReader(fullFileName))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    //  Skip any comments (lines that start with '#').
                    if (line.StartsWith("#"))
                        continue;

                    if (line.StartsWith("vt")) //texture coordinate
                    {
                        AddTextureCoordinate(polygon, line, split);
                        continue;
                    }

                    if (line.StartsWith("vn")) //normal coordinate
                    {
                        AddNormal(polygon, line, split);
                        continue;
                    }

                    if (line.StartsWith("v")) //vertex
                    {
                        AddVertices(polygon, line, split);
                        continue;
                    }

                    if (line.StartsWith("f")) //Face
                    {
                        AddFace(sceneEngine, polygon, mtlName, line, split);
                        continue;
                    }

                    AddMaterials(fullFileName, sceneEngine, line, ref mtlName, actionResult);
                }
            }
        }
        /// <summary>
        /// Load of 3D geometry from the file
        /// </summary>
        /// <param name="fullFileName">The path to the file with 3D geometry</param>
        /// <param name="sceneEngine">The engine of the scene</param>
        /// <returns></returns>
        public ActionResult<SceneElement> LoadGeometry(string fullFileName, ISceneEngine sceneEngine)
        {
            var polygon = new Polygon();
            
            var actionResult = new ActionResult<SceneElement>("Import Wavefront format geometry") {Value = polygon};

            if (!File.Exists(fullFileName))
            {
                actionResult.AddError(Resources.Message_LoadGeometry_File_N_not_found, fullFileName);
                return actionResult;
            }
            try
            {
                LoadGeometryFromFile(fullFileName, sceneEngine, polygon, actionResult);
            }
            catch (Exception e)
            {
                actionResult.AddError(e);
            }
            return actionResult;
        }
Exemplo n.º 10
0
 public void Init()
 {
     _sceneEngine = new SceneEngine();
 }
 public void Init()
 {
     _sceneEngine = new SceneEngine();
 }
Exemplo n.º 12
0
        private static void LoadMaterials(string path, ISceneEngine sceneEngine)
        {
            //  Create a stream reader.
            using (var reader = new StreamReader(path))
            {
                Material mtl   = null;
                float    alpha = 1;

                //  Read line by line.
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    line = line.Trim();

                    //  Skip any comments (lines that start with '#').
                    if (line.StartsWith("#"))
                    {
                        continue;
                    }

                    // newmatl indicates start of material definition.
                    if (line.StartsWith("newmtl"))
                    {
                        // Add new material to scene's assets.
                        mtl = new Material();
                        sceneEngine.AddAsset(mtl);

                        // Name of material is on same line, immediately follows newmatl.
                        mtl.Name = ReadMaterialValue(line);

                        // Reset assumed alpha.
                        alpha = 1;
                    }

                    // Read properties of material.
                    if (mtl != null)
                    {
                        if (line.StartsWith("Ka"))
                        {
                            mtl.Ambient = ReadMaterialColor(line, alpha);
                        }
                        else if (line.StartsWith("Kd"))
                        {
                            mtl.Diffuse = ReadMaterialColor(line, alpha);
                        }
                        else if (line.StartsWith("Ks"))
                        {
                            mtl.Specular = ReadMaterialColor(line, alpha);
                        }
                        else if (line.StartsWith("Ns"))
                        {
                            mtl.Shininess = Convert.ToSingle(ReadMaterialValue(line));
                        }
                        else if (line.StartsWith("map_Ka") ||
                                 line.StartsWith("map_Kd") ||
                                 line.StartsWith("map_Ks"))
                        {
                            // Get texture map.
                            string textureFile = ReadMaterialValue(line);
                            // Set texture for material.
                            mtl.Texture = sceneEngine.LoadOrCreateTexture(path, textureFile);
                        }
                        else if (line.StartsWith("d") || line.StartsWith("Tr"))
                        {
                            alpha = Convert.ToSingle(ReadMaterialValue(line));
                            SetAlphaForMaterial(mtl, alpha);
                        }
                        // TODO: Handle illumination mode (illum)
                    }
                }
            }
        }
Exemplo n.º 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SceneContent" />
 /// </summary>
 /// <param name="sceneEngine">The <see cref="ISceneEngine" /> performing operations with the scene.</param>
 public SceneContent(ISceneEngine sceneEngine)
 {
     SceneEngine     = sceneEngine;
     Navigator       = ServiceLocator.Get <SceneNavigationFactory>().Create();
     Navigator.Move += (s, e) => SceneEngine.Move(e.X, e.Y, e.Z);
 }