Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Squareys <[email protected]>
  • Loading branch information
Squareys committed Jun 8, 2017
1 parent 6302932 commit af3aecd
Show file tree
Hide file tree
Showing 8 changed files with 664 additions and 306 deletions.
841 changes: 590 additions & 251 deletions src/MagnumPlugins/ObjImporter/ObjImporter.cpp

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/MagnumPlugins/ObjImporter/ObjImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ class MAGNUM_OBJIMPORTER_EXPORT ObjImporter: public AbstractImporter {
MAGNUM_OBJIMPORTER_LOCAL void doOpenFile(const std::string& filename) override;
MAGNUM_OBJIMPORTER_LOCAL void doClose() override;

MAGNUM_OBJIMPORTER_LOCAL UnsignedInt doObject3DCount() const override;
MAGNUM_OBJIMPORTER_LOCAL Int doObject3DForName(const std::string& name) override;
MAGNUM_OBJIMPORTER_LOCAL std::string doObject3DName(UnsignedInt id) override;
MAGNUM_OBJIMPORTER_LOCAL std::unique_ptr<ObjectData3D> doObject3D(UnsignedInt id) override;

MAGNUM_OBJIMPORTER_LOCAL UnsignedInt doMesh3DCount() const override;
MAGNUM_OBJIMPORTER_LOCAL Int doMesh3DForName(const std::string& name) override;
MAGNUM_OBJIMPORTER_LOCAL std::string doMesh3DName(UnsignedInt id) override;
Expand All @@ -100,7 +105,7 @@ class MAGNUM_OBJIMPORTER_EXPORT ObjImporter: public AbstractImporter {
MAGNUM_OBJIMPORTER_LOCAL std::optional<ImageData2D> doImage2D(UnsignedInt id);

MAGNUM_OBJIMPORTER_LOCAL void parse();
MAGNUM_OBJIMPORTER_LOCAL void parseMaterialLibrary(std::string libname);
MAGNUM_OBJIMPORTER_LOCAL void parseMaterialLibrary(Containers::ArrayView<char> libname);

Containers::Array<char> _in;
std::unique_ptr<struct ImporterState> _state;
Expand Down
81 changes: 48 additions & 33 deletions src/MagnumPlugins/ObjImporter/Test/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ struct ObjImporterTest: TestSuite::Tester {
void textureCoordinatesNormals();

void emptyFile();
void unnamedMesh();
void namedMesh();
void moreMeshes();
void unnamedFirstMesh();
Expand Down Expand Up @@ -104,7 +103,6 @@ ObjImporterTest::ObjImporterTest() {
&ObjImporterTest::textureCoordinatesNormals,

&ObjImporterTest::emptyFile,
&ObjImporterTest::unnamedMesh,
&ObjImporterTest::namedMesh,
&ObjImporterTest::moreMeshes,
&ObjImporterTest::unnamedFirstMesh,
Expand Down Expand Up @@ -156,11 +154,11 @@ void ObjImporterTest::pointMesh() {
CORRADE_COMPARE(data->positionArrayCount(), 1);
CORRADE_COMPARE(data->positions(0), (std::vector<Vector3>{
{0.5f, 2.0f, 3.0f},
{0.0f, 1.5f, 1.0f},
{2.0f, 3.0f, 5.0f}
{2.0f, 3.0f, 5.0f},
{0.0f, 1.5f, 1.0f}
}));
CORRADE_COMPARE(data->indices(), (std::vector<UnsignedInt>{
0, 2, 1, 0
0, 1, 2, 0
}));
}

Expand Down Expand Up @@ -206,12 +204,32 @@ void ObjImporterTest::triangleMesh() {
void ObjImporterTest::mixedPrimitives() {
ObjImporter importer;
CORRADE_VERIFY(importer.openFile(Utility::Directory::join(OBJIMPORTER_TEST_DIR, "mixedPrimitives.obj")));
CORRADE_COMPARE(importer.mesh3DCount(), 1);
CORRADE_COMPARE(importer.mesh3DCount(), 2);

std::ostringstream out;
Error redirectError{&out};
CORRADE_VERIFY(!importer.mesh3D(0));
CORRADE_COMPARE(out.str(), "Trade::ObjImporter::mesh3D(): mixed primitive MeshPrimitive::Points and MeshPrimitive::Lines\n");
/* point mesh */
auto pointData = importer.mesh3D(0);
CORRADE_VERIFY(pointData);
CORRADE_COMPARE(pointData->primitive(), MeshPrimitive::Points);
CORRADE_COMPARE(pointData->positions(0), (std::vector<Vector3>{
{0.5f, 2.0f, 3.0f},
{2.0f, 3.0f, 5.0f},
{0.0f, 1.5f, 1.0f}
}));
CORRADE_COMPARE(pointData->indices(), (std::vector<UnsignedInt>{
0, 1, 2
}));

auto lineData = importer.mesh3D(1);
CORRADE_VERIFY(lineData);
CORRADE_COMPARE(lineData->primitive(), MeshPrimitive::Lines);
CORRADE_COMPARE(lineData->positions(0), (std::vector<Vector3>{
{0.5f, 2.0f, 3.0f},
{0.0f, 1.5f, 1.0f},
{2.0f, 3.0f, 5.0f}
}));
CORRADE_COMPARE(lineData->indices(), (std::vector<UnsignedInt>{
0, 1, 1, 2
}));
}

void ObjImporterTest::positionsOnly() {
Expand Down Expand Up @@ -261,7 +279,7 @@ void ObjImporterTest::normals() {

const std::optional<MeshData3D> data = importer.mesh3D(0);
CORRADE_VERIFY(data);
CORRADE_COMPARE(data->primitive(), MeshPrimitive::Lines);
CORRADE_COMPARE(data->primitive(), MeshPrimitive::Triangles);
CORRADE_COMPARE(data->positionArrayCount(), 1);
CORRADE_VERIFY(!data->hasTextureCoords2D());
CORRADE_COMPARE(data->normalArrayCount(), 1);
Expand All @@ -278,7 +296,7 @@ void ObjImporterTest::normals() {
{0.5f, 1.0f, 0.5f}
}));
CORRADE_COMPARE(data->indices(), (std::vector<UnsignedInt>{
0, 1, 2, 3, 1, 0
0, 1, 0, 2, 3, 0, 1, 0, 0
}));
}

Expand All @@ -289,7 +307,7 @@ void ObjImporterTest::textureCoordinatesNormals() {

const std::optional<MeshData3D> data = importer.mesh3D(0);
CORRADE_VERIFY(data);
CORRADE_COMPARE(data->primitive(), MeshPrimitive::Lines);
CORRADE_COMPARE(data->primitive(), MeshPrimitive::Triangles);
CORRADE_COMPARE(data->positionArrayCount(), 1);
CORRADE_COMPARE(data->textureCoords2DArrayCount(), 1);
CORRADE_COMPARE(data->normalArrayCount(), 1);
Expand All @@ -315,30 +333,25 @@ void ObjImporterTest::textureCoordinatesNormals() {
{0.5f, 1.0f, 0.5f}
}));
CORRADE_COMPARE(data->indices(), (std::vector<UnsignedInt>{
0, 1, 2, 3, 1, 0, 4, 2
0, 1, 2, 3, 1, 0, 4, 2, 2
}));
}

void ObjImporterTest::emptyFile() {
ObjImporter importer;
CORRADE_VERIFY(importer.openFile(Utility::Directory::join(OBJIMPORTER_TEST_DIR, "emptyFile.obj")));
CORRADE_COMPARE(importer.mesh3DCount(), 1);
}

void ObjImporterTest::unnamedMesh() {
ObjImporter importer;
CORRADE_VERIFY(importer.openFile(Utility::Directory::join(OBJIMPORTER_TEST_DIR, "emptyFile.obj")));
CORRADE_COMPARE(importer.mesh3DCount(), 1);
CORRADE_COMPARE(importer.mesh3DName(0), "");
CORRADE_COMPARE(importer.mesh3DForName(""), -1);
CORRADE_COMPARE(importer.mesh3DCount(), 0);
CORRADE_COMPARE(importer.object3DCount(), 0);
CORRADE_COMPARE(importer.image3DCount(), 0);
}

void ObjImporterTest::namedMesh() {
ObjImporter importer;
CORRADE_VERIFY(importer.openFile(Utility::Directory::join(OBJIMPORTER_TEST_DIR, "namedMesh.obj")));
CORRADE_COMPARE(importer.mesh3DCount(), 1);
CORRADE_COMPARE(importer.mesh3DName(0), "MyMesh");
CORRADE_COMPARE(importer.mesh3DForName("MyMesh"), 0);
CORRADE_COMPARE(importer.mesh3DCount(), 0);
CORRADE_COMPARE(importer.object3DCount(), 1);
CORRADE_COMPARE(importer.object3DName(0), "MyMesh");
CORRADE_COMPARE(importer.object3DForName("MyMesh"), 0);
}

void ObjImporterTest::moreMeshes() {
Expand All @@ -357,7 +370,7 @@ void ObjImporterTest::moreMeshes() {
{0.0f, 1.5f, 1.0f}
}));
CORRADE_COMPARE(data->indices(), (std::vector<UnsignedInt>{
0, 1
0, 1, 0
}));

CORRADE_COMPARE(importer.mesh3DName(1), "LineMesh");
Expand Down Expand Up @@ -393,13 +406,17 @@ void ObjImporterTest::moreMeshes() {
void ObjImporterTest::unnamedFirstMesh() {
ObjImporter importer;
CORRADE_VERIFY(importer.openFile(Utility::Directory::join(OBJIMPORTER_TEST_DIR, "unnamedFirstMesh.obj")));
CORRADE_COMPARE(importer.mesh3DCount(), 2);
CORRADE_COMPARE(importer.mesh3DCount(), 1);
CORRADE_COMPARE(importer.object3DCount(), 2); /* Second mesh is empty, hence 2 objects, but 1 mesh */

CORRADE_COMPARE(importer.object3DName(0), "");
CORRADE_COMPARE(importer.object3DForName(""), 0); // TODO: why is this -1?

CORRADE_COMPARE(importer.mesh3DName(0), "");
CORRADE_COMPARE(importer.mesh3DForName(""), -1);
CORRADE_COMPARE(importer.mesh3DForName(""), 0);

CORRADE_COMPARE(importer.mesh3DName(1), "SecondMesh");
CORRADE_COMPARE(importer.mesh3DForName("SecondMesh"), 1);
CORRADE_COMPARE(importer.object3DName(1), "SecondMesh");
CORRADE_COMPARE(importer.object3DForName("SecondMesh"), 1);
}

void ObjImporterTest::wrongFloat() {
Expand All @@ -410,7 +427,6 @@ void ObjImporterTest::wrongFloat() {

std::ostringstream out;
Error redirectError{&out};
CORRADE_VERIFY(!importer.mesh3D(id));
CORRADE_COMPARE(out.str(), "Trade::ObjImporter::mesh3D(): error while converting numeric data\n");
}

Expand Down Expand Up @@ -745,7 +761,6 @@ void ObjImporterTest::unsupportedKeyword() {
CORRADE_COMPARE(data->positions(0), (std::vector<Vector3>{
{0.0f, 1.0f, 2.0f}
}));
CORRADE_COMPARE(data->indices(), std::vector<UnsignedInt>{0});
}

void ObjImporterTest::unknownKeyword() {
Expand Down
4 changes: 2 additions & 2 deletions src/MagnumPlugins/ObjImporter/Test/moreMeshes.obj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ v 0.5 2 3
v 0 1.5 1
vn 0.5 2 3
vn 0 1.5 1
p 1//1
p 2//2
p 1 2
p 1

# Lines
o LineMesh
Expand Down
18 changes: 8 additions & 10 deletions src/MagnumPlugins/ObjImporter/Test/multiMaterial.obj
Original file line number Diff line number Diff line change
@@ -1,60 +1,58 @@
mtllib multiMaterial.mtl



# Billboard

# positions
# positions
v -2.94249 18.5768 3.27633
v 1.72414 18.9233 -3.20162
v 2.74428 -0.499733 -3.50576
v -1.92235 -0.846268 2.9722


# normals
# normals
vn 0.810085 0.0333816 0.585361
vn 0.810085 0.0333816 0.585361
vn 0.810085 0.0333816 0.585361
vn 0.810085 0.0333816 0.585361


# texture coords
# texture coords
vt 0 1
vt 0 0
vt 1 0
vt 1 1

usemtl mat_0

# faces
# faces
f 1/1/1 2/2/2 3/3/3
f 1/1/1 3/3/3 4/4/4


# Billboard

# positions
# positions
v 2.43556 18.8755 2.23745
v -3.6539 18.6246 -2.16274
v -2.63377 -0.798466 -2.46688
v 3.4557 -0.547535 1.93331


# normals
# normals
vn 0.583961 0.0433639 -0.810622
vn 0.583961 0.0433639 -0.810622
vn 0.583961 0.0433639 -0.810622
vn 0.583961 0.0433639 -0.810622


# texture coords
# texture coords
vt 0 1
vt 0 0
vt 1 0
vt 1 1

usemtl mat_1

# faces
# faces
f 5/5/5 6/6/6 7/7/7
f 5/5/5 7/7/7 8/8/8
8 changes: 4 additions & 4 deletions src/MagnumPlugins/ObjImporter/Test/normals.obj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ v 0 1.5 1
vn 1 0.5 3.5
vn 0.5 1 0.5

# Lines
l 1//1 2//1
l 1//2 2//2
l 2//1 1//1
# Triangles
f 1//1 2//1 1//1
f 1//2 2//2 1//1
f 2//1 1//1 1//1
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ vt 0.5 1
vn 1 0.5 3.5
vn 0.5 1 0.5

# Lines
l 1/1/1 2/1/2
l 1/2/2 2/2/1
l 2/1/2 1/1/1
l 2/2/2 1/2/2
# Faces
f 1/1/1 2/1/2 1/2/2
f 2/2/1 2/1/2 1/1/1
f 2/2/2 1/2/2 1/2/2
2 changes: 2 additions & 0 deletions src/MagnumPlugins/ObjImporter/Test/unnamedFirstMesh.obj
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
v 1 2 3
p 1
o SecondMesh
# empty

0 comments on commit af3aecd

Please sign in to comment.