6 Commits

Author SHA1 Message Date
Syoyo Fujita
98da6829ac Initial support of parsing curve primitive('curv')
Add clang-format style file.
2015-08-20 18:17:13 +09:00
Syoyo Fujita
aa07206fc1 Suppress double -> float conversion warning. Fixes #50 2015-07-24 11:46:30 +09:00
Syoyo Fujita
8329bdd135 Merge pull request #48 from gitter-badger/gitter-badge
Add a Gitter chat badge to README.md
2015-07-17 01:14:43 +09:00
The Gitter Badger
def9fe7f16 Added Gitter badge 2015-07-16 16:02:50 +00:00
Syoyo Fujita
82ae20b833 +1 version num. 2015-06-25 20:32:46 +09:00
Syoyo Fujita
164c152216 Initialized a material.
Add warning message to `err` when material file not found.
2015-06-25 20:24:24 +09:00
6 changed files with 782 additions and 632 deletions

7
.clang-format Normal file
View File

@@ -0,0 +1,7 @@
---
BasedOnStyle: LLVM
IndentWidth: 2
TabWidth: 2
UseTab: Always
BreakBeforeBraces: Attach
Standard: Cpp03

View File

@@ -1,6 +1,8 @@
tinyobjloader tinyobjloader
============= =============
[![Join the chat at https://gitter.im/syoyo/tinyobjloader](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/syoyo/tinyobjloader?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![wercker status](https://app.wercker.com/status/495a3bac400212cdacdeb4dd9397bf4f/m "wercker status")](https://app.wercker.com/project/bykey/495a3bac400212cdacdeb4dd9397bf4f) [![wercker status](https://app.wercker.com/status/495a3bac400212cdacdeb4dd9397bf4f/m "wercker status")](https://app.wercker.com/project/bykey/495a3bac400212cdacdeb4dd9397bf4f)
[![Build status](https://ci.appveyor.com/api/projects/status/tlb421q3t2oyobcn/branch/master?svg=true)](https://ci.appveyor.com/project/syoyo/tinyobjloader/branch/master) [![Build status](https://ci.appveyor.com/api/projects/status/tlb421q3t2oyobcn/branch/master?svg=true)](https://ci.appveyor.com/project/syoyo/tinyobjloader/branch/master)

View File

@@ -11,7 +11,7 @@ solution "TinyObjLoaderSolution"
configurations { "Release", "Debug" } configurations { "Release", "Debug" }
if (os.is("windows")) then if (os.is("windows")) then
platforms { "x64", "x32" } platforms { "x32", "x64" }
else else
platforms { "native", "x32", "x64" } platforms { "native", "x32", "x64" }
end end

38
test.cc
View File

@@ -7,16 +7,15 @@
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials) static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::curve_t>& curves, const std::vector<tinyobj::material_t>& materials)
{ {
std::cout << "# of shapes : " << shapes.size() << std::endl; std::cout << "# of shapes : " << shapes.size() << std::endl;
std::cout << "# of curves : " << curves.size() << std::endl;
std::cout << "# of materials : " << materials.size() << std::endl; std::cout << "# of materials : " << materials.size() << std::endl;
for (size_t i = 0; i < shapes.size(); i++) { for (size_t i = 0; i < shapes.size(); i++) {
printf("shape[%ld].name = %s\n", i, shapes[i].name.c_str()); printf("shape[%ld].name = %s\n", i, shapes[i].name.c_str());
printf("Size of shape[%ld].indices: %ld\n", i, shapes[i].mesh.indices.size()); printf("Size of shape[%ld].indices: %ld\n", i, shapes[i].mesh.indices.size());
printf("Size of shape[%ld].normal_indices: %ld\n", i, shapes[i].mesh.normal_indices.size());
printf("Size of shape[%ld].texcoord_indices: %ld\n", i, shapes[i].mesh.texcoord_indices.size());
printf("Size of shape[%ld].material_ids: %ld\n", i, shapes[i].mesh.material_ids.size()); printf("Size of shape[%ld].material_ids: %ld\n", i, shapes[i].mesh.material_ids.size());
assert((shapes[i].mesh.indices.size() % 3) == 0); assert((shapes[i].mesh.indices.size() % 3) == 0);
for (size_t f = 0; f < shapes[i].mesh.indices.size() / 3; f++) { for (size_t f = 0; f < shapes[i].mesh.indices.size() / 3; f++) {
@@ -33,6 +32,29 @@ static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::ve
} }
} }
for (size_t i = 0; i < curves.size(); i++) {
printf("curve[%ld].name = %s\n", i, curves[i].name.c_str());
printf("Size of curve[%ld].indices: %ld\n", i, curves[i].indices.size());
printf("curves[%ld].vertices: %ld\n", i, curves[i].positions.size());
assert((curves[i].positions.size() % 3) == 0);
for (size_t v = 0; v < curves[i].positions.size() / 3; v++) {
printf(" v[%ld] = (%f, %f, %f)\n", v,
curves[i].positions[3*v+0],
curves[i].positions[3*v+1],
curves[i].positions[3*v+2]);
}
for (size_t v = 0; v < curves[i].u_params.size(); v++) {
printf(" u[%ld] = %f\n", v, curves[i].u_params[v]);
}
for (size_t v = 0; v < curves[i].v_params.size(); v++) {
printf(" u[%ld] = %f\n", v, curves[i].v_params[v]);
}
}
for (size_t i = 0; i < materials.size(); i++) { for (size_t i = 0; i < materials.size(); i++) {
printf("material[%ld].name = %s\n", i, materials[i].name.c_str()); printf("material[%ld].name = %s\n", i, materials[i].name.c_str());
printf(" material.Ka = (%f, %f ,%f)\n", materials[i].ambient[0], materials[i].ambient[1], materials[i].ambient[2]); printf(" material.Ka = (%f, %f ,%f)\n", materials[i].ambient[0], materials[i].ambient[1], materials[i].ambient[2]);
@@ -65,15 +87,16 @@ TestLoadObj(
std::cout << "Loading " << filename << std::endl; std::cout << "Loading " << filename << std::endl;
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::curve_t> curves;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string err = tinyobj::LoadObj(shapes, materials, filename, basepath); std::string err = tinyobj::LoadObj(shapes, curves, materials, filename, basepath);
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << err << std::endl;
return false; return false;
} }
PrintInfo(shapes, materials); PrintInfo(shapes, curves, materials);
return true; return true;
} }
@@ -165,15 +188,16 @@ std::string matStream(
MaterialStringStreamReader matSSReader(matStream); MaterialStringStreamReader matSSReader(matStream);
std::vector<tinyobj::shape_t> shapes; std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::curve_t> curves;
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string err = tinyobj::LoadObj(shapes, materials, objStream, matSSReader); std::string err = tinyobj::LoadObj(shapes, curves, materials, objStream, matSSReader);
if (!err.empty()) { if (!err.empty()) {
std::cerr << err << std::endl; std::cerr << err << std::endl;
return false; return false;
} }
PrintInfo(shapes, materials); PrintInfo(shapes, curves, materials);
return true; return true;
} }

View File

@@ -5,7 +5,10 @@
// //
// //
// version 0.9.12: Fix groups being ignored if they have 'usemtl' just before 'g' (#44) // version 0.9.14: Initial support of parsing curve primitive.
// version 0.9.13: Report "Material file not found message" in `err`(#46)
// version 0.9.12: Fix groups being ignored if they have 'usemtl' just before
// 'g' (#44)
// version 0.9.11: Invert `Tr` parameter(#43) // version 0.9.11: Invert `Tr` parameter(#43)
// version 0.9.10: Fix seg fault on windows. // version 0.9.10: Fix seg fault on windows.
// version 0.9.9 : Replace atof() with custom parser. // version 0.9.9 : Replace atof() with custom parser.
@@ -15,7 +18,8 @@
// version 0.9.6 : Support Ni(index of refraction) mtl parameter. // version 0.9.6 : Support Ni(index of refraction) mtl parameter.
// Parse transmittance material parameter correctly. // Parse transmittance material parameter correctly.
// version 0.9.5 : Parse multiple group name. // version 0.9.5 : Parse multiple group name.
// Add support of specifying the base path to load material file. // Add support of specifying the base path to load material
// file.
// version 0.9.4 : Initial suupport of group tag(g) // version 0.9.4 : Initial suupport of group tag(g)
// version 0.9.3 : Fix parsing triple 'x/y/z' // version 0.9.3 : Fix parsing triple 'x/y/z'
// version 0.9.2 : Add more .mtl load support // version 0.9.2 : Add more .mtl load support
@@ -74,8 +78,10 @@ static inline bool isNewLine(const char c) {
// Make index zero-base, and also support relative index. // Make index zero-base, and also support relative index.
static inline int fixIndex(int idx, int n) { static inline int fixIndex(int idx, int n) {
if (idx > 0) return idx - 1; if (idx > 0)
if (idx == 0) return 0; return idx - 1;
if (idx == 0)
return 0;
return n + idx; // negative value = relative return n + idx; // negative value = relative
} }
@@ -95,7 +101,6 @@ static inline int parseInt(const char *&token) {
return i; return i;
} }
// Tries to parse a floating point number located at s. // Tries to parse a floating point number located at s.
// //
// s_end should be a location in the string where reading should absolutely // s_end should be a location in the string where reading should absolutely
@@ -123,10 +128,8 @@ static inline int parseInt(const char *&token) {
// - s >= s_end. // - s >= s_end.
// - parse failure. // - parse failure.
// //
static bool tryParseDouble(const char *s, const char *s_end, double *result) static bool tryParseDouble(const char *s, const char *s_end, double *result) {
{ if (s >= s_end) {
if (s >= s_end)
{
return false; return false;
} }
@@ -156,23 +159,20 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result)
*/ */
// Find out what sign we've got. // Find out what sign we've got.
if (*curr == '+' || *curr == '-') if (*curr == '+' || *curr == '-') {
{
sign = *curr; sign = *curr;
curr++; curr++;
} } else if (isdigit(*curr)) { /* Pass through. */
else if (isdigit(*curr)) { /* Pass through. */ } } else {
else
{
goto fail; goto fail;
} }
// Read the integer part. // Read the integer part.
while ((end_not_reached = (curr != s_end)) && isdigit(*curr)) while ((end_not_reached = (curr != s_end)) && isdigit(*curr)) {
{
mantissa *= 10; mantissa *= 10;
mantissa += static_cast<int>(*curr - 0x30); mantissa += static_cast<int>(*curr - 0x30);
curr++; read++; curr++;
read++;
} }
// We must make sure we actually got something. // We must make sure we actually got something.
@@ -183,20 +183,17 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result)
goto assemble; goto assemble;
// Read the decimal part. // Read the decimal part.
if (*curr == '.') if (*curr == '.') {
{
curr++; curr++;
read = 1; read = 1;
while ((end_not_reached = (curr != s_end)) && isdigit(*curr)) while ((end_not_reached = (curr != s_end)) && isdigit(*curr)) {
{
// NOTE: Don't use powf here, it will absolutely murder precision. // NOTE: Don't use powf here, it will absolutely murder precision.
mantissa += static_cast<int>(*curr - 0x30) * pow(10.0, -read); mantissa += static_cast<int>(*curr - 0x30) * pow(10.0, -read);
read++; curr++; read++;
curr++;
} }
} } else if (*curr == 'e' || *curr == 'E') {
else if (*curr == 'e' || *curr == 'E') {} } else {
else
{
goto assemble; goto assemble;
} }
@@ -204,28 +201,24 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result)
goto assemble; goto assemble;
// Read the exponent part. // Read the exponent part.
if (*curr == 'e' || *curr == 'E') if (*curr == 'e' || *curr == 'E') {
{
curr++; curr++;
// Figure out if a sign is present and if it is. // Figure out if a sign is present and if it is.
if ((end_not_reached = (curr != s_end)) && (*curr == '+' || *curr == '-')) if ((end_not_reached = (curr != s_end)) && (*curr == '+' || *curr == '-')) {
{
exp_sign = *curr; exp_sign = *curr;
curr++; curr++;
} } else if (isdigit(*curr)) { /* Pass through. */
else if (isdigit(*curr)) { /* Pass through. */ } } else {
else
{
// Empty E is not allowed. // Empty E is not allowed.
goto fail; goto fail;
} }
read = 0; read = 0;
while ((end_not_reached = (curr != s_end)) && isdigit(*curr)) while ((end_not_reached = (curr != s_end)) && isdigit(*curr)) {
{
exponent *= 10; exponent *= 10;
exponent += static_cast<int>(*curr - 0x30); exponent += static_cast<int>(*curr - 0x30);
curr++; read++; curr++;
read++;
} }
exponent *= (exp_sign == '+' ? 1 : -1); exponent *= (exp_sign == '+' ? 1 : -1);
if (read == 0) if (read == 0)
@@ -233,7 +226,8 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result)
} }
assemble: assemble:
*result = (sign == '+'? 1 : -1) * ldexp(mantissa * pow(5.0, exponent), exponent); *result =
(sign == '+' ? 1 : -1) * ldexp(mantissa * pow(5.0, exponent), exponent);
return true; return true;
fail: fail:
return false; return false;
@@ -253,7 +247,6 @@ static inline float parseFloat(const char *&token) {
return f; return f;
} }
static inline void parseFloat2(float &x, float &y, const char *&token) { static inline void parseFloat2(float &x, float &y, const char *&token) {
x = parseFloat(token); x = parseFloat(token);
y = parseFloat(token); y = parseFloat(token);
@@ -300,14 +293,14 @@ static vertex_index parseTriple(const char *&token, int vsize, int vnsize,
return vi; return vi;
} }
static vertex_index static unsigned int
updateVertex(std::map<vertex_index, vertex_index> &vertexCache, updateVertex(std::map<vertex_index, unsigned int> &vertexCache,
std::vector<float> &positions, std::vector<float> &normals, std::vector<float> &positions, std::vector<float> &normals,
std::vector<float> &texcoords, std::vector<float> &texcoords,
const std::vector<float> &in_positions, const std::vector<float> &in_positions,
const std::vector<float> &in_normals, const std::vector<float> &in_normals,
const std::vector<float> &in_texcoords, const vertex_index &i) { const std::vector<float> &in_texcoords, const vertex_index &i) {
const std::map<vertex_index, vertex_index>::iterator it = vertexCache.find(i); const std::map<vertex_index, unsigned int>::iterator it = vertexCache.find(i);
if (it != vertexCache.end()) { if (it != vertexCache.end()) {
// found cache // found cache
@@ -331,14 +324,10 @@ updateVertex(std::map<vertex_index, vertex_index> &vertexCache,
texcoords.push_back(in_texcoords[2 * i.vt_idx + 1]); texcoords.push_back(in_texcoords[2 * i.vt_idx + 1]);
} }
unsigned int v_idx = static_cast<unsigned int>(positions.size() / 3 - 1); unsigned int idx = static_cast<unsigned int>(positions.size() / 3 - 1);
unsigned int vn_idx = static_cast<unsigned int>(normals.size() / 3 - 1); vertexCache[i] = idx;
unsigned int vt_idx = static_cast<unsigned int>(texcoords.size() / 2 - 1);
vertexCache[i].v_idx = v_idx;
vertexCache[i].vn_idx = vn_idx;
vertexCache[i].vt_idx = vt_idx;
return vertexCache[i]; return idx;
} }
void InitMaterial(material_t &material) { void InitMaterial(material_t &material) {
@@ -362,7 +351,7 @@ void InitMaterial(material_t &material) {
} }
static bool exportFaceGroupToShape( static bool exportFaceGroupToShape(
shape_t &shape, std::map<vertex_index, vertex_index> vertexCache, shape_t &shape, std::map<vertex_index, unsigned int> vertexCache,
const std::vector<float> &in_positions, const std::vector<float> &in_positions,
const std::vector<float> &in_normals, const std::vector<float> &in_normals,
const std::vector<float> &in_texcoords, const std::vector<float> &in_texcoords,
@@ -387,27 +376,19 @@ static bool exportFaceGroupToShape(
i1 = i2; i1 = i2;
i2 = face[k]; i2 = face[k];
vertex_index vi0 = updateVertex( unsigned int v0 = updateVertex(
vertexCache, shape.mesh.positions, shape.mesh.normals, vertexCache, shape.mesh.positions, shape.mesh.normals,
shape.mesh.texcoords, in_positions, in_normals, in_texcoords, i0); shape.mesh.texcoords, in_positions, in_normals, in_texcoords, i0);
vertex_index vi1 = updateVertex( unsigned int v1 = updateVertex(
vertexCache, shape.mesh.positions, shape.mesh.normals, vertexCache, shape.mesh.positions, shape.mesh.normals,
shape.mesh.texcoords, in_positions, in_normals, in_texcoords, i1); shape.mesh.texcoords, in_positions, in_normals, in_texcoords, i1);
vertex_index vi2 = updateVertex( unsigned int v2 = updateVertex(
vertexCache, shape.mesh.positions, shape.mesh.normals, vertexCache, shape.mesh.positions, shape.mesh.normals,
shape.mesh.texcoords, in_positions, in_normals, in_texcoords, i2); shape.mesh.texcoords, in_positions, in_normals, in_texcoords, i2);
shape.mesh.indices.push_back(vi0.v_idx); shape.mesh.indices.push_back(v0);
shape.mesh.indices.push_back(vi1.v_idx); shape.mesh.indices.push_back(v1);
shape.mesh.indices.push_back(vi2.v_idx); shape.mesh.indices.push_back(v2);
shape.mesh.normal_indices.push_back(vi0.vn_idx);
shape.mesh.normal_indices.push_back(vi1.vn_idx);
shape.mesh.normal_indices.push_back(vi2.vn_idx);
shape.mesh.texcoord_indices.push_back(vi0.vt_idx);
shape.mesh.texcoord_indices.push_back(vi1.vt_idx);
shape.mesh.texcoord_indices.push_back(vi2.vt_idx);
shape.mesh.material_ids.push_back(material_id); shape.mesh.material_ids.push_back(material_id);
} }
@@ -426,7 +407,9 @@ std::string LoadMtl(std::map<std::string, int> &material_map,
std::istream &inStream) { std::istream &inStream) {
std::stringstream err; std::stringstream err;
// Create a default material anyway.
material_t material; material_t material;
InitMaterial(material);
int maxchars = 8192; // Alloc enough size. int maxchars = 8192; // Alloc enough size.
std::vector<char> buf(maxchars); // Alloc enough size. std::vector<char> buf(maxchars); // Alloc enough size.
@@ -465,8 +448,8 @@ std::string LoadMtl(std::map<std::string, int> &material_map,
if ((0 == strncmp(token, "newmtl", 6)) && isSpace((token[6]))) { if ((0 == strncmp(token, "newmtl", 6)) && isSpace((token[6]))) {
// flush previous material. // flush previous material.
if (!material.name.empty()) { if (!material.name.empty()) {
material_map.insert( material_map.insert(std::pair<std::string, int>(
std::pair<std::string, int>(material.name, static_cast<int>(materials.size()))); material.name, static_cast<int>(materials.size())));
materials.push_back(material); materials.push_back(material);
} }
@@ -570,7 +553,7 @@ std::string LoadMtl(std::map<std::string, int> &material_map,
if (token[0] == 'T' && token[1] == 'r' && isSpace(token[2])) { if (token[0] == 'T' && token[1] == 'r' && isSpace(token[2])) {
token += 2; token += 2;
// Invert value of Tr(assume Tr is in range [0, 1]) // Invert value of Tr(assume Tr is in range [0, 1])
material.dissolve = 1.0 - parseFloat(token); material.dissolve = 1.0f - parseFloat(token);
continue; continue;
} }
@@ -616,8 +599,8 @@ std::string LoadMtl(std::map<std::string, int> &material_map,
} }
} }
// flush last material. // flush last material.
material_map.insert( material_map.insert(std::pair<std::string, int>(
std::pair<std::string, int>(material.name, static_cast<int>(materials.size()))); material.name, static_cast<int>(materials.size())));
materials.push_back(material); materials.push_back(material);
return err.str(); return err.str();
@@ -635,10 +618,17 @@ std::string MaterialFileReader::operator()(const std::string &matId,
} }
std::ifstream matIStream(filepath.c_str()); std::ifstream matIStream(filepath.c_str());
return LoadMtl(matMap, materials, matIStream); std::string err = LoadMtl(matMap, materials, matIStream);
if (!matIStream) {
std::stringstream ss;
ss << "WARN: Material file [ " << filepath
<< " ] not found. Created a default material.";
err += ss.str();
}
return err;
} }
std::string LoadObj(std::vector<shape_t> &shapes, std::string LoadObj(std::vector<shape_t> &shapes, std::vector<curve_t> &curves,
std::vector<material_t> &materials, // [output] std::vector<material_t> &materials, // [output]
const char *filename, const char *mtl_basepath) { const char *filename, const char *mtl_basepath) {
@@ -658,10 +648,11 @@ std::string LoadObj(std::vector<shape_t> &shapes,
} }
MaterialFileReader matFileReader(basePath); MaterialFileReader matFileReader(basePath);
return LoadObj(shapes, materials, ifs, matFileReader); return LoadObj(shapes, curves, materials, ifs, matFileReader);
} }
std::string LoadObj(std::vector<shape_t> &shapes, std::string LoadObj(std::vector<shape_t> &shapes, // [output]
std::vector<curve_t> &curves, // [output]
std::vector<material_t> &materials, // [output] std::vector<material_t> &materials, // [output]
std::istream &inStream, MaterialReader &readMatFn) { std::istream &inStream, MaterialReader &readMatFn) {
std::stringstream err; std::stringstream err;
@@ -672,9 +663,16 @@ std::string LoadObj(std::vector<shape_t> &shapes,
std::vector<std::vector<vertex_index> > faceGroup; std::vector<std::vector<vertex_index> > faceGroup;
std::string name; std::string name;
// curve
int curveType = -1;
int curveDegree = -1;
std::vector<unsigned int> curve_indices;
std::vector<float> u_params;
std::vector<float> v_params;
// material // material
std::map<std::string, int> material_map; std::map<std::string, int> material_map;
std::map<vertex_index, vertex_index> vertexCache; std::map<vertex_index, unsigned int> vertexCache;
int material = -1; int material = -1;
shape_t shape; shape_t shape;
@@ -751,8 +749,9 @@ std::string LoadObj(std::vector<shape_t> &shapes,
std::vector<vertex_index> face; std::vector<vertex_index> face;
while (!isNewLine(token[0])) { while (!isNewLine(token[0])) {
vertex_index vi = vertex_index vi = parseTriple(token, static_cast<int>(v.size() / 3),
parseTriple(token, static_cast<int>(v.size() / 3), static_cast<int>(vn.size() / 3), static_cast<int>(vt.size() / 2)); static_cast<int>(vn.size() / 3),
static_cast<int>(vt.size() / 2));
face.push_back(vi); face.push_back(vi);
size_t n = strspn(token, " \t\r"); size_t n = strspn(token, " \t\r");
token += n; token += n;
@@ -873,6 +872,107 @@ std::string LoadObj(std::vector<shape_t> &shapes,
continue; continue;
} }
// curve type
if ((0 == strncmp(token, "cstype", 6)) && isSpace((token[6]))) {
token += 7;
std::string type = parseString(token);
if (type == "bspline") { // 0
curveType = 0;
} else if (type == "bezier") { // 1
curveType = 1;
} else if (type == "cardinal") {
curveType = 2;
}
}
// degree(curve)
if (token[0] == 'd' && token[1] == 'e' && token[2] == 'g' &&
isSpace((token[3]))) {
token += 4;
curveDegree = parseInt(token);
continue;
}
// curve
if (token[0] == 'c' && token[1] == 'u' && token[2] == 'r' &&
token[3] == 'v' && isSpace((token[4]))) {
token += 5;
// parse u0 and u1
float u0, u1;
parseFloat2(u0, u1, token);
size_t n = strspn(token, " \t\r");
token += n;
// parse vertex indices.
curve_indices.clear();
while (!isNewLine(token[0])) {
vertex_index vi = parseTriple(token, static_cast<int>(v.size() / 3),
static_cast<int>(vn.size() / 3),
static_cast<int>(vt.size() / 2));
curve_indices.push_back(vi.v_idx);
size_t n = strspn(token, " \t\r");
token += n;
}
continue;
}
// curve param
if ((0 == strncmp(token, "parm", 4)) && isSpace((token[4]))) {
token += 5;
std::string name = parseString(token);
if (name == "u") {
u_params.clear();
} else if (name == "v") {
v_params.clear();
}
// parse parameter values.
while (!isNewLine(token[0])) {
float value = parseFloat(token);
if (name == "u") {
u_params.push_back(value);
} else if (name == "v") {
v_params.push_back(value);
}
}
continue;
}
// end(curve)
if (token[0] == 'e' && token[1] == 'n' && token[2] == 'd') {
// size_t n = strspn(token, " \t\r");
// token += n;
// @todo { Consider 'end' at the end of line: parm u 0 0 0 0 ... end }
if ((curveType > -1) && (curveDegree > 0) && (curve_indices.size() > 1)) {
curve_t curve;
curve.name = name;
curve.type = curveType;
curve.degree = curveDegree;
curve.positions = v;
curve.indices = curve_indices;
curve.u_params = u_params;
curve.v_params = v_params;
curves.push_back(curve);
}
u_params.clear();
v_params.clear();
curve_indices.clear();
continue;
}
// Ignore unknown command. // Ignore unknown command.
} }
@@ -885,4 +985,5 @@ std::string LoadObj(std::vector<shape_t> &shapes,
return err.str(); return err.str();
} }
}
} // namespace

View File

@@ -37,12 +37,25 @@ typedef struct {
std::vector<float> positions; std::vector<float> positions;
std::vector<float> normals; std::vector<float> normals;
std::vector<float> texcoords; std::vector<float> texcoords;
std::vector<unsigned int> indices; // indices for vertex std::vector<unsigned int> indices;
std::vector<unsigned int> normal_indices; // indices for normal
std::vector<unsigned int> texcoord_indices; // indices for texcoord
std::vector<int> material_ids; // per-mesh material ID std::vector<int> material_ids; // per-mesh material ID
} mesh_t; } mesh_t;
typedef struct {
std::vector<float> positions;
std::vector<unsigned int> indices;
} line_t;
typedef struct {
std::string name;
std::vector<float> positions; // control points. xyz
std::vector<unsigned int> indices; // index to control point
std::vector<float> u_params;
std::vector<float> v_params;
int degree;
int type; // 0: bspline, 1: bezier, 2: cardinal
} curve_t;
typedef struct { typedef struct {
std::string name; std::string name;
mesh_t mesh; mesh_t mesh;
@@ -73,10 +86,12 @@ private:
/// Loads .obj from a file. /// Loads .obj from a file.
/// 'shapes' will be filled with parsed shape data /// 'shapes' will be filled with parsed shape data
/// 'curves' will be filled with parsed curve data(NURBS, Bezier, etc)
/// The function returns error string. /// The function returns error string.
/// Returns empty string when loading .obj success. /// Returns empty string when loading .obj success.
/// 'mtl_basepath' is optional, and used for base path for .mtl file. /// 'mtl_basepath' is optional, and used for base path for .mtl file.
std::string LoadObj(std::vector<shape_t> &shapes, // [output] std::string LoadObj(std::vector<shape_t> &shapes, // [output]
std::vector<curve_t> &curves, // [output]
std::vector<material_t> &materials, // [output] std::vector<material_t> &materials, // [output]
const char *filename, const char *mtl_basepath = NULL); const char *filename, const char *mtl_basepath = NULL);
@@ -84,6 +99,7 @@ std::string LoadObj(std::vector<shape_t> &shapes, // [output]
/// std::istream for materials. /// std::istream for materials.
/// Returns empty string when loading .obj success. /// Returns empty string when loading .obj success.
std::string LoadObj(std::vector<shape_t> &shapes, // [output] std::string LoadObj(std::vector<shape_t> &shapes, // [output]
std::vector<curve_t> &curves, // [output]
std::vector<material_t> &materials, // [output] std::vector<material_t> &materials, // [output]
std::istream &inStream, MaterialReader &readMatFn); std::istream &inStream, MaterialReader &readMatFn);