最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用Python和C++實(shí)現(xiàn)解析gltf文件

 更新時間:2023年09月17日 15:43:41   作者:Hunter_pcx  
gltf是類似于stl、obj、ply等常見的3D對象存儲格式,它被設(shè)計出來是為了便于渲染的數(shù)據(jù)轉(zhuǎn)換和傳輸,本文為大家介紹了使用Python和C++解析gltf文件的方法,感興趣的可以了解下

gltf是類似于stl、obj、ply等常見的3D對象存儲格式,它被設(shè)計出來是為了便于渲染的數(shù)據(jù)轉(zhuǎn)換和傳輸。如果你的瀏覽器可以連接外網(wǎng),可以通過 glTF Viewer 網(wǎng)址打開瀏覽gltf的3D對象。這里介紹兩種語言下從gltf拿到網(wǎng)格的頂點(diǎn)和面片數(shù)據(jù)。

一、Python

第一步安裝pygltflib:

pip install pygltflib

第二步,python解析:

import pygltflib
import numpy as np
pathGltf="test.gltf"
gltf=pygltflib.GLTF2().load(pathGltf)
scene=gltf.scenes[gltf.scene]
nodes=[gltf.nodes[node] for node in scenes.nodes]
vertices=np.arry([node.mesh.primitives[0].attributes["POSITION"] for node in nodes])
print(vertices)

不知道為什么,通過上面這種方式解析,node.mesh我這里是一個int類型的值,運(yùn)行代碼提示node.mesh沒有primitives屬性,然后在網(wǎng)上找了下面的代碼是ok的:

import pygltflib
import pathlib
import struct
# load a gltf file
fname = pathlib.Path("C:/Users/User/Desktop/cube.gltf")
gltf = GLTF2().load(fname)
# get the first mesh in the current scene
mesh = gltf.meshes[gltf.scenes[gltf.scene].nodes[0]-1]
# get the vertices for each primitive in the mesh
for primitive in mesh.primitives:
    # get the binary data for this mesh primitive from the buffer
    accessor = gltf.accessors[primitive.attributes.POSITION]
    bufferView = gltf.bufferViews[accessor.bufferView]
    buffer = gltf.buffers[bufferView.buffer]
    data = gltf.get_data_from_buffer_uri(buffer.uri)
    # pull each vertex from the binary buffer and convert it into a tuple of python floats
    vertices = []
    for i in range(accessor.count):
        index = bufferView.byteOffset + accessor.byteOffset + i*12  # the location in the buffer of this vertex
        d = data[index:index+12]  # the vertex data
        v = struct.unpack("<fff", d)   # convert from base64 to three floats
        vertices.append(v)
# unpack floats
vertices2 = []
for a,b,c in vertices:
    vertices2 += [a,b,c]
# create triangles
vertices = vertices2
triangles = []
for i in range(0,len(vertices),9):
    triangles.append(vertices[i:i+9])
# print data
print(triangles)

二、C++解析

c++依賴的庫主要是draco,這個庫是開源的,網(wǎng)上可以下載,有了draco之后代碼如下:

#include<draco/io/gltf_decoder.h>
#include<draco/tools/draco_transcoder_lib.h>
// 讀取gltf文件
bool parse_gltf_from_file(const std::string& filename,std::unique_ptr<draco::Mesh>& mesh){
    draco::GltfDecoder gltfDec;
    draco::StatusOr<std::unique_ptr<draco::Mesh>> stormesh=gltfDec.DecodeFromFile(filename);
    if(!stormesh.ok()){
        return false;
    }
    std::unique_ptr<draco::Mesh> pDracomesh=std::move(stormesh).value();
    std::cout<<"faces num:"<<pDracomesh->num_faces()<<std::endl;
    pDracomesh.swap(mesh);
    return true;
}
//解析出頂點(diǎn)和面片數(shù)據(jù)
bool get_faces_vertexes(const std::unique_ptr<draco::Mesh>& dracomesh,
                        std::vector<Eigen::Vector3>& vertexes,
                        std::vector<Eigen::Vector3i>& faces){
    auto dump_attribute_to_vec3=[](const draco::PointAttribute& att,std::vector<Eigen::Vector3>& attD){
        if(att.size()==0) return;
        std::vector<Eigen::Vector3> tmp(att.size());
        for(int i=0;i<att.size();++i){
            if(!att.ConvertValue<float,3>(draco::AttributeValueIndex(i),&tmp[i][0])) return;
        }
        attD=std::move(tmp);
    }
    // 解析頂點(diǎn)
    const draco::PointAttribute* posAtt=nullptr;
    std::vector<Eigen::Vector3> points;
    for(int i=0;i<dracomesh->num_attributes();++i){
        const draco::PointAttribute* pAtt=dracomesh->attribute(i);
        switch(pAtt->attribute_type()){
            case draco::PointAttribute::POSITION:
                posAtt=pAtt;
                dump_attribute_to_vec3(*pAtt,points);
                break;
        }
    }
    vertexes=points;
    // 解析面片
    faces.resize(dracomesh->num_faces());
    for(int i=0;i<dracomesh->num_faces();++i){
        for(int j=0;j<3;++j){
            const draco::PointIndex idx=dracomesh->face(draco::FaceIndex(i))[j];
            faces[i][j]=posAtt->mapped_index(idx).value();
        }
    } 
    return true;
}

