public void CreateDeviceDependentResources(DeviceResources resources, Mesh3D mesh) { var vertices = mesh.Vertices.ToArray(); var vertexBufferDesc = D3D11BufferDesc.From(vertices, D3D11BindOptions.VertexBuffer); this.vertexBuffer = resources.D3DDevice.CreateBuffer(vertexBufferDesc, vertices, 0, 0); var indices = mesh.Indices.ToArray(); var indexBufferDesc = D3D11BufferDesc.From(indices, D3D11BindOptions.IndexBuffer); this.indexBuffer = resources.D3DDevice.CreateBuffer(indexBufferDesc, indices, 0, 0); this.IndicesCount = (uint)indices.Length; this.Texture = mesh.Texture; this.HasAlpha = mesh.HasAlpha; }
public OptMesh(DeviceResources deviceResources, Mesh3D mesh) { this.CreateDeviceDependentResources(deviceResources, mesh); }
private void CreateMeshes(OptFile opt) { var meshes3D = new List <Mesh3D>(); foreach (var optMesh in opt.Meshes) { var positions = optMesh .Vertices .Select(t => new XMFloat3(-t.X * OptFile.ScaleFactor, t.Z * OptFile.ScaleFactor, -t.Y * OptFile.ScaleFactor)) .ToList(); var normals = optMesh .VertexNormals .Select(t => new XMFloat3(t.X, t.Z, t.Y)) .ToList(); var textureCoordinates = optMesh .TextureCoordinates .Select(t => new XMFloat2(t.U, -t.V)) .ToList(); var optLod = optMesh.Lods.FirstOrDefault(); if (optLod == null) { continue; } foreach (var optFaceGroup in optLod.FaceGroups) { var mesh3D = new Mesh3D(); meshes3D.Add(mesh3D); var optTextureName = optFaceGroup.Textures[0]; mesh3D.Texture = optTextureName; opt.Textures.TryGetValue(optTextureName, out Texture texture); mesh3D.HasAlpha = texture == null ? false : texture.HasAlpha; ushort index = 0; foreach (var optFace in optFaceGroup.Faces) { Index positionsIndex = optFace.VerticesIndex; Index normalsIndex = optFace.VertexNormalsIndex; Index textureCoordinatesIndex = optFace.TextureCoordinatesIndex; BasicVertex vertex = new BasicVertex(); vertex.Position = positions.ElementAtOrDefault(positionsIndex.C); vertex.Normal = normals.ElementAtOrDefault(normalsIndex.C); vertex.TextureCoordinates = textureCoordinates.ElementAtOrDefault(textureCoordinatesIndex.C); mesh3D.Vertices.Add(vertex); mesh3D.Indices.Add(index); index++; vertex.Position = positions.ElementAtOrDefault(positionsIndex.B); vertex.Normal = normals.ElementAtOrDefault(normalsIndex.B); vertex.TextureCoordinates = textureCoordinates.ElementAtOrDefault(textureCoordinatesIndex.B); mesh3D.Vertices.Add(vertex); mesh3D.Indices.Add(index); index++; vertex.Position = positions.ElementAtOrDefault(positionsIndex.A); vertex.Normal = normals.ElementAtOrDefault(normalsIndex.A); vertex.TextureCoordinates = textureCoordinates.ElementAtOrDefault(textureCoordinatesIndex.A); mesh3D.Vertices.Add(vertex); mesh3D.Indices.Add(index); index++; if (positionsIndex.D >= 0) { mesh3D.Indices.Add((ushort)(index - 3U)); mesh3D.Indices.Add((ushort)(index - 1U)); vertex.Position = positions.ElementAtOrDefault(positionsIndex.D); vertex.Normal = normals.ElementAtOrDefault(normalsIndex.D); vertex.TextureCoordinates = textureCoordinates.ElementAtOrDefault(textureCoordinatesIndex.D); mesh3D.Vertices.Add(vertex); mesh3D.Indices.Add(index); index++; } } } } foreach (var mesh3d in meshes3D) { var mesh = new OptMesh(this.deviceResources, mesh3d); this.meshes.Add(mesh); } }