OpenGL ES教程——模型加载

198 阅读6分钟

image.png

到目前为止,我们写的所有demo都是绘制一些基础图片,如三角形,就算是绘制立方体,其本质上也是在绘制三角形,如果要绘制复杂的图形怎么办?

OpenGL 绘制任何图形的流程都是

  1. 处理顶点
  2. 处理纹理
  3. 处理光照、材质等
  4. 绘制基础图片

如果我要绘制一个立体的地球,该怎么绘制呢?

可能会遇到的问题有:顶点非常多,该选用什么基础图片,纹理顶点怎么处理等等,如果还按照之前的老方式,我们真的可能无法绘制,那有什么办法能解决它吗

本章就是来讲如何绘制复杂对象的:模型加载

1、我对模型的理解

我需要的模型能解决什么问题呢?

  • 模型中包含大量顶点,不需要我手动设置
  • 模型中包含纹理以及纹理坐标,不需要我手动指定
  • 模型中包含大量各种参数,如光照、材质等等,不用我手动设置
  • 模型中可以用基础图形绘制出来,比如三角形

如果模型相当于一个大的数据源,包含以上所有数据,开发只需要解析这个模型,然后设置这些数据,那是不是就非常方便了。模型就是这个作用。

2、Assimp

Assimp,非常出名的模型加载库。它能加载各种模型,然后把模型保存到Assimp的通用数据结构中。理解这个数据结构至关重要

当使用Assimp导入一个模型的时候,它通常会将整个模型加载进一个场景(Scene)对象,它会包含导入的模型/场景中的所有数据。Assimp会将场景载入为一系列的节点(Node),每个节点包含了场景对象中所储存数据的索引,每个节点都可以有任意数量的子节点。Assimp数据结构的(简化)模型如下

image.png

注意:scene中其实已经包含了所有的数据,它的子节点中包含的mesh数据,其实只是scene里mesh数组的索引

注意mesh里包含的数据:

  • vertices:顶点数据
  • normals:法线
  • texturecoods:纹理坐标
  • faces:其实就是indices,其实是ebo数据,绘制索引
  • materialindex:纹理索引,纹理保存在scene里的materials数据中

从上图可知,模型中包含这些数据,那我们解析模型,把数据保存起来,绘制时设置各种参数,是不是就ok了

3、mesh

一个模型中只有一个scene,scene中会包含大量的mesh,我们需要定义mesh的数据结构,绘制方法,最后整个模型的绘制就是遍历所有的mesh即可


struct Vertex {
    glm::vec3 Position;
    glm::vec3 Normal;
    glm::vec2 TexCoords;
    glm::vec3 Tangent;
    glm::vec3 Bitangent;
    int m_BoneIDs[MAX_BONE_INFLUENCE];
    float m_Weights[MAX_BONE_INFLUENCE];
};

struct TextureInfo {
    unsigned int id;
    std::string type;
    std::string path;
};

class Mesh {
public:
    std::vector<Vertex> vertices;
    std::vector<unsigned int> indices;
    std::vector<TextureInfo> textures;
    unsigned int vao;

    Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices, std::vector<TextureInfo> textures) {
        this->vertices = vertices;
        this->indices = indices;
        this->textures = textures;
        setupMesh();
    }

    void Draw(Shader& shader) {
        unsigned int diffuseNr = 1;
        unsigned int specularNr = 1;
        unsigned int normalNr   = 1;
        unsigned int heightNr   = 1;
        for(unsigned int i = 0; i < textures.size(); i++) {
            glActiveTexture(GL_TEXTURE0 + i);
            std::string number;
            std::string name = textures[i].type;
            if (name == "texture_diffuse") {
                number = std::to_string(diffuseNr++);
            } else if(name == "texture_specular") {
                number = std::to_string(specularNr++); // transfer unsigned int to string
            } else if(name == "texture_normal") {
                number = std::to_string(normalNr++); // transfer unsigned int to string
            } else if(name == "texture_height") {
                number = std::to_string(heightNr++);
            }
            glUniform1i(glGetUniformLocation(shader.ID, (name+ number).c_str()), i);
            glBindTexture(GL_TEXTURE_2D, textures[i].id);
        }
        glBindVertexArray(vao);
        glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(indices.size()), GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);

        glActiveTexture(GL_TEXTURE0);
    }

