Exemple #1
0
        MMesh processMesh(Mesh mesh, Scene scene, string sName, Matrix4d trans)
        {
            // data to fill
            List <TexturedVertex> vertices = new List <TexturedVertex>();
            List <int>            indices  = new List <int>();
            List <MTexture>       textures = new List <MTexture>();

            // Walk through each of the mesh's vertices
            for (int i = 0; i < mesh.VertexCount; i++)
            {
                TexturedVertex vertex = new TexturedVertex();
                Vector3        vector = new Vector3(); // we declare a placeholder vector since assimp uses its own vector class that doesn't directly convert to glm's vec3 class so we transfer the data to this placeholder glm::vec3 first.
                                                       // positions
                vector.X         = mesh.Vertices[i].X;
                vector.Y         = mesh.Vertices[i].Y;
                vector.Z         = mesh.Vertices[i].Z;
                vertex._position = vector;
                // normals
                if (mesh.HasNormals)
                {
                    vector.X       = mesh.Normals[i].X;
                    vector.Y       = mesh.Normals[i].Y;
                    vector.Z       = mesh.Normals[i].Z;
                    vertex._normal = vector;
                }
                // texture coordinates
                if (mesh.HasTextureCoords(0)) // does the mesh contain texture coordinates?
                {
                    Vector2 vec = new Vector2();
                    // a vertex can contain up to 8 different texture coordinates. We thus make the assumption that we won't
                    // use models where a vertex can have multiple texture coordinates so we always take the first set (0).
                    vec.X = mesh.TextureCoordinateChannels[0][i].X;
                    vec.Y = 1 - mesh.TextureCoordinateChannels[0][i].Y;
                    vertex._textureCoordinate = vec;
                }
                else
                {
                    vertex._textureCoordinate = new Vector2(0.0f, 0.0f);
                }

                if (mesh.HasTangentBasis)
                {
                    // tangent
                    vector.X = mesh.Tangents[i].X;
                    vector.Y = mesh.Tangents[i].Y;
                    vector.Z = mesh.Tangents[i].Z;
                    /// vertex.Tangent = vector;
                    // bitangent
                    vector.X = mesh.BiTangents[i].X;
                    vector.Y = mesh.BiTangents[i].Y;
                    vector.Z = mesh.BiTangents[i].Z;
                    /// vertex.BiTangent = vector;
                }
                vertices.Add(vertex);
            }
            // now wak through each of the mesh's faces (a face is a mesh its triangle) and retrieve the corresponding vertex indices.
            for (int i = 0; i < mesh.FaceCount; i++)
            {
                Face face = mesh.Faces[i];
                // retrieve all indices of the face and store them in the indices vector
                for (int j = 0; j < face.IndexCount; j++)
                {
                    indices.Add(face.Indices[j]);
                }
            }
            // process materials
            Material material = scene.Materials[mesh.MaterialIndex];
            // we assume a convention for sampler names in the shaders. Each diffuse texture should be named
            // as 'texture_diffuseN' where N is a sequential number ranging from 1 to MAX_SAMPLER_NUMBER.
            // Same applies to other texture as the following list summarizes:
            // diffuse: texture_diffuseN
            // specular: texture_specularN
            // normal: texture_normalN

            // 1. diffuse maps

            /*
             * List<MTexture> diffuseMaps = loadMaterialTextures(material, TextureType_DIFFUSE, "texture_diffuse");
             * textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
             * // 2. specular maps
             * List<Texture> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular");
             * textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
             * // 3. normal maps
             * List<Texture> normalMaps = loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_normal");
             * textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
             * // 4. height maps
             * List<Texture> heightMaps = loadMaterialTextures(material, aiTextureType_AMBIENT, "texture_height");
             * textures.insert(textures.end(), heightMaps.begin(), heightMaps.end());
             */

            // return a mesh object created from the extracted mesh data
            MMesh m = new MMesh(sName);

            // trans.Transpose();
            m.transform.Position = trans.ExtractTranslation();
            m.transform.Rotation = trans.ExtractRotation();
            m.transform.Scale    = trans.ExtractScale();
            m.AddMaterial(this.material);
            m.SetupMesh(vertices, indices, textures);
            return(m);
        }