• 用C++或者Python解析gltf文件


    gltf是类似于stl、obj、ply等常见的3D对象存储格式,它被设计出来是为了便于渲染的数据转换和传输。如果你的浏览器可以连接外网,可以通过 glTF Viewer 网址打开浏览gltf的3D对象。这里介绍两种语言下从gltf拿到网格的顶点和面片数据。

    一、Python

    第一步安装pygltflib:

    pip install pygltflib

    第二步,python解析:

    1. import pygltflib
    2. import numpy as np
    3. pathGltf="test.gltf"
    4. gltf=pygltflib.GLTF2().load(pathGltf)
    5. scene=gltf.scenes[gltf.scene]
    6. nodes=[gltf.nodes[node] for node in scenes.nodes]
    7. vertices=np.arry([node.mesh.primitives[0].attributes["POSITION"] for node in nodes])
    8. print(vertices)

    不知道为什么,通过上面这种方式解析,node.mesh我这里是一个int类型的值,运行代码提示node.mesh没有primitives属性,然后在网上找了下面的代码是ok的:

    1. import pygltflib
    2. import pathlib
    3. import struct
    4. # load a gltf file
    5. fname = pathlib.Path("C:/Users/User/Desktop/cube.gltf")
    6. gltf = GLTF2().load(fname)
    7. # get the first mesh in the current scene
    8. mesh = gltf.meshes[gltf.scenes[gltf.scene].nodes[0]-1]
    9. # get the vertices for each primitive in the mesh
    10. for primitive in mesh.primitives:
    11. # get the binary data for this mesh primitive from the buffer
    12. accessor = gltf.accessors[primitive.attributes.POSITION]
    13. bufferView = gltf.bufferViews[accessor.bufferView]
    14. buffer = gltf.buffers[bufferView.buffer]
    15. data = gltf.get_data_from_buffer_uri(buffer.uri)
    16. # pull each vertex from the binary buffer and convert it into a tuple of python floats
    17. vertices = []
    18. for i in range(accessor.count):
    19. index = bufferView.byteOffset + accessor.byteOffset + i*12 # the location in the buffer of this vertex
    20. d = data[index:index+12] # the vertex data
    21. v = struct.unpack(", d) # convert from base64 to three floats
    22. vertices.append(v)
    23. # unpack floats
    24. vertices2 = []
    25. for a,b,c in vertices:
    26. vertices2 += [a,b,c]
    27. # create triangles
    28. vertices = vertices2
    29. triangles = []
    30. for i in range(0,len(vertices),9):
    31. triangles.append(vertices[i:i+9])
    32. # print data
    33. print(triangles)

    二、C++解析

    c++依赖的库主要是draco,这个库是开源的,网上可以下载,有了draco之后代码如下:

    1. #include
    2. #include
    3. // 读取gltf文件
    4. bool parse_gltf_from_file(const std::string& filename,std::unique_ptr& mesh){
    5. draco::GltfDecoder gltfDec;
    6. draco::StatusOr> stormesh=gltfDec.DecodeFromFile(filename);
    7. if(!stormesh.ok()){
    8. return false;
    9. }
    10. std::unique_ptr pDracomesh=std::move(stormesh).value();
    11. std::cout<<"faces num:"<num_faces()<
    12. pDracomesh.swap(mesh);
    13. return true;
    14. }
    15. //解析出顶点和面片数据
    16. bool get_faces_vertexes(const std::unique_ptr& dracomesh,
    17. std::vector& vertexes,
    18. std::vector& faces){
    19. auto dump_attribute_to_vec3=[](const draco::PointAttribute& att,std::vector& attD){
    20. if(att.size()==0) return;
    21. std::vector tmp(att.size());
    22. for(int i=0;isize();++i){
    23. if(!att.ConvertValue<float,3>(draco::AttributeValueIndex(i),&tmp[i][0])) return;
    24. }
    25. attD=std::move(tmp);
    26. }
    27. // 解析顶点
    28. const draco::PointAttribute* posAtt=nullptr;
    29. std::vector points;
    30. for(int i=0;inum_attributes();++i){
    31. const draco::PointAttribute* pAtt=dracomesh->attribute(i);
    32. switch(pAtt->attribute_type()){
    33. case draco::PointAttribute::POSITION:
    34. posAtt=pAtt;
    35. dump_attribute_to_vec3(*pAtt,points);
    36. break;
    37. }
    38. }
    39. vertexes=points;
    40. // 解析面片
    41. faces.resize(dracomesh->num_faces());
    42. for(int i=0;inum_faces();++i){
    43. for(int j=0;j<3;++j){
    44. const draco::PointIndex idx=dracomesh->face(draco::FaceIndex(i))[j];
    45. faces[i][j]=posAtt->mapped_index(idx).value();
    46. }
    47. }
    48. return true;
    49. }
  • 相关阅读:
    为了 Vue 组件测试,你需要为每个事件绑定的方法加上括号吗?
    关于虚拟机中IPI中断的思考
    Python学习之CSDN21天学习挑战赛计划之10
    数学建模--模型总结(5)
    C#编程学习
    设计模式之适配器与装饰器
    MATLAB中M文件编写
    mysql表的导出和导入
    nginx转发规则
    RF和SVM的特点
  • 原文地址:https://blog.csdn.net/Hunter_pcx/article/details/132903557