4 Commits

Author SHA1 Message Date
Syoyo Fujita
609139f370 Fix compilation on recent clang. 2017-12-28 18:45:14 +09:00
Syoyo Fujita
cc73127bf0 Merge branch 'tigrazone' into devel 2017-12-13 18:37:37 +09:00
Syoyo Fujita
7b6e33da52 Rename vector type.
Refactor file layout of extperimental code.
2017-11-12 16:46:25 +09:00
Richard Fabian
5896933508 Added ear clipping triangulation. Added a tiny vertex type to the library to facilitate the change. 2017-11-11 22:37:52 +00:00
16 changed files with 239 additions and 74 deletions

View File

@@ -26,11 +26,20 @@ Old version is available `v0.9.x` branch https://github.com/syoyo/tinyobjloader/
## What's new ## What's new
### Version 2.x
* Refactor API
* Support triangulation for concave polygons(#151)
### Version 1.x
Avaiable in `v1.x.y` branch.
* 20 Aug, 2016 : Bump version v1.0.0. New data structure and API! * 20 Aug, 2016 : Bump version v1.0.0. New data structure and API!
### Old version ### Older version
Previous old version is avaiable in `v0.9.x` branch. Older version is avaiable in `v0.9.x` branch.
## Example ## Example
@@ -49,7 +58,11 @@ http://casual-effects.com/data/index.html
TinyObjLoader is successfully used in ... TinyObjLoader is successfully used in ...
### New version(v1.0.x) ### New version(v2.x)
* Your project here! (Letting us know via github issue is welcome!)
### Old version(v1.x)
* Double precision support through `TINYOBJLOADER_USE_DOUBLE` thanks to noma * Double precision support through `TINYOBJLOADER_USE_DOUBLE` thanks to noma
* Loading models in Vulkan Tutorial https://vulkan-tutorial.com/Loading_models * Loading models in Vulkan Tutorial https://vulkan-tutorial.com/Loading_models
@@ -60,7 +73,7 @@ TinyObjLoader is successfully used in ...
* VFPR - a Vulkan Forward Plus Renderer : https://github.com/WindyDarian/Vulkan-Forward-Plus-Renderer * VFPR - a Vulkan Forward Plus Renderer : https://github.com/WindyDarian/Vulkan-Forward-Plus-Renderer
* Your project here! (Letting us know via github issue is welcome!) * Your project here! (Letting us know via github issue is welcome!)
### Old version(v0.9.x) ### Older version(v0.9.x)
* bullet3 https://github.com/erwincoumans/bullet3 * bullet3 https://github.com/erwincoumans/bullet3
* pbrt-v2 https://github.com/mmp/pbrt-v2 * pbrt-v2 https://github.com/mmp/pbrt-v2
@@ -228,12 +241,12 @@ for (size_t s = 0; s < shapes.size(); s++) {
for (size_t v = 0; v < fv; v++) { for (size_t v = 0; v < fv; v++) {
// access to vertex // access to vertex
tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v]; tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
tinyobj::real_t vx = attrib.vertices[3*idx.vertex_index+0]; tinyobj::real_t vx = attrib.vertices[idx.vertex_index].x;
tinyobj::real_t vy = attrib.vertices[3*idx.vertex_index+1]; tinyobj::real_t vy = attrib.vertices[idx.vertex_index].y;
tinyobj::real_t vz = attrib.vertices[3*idx.vertex_index+2]; tinyobj::real_t vz = attrib.vertices[idx.vertex_index].z;
tinyobj::real_t nx = attrib.normals[3*idx.normal_index+0]; tinyobj::real_t nx = attrib.normals[idx.normal_index].x;
tinyobj::real_t ny = attrib.normals[3*idx.normal_index+1]; tinyobj::real_t ny = attrib.normals[idx.normal_index].y;
tinyobj::real_t nz = attrib.normals[3*idx.normal_index+2]; tinyobj::real_t nz = attrib.normals[idx.normal_index].z;
tinyobj::real_t tx = attrib.texcoords[2*idx.texcoord_index+0]; tinyobj::real_t tx = attrib.texcoords[2*idx.texcoord_index+0];
tinyobj::real_t ty = attrib.texcoords[2*idx.texcoord_index+1]; tinyobj::real_t ty = attrib.texcoords[2*idx.texcoord_index+1];
// Optional: vertex colors // Optional: vertex colors

View File

@@ -29,6 +29,12 @@ extern "C" {
#endif #endif
#endif #endif
#ifdef __clang__
#if __has_warning("-Wzero-as-null-pointer-constant")
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif
#endif
class timerutil { class timerutil {
public: public:
#ifdef _WIN32 #ifdef _WIN32
@@ -106,24 +112,24 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
std::cout << "# of shapes : " << shapes.size() << std::endl; std::cout << "# of shapes : " << shapes.size() << std::endl;
std::cout << "# of materials : " << materials.size() << std::endl; std::cout << "# of materials : " << materials.size() << std::endl;
for (size_t v = 0; v < attrib.vertices.size() / 3; v++) { for (size_t v = 0; v < attrib.vertices.size(); v++) {
printf(" v[%ld] = (%f, %f, %f)\n", static_cast<long>(v), printf(" v[%ld] = (%f, %f, %f)\n", static_cast<long>(v),
static_cast<const double>(attrib.vertices[3 * v + 0]), static_cast<const double>(attrib.vertices[v].x),
static_cast<const double>(attrib.vertices[3 * v + 1]), static_cast<const double>(attrib.vertices[v].y),
static_cast<const double>(attrib.vertices[3 * v + 2])); static_cast<const double>(attrib.vertices[v].z));
} }
for (size_t v = 0; v < attrib.normals.size() / 3; v++) { for (size_t v = 0; v < attrib.normals.size(); v++) {
printf(" n[%ld] = (%f, %f, %f)\n", static_cast<long>(v), printf(" n[%ld] = (%f, %f, %f)\n", static_cast<long>(v),
static_cast<const double>(attrib.normals[3 * v + 0]), static_cast<const double>(attrib.normals[v].x),
static_cast<const double>(attrib.normals[3 * v + 1]), static_cast<const double>(attrib.normals[v].y),
static_cast<const double>(attrib.normals[3 * v + 2])); static_cast<const double>(attrib.normals[v].z));
} }
for (size_t v = 0; v < attrib.texcoords.size() / 2; v++) { for (size_t v = 0; v < attrib.texcoords.size(); v++) {
printf(" uv[%ld] = (%f, %f)\n", static_cast<long>(v), printf(" uv[%ld] = (%f, %f)\n", static_cast<long>(v),
static_cast<const double>(attrib.texcoords[2 * v + 0]), static_cast<const double>(attrib.texcoords[v].x),
static_cast<const double>(attrib.texcoords[2 * v + 1])); static_cast<const double>(attrib.texcoords[v].y));
} }
// For each shape // For each shape

View File

@@ -22,22 +22,22 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
for (size_t v = 0; v < attrib.vertices.size() / 3; v++) { for (size_t v = 0; v < attrib.vertices.size() / 3; v++) {
printf(" v[%ld] = (%f, %f, %f)\n", v, printf(" v[%ld] = (%f, %f, %f)\n", v,
static_cast<const double>(attrib.vertices[3*v+0]), static_cast<const double>(attrib.vertices[v].x),
static_cast<const double>(attrib.vertices[3*v+1]), static_cast<const double>(attrib.vertices[v].y),
static_cast<const double>(attrib.vertices[3*v+2])); static_cast<const double>(attrib.vertices[v].z));
} }
for (size_t v = 0; v < attrib.normals.size() / 3; v++) { for (size_t v = 0; v < attrib.normals.size() / 3; v++) {
printf(" n[%ld] = (%f, %f, %f)\n", v, printf(" n[%ld] = (%f, %f, %f)\n", v,
static_cast<const double>(attrib.normals[3*v+0]), static_cast<const double>(attrib.normals[v].x),
static_cast<const double>(attrib.normals[3*v+1]), static_cast<const double>(attrib.normals[v].y),
static_cast<const double>(attrib.normals[3*v+2])); static_cast<const double>(attrib.normals[v].z));
} }
for (size_t v = 0; v < attrib.texcoords.size() / 2; v++) { for (size_t v = 0; v < attrib.texcoords.size() / 2; v++) {
printf(" uv[%ld] = (%f, %f)\n", v, printf(" uv[%ld] = (%f, %f)\n", v,
static_cast<const double>(attrib.texcoords[2*v+0]), static_cast<const double>(attrib.texcoords[v].x),
static_cast<const double>(attrib.texcoords[2*v+1])); static_cast<const double>(attrib.texcoords[v].y));
} }
for (size_t i = 0; i < shapes.size(); i++) { for (size_t i = 0; i < shapes.size(); i++) {

View File

@@ -23,7 +23,8 @@ THE SOFTWARE.
*/ */
// //
// version 1.1.2 : Used new hashed keywords // version 2.1.0 : Used new hashed keywords
// version 2.0.0 : New API! And support triangulation of concave polygon(#151).
// version 1.1.0 : Support parsing vertex color(#144) // version 1.1.0 : Support parsing vertex color(#144)
// version 1.0.8 : Fix parsing `g` tag just after `usemtl`(#138) // version 1.0.8 : Fix parsing `g` tag just after `usemtl`(#138)
// version 1.0.7 : Support multiple tex options(#126) // version 1.0.7 : Support multiple tex options(#126)
@@ -52,6 +53,13 @@ THE SOFTWARE.
namespace tinyobj { namespace tinyobj {
#ifdef __clang__
#pragma clang diagnostic push
#if __has_warning("-Wzero-as-null-pointer-constant")
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif
#endif
// https://en.wikipedia.org/wiki/Wavefront_.obj_file says ... // https://en.wikipedia.org/wiki/Wavefront_.obj_file says ...
// //
// -blendu on | off # set horizontal texture blending // -blendu on | off # set horizontal texture blending
@@ -109,6 +117,44 @@ typedef double real_t;
typedef float real_t; typedef float real_t;
#endif #endif
struct vec3_t {
real_t x, y, z;
};
inline vec3_t operator+(const vec3_t &l, const vec3_t &r) {
vec3_t o;
o.x = l.x + r.x;
o.y = l.y + r.y;
o.z = l.z + r.z;
return o;
}
inline vec3_t operator-(const vec3_t &l, const vec3_t &r) {
vec3_t o;
o.x = l.x - r.x;
o.y = l.y - r.y;
o.z = l.z - r.z;
return o;
}
inline vec3_t operator*(const vec3_t &l, real_t r) {
vec3_t o;
o.x = l.x * r;
o.y = l.y * r;
o.z = l.z * r;
return o;
}
inline vec3_t cross(const vec3_t &l, const vec3_t &r) {
vec3_t o;
o.x = l.y * r.z - r.y * l.z;
o.y = l.z * r.x - r.z * l.x;
o.z = l.x * r.y - r.x * l.y;
return o;
}
inline real_t dot(const vec3_t &l, const vec3_t &r) {
return l.x * r.x + l.y * r.y + l.z * r.z;
}
struct vec2_t {
real_t x, y;
};
typedef enum { typedef enum {
TEXTURE_TYPE_NONE, // default TEXTURE_TYPE_NONE, // default
TEXTURE_TYPE_SPHERE, TEXTURE_TYPE_SPHERE,
@@ -229,9 +275,9 @@ typedef struct {
// Vertex attributes // Vertex attributes
typedef struct { typedef struct {
std::vector<real_t> vertices; // 'v' std::vector<vec3_t> vertices; // 'v'
std::vector<real_t> normals; // 'vn' std::vector<vec3_t> normals; // 'vn'
std::vector<real_t> texcoords; // 'vt' std::vector<vec2_t> texcoords; // 'vt'
std::vector<real_t> colors; // extension: vertex colors std::vector<real_t> colors; // extension: vertex colors
} attrib_t; } attrib_t;
@@ -1311,7 +1357,7 @@ static void InitMaterial(material_t *material) {
static bool exportFaceGroupToShape( static bool exportFaceGroupToShape(
shape_t *shape, const std::vector<std::vector<vertex_index> > &faceGroup, shape_t *shape, const std::vector<std::vector<vertex_index> > &faceGroup,
const std::vector<tag_t> &tags, const int material_id, const std::vector<tag_t> &tags, const int material_id,
const std::string &name, bool triangulate) { const std::string &name, bool triangulate, const std::vector<vec3_t> &v) {
if (faceGroup.empty()) { if (faceGroup.empty()) {
return false; return false;
} }
@@ -1327,28 +1373,128 @@ static bool exportFaceGroupToShape(
size_t npolys = face.size(); size_t npolys = face.size();
if (triangulate) { if (triangulate) {
// Polygon -> triangle fan conversion vec3_t face_normal = {0, 0, 0};
for (size_t k = 2; k < npolys; k++) {
i1 = i2;
i2 = face[k];
index_t idx0, idx1, idx2; for (size_t k = 0; k < npolys; ++k) {
idx0.vertex_index = i0.v_idx; int vi0 = face[(k + 0) % npolys].v_idx;
idx0.normal_index = i0.vn_idx; int vi1 = face[(k + 1) % npolys].v_idx;
idx0.texcoord_index = i0.vt_idx; int vi2 = face[(k + 2) % npolys].v_idx;
idx1.vertex_index = i1.v_idx; const vec3_t &v0 = v[size_t(vi0)];
idx1.normal_index = i1.vn_idx; const vec3_t &v1 = v[size_t(vi1)];
idx1.texcoord_index = i1.vt_idx; const vec3_t &v2 = v[size_t(vi2)];
idx2.vertex_index = i2.v_idx; const vec3_t e0 = v1 - v0;
idx2.normal_index = i2.vn_idx; const vec3_t e1 = v2 - v1;
idx2.texcoord_index = i2.vt_idx; const vec3_t n = cross(e0, e1);
face_normal = face_normal + n;
}
shape->mesh.indices.push_back(idx0); // face_normal is currently area of face
shape->mesh.indices.push_back(idx1); {
shape->mesh.indices.push_back(idx2); real_t l = std::sqrt(dot(face_normal, face_normal));
face_normal = face_normal * (static_cast<real_t>(1.0) / l);
}
shape->mesh.num_face_vertices.push_back(3); int maxRounds =
shape->mesh.material_ids.push_back(material_id); 10; // arbitrary max loop count to protect against unexpected errors
std::vector<vertex_index> remainingFace = face;
size_t guess_vert = 0;
while (remainingFace.size() > 3 && maxRounds > 0) {
npolys = remainingFace.size();
if (guess_vert >= npolys) {
maxRounds -= 1;
guess_vert -= npolys;
}
i0 = remainingFace[(guess_vert + 0) % npolys];
i1 = remainingFace[(guess_vert + 1) % npolys];
i2 = remainingFace[(guess_vert + 2) % npolys];
int vi0 = i0.v_idx;
int vi1 = i1.v_idx;
int vi2 = i2.v_idx;
const vec3_t &v0 = v[size_t(vi0)];
const vec3_t &v1 = v[size_t(vi1)];
const vec3_t &v2 = v[size_t(vi2)];
const vec3_t e0 = v1 - v0;
const vec3_t e1 = v2 - v1;
const vec3_t n = cross(e0, e1);
bool internal = dot(n, face_normal) < 0.0f;
if (internal) {
guess_vert += 1;
continue;
}
// check all other verts in case they are inside this triangle
bool overlap = false;
for (size_t otherVert = 3; otherVert < npolys; ++otherVert) {
int ovi = remainingFace[(guess_vert + otherVert) % npolys].v_idx;
const vec3_t &ov = v[size_t(ovi)];
if (dot(face_normal, cross(e0, ov - v0)) < 0) continue;
if (dot(face_normal, cross(e1, ov - v1)) < 0) continue;
if (dot(face_normal, cross(v0 - v2, ov - v2)) < 0) continue;
// vert inside triangle
overlap = true;
break;
}
if (overlap) {
guess_vert += 1;
continue;
}
// this triangle is an ear
{
index_t idx0, idx1, idx2;
idx0.vertex_index = i0.v_idx;
idx0.normal_index = i0.vn_idx;
idx0.texcoord_index = i0.vt_idx;
idx1.vertex_index = i1.v_idx;
idx1.normal_index = i1.vn_idx;
idx1.texcoord_index = i1.vt_idx;
idx2.vertex_index = i2.v_idx;
idx2.normal_index = i2.vn_idx;
idx2.texcoord_index = i2.vt_idx;
shape->mesh.indices.push_back(idx0);
shape->mesh.indices.push_back(idx1);
shape->mesh.indices.push_back(idx2);
shape->mesh.num_face_vertices.push_back(3);
shape->mesh.material_ids.push_back(material_id);
}
// remove v1 from the list
size_t removed_vert_index = (guess_vert + 1) % npolys;
while (removed_vert_index + 1 < npolys) {
remainingFace[removed_vert_index] =
remainingFace[removed_vert_index + 1];
removed_vert_index += 1;
}
remainingFace.pop_back();
}
if (remainingFace.size() == 3) {
i0 = remainingFace[0];
i1 = remainingFace[1];
i2 = remainingFace[2];
{
index_t idx0, idx1, idx2;
idx0.vertex_index = i0.v_idx;
idx0.normal_index = i0.vn_idx;
idx0.texcoord_index = i0.vt_idx;
idx1.vertex_index = i1.v_idx;
idx1.normal_index = i1.vn_idx;
idx1.texcoord_index = i1.vt_idx;
idx2.vertex_index = i2.v_idx;
idx2.normal_index = i2.vn_idx;
idx2.texcoord_index = i2.vt_idx;
shape->mesh.indices.push_back(idx0);
shape->mesh.indices.push_back(idx1);
shape->mesh.indices.push_back(idx2);
shape->mesh.num_face_vertices.push_back(3);
shape->mesh.material_ids.push_back(material_id);
}
} }
} else { } else {
for (size_t k = 0; k < npolys; k++) { for (size_t k = 0; k < npolys; k++) {
@@ -1952,9 +2098,9 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
bool triangulate) { bool triangulate) {
std::stringstream errss; std::stringstream errss;
std::vector<real_t> v; std::vector<vec3_t> v;
std::vector<real_t> vn; std::vector<vec3_t> vn;
std::vector<real_t> vt; std::vector<vec2_t> vt;
std::vector<real_t> vc; std::vector<real_t> vc;
std::vector<tag_t> tags; std::vector<tag_t> tags;
std::vector<std::vector<vertex_index> > faceGroup; std::vector<std::vector<vertex_index> > faceGroup;
@@ -2007,12 +2153,10 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
// vertex // vertex
if (token[0] == 'v') { if (token[0] == 'v') {
token += 2; token += 2;
real_t x, y, z; vec3_t vtx;
real_t r, g, b; real_t r, g, b;
parseVertexWithColor(&x, &y, &z, &r, &g, &b, &token); parseVertexWithColor(&vtx.x, &vtx.y, &vtx.z, &r, &g, &b, &token);
v.push_back(x); v.push_back(vtx);
v.push_back(y);
v.push_back(z);
vc.push_back(r); vc.push_back(r);
vc.push_back(g); vc.push_back(g);
@@ -2056,7 +2200,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
if (token[0] == 'g') { if (token[0] == 'g') {
// flush previous face group. // flush previous face group.
bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material,
name, triangulate); name, triangulate, v);
(void)ret; // return value not used. (void)ret; // return value not used.
if (shape.mesh.indices.size() > 0) { if (shape.mesh.indices.size() > 0) {
@@ -2093,7 +2237,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
if (token[0] == 'o') { if (token[0] == 'o') {
// flush previous face group. // flush previous face group.
bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material,
name, triangulate); name, triangulate, v);
if (ret) { if (ret) {
shapes->push_back(shape); shapes->push_back(shape);
} }
@@ -2149,21 +2293,18 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
// normal // normal
if (token[1] == 'n') { if (token[1] == 'n') {
token += 3; token += 3;
real_t x, y, z; vec3_t n;
parseReal3(&x, &y, &z, &token); parseReal3(&n.x, &n.y, &n.z, &token);
vn.push_back(x); vn.push_back(n);
vn.push_back(y);
vn.push_back(z);
continue; continue;
} }
// texcoord // texcoord
if (token[1] == 't') { if (token[1] == 't') {
token += 3; token += 3;
real_t x, y; vec2_t tcoord;
parseReal2(&x, &y, &token); parseReal2(&tcoord.x, &tcoord.y, &token);
vt.push_back(x); vt.push_back(tcoord);
vt.push_back(y);
continue; continue;
} }
} }
@@ -2208,7 +2349,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
// this time. // this time.
// just clear `faceGroup` after `exportFaceGroupToShape()` call. // just clear `faceGroup` after `exportFaceGroupToShape()` call.
exportFaceGroupToShape(&shape, faceGroup, tags, material, name, exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
triangulate); triangulate, v);
faceGroup.clear(); faceGroup.clear();
material = newMaterialId; material = newMaterialId;
} }
@@ -2266,7 +2407,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
} }
bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name, bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
triangulate); triangulate, v);
// exportFaceGroupToShape return false when `usemtl` is called in the last // exportFaceGroupToShape return false when `usemtl` is called in the last
// line. // line.
// we also add `shape` to `shapes` when `shape.mesh` has already some // we also add `shape` to `shapes` when `shape.mesh` has already some
@@ -2615,6 +2756,11 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
return true; return true;
} }
#ifdef __clang__
#pragma clang diagnostic pop
#endif
} // namespace tinyobj } // namespace tinyobj
#endif #endif