Ejemplo n.º 1
0
        public static SimpleModel Load(string inputFile, int modelId)
        {
            Positions = new List<Vector3>();
            Normals = new List<Vector3>();
            UVs = new List<Vector2>();

            String[] text = File.ReadAllText(inputFile).Split(System.Environment.NewLine.ToCharArray());

            String modelName = Path.GetFileNameWithoutExtension(inputFile);

            List<ObjectGroup> meshes = new List<ObjectGroup>();
            Dictionary<string, string> textureBindings = new Dictionary<string, string>();

            ObjectGroup currentMesh = null;
            var model = new GameEditor.SimpleModel(modelId, modelName);

            foreach (string line in text)
            {
                string[] tokens = line.Trim().Split(' ');

                if (tokens[0] == "")
                    continue;

                switch (tokens[0])
                {
                    case "v":  Positions.Add(ReadVector3(tokens)); break;
                    case "vn": Normals.Add(ReadVector3(tokens)); break;
                    case "vt": UVs.Add(ReadVector2(tokens)); break;
                    case "f":  if (currentMesh == null)
                               {
                                   currentMesh = new ObjectGroup(modelName); // First mesh has just the name of the model
                                   meshes.Add(currentMesh);
                               }
                               currentMesh.Faces.Add( ReadFace(tokens) );
                               break;
                    case "o":  currentMesh = new ObjectGroup(tokens[1]);
                               meshes.Add(currentMesh);
                               break;
                    case "#":  if (tokens[1] == "texture")
                               {
                                   textureBindings.Add(tokens[2], tokens[3]);
                               }
                               break;
                    default:   break;
                }
            }

            foreach (var mesh in meshes)
            {
                mesh.GenerateVerticesAndIndices();

                var vertices = mesh.Vertices;
                var indices = mesh.Indices;

                var _mesh = new GameEditor.Mesh(vertices, indices);

                string texturesFolder = Path.GetDirectoryName(inputFile) + @"\..\textures\";

                string textureName = textureBindings.ContainsKey(mesh.Name) ? textureBindings[mesh.Name] : "no_texture";

                FileStream stream = File.OpenRead(texturesFolder + textureName + ".png");
                _mesh.Texture = Texture2D.FromStream(Game1.Instance.GraphicsDevice, stream);
                stream.Close();

                model.AddMesh(_mesh);
            }

            //Console.WriteLine("Meshes: " + model.Meshes.Count + " Vertices: " + model.Meshes[0].VertexBuffer.VertexCount);
            model.bbox = model.BoundingBoxFromMeshes();
            return model;
        }
Ejemplo n.º 2
0
 public void AddMesh(Mesh mesh)
 {
     Meshes.Add(mesh);
 }