Exemple #1
0
        /// <summary>
        /// Create a mesh from wavefront obj file format using Tangent and Binormal vertex format
        /// </summary>
        /// <param name="device">Device</param>
        /// <param name="filename">Filename</param>
        /// <returns>Mesh</returns>
        public static SharpMesh CreateNormalMappedFromObj(SharpDevice device, string filename)
        {
            SharpMesh mesh = new SharpMesh(device);

            WaveFrontModel[] modelParts = WaveFrontModel.CreateFromObj(filename);
            mesh.Device  = device;
            mesh.SubSets = new List <SharpSubSet>();

            List <TangentVertex> vertices = new List <TangentVertex>();
            List <int>           indices  = new List <int>();

            int vcount = 0;
            int icount = 0;

            foreach (WaveFrontModel model in modelParts)
            {
                vertices.AddRange(model.TangentData);
                indices.AddRange(model.IndexData.Select(i => i + vcount));

                var mate = model.MeshMaterial.First();


                ShaderResourceView tex  = null;
                ShaderResourceView ntex = null;

                if (!string.IsNullOrEmpty(mate.DiffuseMap))
                {
                    string textureFile = Path.GetDirectoryName(filename) + "\\" + Path.GetFileName(mate.DiffuseMap);
                    tex = device.LoadTextureFromFile(textureFile);

                    string normalMap = Path.GetDirectoryName(textureFile) + "\\" + Path.GetFileNameWithoutExtension(textureFile) + "N" + Path.GetExtension(textureFile);
                    ntex = device.LoadTextureFromFile(normalMap);
                }

                mesh.SubSets.Add(new SharpSubSet()
                {
                    IndexCount = model.IndexData.Count,
                    StartIndex = icount,
                    DiffuseMap = tex,
                    NormalMap  = ntex
                });

                vcount += model.VertexData.Count;
                icount += model.IndexData.Count;
            }

            mesh.VertexBuffer = Buffer11.Create <TangentVertex>(device.Device, BindFlags.VertexBuffer, vertices.ToArray());
            mesh.IndexBuffer  = Buffer11.Create(device.Device, BindFlags.IndexBuffer, indices.ToArray());
            mesh.VertexSize   = SharpDX.Utilities.SizeOf <TangentVertex>();

            return(mesh);
        }