3 Commits

Author SHA1 Message Date
Syoyo Fujita
47aad5a129 Merge branch 'normal-texcoord-indices' of github.com:syoyo/tinyobjloader into normal-texcoord-indices
Conflicts:
	tiny_obj_loader.cc
2015-06-24 21:50:23 +09:00
Syoyo Fujita
8b86d6383c Add indices for texcoord and normal. 2015-06-24 21:49:38 +09:00
Syoyo Fujita
e568dd11f7 Add indices for texcoord and normal. 2015-06-24 14:22:52 +09:00
6 changed files with 632 additions and 782 deletions

View File

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

View File

@@ -1,8 +1,6 @@
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 { "x32", "x64" } platforms { "x64", "x32" }
else else
platforms { "native", "x32", "x64" } platforms { "native", "x32", "x64" }
end end

38
test.cc
View File

@@ -7,15 +7,16 @@
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::curve_t>& curves, const std::vector<tinyobj::material_t>& materials) static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, 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++) {
@@ -32,29 +33,6 @@ 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]);
@@ -87,16 +65,15 @@ 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, curves, materials, filename, basepath); std::string err = tinyobj::LoadObj(shapes, 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, curves, materials); PrintInfo(shapes, materials);
return true; return true;
} }
@@ -188,16 +165,15 @@ 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, curves, materials, objStream, matSSReader); std::string err = tinyobj::LoadObj(shapes, 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, curves, materials); PrintInfo(shapes, materials);
return true; return true;
} }

View File