以上就是利用Python和C++實(shí)現(xiàn)解析gltf文件的詳細(xì)內(nèi)容,更多關(guān)于Python解析gltf文件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python 元組(Tuple)操作詳解

    Python 元組(Tuple)操作詳解

    Python的元組與列表類似,不同之處在于元組的元素不能修改,元組使用小括號,列表使用方括號,元組創(chuàng)建很簡單,只需要在括號中添加元素,并使用逗號隔開即可
    2014-03-03
  • Pytorch實(shí)現(xiàn)網(wǎng)絡(luò)部分層的固定不進(jìn)行回傳更新問題及思路詳解

    Pytorch實(shí)現(xiàn)網(wǎng)絡(luò)部分層的固定不進(jìn)行回傳更新問題及思路詳解

    這篇文章主要介紹了Pytorch實(shí)現(xiàn)網(wǎng)絡(luò)部分層的固定不進(jìn)行回傳更新,實(shí)現(xiàn)思路就是利用tensor的requires_grad,每一個tensor都有自己的requires_grad成員,值只能為True和False,具體內(nèi)容詳情跟隨小編一起看看吧
    2021-08-08
  • PyCharm 接入 DeepSeek最新完整教程

    PyCharm 接入 DeepSeek最新完整教程

    文章介紹了DeepSeek-V3模型的性能提升以及如何在PyCharm中接入和使用DeepSeek進(jìn)行代碼開發(fā),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2025-02-02
  • Django框架model模型對象驗(yàn)證實(shí)現(xiàn)方法分析

    Django框架model模型對象驗(yàn)證實(shí)現(xiàn)方法分析

    這篇文章主要介紹了Django框架model模型對象驗(yàn)證實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Django框架model模型對象驗(yàn)證相關(guān)原理、實(shí)現(xiàn)步驟及操作注意事項(xiàng),需要的朋友可以參考下
    2019-10-10
  • pip install urllib2不能安裝的解決方法

    pip install urllib2不能安裝的解決方法

    今天小編就為大家分享一篇pip install urllib2不能安裝的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • python如何利用turtle繪制正方形

    python如何利用turtle繪制正方形

    這篇文章主要介紹了python如何利用turtle繪制正方形,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Pygame框架實(shí)現(xiàn)飛機(jī)大戰(zhàn)

    Pygame框架實(shí)現(xiàn)飛機(jī)大戰(zhàn)

    這篇文章主要為大家詳細(xì)介紹了Pygame框架實(shí)現(xiàn)飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • 解決python xlrd無法讀取excel文件的問題

    解決python xlrd無法讀取excel文件的問題

    今天小編就為大家分享一篇解決python xlrd無法讀取excel文件的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • 重構(gòu)Python代碼的六個實(shí)例

    重構(gòu)Python代碼的六個實(shí)例

    這篇文章主要給大家介紹了關(guān)于重構(gòu)Python代碼的六個實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Flask應(yīng)用中執(zhí)行指定JavaScript腳本的6種方法

    Flask應(yīng)用中執(zhí)行指定JavaScript腳本的6種方法

    在 Flask 應(yīng)用中執(zhí)行指定 JavaScript 腳本,需結(jié)合后端路由,模板渲染及前后端交互技術(shù),本文主要介紹了六種核心方法及其實(shí)現(xiàn)細(xì)節(jié),大家可以根據(jù)需要進(jìn)行選擇
    2025-06-06

最新評論

桐乡市| 枝江市| 金门县| 明光市| 太保市| 陵川县| 太康县| 永安市| 乌拉特中旗| 铜梁县| 荣成市| 临朐县| 临海市| 佛教| 平陆县| 文山县| 广汉市| 浦江县| 双辽市| 彝良县| 新巴尔虎右旗| 青岛市| 许昌县| 治多县| 介休市| 长丰县| 谷城县| 阿拉善左旗| 枣阳市| 隆尧县| 依安县| 鄂尔多斯市| 棋牌| 贡山| 英吉沙县| 绩溪县| 丹凤县| 光泽县| 瑞安市| 鹿泉市| 文化|