示例#1
0
        public Model(MeshVertex[] vertices, uint[] indices = null, MeshMaterial material = default, Matrix4 state = default, string name = "", RenderFlags renderFlags = RenderFlags.Solid)
        {
            Meshes.Add(new Mesh(name, vertices, indices ?? new uint[0], new MeshTexture[0], material));  // TODO: perhaps replace empty array with nulls?

            State = state == default ? Matrix4.Identity : state;

            RenderFlags = renderFlags;
        }
示例#2
0
        public Mesh(string name, MeshVertex[] vertices, uint[] indices, MeshTexture[] textures, MeshMaterial material)
        {
            Name     = name;
            Vertices = vertices;
            Indices  = indices;
            Textures = textures;
            Material = material;

            // setup can be done only on the main thread, holding the GL context
            if (Thread.CurrentThread == MainWindow.MainThread)
            {
                SetupMesh();
            }
            else
            {
                // send necessary actions to dispatcher
                Dispatcher.RenderActions.Enqueue(() =>
                {
                    SetupMesh();
                });
            }
        }
示例#3
0
        private Mesh ProcessMesh(Assimp.Mesh mesh, Scene scene)
        {
            var vertices = new List <MeshVertex>();
            var indices  = new List <uint>();
            var textures = new List <MeshTexture>();
            var color    = new MeshMaterial();

            for (int i = 0; i < mesh.VertexCount; i++)
            {
                var vertex = new MeshVertex();

                // process vertex positions, normals and texture coordinates
                var pos = mesh.Vertices[i];
                vertex.Position = new Vector3(pos.X, pos.Y, pos.Z);

                var norm = mesh.Normals[i];
                vertex.Normal = new Vector3(norm.X, norm.Y, norm.Z);

                if (mesh.HasTextureCoords(0))  // does the mesh contain texture coordinates?
                {
                    var tex = mesh.TextureCoordinateChannels[0][i];
                    vertex.TexCoords = new Vector2(tex.X, tex.Y);
                }

                vertices.Add(vertex);
            }

            // process indices
            for (int i = 0; i < mesh.FaceCount; i++)
            {
                Face face = mesh.Faces[i];
                for (int j = 0; j < face.IndexCount; j++)
                {
                    indices.Add((uint)face.Indices[j]);
                }
            }

            // process material
            if (mesh.MaterialIndex >= 0)
            {
                Material material = scene.Materials[mesh.MaterialIndex];

                // get all the needed material textures
                if (material.HasTextureDiffuse)
                {
                    List <MeshTexture> diffuseMaps = LoadMaterialTextures(material, TextureType.Diffuse, "texture_diffuse");
                    textures.AddRange(diffuseMaps);
                }
                if (material.HasTextureSpecular)
                {
                    List <MeshTexture> specularMaps = LoadMaterialTextures(material, TextureType.Specular, "texture_specular");
                    textures.AddRange(specularMaps);
                }

                // get all the needed material colors (default values if they're not presented)
                color.Ambient = new Vector4(
                    material.ColorAmbient.R,
                    material.ColorAmbient.G,
                    material.ColorAmbient.B,
                    material.ColorAmbient.A);
                color.Diffuse = new Vector4(
                    material.ColorDiffuse.R,
                    material.ColorDiffuse.G,
                    material.ColorDiffuse.B,
                    material.ColorDiffuse.A);
                color.Specular = new Vector4(
                    material.ColorSpecular.R,
                    material.ColorSpecular.G,
                    material.ColorSpecular.B,
                    material.ColorSpecular.A);
                color.Shininess = material.Shininess;
            }

            return(new Mesh(mesh.Name, vertices.ToArray(), indices.ToArray(), textures.ToArray(), color));
        }