Skip to content

Commit b9ac41f

Browse files
committed
exporting gltf with lines
1 parent ab4f48b commit b9ac41f

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

sources/libcore/mesh/exportGltf.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ namespace cage
1717
Holder<PointerRange<char>> exportImpl(const String &filename, const MeshExportGltfConfig &config)
1818
{
1919
CAGE_ASSERT(config.mesh);
20-
if (config.mesh->type() != MeshTypeEnum::Triangles)
21-
CAGE_THROW_ERROR(Exception, "exporting gltf requires triangle mesh");
20+
if (config.mesh->type() != MeshTypeEnum::Triangles && config.mesh->type() != MeshTypeEnum::Lines)
21+
CAGE_THROW_ERROR(Exception, "exporting gltf requires triangle or line mesh");
2222
if (config.mesh->verticesCount() > 0 && config.mesh->indicesCount() == 0)
2323
CAGE_THROW_ERROR(Exception, "exporting gltf requires indexed mesh");
2424

@@ -289,7 +289,8 @@ namespace cage
289289
if (!textures.empty())
290290
tiJson = std::string() + ", \"textures\":[" + texturesJson + "],\"images\":[" + imagesJson + "]";
291291

292-
json = std::string() + "{\"scene\":0,\"scenes\":[{\"nodes\":[0]}],\"nodes\":[{\"mesh\":0,\"name\":\"" + config.name.c_str() + "\"}],\"meshes\":[{\"primitives\":[{\"attributes\":{" + attributesJson + "},\"indices\":" + (Stringizer() + indicesIndex).value.c_str() + ",\"material\":0}],\"name\":\"" + config.name.c_str() + "\"}],\"materials\":[{" + materialJson + "}]" + tiJson + ",\"buffers\":[{\"byteLength\":" + (Stringizer() + buffer.size()).value.c_str() + "}],\"bufferViews\":[" + viewsJson + "],\"accessors\":[" + accessorsJson + "],\"asset\":{\"version\":\"2.0\"}}";
292+
const uint32 primitiveMode = (config.mesh->type() == MeshTypeEnum::Lines) ? 1 : 4; // 1=LINES, 4=TRIANGLES
293+
json = std::string() + "{\"scene\":0,\"scenes\":[{\"nodes\":[0]}],\"nodes\":[{\"mesh\":0,\"name\":\"" + config.name.c_str() + "\"}],\"meshes\":[{\"primitives\":[{\"attributes\":{" + attributesJson + "},\"indices\":" + (Stringizer() + indicesIndex).value.c_str() + ",\"material\":0,\"mode\":" + (Stringizer() + primitiveMode).value.c_str() + "}],\"name\":\"" + config.name.c_str() + "\"}],\"materials\":[{" + materialJson + "}]" + tiJson + ",\"buffers\":[{\"byteLength\":" + (Stringizer() + buffer.size()).value.c_str() + "}],\"bufferViews\":[" + viewsJson + "],\"accessors\":[" + accessorsJson + "],\"asset\":{\"version\":\"2.0\"}}";
293294

294295
while ((json.length() % 4) != 0)
295296
json += ' ';

sources/test-core/mesh.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,23 @@ namespace
951951
approxEqual(msh->boundingBox(), Aabb(Vec3(-1, -1, -2), Vec3(1, 1, 2)));
952952
meshApplyTransform(+msh, Mat4(Mat3(10, 0, 0, 0, 0, 10, 0, -10, 0)));
953953
approxEqual(msh->boundingBox(), Aabb(Vec3(-10, -20, -10), Vec3(10, 20, 10)));
954+
955+
{
956+
CAGE_TESTCASE("export gltf");
957+
MeshExportGltfConfig cfg;
958+
cfg.mesh = +msh;
959+
meshExportFiles("meshes/exports/lines.glb", cfg);
960+
}
961+
962+
{
963+
CAGE_TESTCASE("import gltf");
964+
const auto imp = meshImportFiles("meshes/exports/lines.glb");
965+
CAGE_TEST(imp.parts.size() == 1);
966+
CAGE_TEST(imp.parts[0].mesh->type() == MeshTypeEnum::Lines);
967+
approxEqual(imp.parts[0].mesh->boundingBox(), Aabb(Vec3(-10, -20, -10), Vec3(10, 20, 10)));
968+
CAGE_TEST(imp.parts[0].mesh->verticesCount() == msh->verticesCount());
969+
CAGE_TEST(imp.parts[0].mesh->indicesCount() == msh->indicesCount());
970+
}
954971
}
955972

956973
void testMeshConsistentWinding()

0 commit comments

Comments
 (0)