private:
    unsigned int vbo, ebo;

    void setupMesh() {
        glGenVertexArrays(1, &vao);
        glGenBuffers(1, &vbo);
        glGenBuffers(1, &ebo);

        glBindVertexArray(vao);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);

        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
        glEnableVertexAttribArray(1);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
        // vertex texture coords
        glEnableVertexAttribArray(2);
        glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, TexCoords));
        // vertex tangent
        glEnableVertexAttribArray(3);
        glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Tangent));
        // vertex bitangent
        glEnableVertexAttribArray(4);
        glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Bitangent));
        // ids
        glEnableVertexAttribArray(5);
        glVertexAttribIPointer(5, 4, GL_INT, sizeof(Vertex), (void*)offsetof(Vertex, m_BoneIDs));

        // weights
        glEnableVertexAttribArray(6);
        glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, m_Weights));
        glBindVertexArray(0);
    }
};


mesh的类结构应如上所示,有构建、绘制。

  • setupMesh,设置各种顶点数据
  • draw,设置纹理,设置各种uniform数据,绘制基础图形(三角形)

这里只有一点不太好理解,即这段代码:


for(unsigned int i = 0; i < textures.size(); i++) {
    glActiveTexture(GL_TEXTURE0 + i);
    std::string number;
    std::string name = textures[i].type;
    if (name == "texture_diffuse") {
        number = std::to_string(diffuseNr++);
    } else if(name == "texture_specular") {
        number = std::to_string(specularNr++); // transfer unsigned int to string
    } else if(name == "texture_normal") {
        number = std::to_string(normalNr++); // transfer unsigned int to string
    } else if(name == "texture_height") {
        number = std::to_string(heightNr++);
    }
    LOGI("name = %s", (name+ number).c_str());
    glUniform1i(glGetUniformLocation(shader.ID, (name+ number).c_str()), i);
    glBindTexture(GL_TEXTURE_2D, textures[i].id);
}

模型中有很多纹理,纹理肯定是uniform类型数据,设置uniform类型数据时,需要指定它在着色器代码中的变量名。开发肯定不知道模型中有多少个纹理,那么也不知道要在着色器代码中定义多少个变量,名字怎么定义?

模型中纹理有好几种类型,即:

  • texture_diffuse
  • texture_specular
  • texture_normal
  • texture_height

默认认为uniform变量名为这些类型名后加数字,这样方便开发

4、model

model中要做的事情,其实只有3个了,如何加载模型、解析模型、遍历mesh绘制模型

unsigned int TextureFromFile(const char *path, const string &directory, bool gamma = false);

class Model {
public:
    vector<TextureInfo> textures_loaded;
    vector<Mesh> meshes;
    string directory;
    bool gammaCorrection;

    Model(){}

    Model(string const& path, bool gamma = false): gammaCorrection(gamma) {
        loadModel(path);
    }

