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.Clamp, AddressV = TextureAddressMode.Clamp, AddressW = TextureAddressMode.Clamp, BorderColor = new Color4(0, 0, 0, 0), ComparisonFunction = Comparison.Never, Filter = Filter.MinMagMipLinear, MaximumAnisotropy = 16, MaximumLod = float.MaxValue, MinimumLod = 0, MipLodBias = 0.0f })); }
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 })); }
/// <summary> /// Create vertex with position, normal, color, uv coordinates, and skinning /// </summary> /// <param name="position"></param> /// <param name="normal"></param> /// <param name="color"></param> public Vertex(Vector3 position, Vector3 normal, Color color, Vector2 uv, Common.Mesh.SkinningVertex skin) { Position = position; Normal = normal; Color = color; UV = uv; Skin = skin; }
protected override void CreateDeviceDependentResources() { base.CreateDeviceDependentResources(); // release resources vertexBuffers.ForEach(b => RemoveAndDispose(ref b)); vertexBuffers.Clear(); indexBuffers.ForEach(b => RemoveAndDispose(ref b)); indexBuffers.Clear(); textureViews.ForEach(t => RemoveAndDispose(ref t)); textureViews.Clear(); RemoveAndDispose(ref samplerState); var device = DeviceManager.Direct3DDevice; // Create the vertex buffers for (int i = 0; i < mesh.VertexBuffers.Count; i++) { var vb = mesh.VertexBuffers[i]; Vertex[] vertices = new Vertex[vb.Length]; for (var j = 0; j < vb.Length; j++) { // Retrieve skinning info for vertex Common.Mesh.SkinningVertex skin = new Common.Mesh.SkinningVertex(); if (mesh.SkinningVertexBuffers.Count > 0) { skin = mesh.SkinningVertexBuffers[i][j]; } // create vertex vertices[j] = new Vertex(vb[j].Position, vb[j].Normal, vb[j].Color, vb[j].UV, skin); } vertexBuffers.Add(ToDispose(Buffer.Create(device, BindFlags.VertexBuffer, vertices.ToArray()))); vertexBuffers[vertexBuffers.Count - 1].DebugName = "VertexBuffer_" + vertexBuffers.Count.ToString(); } // Create the 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(); } // Create the textures resources views // The CMO file format supports up to 8 per material foreach (var m in mesh.Materials) { // Diffuse color for (var i = 0; i < m.Textures.Length; i++) { textureViews.Add(SharpDX.IO.NativeFile.Exists(m.Textures[i]) ? ToDispose(ShaderResourceView.FromFile(device, m.Textures[i])) : null); } } // Create the sampler state samplerState = ToDispose(new SamplerState(device, new SamplerStateDescription { AddressU = TextureAddressMode.Clamp, AddressV = TextureAddressMode.Clamp, AddressW = TextureAddressMode.Clamp, BorderColor = Color.Black, ComparisonFunction = Comparison.Never, Filter = Filter.MinMagMipLinear, MaximumAnisotropy = 16, MaximumLod = float.MaxValue, MinimumLod = 0, MipLodBias = 0.0f })); }