|
| 1 | +// The installed cgltf wrapper: a triangle whose buffer is in the document, |
| 2 | +// parsed, loaded, validated and read; a broken document refused with its |
| 3 | +// reason; the writer's output parsed again. |
| 4 | +import std; |
| 5 | +import mcpplibs.mwrap.cgltf; |
| 6 | + |
| 7 | +namespace { |
| 8 | + |
| 9 | +// Three positions (36 bytes of floats) and three 16-bit indices (6 bytes, padded to 44), as base64. |
| 10 | +constexpr std::string_view triangle = R"({ |
| 11 | + "asset": {"version": "2.0"}, |
| 12 | + "scene": 0, |
| 13 | + "scenes": [{"nodes": [0]}], |
| 14 | + "nodes": [{"name": "a triangle", "mesh": 0, "translation": [1, 2, 3]}], |
| 15 | + "meshes": [{"name": "tri", "primitives": [{"attributes": {"POSITION": 0}, "indices": 1, "material": 0}]}], |
| 16 | + "materials": [{"name": "red", "pbrMetallicRoughness": {"baseColorFactor": [1, 0, 0, 1], "metallicFactor": 0.25}}], |
| 17 | + "buffers": [{"byteLength": 44, "uri": "data:application/octet-stream;base64,AAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAgD8AAAAAAAABAAIAAAA="}], |
| 18 | + "bufferViews": [{"buffer": 0, "byteLength": 36}, {"buffer": 0, "byteOffset": 36, "byteLength": 6}], |
| 19 | + "accessors": [ |
| 20 | + {"bufferView": 0, "componentType": 5126, "count": 3, "type": "VEC3", "min": [0, 0, 0], "max": [1, 1, 0]}, |
| 21 | + {"bufferView": 1, "componentType": 5123, "count": 3, "type": "SCALAR"} |
| 22 | + ] |
| 23 | +})"; |
| 24 | + |
| 25 | +} // namespace |
| 26 | + |
| 27 | +int main() { |
| 28 | + const cgltf_options options{}; |
| 29 | + cgltf_data* data = nullptr; |
| 30 | + if (cgltf_parse(&options, triangle.data(), triangle.size(), &data) != cgltf_result_success) return 1; |
| 31 | + if (cgltf_load_buffers(&options, data, nullptr) != cgltf_result_success || cgltf_validate(data) != cgltf_result_success) return 2; |
| 32 | + if (data->meshes_count != 1 || data->nodes[0].mesh != &data->meshes[0] || std::string_view(data->nodes[0].name) != "a triangle") return 3; |
| 33 | + |
| 34 | + const cgltf_primitive& primitive = data->meshes[0].primitives[0]; |
| 35 | + const cgltf_accessor* positions = cgltf_find_accessor(&primitive, cgltf_attribute_type_position, 0); |
| 36 | + std::array<cgltf_float, 9> corners{}; |
| 37 | + if (!positions || cgltf_accessor_unpack_floats(positions, corners.data(), corners.size()) != 9) return 4; |
| 38 | + if (corners != std::array<cgltf_float, 9>{0, 0, 0, 1, 0, 0, 0, 1, 0}) return 5; |
| 39 | + if (!primitive.indices || cgltf_accessor_read_index(primitive.indices, 2) != 2) return 6; |
| 40 | + if (!primitive.material || primitive.material->pbr_metallic_roughness.metallic_factor != 0.25f) return 7; |
| 41 | + std::array<cgltf_float, 16> world{}; |
| 42 | + cgltf_node_transform_world(&data->nodes[0], world.data()); |
| 43 | + if (world[12] != 1 || world[13] != 2 || world[14] != 3) return 8; |
| 44 | + |
| 45 | + // The writer (mcpplibs.mwrap.cgltf.write, re-exported) gives a document that parses the same. |
| 46 | + std::string written(cgltf_write(&options, nullptr, 0, data), '\0'); |
| 47 | + if (written.empty() || cgltf_write(&options, written.data(), written.size(), data) != written.size()) return 9; |
| 48 | + cgltf_data* again = nullptr; |
| 49 | + if (cgltf_parse(&options, written.data(), written.size() - 1, &again) != cgltf_result_success) return 10; |
| 50 | + const bool same = again->accessors_count == 2 && std::string_view(again->materials[0].name) == "red" && again->nodes[0].translation[2] == 3; |
| 51 | + cgltf_free(again); |
| 52 | + cgltf_free(data); |
| 53 | + if (!same) return 11; |
| 54 | + |
| 55 | + // What is not glTF is refused with its reason. |
| 56 | + const std::string_view broken = R"({"asset": {"version": "2.0"}, "meshes": [)"; |
| 57 | + const std::string_view past = R"({"asset": {"version": "2.0"}, "nodes": [{"mesh": 3}]})"; |
| 58 | + cgltf_data* none = nullptr; |
| 59 | + if (cgltf_parse(&options, broken.data(), broken.size(), &none) != cgltf_result_invalid_json) return 12; |
| 60 | + if (cgltf_parse(&options, past.data(), past.size(), &none) != cgltf_result_invalid_gltf) return 13; |
| 61 | + |
| 62 | + std::println("cgltf 1.15 through import mcpplibs.mwrap.cgltf: a triangle of {} positions read, written in {} bytes and parsed again", corners.size() / 3, written.size() - 1); |
| 63 | + return 0; |
| 64 | +} |
0 commit comments