google / google/draco

C++ draco encoding example?

Open
#1,101 1 comment 0 reactions 0 assignees View on GitHub
documentation question
Dominant language
C++
Stars
7.5k
Forks
1.1k
Avg merge
47m
Merged PRs (30d)
1

Description

I see that there is a Javascript encoding example, but not a C++ one. I am having trouble building a simple draco mesh encoder, which takes mesh vertices and triangle indices as input. Ignoring the JSON part of the code, the function fails at `EncodeMeshToBuffer`, possibly because of an incorrectly specified mesh.

What am I doing wrong?

```
void dracoEncodeMesh(nlohmann::json& resultJson, const vector& occPoints, const vector& occIndices) {
draco::Mesh dracoMesh;
int numFaces = occIndices.size() / 3;
int numVertices = occPoints.size() / 3;
dracoMesh.SetNumFaces(numFaces);
dracoMesh.set_num_points(numVertices);

// define position attribute
draco::GeometryAttribute posAttribute;
posAttribute.Init(draco::GeometryAttribute::POSITION, nullptr, 3, draco::DT_FLOAT32, false, sizeof(float) * 3, 0);
int posAttributeId = dracoMesh.AddAttribute(posAttribute, false, numVertices);

// set vertex positions and faces
for (int i = 0; i < numVertices; ++i) {
dracoMesh.attribute(posAttributeId)->SetAttributeValue(draco::AttributeValueIndex(i), &occPoints[i * 3]);
}

for (int i = 0; i < numFaces; ++i) {
draco::Mesh::Face face;
for (int j = 0; j < 3; ++j) {
face[j] = draco::PointIndex(occIndices[i * 3 + j]);
}
dracoMesh.SetFace(draco::FaceIndex(i), face);
}

// encoding
draco::Encoder encoder;
draco::EncoderBuffer buffer;
draco::Status status = encoder.EncodeMeshToBuffer(dracoMesh, &buffer);
// if (!status.ok()) {
// resultJson["draco_error"] = "Encoding failed: " + status.error_msg_string();
// } else {
// string encodedStr = base64Encode(reinterpret_cast(buffer.data()), buffer.size());
// resultJson["draco_data"] = encodedStr;
// }
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.