Assimp库支持导入的3D文件格式

Assimp库支持导入的3D文件格式

采用assimp库,用函数的方法打印出可以导入的模型格式,非常具有可信性,我看有些博主写的根本不对,这个库不支持导入fbx格式文件。
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Assimp 是一个开源的模型导入,它支持多种 3D 模型格式,包括 PLY 格式。下面是一个简单的示例,演示了如何使用 Assimp 读取 PLY 文件并将其渲染到 OpenGL 中: ```cpp #include <iostream> #include <vector> #include <GL/glew.h> #include <GLFW/glfw3.h> #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/type_ptr.hpp> #include <assimp/Importer.hpp> #include <assimp/scene.h> #include <assimp/postprocess.h> using namespace std; vector<glm::vec3> vertices; void loadModel(const char* path) { Assimp::Importer importer; const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs); if (!scene || scene->mFlags &amp; AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) { cerr << "Error loading model: " << importer.GetErrorString() << endl; return; } aiMesh* mesh = scene->mMeshes[0]; for (unsigned int i = 0; i < mesh->mNumVertices; i++) { aiVector3D vertex = mesh->mVertices[i]; vertices.push_back(glm::vec3(vertex.x, vertex.y, vertex.z)); } } int main() { if (!glfwInit()) { cerr << "Error initializing GLFW" << endl; return -1; } GLFWwindow* window = glfwCreateWindow(800, 600, "Assimp PLY Demo", nullptr, nullptr); if (!window) { cerr << "Error creating window" << endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glfwSwapInterval(1); if (glewInit() != GLEW_OK) { cerr << "Error initializing GLEW" << endl; glfwTerminate(); return -1; } loadModel("model.ply"); GLuint vao, vbo; glGenVertexArrays(1, &amp;vao); glGenBuffers(1, &amp;vbo); glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void*)0); glEnableVertexAttribArray(0); glBindVertexArray(0); while (!glfwWindowShouldClose(window)) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -5.0f); glBindVertexArray(vao); glDrawArrays(GL_TRIANGLES, 0, vertices.size()); glBindVertexArray(0); glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); return 0; } ``` 这个示例中,我们使用 Assimp 中的 `Assimp::Importer` 类读取 PLY 文件,并将其转换为 OpenGL 可以使用的格式。然后,我们使用 OpenGL 将顶点数据渲染到屏幕上。 需要注意的是,Assimp 可以读取多个模型格式,所以你需要在导入模型之前检查文件扩展名,以确保你正在读取正确的文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值