@@ -5,10 +5,7 @@
// //
// //
// version 0.9.14: Initial support of parsing curve primitive. // version 0.9.12: Fix groups being ignored if they have 'usemtl' just before 'g' (#44)
// 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.
@@ -18,8 +15,7 @@
// 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 // Add support of specifying the base path to load material file.
// 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
@@ -78,10 +74,8 @@ 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) if (idx > 0) return idx - 1;
return idx - 1; if (idx == 0) return 0;
if (idx == 0)
return 0;
return n + idx; // negative value = relative return n + idx; // negative value = relative
} }
@@ -101,6 +95,7 @@ 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
@@ -128,8 +123,10 @@ 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;
} }
@@ -159,20 +156,23 @@ 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 { else if (isdigit(*curr)) { /* Pass through. */ }
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++; curr++; read++;
read++;
} }
// We must make sure we actually got something. // We must make sure we actually got something.
@@ -183,17 +183,20 @@ 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++; read++; curr++;
curr++;
} }
} else if (*curr == 'e' || *curr == 'E') { }
} else { else if (*curr == 'e' || *curr == 'E') {}
else
{
goto assemble; goto assemble;
} }
@@ -201,33 +204,36 @@ 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 { else if (isdigit(*curr)) { /* Pass through. */ }
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++; curr++; read++;
read++;
} }
exponent *= (exp_sign == '+' ? 1 : -1); exponent *= (exp_sign == '+'? 1 : -1);
if (read == 0) if (read == 0)
goto fail; goto fail;
} }
assemble: assemble:
*result = *result = (sign == '+'? 1 : -1) * ldexp(mantissa * pow(5.0, exponent), exponent);
(sign == '+' ? 1 : -1) * ldexp(mantissa * pow(5.0, exponent), exponent);
return true; return true;
fail: fail:
return false; return false;
@@ -247,6 +253,7 @@ 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);
@@ -293,14 +300,14 @@ static vertex_index parseTriple(const char *&token, int vsize, int vnsize,
return vi; return vi;
} }
static unsigned int static vertex_index
updateVertex(std::map<vertex_index, unsigned int> &vertexCache, updateVertex(std::map<vertex_index, vertex_index> &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, unsigned int>::iterator it = vertexCache.find(i); const std::map<vertex_index, vertex_index>::iterator it = vertexCache.find(i);
if (it != vertexCache.end()) { if (it != vertexCache.end()) {
// found cache // found cache
@@ -324,10 +331,14 @@ updateVertex(std::map<vertex_index, unsigned int> &vertexCache,
texcoords.push_back(in_texcoords[2 * i.vt_idx + 1]); texcoords.push_back(in_texcoords[2 * i.vt_idx + 1]);
} }
unsigned int idx = static_cast<unsigned int>(positions.size() / 3 - 1); unsigned int v_idx = static_cast<unsigned int>(positions.size() / 3 - 1);
vertexCache[i] = idx; unsigned int vn_idx = static_cast<unsigned int>(normals.size() / 3 - 1);
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 idx; return vertexCache[i];
} }
void InitMaterial(material_t &material) { void InitMaterial(material_t &material) {
@@ -351,7 +362,7 @@ void InitMaterial(material_t &material) {
} }
static bool exportFaceGroupToShape( static bool exportFaceGroupToShape(
shape_t &shape, std::map<vertex_index, unsigned int> vertexCache, shape_t &shape, std::map<vertex_index, vertex_index> 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,
@@ -376,19 +387,27 @@ static bool exportFaceGroupToShape(
i1 = i2; i1 = i2;
i2 = face[k]; i2 = face[k];
unsigned int v0 = updateVertex( vertex_index vi0 = 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);
unsigned int v1 = updateVertex( vertex_index vi1 = 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);
unsigned int v2 = updateVertex( vertex_index vi2 = 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(v0); shape.mesh.indices.push_back(vi0.v_idx);
shape.mesh.indices.push_back(v1); shape.mesh.indices.push_back(vi1.v_idx);
shape.mesh.indices.push_back(v2); shape.mesh.indices.push_back(vi2.v_idx);
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);
} }
@@ -407,9 +426,7 @@ 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.
@@ -448,8 +465,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(std::pair<std::string, int>( material_map.insert(
material.name, static_cast<int>(materials.size()))); std::pair<std::string, int>(material.name, static_cast<int>(materials.size())));
materials.push_back(material); materials.push_back(material);
} }
@@ -553,7 +570,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.0f - parseFloat(token); material.dissolve = 1.0 - parseFloat(token);
continue; continue;
} }
@@ -599,8 +616,8 @@ std::string LoadMtl(std::map<std::string, int> &material_map,
} }
} }
// flush last material. // flush last material.
material_map.insert(std::pair<std::string, int>( material_map.insert(
material.name, static_cast<int>(materials.size()))); std::pair<std::string, int>(material.name, static_cast<int>(materials.size())));
materials.push_back(material); materials.push_back(material);
return err.str(); return err.str();
@@ -618,17 +635,10 @@ std::string MaterialFileReader::operator()(const std::string &matId,
} }
std::ifstream matIStream(filepath.c_str()); std::ifstream matIStream(filepath.c_str());
std::string err = LoadMtl(matMap, materials, matIStream); return 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::vector<curve_t> &curves, std::string LoadObj(std::vector<shape_t> &shapes,
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) {
@@ -648,11 +658,10 @@ std::string LoadObj(std::vector<shape_t> &shapes, std::vector<curve_t> &curves,
} }
MaterialFileReader matFileReader(basePath); MaterialFileReader matFileReader(basePath);
return LoadObj(shapes, curves, materials, ifs, matFileReader); return LoadObj(shapes, materials, ifs, matFileReader);
} }
std::string LoadObj(std::vector<shape_t> &shapes, // [output] std::string LoadObj(std::vector<shape_t> &shapes,
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;
@@ -663,16 +672,9 @@ std::string LoadObj(std::vector<shape_t> &shapes, // [output]
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, unsigned int> vertexCache; std::map<vertex_index, vertex_index> vertexCache;
int material = -1; int material = -1;
shape_t shape; shape_t shape;
@@ -749,9 +751,8 @@ std::string LoadObj(std::vector<shape_t> &shapes, // [output]
std::vector<vertex_index> face; std::vector<vertex_index> face;
while (!isNewLine(token[0])) { while (!isNewLine(token[0])) {
vertex_index vi = parseTriple(token, static_cast<int>(v.size() / 3), vertex_index vi =
static_cast<int>(vn.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>(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;
@@ -872,107 +873,6 @@ std::string LoadObj(std::vector<shape_t> &shapes, // [output]
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.
} }
@@ -985,5 +885,4 @@ std::string LoadObj(std::vector<shape_t> &shapes, // [output]
return err.str(); return err.str();
} }
}
} // namespace

View File

@@ -37,25 +37,12 @@ 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; std::vector<unsigned int> indices; // indices for vertex
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;
@@ -86,12 +73,10 @@ 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);
@@ -99,7 +84,6 @@ 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);