Exemple #1
0
 public static bool Load(ObjMesh mesh, string fileName)
 {
     try
     {
         using (var streamReader = new StreamReader(fileName))
         {
             Load(mesh, streamReader);
             LoadTextures(mesh, fileName);
             streamReader.Close();
             return true;
         }
     }
     catch { return false; }
 }
Exemple #2
0
        static void LoadTextures(ObjMesh mesh, string filename)
        {
            string texdir = Path.GetFileNameWithoutExtension(filename);
            string fulldir = @"Textures/"+texdir+@"/";
            string[] files = Directory.GetFiles(fulldir);
            var textureList = new List<ObjMesh.ObjTexture>();

            foreach (string file in files)
            {
                string textureUniform = Path.GetFileNameWithoutExtension(file);
                uint textureId;
                TextureTarget textureTarget;

                ImageDDS.LoadFromDisk(file, out textureId, out textureTarget);

                GL.BindTexture(textureTarget, textureId);
                GL.TexParameter(textureTarget, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

                var mipMapCount = new int[1];
                GL.GetTexParameter(textureTarget, GetTextureParameter.TextureMaxLevel, out mipMapCount[0]);

                if (mipMapCount[0] == 0) GL.TexParameter(textureTarget, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
                else GL.TexParameter(textureTarget, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);

                var newTexture = new ObjMesh.ObjTexture
                {
                    TextureId = textureId,
                    TextureTarg = textureTarget,
                    TextureUniform = textureUniform
                };

                switch (textureUniform)
                {
                    case "diffuse":
                        newTexture.TextureNumber = TextureUnit.Texture0;
                        break;
                    case "normal":
                        newTexture.TextureNumber = TextureUnit.Texture1;
                        break;
                    case "specular":
                        newTexture.TextureNumber = TextureUnit.Texture2;
                        break;
                }

                textureList.Add(newTexture);
            }

            mesh.Textures = textureList.ToArray();
        }
Exemple #3
0
        static void Load(ObjMesh mesh, TextReader textReader)
        {
            _vertices = new List<Vector3>();
            _normals = new List<Vector3>();
            _texCoords = new List<Vector2>();
            _objVertices = new List<ObjMesh.ObjVertex>();
            _objTriangles = new List<ObjMesh.ObjTriangle>();
            _objVerticesIndexDictionary = new Dictionary<ObjMesh.ObjVertex, int>();
            _objQuads = new List<ObjMesh.ObjQuad>();

            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:
                                var objTriangle = new ObjMesh.ObjTriangle
                                {
                                    Index0 = ParseFaceParameter(parameters[1]),
                                    Index1 = ParseFaceParameter(parameters[2]),
                                    Index2 = ParseFaceParameter(parameters[3])
                                };
                                _objTriangles.Add(objTriangle);
                                break;

                            case 5:
                                var objQuad = new ObjMesh.ObjQuad
                                {
                                    Index0 = ParseFaceParameter(parameters[1]),
                                    Index1 = ParseFaceParameter(parameters[2]),
                                    Index2 = ParseFaceParameter(parameters[3]),
                                    Index3 = ParseFaceParameter(parameters[4])
                                };
                                _objQuads.Add(objQuad);
                                break;
                        }
                        break;
                }
            }

            // FillTangentData(); // Fill the triangles with tangents / bitangents
            // IndexTangentsFix(); // Attempt to average the tangents / bitangents

            mesh.Vertices = _objVertices.ToArray();
            mesh.Triangles = _objTriangles.ToArray();
            mesh.Quads = _objQuads.ToArray();

            _objVerticesIndexDictionary = null;
            _vertices = null;
            _normals = null;
            _texCoords = null;
            _objVertices = null;
            _objTriangles = null;
            _objQuads = null;
        }