参照google源码中的示例draco_decoder,draco_encoder基本大概可以了解到Draco的使用逻辑:
#include <core/decoder_buffer.h>
#include <io/mesh_io.h>#include <fstream>
#include <iostream>using namespace draco;
using namespace std;int main() {string filePath = "D:/1.bin";ifstream infile(filePath, ios::binary);infile.seekg(0, std::ios::end);size_t data_size = infile.tellg();infile.seekg(0, std::ios::beg);vector<char> data(data_size, 0);infile.read(data.data(), data_size);DecoderBuffer buffer;buffer.Init(data.data(), data_size);//解压缩std::unique_ptr<draco::PointCloud> pc;auto type_statusor = draco::Decoder::GetEncodedGeometryType(&buffer);if (!type_statusor.ok()) {return 1;}//解析数据const draco::EncodedGeometryType geom_type = type_statusor.value();if (geom_type == draco::TRIANGULAR_MESH) {draco::Decoder decoder;auto statusor = decoder.DecodeMeshFromBuffer(&buffer);if (!statusor.ok()) {return 1;}std::unique_ptr<draco::Mesh> mesh = std::move(statusor).value();if (mesh) {const int pos_att_id =mesh->GetNamedAttributeId(GeometryAttribute::POSITION);//解析顶点属性for (PointIndex v(0); v < mesh->num_points(); ++v) {const auto *const pos_att = mesh->attribute(pos_att_id);const uint8_t *pos = pos_att->GetAddress(pos_att->mapped_index(v));int64_t length = pos_att->byte_stride();float temp[3];memcpy(temp, pos, length);printf("%f,%f,%f\t", temp[0], temp[1], temp[2]);}//解析顶点索引for (FaceIndex f(0); f < mesh->num_faces(); ++f) {printf("%d,%d,%d\t", mesh->face(f)[0].value(), mesh->face(f)[1].value(),mesh->face(f)[2].value());}}}
}