-
Notifications
You must be signed in to change notification settings - Fork 1k
Open
Labels
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<float>& occPoints, const vector<int>& 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<const uint8_t*>(buffer.data()), buffer.size());
// resultJson["draco_data"] = encodedStr;
// }
}