    void Draw(Shader& shader) {
        for (unsigned int i = 0; i < meshes.size(); ++i) {
            meshes[i].Draw(shader);
        }
    }

private:
    void loadModel(string const& path) {
        Assimp::Importer importer;
        const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate
            | aiProcess_GenSmoothNormals
            | aiProcess_FlipUVs
            | aiProcess_CalcTangentSpace);
        if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
            LOGI("ERROR::ASSIMP:: %s", importer.GetErrorString());
            return;
        }
        // retrieve the directory path of the filepath
        directory = path.substr(0, path.find_last_of('/'));
        LOGI("directory:: %s", directory.c_str());
        processNode(scene->mRootNode, scene);
    }

    void processNode(aiNode *node, const aiScene *scene) {
        for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
            aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
            meshes.push_back(processMesh(mesh, scene));
        }
        for (int i = 0; i < node->mNumChildren; ++i) {
            processNode(node->mChildren[i], scene);
        }
    }

    Mesh processMesh(aiMesh *mesh, const aiScene *scene) {
        vector<Vertex> vertices;
        vector<unsigned int> indices;
        vector<TextureInfo> textures;
        for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
            Vertex vertex;
            glm::vec3 vector;
            vector.x = mesh->mVertices[i].x;
            vector.y = mesh->mVertices[i].y;
            vector.z = mesh->mVertices[i].z;
            vertex.Position = vector;

            if (mesh->HasNormals())
            {
                vector.x = mesh->mNormals[i].x;
                vector.y = mesh->mNormals[i].y;
                vector.z = mesh->mNormals[i].z;
                vertex.Normal = vector;
            }

            if(mesh->mTextureCoords[0]) // does the mesh contain texture coordinates?
            {
                glm::vec2 vec;
                // 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->mTextureCoords[0][i].x;
                vec.y = mesh->mTextureCoords[0][i].y;
                vertex.TexCoords = vec;
                // tangent
                vector.x = mesh->mTangents[i].x;
                vector.y = mesh->mTangents[i].y;
                vector.z = mesh->mTangents[i].z;
                vertex.Tangent = vector;
                // bitangent
                vector.x = mesh->mBitangents[i].x;
                vector.y = mesh->mBitangents[i].y;
                vector.z = mesh->mBitangents[i].z;
                vertex.Bitangent = vector;
            } else {
                vertex.TexCoords = glm::vec2(0.0f, 0.0f);
            }
            vertices.push_back(vertex);
        }

        // now wak through each of the mesh's faces (a face is a mesh its triangle) and retrieve the corresponding vertex indices.
        for(unsigned int i = 0; i < mesh->mNumFaces; i++)
        {
            aiFace face = mesh->mFaces[i];
            // retrieve all indices of the face and store them in the indices vector
            for(unsigned int j = 0; j < face.mNumIndices; j++) {
                indices.push_back(face.mIndices[j]);
            }
        }

        aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
        vector<TextureInfo> diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse");
        textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
        // 2. specular maps
        vector<TextureInfo> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular");
        textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
        // 3. normal maps
        std::vector<TextureInfo> normalMaps = loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_normal");
        textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
        // 4. height maps
        std::vector<TextureInfo> 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
        return Mesh(vertices, indices, textures);
    }

    vector<TextureInfo> loadMaterialTextures(aiMaterial *mat, aiTextureType type, string typeName) {
        vector<TextureInfo> textures;
        for (int i = 0; i < mat->GetTextureCount(type); ++i) {
            aiString str;
            mat->GetTexture(type, i, &str);
            bool skip = false;
            for (int j = 0; j < textures_loaded.size(); ++j) {
                if (std::strcmp(textures_loaded[j].path.data(), str.C_Str()) == 0) {
                    textures.push_back(textures_loaded[j]);
                    skip = true;
                    break;
                }
            }
            if (!skip) {
                TextureInfo textureInfo;
                textureInfo.id = TextureFromFile(str.C_Str(), directory);
                textureInfo.type = typeName;
                textureInfo.path = str.C_Str();
                textures.push_back(textureInfo);
                textures_loaded.push_back(textureInfo);
            }
        }
        return textures;
    }

    unsigned int TextureFromFile(const char* path, const string& directory, bool gamma = false) {
        string filename =string(path);
        filename = directory + '/' + filename;
        unsigned int textureID;
        glGenTextures(1, &textureID);

        LOGI("TextureFromFile path = %s", filename.c_str());
        cv::Mat textureImage = cv::imread(filename);
        if (!textureImage.empty()) {
            cv::cvtColor(textureImage, textureImage, CV_BGR2RGB);
            glBindTexture(GL_TEXTURE_2D, textureID);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureImage.cols,
                         textureImage.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, textureImage.data);
            glGenerateMipmap(GL_TEXTURE_2D);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        } else {
            LOGI("TextureFromFile cv read is empty");
        }
        return textureID;
    }
};

这部分代码如上,没什么理解难度,唯一有一点:解析出一个纹理了,会得到这个纹理的路径,如何从一个路径中得到纹理呢?

之前我们获取纹理的方式,是通过java端的反射,拿到bitmap的buf,设置纹理buf,现在这种方式有点不太方便了,有没有更方便的方式呢?有的,用opencv,opencv是非常出名的图片处理框架,在人工智能这方面用得尤其多,用它来解析图片就不用绕java那一层了,方便很多。

5、总结

理解模型,最重要的一点就是把模型当成一个大的数据buf,解析再设置即可,虽然代码看起来很多,但其实是个纸老虎,都是之前学习过的