如何访问Assimp模型数据以进行碰撞

How to access assimp model data for collisions?

本文关键字:数据 碰撞 模型 Assimp 何访问 访问      更新时间:2023-10-16

这似乎是一个简单的任务,但是我似乎无法弄清楚如何使用Assimp中导入的数据来测试三角碰撞。我已经确定我的三角碰撞算法效果很好,并将顶点和索引缓冲到OpenGL EBO和VBO中,以使绘图完美地工作。我被认为是我从std :: vector中访问数据和索引向量不正确的方式的方式。目前,我正在使用这些指示为顶点向量的索引。

 void loadModel(std::string path) {
        Assimp::Importer importer;
        const aiScene * scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_JoinIdenticalVertices);
        if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
            printf_s("Assimp loading error n%sn", importer.GetErrorString());
            return;
        }
        directory = path.substr(0, path.find_last_of('/'));
        processNode(scene->mRootNode, scene);
    }
    void processNode(aiNode * node, const aiScene * scene) {
        for (unsigned int i = 0; i < node->mNumMeshes; i++) {
            //processes all the nodes meshes
            aiMesh * mesh = scene->mMeshes[node->mMeshes[i]];
            meshes.push_back(processMesh(mesh, scene));
        }
        for (unsigned int i = 0; i < node->mNumChildren; i++) {
            processNode(node->mChildren[i], scene);
        }
    }
    Mesh processMesh(aiMesh * mesh, const aiScene * scene) {
        std::vector<Vertex> vertices;
        std::vector<unsigned int> indices;
        std::vector<Texture> 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;
            //get other vertex information
            vertices.push_back(vertex);
            //for all vertices in the mesh, adds the data to a vector
        }
        for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
            aiFace face = mesh->mFaces[i];
            if (face.mNumIndices == 3) {
                indices.push_back(face.mIndices[0]);                    
                indices.push_back(face.mIndices[1]);
                indices.push_back(face.mIndices[2]);
                //for all the indices in each face, add each indice
            }
            else {
                printf("Odd mNumIndices n");
                //added as a just in case - but in my situation this case is never executed -> all faces are triangles
                for (unsigned int j = 0; j < face.mNumIndices; j++) {
                    indices.push_back(face.mIndices[j]);
                }
            }
        }

现在,要访问此数据,我只是简单地通过模型的所有网格迭代,对于网格的每个INDICE,我都可以访问其相应的顶点。

bool collision(glm::mat4 worldSpaceTransform, glm::vec3 testVector) {
        for (Mesh mesh : meshes) {
            for (int i = 0; i < mesh.indices.size(); i += 3) {
                //iterate through all faces of the mesh since each face has 3 vertices
                glm::vec3 a = glm::vec3(worldSpaceTransform * glm::vec4(mesh.verticies[mesh.indices[i]].position, 1));
                glm::vec3 b = glm::vec3(worldSpaceTransform * glm::vec4(mesh.verticies[mesh.indices[i + 1]].position, 1));
                glm::vec3 c = glm::vec3(worldSpaceTransform * glm::vec4(mesh.verticies[mesh.indices[i + 2]].position, 1)); 
               //use vector a, b and c (transformed to world space) for collision test with the test vector
               //return true if the test vector collides with the triangle
            }
        }
        return false;
    }

因此,我已经使用打印语句来输出应成为三角形的向量A B和C的坐标。在一种情况下,我找不到模型的RAW .OBJ文件中的这些确切的向量,我找到了它们的X,Y和Z坐标,但在一个向量中没有在一起(是的,当我检查此矢量时,我打印了本地空间坐标。(。在另一个中,应该制造三角形的三个向量最终形成了一条线(三个向量中的两个具有相同的坐标(。另外,我知道用模型的所有原始素对向量进行测试效率低下,但是现在,我专注于在查看优化之前获得的工作。对于AABB而言,许多模型太复杂了,因此这是我提出的"最佳"解决方案。所以,我不确定我在这里做错了什么,任何提示都非常感谢!

obj-format仅一次存储每个顶点,并将其引用在面上。因此,Assimp将从这些位置中产生渲染本身的顶点。这就是您无法以OBJ将其存储在格式中的形式的进口OBJ文件中的原始信息的原因。OBJ是针对尺寸进行了优化的,Assimp使用的中间格式已优化用于渲染。

存储碰撞信息的好策略在很大程度上取决于您的模型。对我来说,它为整个模型和子盒中存储的每个网格盒中存储的局部边界框生成了本地边界框。因此,以下方法可能有效:

  • 检查与场景的框是您的三角形与此框相撞
  • 如果是这种情况:
    • 检查所有节点边界框
    • 如果您能够检测到碰撞:检查网格中此节点的所有三角形以找到正确的一个