Exemple #1
0
        MeshGroup(MeshGroup mg)
        {
            offset        = mg.offset;     // +parent.mesh.offset;
            this.Rotation = mg.Rotation;
            this.scale    = mg.scale;

            //physics implementation
            rotVelocity = mg.rotVelocity;
            posVelocity = mg.posVelocity;

            meshes = mg.meshes;
        }
 public bool Load(MeshGroup mesh, string fileName)
 {
     try
     {
         using (StreamReader streamReader = new StreamReader(fileName))
         {
             filepath = fileName.Substring(0, fileName.LastIndexOf("/") + 1);
             Load(mesh, streamReader);
             streamReader.Close();
             return(true);
         }
     }
     catch { return(false); }
 }
        void Load(MeshGroup meshgroup, TextReader textReader)
        {
            Mesh mesh = new Mesh(null, null, null, 0);

            vertices     = new List <Vector3>();
            normals      = new List <Vector3>();
            texCoords    = new List <Vector2>();
            objVertices  = new List <Mesh.ObjVertex>();
            objTriangles = new List <Mesh.ObjTriangle>();
            objQuads     = new List <Mesh.ObjQuad>();
            int    firsto = 0;
            string line;

            while ((line = textReader.ReadLine()) != null)
            {
                line = line.Trim(splitCharacters);
                line = line.Replace("  ", " ");
                string[] parameters = line.Split(splitCharacters);
                switch (parameters[0])
                {
                case "p":                         // point
                    break;

                case "v":                         // vertex
                    float x = float.Parse(parameters[1]);
                    float y = float.Parse(parameters[2]);
                    float z = float.Parse(parameters[3]);
                    vertices.Add(new Vector3(x, y, z));
                    break;

                case "vt":                         // texCoord
                    float u = float.Parse(parameters[1]);
                    float v = float.Parse(parameters[2]);
                    texCoords.Add(new Vector2(u, v));
                    break;

                case "vn":                         // normal
                    float nx = float.Parse(parameters[1]);
                    float ny = float.Parse(parameters[2]);
                    float nz = float.Parse(parameters[3]);
                    normals.Add(new Vector3(nx, ny, nz));
                    break;

                case "f":
                    switch (parameters.Length)
                    {
                    case 4:
                        Mesh.ObjTriangle objTriangle = new Mesh.ObjTriangle();
                        objTriangle.Index0 = ParseFaceParameter(parameters[1]);
                        objTriangle.Index1 = ParseFaceParameter(parameters[2]);
                        objTriangle.Index2 = ParseFaceParameter(parameters[3]);
                        objTriangles.Add(objTriangle);
                        break;

                    case 5:
                        Mesh.ObjQuad objQuad = new Mesh.ObjQuad();
                        objQuad.Index0 = ParseFaceParameter(parameters[1]);
                        objQuad.Index1 = ParseFaceParameter(parameters[2]);
                        objQuad.Index2 = ParseFaceParameter(parameters[3]);
                        objQuad.Index3 = ParseFaceParameter(parameters[4]);
                        break;
                    }
                    break;

                case "mtllib":
                    string mtl = parameters[1];
                    for (int i = 2; i < parameters.Length; i++)
                    {
                        mtl += " " + parameters[i];
                    }
                    LoadMTLFile(mtl);
                    break;

                case "usemtl":
                    string umtl = "";
                    for (int i = 1; i < parameters.Length; i++)
                    {
                        umtl += parameters[i];
                    }
                    if (ignoreMaterials)
                    {
                        uiCurMaterial = 1;
                    }
                    else
                    {
                        uiCurMaterial = Materials[umtl].id;
                    }
                    break;

                case "o":
                    Console.WriteLine(parameters[1]);
                    if (firsto > 0)
                    {
                        mesh.vertices  = objVertices.ToArray();
                        mesh.triangles = objTriangles.ToArray();
                        mesh.quads     = objQuads.ToArray();
                        mesh.texture   = uiCurMaterial;
                        meshgroup.AddMesh(mesh);
                    }
                    mesh = new Mesh(null, null, null, 0);
                    firsto++;
                    //vertices = new List<Vector3>();
                    //normals = new List<Vector3>();
                    //texCoords = new List<Vector2>();
                    objVertices  = new List <Mesh.ObjVertex>();
                    objTriangles = new List <Mesh.ObjTriangle>();
                    objQuads     = new List <Mesh.ObjQuad>();
                    break;
                }
            }
            Console.WriteLine();
            mesh.vertices  = objVertices.ToArray();
            mesh.triangles = objTriangles.ToArray();
            mesh.quads     = objQuads.ToArray();
            mesh.texture   = uiCurMaterial;
            meshgroup.AddMesh(mesh);
            vertices     = null;
            normals      = null;
            texCoords    = null;
            objVertices  = null;
            objTriangles = null;
            objQuads     = null;
        }