Пример #1
0
        /// <summary>
        /// Create a quad for Multiple Render Target
        /// </summary>
        /// <param name="device">Device</param>
        /// <returns>Mesh</returns>
        public static SharpMesh CreateQuad(SharpDevice device, float size = 1, bool IsDoubleSide = false)
        {
            Vector3[] vertices = new Vector3[]
            {
                new Vector3(-1 * size, 1 * size, 0),
                new Vector3(-1 * size, -1 * size, 0),
                new Vector3(1 * size, 1 * size, 0),
                new Vector3(1 * size, -1 * size, 0)
            };

            int[] indices = IsDoubleSide
                                ? new int[] { 0, 2, 1, 2, 3, 1, 1, 2, 0, 3, 2, 1 }
                                : new int[] { 0, 2, 1, 2, 3, 1 };

            SharpMesh mesh = new SharpMesh(device, vertices, indices);

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

            mesh.SubSets.Add(new SharpSubSet()
            {
                DiffuseColor = new Vector4(1, 1, 1, 1),
                IndexCount   = indices.Count()
            });

            return(mesh);
        }
Пример #2
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);
        }
Пример #3
0
        /// <summary>
        /// Create From Vertices and Indices array
        /// </summary>
        /// <typeparam name="VType">Vertex Type</typeparam>
        /// <param name="device">Device</param>
        /// <param name="vertices">Vertices</param>
        /// <param name="indices">Indices</param>
        /// <returns>Mesh</returns>
        public static SharpMesh Create <VType>(SharpDevice device, VType[] vertices, int[] indices) where VType : struct
        {
            SharpMesh mesh = new SharpMesh(device);

            mesh.VertexBuffer = Buffer11.Create <VType>(device.Device, BindFlags.VertexBuffer, vertices);
            mesh.IndexBuffer  = Buffer11.Create(device.Device, BindFlags.IndexBuffer, indices);
            mesh.VertexSize   = SharpDX.Utilities.SizeOf <VType>();
            mesh.SubSets.Add(new SharpSubSet()
            {
                DiffuseColor = new Vector4(1, 1, 1, 1),
                IndexCount   = indices.Count()
            });
            return(mesh);
        }