Added a LoadObj function that reads from a std::istream.

+ Added a LoadObj function that reads from a std::istream.  This should allow
   more generic usage and possibly make testing a little easier.
   o This LoadObj accepts a function that returns a
     std::unique_ptr<std::istream> for a material.
 + Modified LoadMtl to read from a std::istream to allow more generic
   usage.
 + Modified test.cc to check that the changes work as expected and nothing was
   broken.

Tests:
 + Compiled test.cc, checked diff of output against pre change output.
   Same output where expected.
This commit is contained in:
E J Burns
2014-04-28 14:37:32 +01:00
parent c33b0cf6cc
commit 08ff49d2d0
3 changed files with 179 additions and 42 deletions

137
test.cc
View File

@@ -3,23 +3,15 @@
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#include <iostream>
#include <sstream>
#include <fstream>
#include <functional>
#include <memory>
static bool
TestLoadObj(
const char* filename,
const char* basepath = NULL)
static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes)
{
std::cout << "Loading " << filename << std::endl;
std::vector<tinyobj::shape_t> shapes;
std::string err = tinyobj::LoadObj(shapes, filename, basepath);
if (!err.empty()) {
std::cerr << err << std::endl;
return false;
}
std::cout << "# of shapes : " << shapes.size() << std::endl;
for (size_t i = 0; i < shapes.size(); i++) {
@@ -53,14 +45,124 @@ TestLoadObj(
printf(" material.map_Kd = %s\n", shapes[i].material.diffuse_texname.c_str());
printf(" material.map_Ks = %s\n", shapes[i].material.specular_texname.c_str());
printf(" material.map_Ns = %s\n", shapes[i].material.normal_texname.c_str());
std::map<std::string, std::string>::iterator it(shapes[i].material.unknown_parameter.begin());
std::map<std::string, std::string>::iterator itEnd(shapes[i].material.unknown_parameter.end());
auto it = shapes[i].material.unknown_parameter.begin();
auto itEnd = shapes[i].material.unknown_parameter.end();
for (; it != itEnd; it++) {
printf(" material.%s = %s\n", it->first.c_str(), it->second.c_str());
}
printf("\n");
}
}
static bool
TestLoadObj(
const char* filename,
const char* basepath = NULL)
{
std::cout << "Loading " << filename << std::endl;
std::vector<tinyobj::shape_t> shapes;
std::string err = tinyobj::LoadObj(shapes, filename, basepath);
if (!err.empty()) {
std::cerr << err << std::endl;
return false;
}
PrintInfo(shapes);
return true;
}
static bool
TestStreamLoadObj()
{
std::cout << "Stream Loading " << std::endl;
std::stringstream objStream;
objStream
<< "mtllib cube.mtl\n"
"\n"
"v 0.000000 2.000000 2.000000\n"
"v 0.000000 0.000000 2.000000\n"
"v 2.000000 0.000000 2.000000\n"
"v 2.000000 2.000000 2.000000\n"
"v 0.000000 2.000000 0.000000\n"
"v 0.000000 0.000000 0.000000\n"
"v 2.000000 0.000000 0.000000\n"
"v 2.000000 2.000000 0.000000\n"
"# 8 vertices\n"
"\n"
"g front cube\n"
"usemtl white\n"
"f 1 2 3 4\n"
"g back cube\n"
"# expects white material\n"
"f 8 7 6 5\n"
"g right cube\n"
"usemtl red\n"
"f 4 3 7 8\n"
"g top cube\n"
"usemtl white\n"
"f 5 1 4 8\n"
"g left cube\n"
"usemtl green\n"
"f 5 6 2 1\n"
"g bottom cube\n"
"usemtl white\n"
"f 2 6 7 3\n"
"# 6 elements";
auto getMatFileIStreamFunc =
[](const std::string& matId)
{
if (matId == "cube.mtl") {
std::unique_ptr<std::stringstream> matStream(
new std::stringstream(
"newmtl white\n"
"Ka 0 0 0\n"
"Kd 1 1 1\n"
"Ks 0 0 0\n"
"\n"
"newmtl red\n"
"Ka 0 0 0\n"
"Kd 1 0 0\n"
"Ks 0 0 0\n"
"\n"
"newmtl green\n"
"Ka 0 0 0\n"
"Kd 0 1 0\n"
"Ks 0 0 0\n"
"\n"
"newmtl blue\n"
"Ka 0 0 0\n"
"Kd 0 0 1\n"
"Ks 0 0 0\n"
"\n"
"newmtl light\n"
"Ka 20 20 20\n"
"Kd 1 1 1\n"
"Ks 0 0 0"));
return matStream;
}
std::unique_ptr<std::stringstream> emptyUP( nullptr );
return emptyUP;
};
std::vector<tinyobj::shape_t> shapes;
std::string err = tinyobj::LoadObj(shapes, objStream, getMatFileIStreamFunc);
if (!err.empty()) {
std::cerr << err << std::endl;
return false;
}
PrintInfo(shapes);
return true;
}
@@ -79,7 +181,8 @@ main(
} else {
assert(true == TestLoadObj("cornell_box.obj"));
assert(true == TestLoadObj("cube.obj"));
assert(true == TestStreamLoadObj());
}
return 0;
}