Esempio n. 1
0
        void LoadMesh(string fileName)
        {
            string data = null;

            try
            {
                using (System.IO.StreamReader file = new System.IO.StreamReader(Settings.ModelDir + fileName))
                {
                    // tiedosto muistiin
                    data = file.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                Log.Error(e.ToString());
            }

            //
            string[] lines = data.Split('\n');

            int line = 0;

            while (true)
            {
                // new mesh
                if (lines[line].StartsWith("o"))
                {
                    ObjMesh mesh = new ObjMesh();
                    mesh.Name = lines[line].Split(' ')[1];

                    while (true)
                    {
                        if (lines[line + 1].StartsWith("v ") == false)
                        {
                            break;
                        }

                        string[] vert = lines[line + 1].Split(' ');
                        // [-1,1] -> [screenwidth, screenheight]
                        float x = ((MathExt.GetFloat(vert[1]) + 1) * 0.5f) * Settings.Width;
                        float y = MathExt.GetFloat(vert[2]); // not needed
                        float z = Settings.Height - (((MathExt.GetFloat(vert[3]) + 1) * 0.5f) * Settings.Height);
                        mesh.Vertices.Add(new Vector3(x, y, z));
                        line++;
                    }

                    // seuraava mesh
                    if (lines[line + 1].StartsWith("o "))
                    {
                        Meshes.Add(mesh);
                        continue;
                    }

                    while (lines[line + 1].StartsWith("usemtl ") == false)
                    {
                        line++;
                    }


                    string[] mat = lines[line + 1].Split(' ');
                    if (mat[1].StartsWith("_"))
                    {
                        mesh.UseMat = mat[1].Substring(1);
                    }
                    Meshes.Add(mesh);

                    // luo texture jos meshillä on materiaali
                    if (mesh.UseMat != "")
                    {
                        mesh.Tex = Texture2D.Load(Settings.TextureDir + mesh.UseMat);

                        // lev ja kor
                        float w = mesh.Vertices[0].X - mesh.Vertices[1].X;
                        float h = mesh.Vertices[0].Z - mesh.Vertices[2].Z;

                        // tuo on se koko millä se pitää piirtää esim 20x20
                        // mutta jos oikea kuva on 100x100, se pitää skaalata viidesosaks eli 0.2
                        // eli  20/100 = 0.2 eli  w/img.w

                        mesh.ScaleX = w / (float)mesh.Tex.Width;
                        mesh.ScaleY = h / (float)mesh.Tex.Height;
                    }

                    if (mesh.Name.Contains("START"))
                    {
                        SX = mesh.Vertices[0].X;
                        SY = mesh.Vertices[0].Z;
                    }
                }

                line++;
                if (line == lines.Length)
                {
                    break;
                }
            }
        }
Esempio n. 2
0
        void LoadMesh(string fileName)
        {
            string data = null;
            try
            {
                using (System.IO.StreamReader file = new System.IO.StreamReader(Settings.ModelDir + fileName))
                {
                    // tiedosto muistiin
                    data = file.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                Log.Error(e.ToString());
            }

            //
            string[] lines = data.Split('\n');

            int line = 0;
            while (true)
            {
                // new mesh
                if (lines[line].StartsWith("o"))
                {
                    ObjMesh mesh = new ObjMesh();
                    mesh.Name = lines[line].Split(' ')[1];

                    while (true)
                    {
                        if (lines[line + 1].StartsWith("v ") == false)
                            break;

                        string[] vert = lines[line + 1].Split(' ');
                        // [-1,1] -> [screenwidth, screenheight]
                        float x = ((MathExt.GetFloat(vert[1]) + 1) * 0.5f) * Settings.Width;
                        float y = MathExt.GetFloat(vert[2]); // not needed
                        float z = Settings.Height - (((MathExt.GetFloat(vert[3]) + 1) * 0.5f) * Settings.Height);
                        mesh.Vertices.Add(new Vector3(x, y, z));
                        line++;
                    }

                    // seuraava mesh
                    if (lines[line + 1].StartsWith("o "))
                    {
                        Meshes.Add(mesh);
                        continue;
                    }

                    while (lines[line + 1].StartsWith("usemtl ") == false)
                        line++;


                    string[] mat = lines[line + 1].Split(' ');
                    if (mat[1].StartsWith("_"))
                    {

                        mesh.UseMat = mat[1].Substring(1);
                    }
                    Meshes.Add(mesh);

                    // luo texture jos meshillä on materiaali
                    if (mesh.UseMat != "")
                    {
                        mesh.Tex = Texture2D.Load(Settings.TextureDir + mesh.UseMat);

                        // lev ja kor
                        float w = mesh.Vertices[0].X - mesh.Vertices[1].X;
                        float h = mesh.Vertices[0].Z - mesh.Vertices[2].Z;

                        // tuo on se koko millä se pitää piirtää esim 20x20
                        // mutta jos oikea kuva on 100x100, se pitää skaalata viidesosaks eli 0.2
                        // eli  20/100 = 0.2 eli  w/img.w

                        mesh.ScaleX = w / (float)mesh.Tex.Width;
                        mesh.ScaleY = h / (float)mesh.Tex.Height;
                    }

                    if (mesh.Name.Contains("START"))
                    {
                        SX = mesh.Vertices[0].X;
                        SY = mesh.Vertices[0].Z;
                    }

                }

                line++;
                if (line == lines.Length)
                    break;
            }
        }