如何访问Assimp模型数据以进行碰撞
How to access assimp model data for collisions?
这似乎是一个简单的任务,但是我似乎无法弄清楚如何使用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使用的中间格式已优化用于渲染。
存储碰撞信息的好策略在很大程度上取决于您的模型。对我来说,它为整个模型和子盒中存储的每个网格盒中存储的局部边界框生成了本地边界框。因此,以下方法可能有效:
- 检查与场景的框是您的三角形与此框相撞
- 如果是这种情况:
- 检查所有节点边界框
- 如果您能够检测到碰撞:检查网格中此节点的所有三角形以找到正确的一个
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- 数据成员SFINAE的C++17测试:gcc vs clang
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何对点云数据进行排序
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 如何访问Assimp模型数据以进行碰撞
- 使用C 中的单元格 /图块 /网格进行碰撞检测的最佳数据结构