示例#1
0
        protected override void CreateDeviceDependentResources()
        {
            // Dispose of each vertex and index buffer
            vertexBuffers.ForEach(vb => RemoveAndDispose(ref vb));
            vertexBuffers.Clear();
            indexBuffers.ForEach(ib => RemoveAndDispose(ref ib));
            indexBuffers.Clear();
            textureViews.ForEach(tv => RemoveAndDispose(ref tv));
            textureViews.Clear();
            RemoveAndDispose(ref samplerState);

            // Retrieve our SharpDX.Direct3D11.Device1 instance
            var device = this.DeviceManager.Direct3DDevice;

            // Initialize vertex buffers
            for (int indx = 0; indx < mesh.VertexBuffers.Count; indx++)
            {
                var      vb       = mesh.VertexBuffers[indx];
                Vertex[] vertices = new Vertex[vb.Length];
                for (var i = 0; i < vb.Length; i++)
                {
                    // Retrieve skinning information for vertex
                    Common.Mesh.SkinningVertex skin = new Common.Mesh.SkinningVertex();
                    if (mesh.SkinningVertexBuffers.Count > 0)
                    {
                        skin = mesh.SkinningVertexBuffers[indx][i];
                    }

                    // Create vertex
                    vertices[i] = new Vertex(vb[i].Position, vb[i].Normal, vb[i].Color, vb[i].UV, skin);
                }

                vertexBuffers.Add(ToDispose(Buffer.Create(device, BindFlags.VertexBuffer, vertices.ToArray())));
                vertexBuffers[vertexBuffers.Count - 1].DebugName = "VertexBuffer_" + indx.ToString();
            }

            // Initialize index buffers
            foreach (var ib in mesh.IndexBuffers)
            {
                indexBuffers.Add(ToDispose(Buffer.Create(device, BindFlags.IndexBuffer, ib)));
                indexBuffers[indexBuffers.Count - 1].DebugName = "IndexBuffer_" + (indexBuffers.Count - 1).ToString();
            }

            // Load textures if a material has any.
            foreach (var m in mesh.Materials)
            {
                for (var i = 0; i < m.Textures.Length; i++)
                {
                    if (System.IO.File.Exists(m.Textures[i]))
                    {
                        textureViews.Add(ToDispose(ShaderResourceView.FromFile(device, m.Textures[i])));
                    }
                    else
                    {
                        textureViews.Add(null);
                    }
                }
            }

            // Create our sampler state
            samplerState = ToDispose(new SamplerState(device, new SamplerStateDescription()
            {
                AddressU           = TextureAddressMode.Wrap,
                AddressV           = TextureAddressMode.Wrap,
                AddressW           = TextureAddressMode.Wrap,
                BorderColor        = new Color4(0, 0, 0, 0),
                ComparisonFunction = Comparison.Never,
                Filter             = Filter.Anisotropic,
                MaximumAnisotropy  = 16,
                MaximumLod         = float.MaxValue,
                MinimumLod         = 0,
                MipLodBias         = 0.0f
            }));
        }
示例#2
0
        protected override void CreateDeviceDependentResources()
        {
            base.CreateDeviceDependentResources();
            // release resources
            vertexBuffers.ForEach(b => RemoveAndDispose(ref b));
            vertexBuffers.Clear();
            indexBuffers.ForEach(b => RemoveAndDispose(ref b));
            indexBuffers.Clear();

            RemoveAndDispose(ref frontState);

            var device = DeviceManager.Direct3DDevice;

            // Create the vertex buffers
            Vertex[] verts = new Vertex[faces.Count];

            for (int i = 0; i < faces.Count; i++)
            {
                // create vertex
                var f = faces[i];
                verts[i] = new Vertex(new Vector3(vertices[(int)f.PosId - 1]), new Vector3(normals[(int)f.NormId - 1]), Color.Gray);
                indices.Add((ushort)f.PosId);
            }
            vertexBuffers.Add(ToDispose(Buffer.Create(device, BindFlags.VertexBuffer, verts.ToArray())));
            vertexBinding_ = new VertexBufferBinding(vertexBuffers.First(), Utilities.SizeOf <Vertex>(), 0);

            // Create the index buffers
            indexBuffers.Add(ToDispose(Buffer.Create(device, BindFlags.IndexBuffer, indices.ToArray())));

            frontState = ToDispose(new RasterizerState(DeviceManager.Direct3DDevice, new RasterizerStateDescription
            {
                FillMode = FillMode.Solid,
                CullMode = CullMode.Front,
                IsFrontCounterClockwise = false
            }));

            var max    = verts.Max().Position;
            var min    = verts.Min().Position;
            var center = (max - min) * .5f;

            meshExtent = new Mesh.MeshExtent
            {
                Min    = min,
                Max    = max,
                Radius = 0.5f,
                Center = center
            };
        }