Exemplo n.º 1
0
        /// <summary>
        /// lataa mesh tiedosto
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="xs"></param>
        /// <param name="ys"></param>
        /// <param name="zs"></param>
        public void Load(string fileName, float xs, float ys, float zs)
        {
            pathData.Clear();
            ObjModel       mesh    = null;
            List <Vector3> _vertex = new List <Vector3>();
            List <Vector3> _normal = new List <Vector3>();
            List <Vector2> _uv     = new List <Vector2>();

            string dir = Settings.DataDir;

            if (fileName.Contains("\\"))
            {
                int l = fileName.LastIndexOf("\\");
                dir = dir + fileName.Substring(0, l + 1);
            }
            else if (fileName.Contains("/"))
            {
                int l = fileName.LastIndexOf("/");
                dir = dir + fileName.Substring(0, l + 1);
            }

            fileName = Settings.DataDir + fileName;
            bool path = false; // jos reitti

            using (System.IO.StreamReader file = new System.IO.StreamReader(fileName))
            {
                // tiedosto muistiin
                string data = file.ReadToEnd();
                data = data.Replace('\r', ' ');

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

                int numOfFaces = 0;
                for (int q = 0; q < lines.Length; q++)
                {
                    string[] ln = lines[q].Split(' '); // pilko datat
                    if (ln[0] == "f")
                    {
                        numOfFaces++;
                    }
                }

                // lue kaikki datat objektiin ja indexit mesheihin
                for (int q = 0; q < lines.Length; q++)
                {
                    string line = lines[q];
                    if (line.StartsWith("#"))
                    {
                        continue;
                    }
                    string[] ln = line.Split(' '); // pilko datat
                    if (ln[0] == "v")              // vertex x y z
                    {
                        float x = (Util.GetFloat(ln[1]) - mesh.Position.X) * xs;
                        float y = (Util.GetFloat(ln[2]) - mesh.Position.Y) * ys;
                        float z = (Util.GetFloat(ln[3]) - mesh.Position.Z) * zs;

                        if (path)
                        {
                            pathData.Add(new Vector3(x, y, z));
                        }
                        else
                        {
                            _vertex.Add(new Vector3(x, y, z));
                        }

                        continue;
                    }

                    if (ln[0] == "vn") // normal x y z
                    {
                        _normal.Add(new Vector3(Util.GetFloat(ln[1]), Util.GetFloat(ln[2]), Util.GetFloat(ln[3])));
                        continue;
                    }

                    if (ln[0] == "vt") // texcoord U V
                    {
                        _uv.Add(new Vector2(Util.GetFloat(ln[1]), Util.GetFloat(ln[2])));
                        continue;
                    }

                    // uusi objekti
                    if (ln[0] == "o" || ln[0] == "g")
                    {
                        if (mesh != null)
                        {
                            meshes.Add(mesh);               // talteen
                        }
                        mesh          = new ObjModel(ln[1]);
                        mesh.material = material;

                        // Nimessä voi olla ohjeita mitä muuta halutaan, esim:
                        // * Path_reitti1  jolloin ei ladata objektia mutta reitti jota pitkin kamera/objektit voi kulkea.
                        // * BBox_nimi/BSphere_nimi jolloin tämä onkin nimi-objektin bounding box/sphere.
                        if (mesh.Name.Contains("Path_"))
                        {
                            path = true;
                        }
                        else if (mesh.Name.Contains("BBox_") || mesh.Name.Contains("BSphere_"))
                        {
                            // TODO: bbox_ bsphere_
                        }

                        continue;
                    }

                    // materiaali
                    if (ln[0] == "usemtl")
                    {
                        // jos kesken meshin materiaali vaihtuu, luodaan uusi obu johon loput facet
                        if (lines[q - 1].StartsWith("f"))
                        {
                            meshes.Add(mesh);
                            Vector3 tmpPos = mesh.Position;

                            mesh          = new ObjModel(mesh.Name);
                            mesh.material = material;
                            mesh.Position = tmpPos; // samaa objektia, niin sama Position
                        }

                        mesh.MaterialName = ln[1];
                        continue;
                    }

                    if (ln[0] == "f")
                    {
                        // ota talteen  f rivi:
                        // f vertex/uv/normal vertex/uv/normal vertex/uv/normal
                        // eli esim: f 4/4/2 5/5/3 7/2/4
                        // tarkistetaan jos ilman texcoordei eli f 4/4 5/4 6/4  tai f 2//3 jne tai ilman / merkkejä.
                        int dv = 0;
                        for (int t = 0; t < line.Length; t++)
                        {
                            if (line[t] == '/')
                            {
                                dv++;
                            }
                        }
                        if (line.Contains("//"))
                        {
                            dv = 0;                      // ei ole texindexei
                        }
                        line = line.Replace("//", " ");
                        line = line.Replace("/", " ");
                        string[] _ln = line.Split(' ');

                        if (dv == 0 || dv == 3) // luultavasti nyt ei ole texturecoordinaatteja
                        {
                            mesh._vertexInd.Add(Int32.Parse(_ln[1]) - 1);
                            mesh._vertexInd.Add(Int32.Parse(_ln[3]) - 1);
                            mesh._vertexInd.Add(Int32.Parse(_ln[5]) - 1);

                            mesh._normalInd.Add(Int32.Parse(_ln[2]) - 1);
                            mesh._normalInd.Add(Int32.Parse(_ln[4]) - 1);
                            mesh._normalInd.Add(Int32.Parse(_ln[6]) - 1);
                        }
                        else // kaikki mukana
                        {
                            mesh._vertexInd.Add(Int32.Parse(_ln[1]) - 1);
                            mesh._vertexInd.Add(Int32.Parse(_ln[4]) - 1);
                            mesh._vertexInd.Add(Int32.Parse(_ln[7]) - 1);

                            mesh._uvInd.Add(Int32.Parse(_ln[2]) - 1);
                            mesh._uvInd.Add(Int32.Parse(_ln[5]) - 1);
                            mesh._uvInd.Add(Int32.Parse(_ln[8]) - 1);

                            mesh._normalInd.Add(Int32.Parse(_ln[3]) - 1);
                            mesh._normalInd.Add(Int32.Parse(_ln[6]) - 1);
                            mesh._normalInd.Add(Int32.Parse(_ln[9]) - 1);
                        }
                        continue;
                    }

                    // materiaalitiedosto
                    if (ln[0] == "mtllib")
                    {
                        try
                        {
                            // ladataan objektille materiaalitiedot (mesheille otetaan talteen materialname joka viittaa sitten näihin materiaaleihin)
                            material = new Material();
                            material.Load(dir + ln[1], Texture.LoadTextures);
                        }
                        catch (Exception e)
                        {
                            Log.WriteDebugLine(e.ToString());
                        }
                    }
                }

                if (mesh != null)
                {
                    meshes.Add(mesh);
                }

                // pathille ei luoda objektia, se on vain kasa vertexejä
                if (path == false)
                {
                    int cc = 0;

                    vertices = new Vertex[numOfFaces * 3];
                    for (int m = 0; m < meshes.Count; m++)
                    {
                        meshes[m].vertices = new Vertex[meshes[m]._vertexInd.Count];

                        for (int q = 0; q < meshes[m]._vertexInd.Count; q++)
                        {
                            // mesh datat
                            meshes[m].vertices[q].vertex = _vertex[meshes[m]._vertexInd[q]];
                            meshes[m].vertices[q].normal = _normal[meshes[m]._normalInd[q]];
                            if (meshes[m]._uvInd.Count != 0)
                            {
                                meshes[m].vertices[q].uv_or_color = new Vector4(_uv[meshes[m]._uvInd[q]].X, _uv[meshes[m]._uvInd[q]].Y,
                                                                                _uv[meshes[m]._uvInd[q]].X, _uv[meshes[m]._uvInd[q]].Y);
                            }

                            // pistetään myös objektille kaikki vertexit yhteen klimppiin.

//TODO Miks? vie vaa muistia nii paljo, mesheissä kumminki nuo datat säilytetään.
// jos tää on VAIN collisionia varten, siihe ny o helppo tehdä se et se menee objektin puun läpi
// ni sit ei tartte tätä
                            vertices[cc].vertex = _vertex[meshes[m]._vertexInd[q]];
                            vertices[cc].normal = _normal[meshes[m]._normalInd[q]];
                            if (meshes[m]._uvInd.Count != 0)
                            {
                                vertices[cc].uv_or_color = new Vector4(_uv[meshes[m]._uvInd[q]].X, _uv[meshes[m]._uvInd[q]].Y,
                                                                       _uv[meshes[m]._uvInd[q]].X, _uv[meshes[m]._uvInd[q]].Y);
                            }
                            cc++;
                        }

                        // index taulukko
                        meshes[m].indices = new int[meshes[m]._vertexInd.Count];
                        for (int q = 0; q < meshes[m]._vertexInd.Count; q++)
                        {
                            meshes[m].indices[q] = q;
                        }

                        meshes[m].vbo = new VBO();
                        meshes[m].vbo.DataToVBO(meshes[m].vertices, meshes[m].indices);

                        // meshin bounding volume
                        meshes[m].Boundings = new BoundingVolume();
                        meshes[m].Boundings.CreateBoundingVolume(meshes[m]);

                        // lataa glsl koodit
                        string shader = material.GetMaterial(meshes[m].MaterialName).ShaderName;
                        if (shader != "")
                        {
                            mesh.Shader = new GLSL();
                            mesh.Shader.Load(shader + ".vert", shader + ".frag");
                        }
                        if (material.GetMaterial(meshes[m].MaterialName).Dissolve < 1.0f)
                        {
                            IsTranslucent = true;
                        }
                    }

                    // koko objektin bounding volume
                    IsRendObj = false; // childeissä rendattavat objektit, tässä ei ole rendattavaa, vain paikka
                    Boundings = new BoundingVolume();
                    Boundings.CreateBoundingVolume(this);

                    // lisätään objektille meshit childeiks
                    for (int q = 0; q < meshes.Count; q++)
                    {
                        Add(meshes[q]);
                    }

                    Log.WriteDebugLine("Object: " + Name + "  meshes: " + meshes.Count);
                }

                _vertex.Clear();
                _normal.Clear();
                _uv.Clear();
                for (int q = 0; q < meshes.Count; q++)
                {
                    meshes[q]._vertexInd.Clear();
                    meshes[q]._normalInd.Clear();
                    meshes[q]._uvInd.Clear();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// lataa mesh tiedosto
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="xs"></param>
        /// <param name="ys"></param>
        /// <param name="zs"></param>
        public void Load(string fileName, float xs, float ys, float zs)
        {
            pathData.Clear();
            ObjModel mesh = null;
            List<Vector3> _vertex = new List<Vector3>();
            List<Vector3> _normal = new List<Vector3>();
            List<Vector2> _uv = new List<Vector2>();

            string dir = Settings.DataDir;
            if (fileName.Contains("\\"))
            {
                int l = fileName.LastIndexOf("\\");
                dir = dir + fileName.Substring(0, l + 1);
            }
            else if (fileName.Contains("/"))
            {
                int l = fileName.LastIndexOf("/");
                dir = dir + fileName.Substring(0, l + 1);
            }

            fileName = Settings.DataDir + fileName;
            bool path = false; // jos reitti

            using (System.IO.StreamReader file = new System.IO.StreamReader(fileName))
            {
                // tiedosto muistiin
                string data = file.ReadToEnd();
                data = data.Replace('\r', ' ');

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

                int numOfFaces = 0;
                for (int q = 0; q < lines.Length; q++)
                {
                    string[] ln = lines[q].Split(' '); // pilko datat
                    if (ln[0] == "f") numOfFaces++;
                }

                // lue kaikki datat objektiin ja indexit mesheihin
                for (int q = 0; q < lines.Length; q++)
                {
                    string line = lines[q];
                    if (line.StartsWith("#")) continue;
                    string[] ln = line.Split(' '); // pilko datat
                    if (ln[0] == "v") // vertex x y z
                    {
                        float x = (Util.GetFloat(ln[1]) - mesh.Position.X) * xs;
                        float y = (Util.GetFloat(ln[2]) - mesh.Position.Y) * ys;
                        float z = (Util.GetFloat(ln[3]) - mesh.Position.Z) * zs;

                        if (path) pathData.Add(new Vector3(x, y, z));
                        else _vertex.Add(new Vector3(x, y, z));

                        continue;
                    }

                    if (ln[0] == "vn") // normal x y z
                    {
                        _normal.Add(new Vector3(Util.GetFloat(ln[1]), Util.GetFloat(ln[2]), Util.GetFloat(ln[3])));
                        continue;
                    }

                    if (ln[0] == "vt") // texcoord U V
                    {
                        _uv.Add(new Vector2(Util.GetFloat(ln[1]), Util.GetFloat(ln[2])));
                        continue;
                    }

                    // uusi objekti
                    if (ln[0] == "o" || ln[0] == "g")
                    {
                        if (mesh != null) meshes.Add(mesh); // talteen
                        mesh = new ObjModel(ln[1]);
                        mesh.material = material;

                        // Nimessä voi olla ohjeita mitä muuta halutaan, esim:
                        // * Path_reitti1  jolloin ei ladata objektia mutta reitti jota pitkin kamera/objektit voi kulkea.
                        // * BBox_nimi/BSphere_nimi jolloin tämä onkin nimi-objektin bounding box/sphere.
                        if (mesh.Name.Contains("Path_"))
                        {
                            path = true;
                        }
                        else if (mesh.Name.Contains("BBox_") || mesh.Name.Contains("BSphere_"))
                        {
                            // TODO: bbox_ bsphere_
                        }

                        continue;
                    }

                    // materiaali
                    if (ln[0] == "usemtl")
                    {
                        // jos kesken meshin materiaali vaihtuu, luodaan uusi obu johon loput facet
                        if (lines[q - 1].StartsWith("f"))
                        {
                            meshes.Add(mesh);
                            Vector3 tmpPos = mesh.Position;

                            mesh = new ObjModel(mesh.Name);
                            mesh.material = material;
                            mesh.Position = tmpPos; // samaa objektia, niin sama Position

                        }

                        mesh.MaterialName = ln[1];
                        continue;
                    }

                    if (ln[0] == "f")
                    {
                        // ota talteen  f rivi:
                        // f vertex/uv/normal vertex/uv/normal vertex/uv/normal
                        // eli esim: f 4/4/2 5/5/3 7/2/4
                        // tarkistetaan jos ilman texcoordei eli f 4/4 5/4 6/4  tai f 2//3 jne tai ilman / merkkejä.
                        int dv = 0;
                        for (int t = 0; t < line.Length; t++) if (line[t] == '/') dv++;
                        if (line.Contains("//")) dv = 0; // ei ole texindexei

                        line = line.Replace("//", " ");
                        line = line.Replace("/", " ");
                        string[] _ln = line.Split(' ');

                        if (dv == 0 || dv == 3) // luultavasti nyt ei ole texturecoordinaatteja
                        {
                            mesh._vertexInd.Add(Int32.Parse(_ln[1]) - 1);
                            mesh._vertexInd.Add(Int32.Parse(_ln[3]) - 1);
                            mesh._vertexInd.Add(Int32.Parse(_ln[5]) - 1);

                            mesh._normalInd.Add(Int32.Parse(_ln[2]) - 1);
                            mesh._normalInd.Add(Int32.Parse(_ln[4]) - 1);
                            mesh._normalInd.Add(Int32.Parse(_ln[6]) - 1);
                        }
                        else // kaikki mukana
                        {
                            mesh._vertexInd.Add(Int32.Parse(_ln[1]) - 1);
                            mesh._vertexInd.Add(Int32.Parse(_ln[4]) - 1);
                            mesh._vertexInd.Add(Int32.Parse(_ln[7]) - 1);

                            mesh._uvInd.Add(Int32.Parse(_ln[2]) - 1);
                            mesh._uvInd.Add(Int32.Parse(_ln[5]) - 1);
                            mesh._uvInd.Add(Int32.Parse(_ln[8]) - 1);

                            mesh._normalInd.Add(Int32.Parse(_ln[3]) - 1);
                            mesh._normalInd.Add(Int32.Parse(_ln[6]) - 1);
                            mesh._normalInd.Add(Int32.Parse(_ln[9]) - 1);
                        }
                        continue;
                    }

                    // materiaalitiedosto
                    if (ln[0] == "mtllib")
                    {
                        try
                        {
                            // ladataan objektille materiaalitiedot (mesheille otetaan talteen materialname joka viittaa sitten näihin materiaaleihin)
                            material = new Material();
                            material.Load(dir + ln[1], Texture.LoadTextures);
                        }
                        catch (Exception e)
                        {
                            Log.WriteDebugLine(e.ToString());
                        }
                    }
                }

                if (mesh != null) meshes.Add(mesh);

                // pathille ei luoda objektia, se on vain kasa vertexejä
                if (path == false)
                {
                    int cc = 0;

                    vertices = new Vertex[numOfFaces * 3];
                    for (int m = 0; m < meshes.Count; m++)
                    {
                        meshes[m].vertices = new Vertex[meshes[m]._vertexInd.Count];

                        for (int q = 0; q < meshes[m]._vertexInd.Count; q++)
                        {
                            // mesh datat
                            meshes[m].vertices[q].vertex = _vertex[meshes[m]._vertexInd[q]];
                            meshes[m].vertices[q].normal = _normal[meshes[m]._normalInd[q]];
                            if (meshes[m]._uvInd.Count != 0)
                                meshes[m].vertices[q].uv_or_color = new Vector4(_uv[meshes[m]._uvInd[q]].X, _uv[meshes[m]._uvInd[q]].Y,
                                                                                _uv[meshes[m]._uvInd[q]].X, _uv[meshes[m]._uvInd[q]].Y);

                            // pistetään myös objektille kaikki vertexit yhteen klimppiin.

            //TODO Miks? vie vaa muistia nii paljo, mesheissä kumminki nuo datat säilytetään.
            // jos tää on VAIN collisionia varten, siihe ny o helppo tehdä se et se menee objektin puun läpi
            // ni sit ei tartte tätä
                            vertices[cc].vertex = _vertex[meshes[m]._vertexInd[q]];
                            vertices[cc].normal = _normal[meshes[m]._normalInd[q]];
                            if (meshes[m]._uvInd.Count != 0)
                                vertices[cc].uv_or_color = new Vector4(_uv[meshes[m]._uvInd[q]].X, _uv[meshes[m]._uvInd[q]].Y,
                                                                                _uv[meshes[m]._uvInd[q]].X, _uv[meshes[m]._uvInd[q]].Y);
                            cc++;
                        }

                        // index taulukko
                        meshes[m].indices = new int[meshes[m]._vertexInd.Count];
                        for (int q = 0; q < meshes[m]._vertexInd.Count; q++) meshes[m].indices[q] = q;

                        meshes[m].vbo = new VBO();
                        meshes[m].vbo.DataToVBO(meshes[m].vertices, meshes[m].indices);

                        // meshin bounding volume
                        meshes[m].Boundings = new BoundingVolume();
                        meshes[m].Boundings.CreateBoundingVolume(meshes[m]);

                        // lataa glsl koodit
                        string shader = material.GetMaterial(meshes[m].MaterialName).ShaderName;
                        if (shader != "")
                        {
                            mesh.Shader = new GLSL();
                            mesh.Shader.Load(shader + ".vert", shader + ".frag");
                        }
                        if (material.GetMaterial(meshes[m].MaterialName).Dissolve < 1.0f) IsTranslucent = true;

                    }

                    // koko objektin bounding volume
                    IsRendObj = false; // childeissä rendattavat objektit, tässä ei ole rendattavaa, vain paikka
                    Boundings = new BoundingVolume();
                    Boundings.CreateBoundingVolume(this);

                    // lisätään objektille meshit childeiks
                    for (int q = 0; q < meshes.Count; q++)
                    {
                        Add(meshes[q]);
                    }

                    Log.WriteDebugLine("Object: " + Name + "  meshes: " + meshes.Count);
                }

                _vertex.Clear();
                _normal.Clear();
                _uv.Clear();
                for (int q = 0; q < meshes.Count; q++)
                {
                    meshes[q]._vertexInd.Clear();
                    meshes[q]._normalInd.Clear();
                    meshes[q]._uvInd.Clear();
                }

            }
        }