Compare commits
58 Commits
smoothing-
...
warn
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
934788785e | ||
|
|
e07a835f02 | ||
|
|
1cdfd786d8 | ||
|
|
803b65b8a0 | ||
|
|
50518a515b | ||
|
|
c9b1bccf97 | ||
|
|
1f17833657 | ||
|
|
bb58a8f8c3 | ||
|
|
b1f594d682 | ||
|
|
02df4943f9 | ||
|
|
826a892d0b | ||
|
|
68350e2fc7 | ||
|
|
7d20e9b901 | ||
|
|
fdc70abdc6 | ||
|
|
8a885e14b8 | ||
|
|
cd65de860b | ||
|
|
4924857fd3 | ||
|
|
1a7bdc6192 | ||
|
|
fd06fa49e4 | ||
|
|
0d68262246 | ||
|
|
0e950513a3 | ||
|
|
a4b115a584 | ||
|
|
bb3e27d4f3 | ||
|
|
c4e7e65acb | ||
|
|
eba327b9c0 | ||
|
|
3cbf45a572 | ||
|
|
6650dbf397 | ||
|
|
7befd59de4 | ||
|
|
c5b3139653 | ||
|
|
adb2309110 | ||
|
|
3edca81a75 | ||
|
|
7a88cddefc | ||
|
|
d541711a79 | ||
|
|
d1ce2082f6 | ||
|
|
8fd9f6e57b | ||
|
|
24bd8b49ff | ||
|
|
4a0e79985d | ||
|
|
06f6139d1f | ||
|
|
0c8db8ee23 | ||
|
|
64d1e3f883 | ||
|
|
a39a6b481c | ||
|
|
bce1bb8387 | ||
|
|
7fcfafb39a | ||
|
|
5eda671225 | ||
|
|
f95510b04b | ||
|
|
94f1dc15b3 | ||
|
|
a4eabf54b1 | ||
|
|
72c38f64d5 | ||
|
|
d174e625a2 | ||
|
|
707014f843 | ||
|
|
12837cc8b2 | ||
|
|
b85714b4cf | ||
|
|
e060b4f4aa | ||
|
|
2dca72724f | ||
|
|
72529f02fe | ||
|
|
16f3041c76 | ||
|
|
73e9b4dc3a | ||
|
|
5113cd65cf |
@@ -129,6 +129,7 @@ int main(int argc, char **argv) {
|
||||
cb.object_cb = object_cb;
|
||||
|
||||
MyMesh mesh;
|
||||
std::string warn;
|
||||
std::string err;
|
||||
std::string filename = "../../models/cornell_box.obj";
|
||||
if (argc > 1) {
|
||||
@@ -143,7 +144,11 @@ int main(int argc, char **argv) {
|
||||
|
||||
tinyobj::MaterialFileReader mtlReader("../../models/");
|
||||
|
||||
bool ret = tinyobj::LoadObjWithCallback(ifs, cb, &mesh, &mtlReader, &err);
|
||||
bool ret = tinyobj::LoadObjWithCallback(ifs, cb, &mesh, &mtlReader, &warn, &err);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
//
|
||||
// Stiches multiple .obj files into one .obj.
|
||||
//
|
||||
#include "../../tiny_obj_loader.h"
|
||||
#include "obj_writer.h"
|
||||
|
||||
#include "../../tiny_obj_loader.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
@@ -11,27 +12,59 @@
|
||||
|
||||
typedef std::vector<tinyobj::shape_t> Shape;
|
||||
typedef std::vector<tinyobj::material_t> Material;
|
||||
typedef tinyobj::attrib_t Attribute;
|
||||
|
||||
void
|
||||
StichObjs(
|
||||
tinyobj::attrib_t& out_attribute,
|
||||
std::vector<tinyobj::shape_t>& out_shape,
|
||||
std::vector<tinyobj::material_t>& out_material,
|
||||
const std::vector<Attribute>& attributes,
|
||||
const std::vector<Shape>& shapes,
|
||||
const std::vector<Material>& materials)
|
||||
{
|
||||
int numShapes = 0;
|
||||
for (size_t i = 0; i < shapes.size(); i++) {
|
||||
numShapes += (int)shapes[i].size();
|
||||
// The amount of attributes, shape-vectors and material-vecotrs should be the same.
|
||||
if(attributes.size() != shapes.size() && attributes.size() != materials.size()){
|
||||
std::cerr << "Size of attributes, shapes and Materials don't fit!" << attributes.size() << " " << shapes.size() <<" " << materials.size() << std::endl;;
|
||||
exit(1);
|
||||
}
|
||||
int num_shapes = 0;
|
||||
// 4 values (vertices, normals, texcoords, colors)
|
||||
std::vector<int> num_attributes(4, 0);
|
||||
int num_materials = 0;
|
||||
for(int i = 0; i < shapes.size(); i++){
|
||||
num_shapes += shapes[i].size();
|
||||
}
|
||||
for(int i = 0; i < attributes.size(); i++){
|
||||
num_attributes[0] += attributes[i].vertices.size();
|
||||
num_attributes[1] += attributes[i].normals.size();
|
||||
num_attributes[2] += attributes[i].texcoords.size();
|
||||
num_attributes[3] += attributes[i].colors.size();
|
||||
}
|
||||
for(int i = 0; i < materials.size(); i++){
|
||||
num_materials += materials[i].size();
|
||||
}
|
||||
|
||||
printf("Total # of shapes = %d\n", numShapes);
|
||||
int materialIdOffset = 0;
|
||||
// More performant, than push_back
|
||||
out_attribute.vertices.resize(num_attributes[0]);
|
||||
out_attribute.normals.resize(num_attributes[1]);
|
||||
out_attribute.texcoords.resize(num_attributes[2]);
|
||||
out_attribute.colors.resize(num_attributes[3]);
|
||||
out_shape.resize(num_shapes);
|
||||
out_material.resize(num_materials);
|
||||
|
||||
size_t face_offset = 0;
|
||||
int material_id_offset = 0;
|
||||
int shape_id_offset = 0;
|
||||
int vertex_idx_offset = 0;
|
||||
int normal_idx_offset = 0;
|
||||
int texcoord_idx_offset = 0;
|
||||
int color_idx_offset = 0;
|
||||
|
||||
// shapes.size() = attributes.size() = materials.size()
|
||||
for (size_t i = 0; i < shapes.size(); i++) {
|
||||
|
||||
// Copy shapes
|
||||
for (size_t k = 0; k < shapes[i].size(); k++) {
|
||||
|
||||
std::string new_name = shapes[i][k].name;
|
||||
// Add suffix
|
||||
char buf[1024];
|
||||
@@ -39,36 +72,51 @@ StichObjs(
|
||||
new_name += std::string(buf);
|
||||
|
||||
printf("shape[%ld][%ld].name = %s\n", i, k, shapes[i][k].name.c_str());
|
||||
assert((shapes[i][k].mesh.indices.size() % 3) == 0);
|
||||
assert((shapes[i][k].mesh.positions.size() % 3) == 0);
|
||||
|
||||
tinyobj::shape_t new_shape = shapes[i][k];
|
||||
// Add offset.
|
||||
// Add material offset.
|
||||
for(size_t f = 0; f < new_shape.mesh.material_ids.size(); f++) {
|
||||
new_shape.mesh.material_ids[f] += materialIdOffset;
|
||||
new_shape.mesh.material_ids[f] += material_id_offset;
|
||||
}
|
||||
// Add indices offset.
|
||||
for(size_t f = 0; f < new_shape.mesh.indices.size(); f++){
|
||||
tinyobj::index_t& ref = new_shape.mesh.indices[f];
|
||||
if(ref.vertex_index > -1){
|
||||
ref.vertex_index += vertex_idx_offset;
|
||||
}
|
||||
if(ref.normal_index > -1){
|
||||
ref.normal_index += normal_idx_offset;
|
||||
}
|
||||
if(ref.texcoord_index > -1){
|
||||
ref.texcoord_index += texcoord_idx_offset;
|
||||
}
|
||||
}
|
||||
|
||||
new_shape.name = new_name;
|
||||
printf("shape[%ld][%ld].new_name = %s\n", i, k, new_shape.name.c_str());
|
||||
|
||||
out_shape.push_back(new_shape);
|
||||
out_shape[shape_id_offset++] = new_shape;
|
||||
}
|
||||
|
||||
materialIdOffset += materials[i].size();
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < materials.size(); i++) {
|
||||
// Copy materials
|
||||
for (size_t k = 0; k < materials[i].size(); k++) {
|
||||
out_material.push_back(materials[i][k]);
|
||||
out_material[material_id_offset++] = materials[i][k];
|
||||
}
|
||||
|
||||
// Copy attributes (3 floats per vertex, 3 floats per normal, 2 floats per texture-coordinate, 3 floats per color)
|
||||
// You could also include a check here, if the sizes are dividable by 3 (resp. 2), but it's safe to simply assume, they do.
|
||||
std::copy(attributes[i].vertices.begin(), attributes[i].vertices.end(), out_attribute.vertices.begin() + vertex_idx_offset * 3);
|
||||
vertex_idx_offset += attributes[i].vertices.size() / 3;
|
||||
std::copy(attributes[i].normals.begin(), attributes[i].normals.end(), out_attribute.normals.begin() + normal_idx_offset * 3);
|
||||
normal_idx_offset += attributes[i].normals.size() / 3;
|
||||
std::copy(attributes[i].texcoords.begin(), attributes[i].texcoords.end(), out_attribute.texcoords.begin() + texcoord_idx_offset * 2);
|
||||
texcoord_idx_offset += attributes[i].texcoords.size() / 2;
|
||||
std::copy(attributes[i].colors.begin(), attributes[i].colors.end(), out_attribute.colors.begin() + color_idx_offset);
|
||||
color_idx_offset += attributes[i].colors.size();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int
|
||||
main(
|
||||
int argc,
|
||||
char **argv)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc < 3) {
|
||||
printf("Usage: obj_sticher input0.obj input1.obj ... output.obj\n");
|
||||
@@ -78,16 +126,16 @@ main(
|
||||
int num_objfiles = argc - 2;
|
||||
std::string out_filename = std::string(argv[argc-1]); // last element
|
||||
|
||||
std::vector<Shape> shapes;
|
||||
std::vector<Material> materials;
|
||||
shapes.resize(num_objfiles);
|
||||
materials.resize(num_objfiles);
|
||||
std::vector<Attribute> attributes(num_objfiles);
|
||||
std::vector<Shape> shapes(num_objfiles);
|
||||
std::vector<Material> materials(num_objfiles);
|
||||
|
||||
for (int i = 0; i < num_objfiles; i++) {
|
||||
std::cout << "Loading " << argv[i+1] << " ... " << std::flush;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(shapes[i], materials[i], err, argv[i+1]);
|
||||
bool ret = tinyobj::LoadObj(&attributes[i], &shapes[i], &materials[i], &warn, &err, argv[i+1]);
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
}
|
||||
@@ -98,12 +146,13 @@ main(
|
||||
std::cout << "DONE." << std::endl;
|
||||
}
|
||||
|
||||
std::vector<tinyobj::shape_t> out_shape;
|
||||
std::vector<tinyobj::material_t> out_material;
|
||||
StichObjs(out_shape, out_material, shapes, materials);
|
||||
Attribute out_attribute;
|
||||
Shape out_shape;
|
||||
Material out_material;
|
||||
StichObjs(out_attribute, out_shape, out_material, attributes, shapes, materials);
|
||||
|
||||
bool coordTransform = true;
|
||||
bool ret = WriteObj(out_filename, out_shape, out_material, coordTransform);
|
||||
bool ret = WriteObj(out_filename, out_attribute, out_shape, out_material, coordTransform);
|
||||
assert(ret);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -30,6 +30,8 @@ bool WriteMat(const std::string& filename, const std::vector<tinyobj::material_t
|
||||
fprintf(fp, "Ke %f %f %f\n", mat.emission[0], mat.emission[1], mat.emission[2]);
|
||||
fprintf(fp, "Ns %f\n", mat.shininess);
|
||||
fprintf(fp, "Ni %f\n", mat.ior);
|
||||
fprintf(fp, "illum %d\n", mat.illum);
|
||||
fprintf(fp, "\n");
|
||||
// @todo { texture }
|
||||
}
|
||||
|
||||
@@ -38,7 +40,7 @@ bool WriteMat(const std::string& filename, const std::vector<tinyobj::material_t
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteObj(const std::string& filename, const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials, bool coordTransform) {
|
||||
bool WriteObj(const std::string& filename, const tinyobj::attrib_t& attributes, const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials, bool coordTransform) {
|
||||
FILE* fp = fopen(filename.c_str(), "w");
|
||||
if (!fp) {
|
||||
fprintf(stderr, "Failed to open file [ %s ] for write.\n", filename.c_str());
|
||||
@@ -48,17 +50,53 @@ bool WriteObj(const std::string& filename, const std::vector<tinyobj::shape_t>&
|
||||
std::string basename = GetFileBasename(filename);
|
||||
std::string material_filename = basename + ".mtl";
|
||||
|
||||
int v_offset = 0;
|
||||
int vn_offset = 0;
|
||||
int vt_offset = 0;
|
||||
int prev_material_id = -1;
|
||||
|
||||
fprintf(fp, "mtllib %s\n", material_filename.c_str());
|
||||
fprintf(fp, "mtllib %s\n\n", material_filename.c_str());
|
||||
|
||||
// facevarying vtx
|
||||
for (size_t k = 0; k < attributes.vertices.size(); k+=3) {
|
||||
if (coordTransform) {
|
||||
fprintf(fp, "v %f %f %f\n",
|
||||
attributes.vertices[k + 0],
|
||||
attributes.vertices[k + 2],
|
||||
-attributes.vertices[k + 1]);
|
||||
} else {
|
||||
fprintf(fp, "v %f %f %f\n",
|
||||
attributes.vertices[k + 0],
|
||||
attributes.vertices[k + 1],
|
||||
attributes.vertices[k + 2]);
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(fp, "\n");
|
||||
|
||||
// facevarying normal
|
||||
for (size_t k = 0; k < attributes.normals.size(); k += 3) {
|
||||
if (coordTransform) {
|
||||
fprintf(fp, "vn %f %f %f\n",
|
||||
attributes.normals[k + 0],
|
||||
attributes.normals[k + 2],
|
||||
-attributes.normals[k + 1]);
|
||||
} else {
|
||||
fprintf(fp, "vn %f %f %f\n",
|
||||
attributes.normals[k + 0],
|
||||
attributes.normals[k + 1],
|
||||
attributes.normals[k + 2]);
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(fp, "\n");
|
||||
|
||||
// facevarying texcoord
|
||||
for (size_t k = 0; k < attributes.texcoords.size(); k += 2) {
|
||||
fprintf(fp, "vt %f %f\n",
|
||||
attributes.texcoords[k + 0],
|
||||
attributes.texcoords[k + 1]);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < shapes.size(); i++) {
|
||||
|
||||
bool has_vn = false;
|
||||
bool has_vt = false;
|
||||
fprintf(fp, "\n");
|
||||
|
||||
if (shapes[i].name.empty()) {
|
||||
fprintf(fp, "g Unknown\n");
|
||||
@@ -66,101 +104,53 @@ bool WriteObj(const std::string& filename, const std::vector<tinyobj::shape_t>&
|
||||
fprintf(fp, "g %s\n", shapes[i].name.c_str());
|
||||
}
|
||||
|
||||
//if (!shapes[i].material.name.empty()) {
|
||||
// fprintf(fp, "usemtl %s\n", shapes[i].material.name.c_str());
|
||||
//}
|
||||
|
||||
// facevarying vtx
|
||||
for (size_t k = 0; k < shapes[i].mesh.indices.size() / 3; k++) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
int idx = shapes[i].mesh.indices[3*k+j];
|
||||
if (coordTransform) {
|
||||
fprintf(fp, "v %f %f %f\n",
|
||||
shapes[i].mesh.positions[3*idx+0],
|
||||
shapes[i].mesh.positions[3*idx+2],
|
||||
-shapes[i].mesh.positions[3*idx+1]);
|
||||
} else {
|
||||
fprintf(fp, "v %f %f %f\n",
|
||||
shapes[i].mesh.positions[3*idx+0],
|
||||
shapes[i].mesh.positions[3*idx+1],
|
||||
shapes[i].mesh.positions[3*idx+2]);
|
||||
bool has_vn = false;
|
||||
bool has_vt = false;
|
||||
// Assumes normals and textures are set shape-wise.
|
||||
if(shapes[i].mesh.indices.size() > 0){
|
||||
has_vn = shapes[i].mesh.indices[0].normal_index != -1;
|
||||
has_vt = shapes[i].mesh.indices[0].texcoord_index != -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// facevarying normal
|
||||
if (shapes[i].mesh.normals.size() > 0) {
|
||||
for (size_t k = 0; k < shapes[i].mesh.indices.size() / 3; k++) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
int idx = shapes[i].mesh.indices[3*k+j];
|
||||
if (coordTransform) {
|
||||
fprintf(fp, "vn %f %f %f\n",
|
||||
shapes[i].mesh.normals[3*idx+0],
|
||||
shapes[i].mesh.normals[3*idx+2],
|
||||
-shapes[i].mesh.normals[3*idx+1]);
|
||||
} else {
|
||||
fprintf(fp, "vn %f %f %f\n",
|
||||
shapes[i].mesh.normals[3*idx+0],
|
||||
shapes[i].mesh.normals[3*idx+1],
|
||||
shapes[i].mesh.normals[3*idx+2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (shapes[i].mesh.normals.size() > 0) has_vn = true;
|
||||
|
||||
// facevarying texcoord
|
||||
if (shapes[i].mesh.texcoords.size() > 0) {
|
||||
for (size_t k = 0; k < shapes[i].mesh.indices.size() / 3; k++) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
int idx = shapes[i].mesh.indices[3*k+j];
|
||||
fprintf(fp, "vt %f %f\n",
|
||||
shapes[i].mesh.texcoords[2*idx+0],
|
||||
shapes[i].mesh.texcoords[2*idx+1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (shapes[i].mesh.texcoords.size() > 0) has_vt = true;
|
||||
|
||||
// face
|
||||
for (size_t k = 0; k < shapes[i].mesh.indices.size() / 3; k++) {
|
||||
|
||||
// Face index is 1-base.
|
||||
//int v0 = shapes[i].mesh.indices[3*k+0] + 1 + v_offset;
|
||||
//int v1 = shapes[i].mesh.indices[3*k+1] + 1 + v_offset;
|
||||
//int v2 = shapes[i].mesh.indices[3*k+2] + 1 + v_offset;
|
||||
int v0 = (3*k + 0) + 1 + v_offset;
|
||||
int v1 = (3*k + 1) + 1 + v_offset;
|
||||
int v2 = (3*k + 2) + 1 + v_offset;
|
||||
|
||||
int vt0 = (3*k + 0) + 1 + vt_offset;
|
||||
int vt1 = (3*k + 1) + 1 + vt_offset;
|
||||
int vt2 = (3*k + 2) + 1 + vt_offset;
|
||||
|
||||
int material_id = shapes[i].mesh.material_ids[k];
|
||||
int face_index = 0;
|
||||
for (size_t k = 0; k < shapes[i].mesh.indices.size(); k += shapes[i].mesh.num_face_vertices[face_index++]) {
|
||||
// Check Materials
|
||||
int material_id = shapes[i].mesh.material_ids[face_index];
|
||||
if (material_id != prev_material_id) {
|
||||
std::string material_name = materials[material_id].name;
|
||||
fprintf(fp, "usemtl %s\n", material_name.c_str());
|
||||
prev_material_id = material_id;
|
||||
}
|
||||
|
||||
unsigned char v_per_f = shapes[i].mesh.num_face_vertices[face_index];
|
||||
// Imperformant, but if you want to have variable vertices per face, you need some kind of a dynamic loop.
|
||||
fprintf(fp, "f");
|
||||
for(int l = 0; l < v_per_f; l++){
|
||||
const tinyobj::index_t& ref = shapes[i].mesh.indices[k + l];
|
||||
if(has_vn && has_vt){
|
||||
fprintf(fp, "f %d/%d/%d %d/%d/%d %d/%d/%d\n",
|
||||
v0, vt0, v0, v1, vt1, v1, v2, vt2, v2);
|
||||
} else if (has_vn && !has_vt) {
|
||||
fprintf(fp, "f %d//%d %d//%d %d//%d\n", v0, v0, v1, v1, v2, v2);
|
||||
} else if (!has_vn && has_vt) {
|
||||
fprintf(fp, "f %d/%d %d/%d %d/%d\n", v0, v0, v1, v1, v2, v2);
|
||||
} else {
|
||||
fprintf(fp, "f %d %d %d\n", v0, v1, v2);
|
||||
// v0/t0/vn0
|
||||
fprintf(fp, " %d/%d/%d", ref.vertex_index + 1, ref.texcoord_index + 1, ref.normal_index + 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
if(has_vn && !has_vt){
|
||||
// v0//vn0
|
||||
fprintf(fp, " %d//%d", ref.vertex_index + 1, ref.normal_index + 1);
|
||||
continue;
|
||||
}
|
||||
if(!has_vn && has_vt){
|
||||
// v0/vt0
|
||||
fprintf(fp, " %d/%d", ref.vertex_index + 1, ref.texcoord_index + 1);
|
||||
continue;
|
||||
}
|
||||
if(!has_vn && !has_vt){
|
||||
// v0 v1 v2
|
||||
fprintf(fp, " %d", ref.vertex_index + 1);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
fprintf(fp, "\n");
|
||||
}
|
||||
|
||||
v_offset += shapes[i].mesh.indices.size();
|
||||
//vn_offset += shapes[i].mesh.normals.size() / 3;
|
||||
vt_offset += shapes[i].mesh.texcoords.size() / 2;
|
||||
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
#include "../../tiny_obj_loader.h"
|
||||
|
||||
extern bool WriteObj(const std::string& filename, const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials, bool coordTransform = false);
|
||||
|
||||
extern bool WriteObj(const std::string& filename, const tinyobj::attrib_t& attributes, const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials, bool coordTransform = false);
|
||||
|
||||
#endif // __OBJ_WRITER_H__
|
||||
|
||||
@@ -308,9 +308,13 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
|
||||
base_dir += "/";
|
||||
#endif
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename,
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename,
|
||||
base_dir.c_str());
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
}
|
||||
|
||||
@@ -9,8 +9,9 @@ bool Voxelize(const char* filename, float voxelsizex, float voxelsizey, float vo
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename);
|
||||
|
||||
if (!err.empty()) {
|
||||
printf("err: %s\n", err.c_str());
|
||||
|
||||
@@ -6,11 +6,21 @@
|
||||
|
||||
* C++-11 compiler
|
||||
|
||||
## How to build
|
||||
|
||||
```
|
||||
$ premak5 gmake
|
||||
```
|
||||
|
||||
## Compile options
|
||||
|
||||
* zstd compressed .obj support. `--with-zstd` premake option.
|
||||
* gzip compressed .obj support. `--with-zlib` premake option.
|
||||
|
||||
## Notes on AMD GPU + Linux
|
||||
|
||||
You may need to link with libdrm(`-ldrm`).
|
||||
|
||||
## Licenses
|
||||
|
||||
* lfpAlloc : MIT license.
|
||||
|
||||
@@ -312,6 +312,10 @@ typedef struct {
|
||||
|
||||
typedef struct {
|
||||
std::string name; // group name or object name.
|
||||
// Shape's corresponding faces are accessed by attrib.indices[face_offset,
|
||||
// face_offset + length] NOTE: you'll need to sum up
|
||||
// attrib.face_num_verts[face_offset, face_offset + length] to find actual
|
||||
// number of faces.
|
||||
unsigned int face_offset;
|
||||
unsigned int length;
|
||||
} shape_t;
|
||||
@@ -330,7 +334,14 @@ typedef struct {
|
||||
std::vector<float, lfpAlloc::lfpAllocator<float> > normals;
|
||||
std::vector<float, lfpAlloc::lfpAllocator<float> > texcoords;
|
||||
std::vector<index_t, lfpAlloc::lfpAllocator<index_t> > indices;
|
||||
|
||||
// # of vertices for each face.
|
||||
// 3 for triangle, 4 for qual, ...
|
||||
// If triangulation is enabled and the original face are quad,
|
||||
// face_num_verts will be 6(3 + 3)
|
||||
std::vector<int, lfpAlloc::lfpAllocator<int> > face_num_verts;
|
||||
|
||||
// Per-face material IDs.
|
||||
std::vector<int, lfpAlloc::lfpAllocator<int> > material_ids;
|
||||
} attrib_t;
|
||||
|
||||
@@ -1238,6 +1249,8 @@ typedef struct {
|
||||
// 3. Do parallel parsing for each line.
|
||||
// 4. Reconstruct final mesh data structure.
|
||||
|
||||
// Raise # of max threads if you have more CPU cores...
|
||||
// In 2018, 32 cores are getting common in high-end workstaion PC.
|
||||
#define kMaxThreads (32)
|
||||
|
||||
static inline bool is_line_ending(const char *p, size_t i, size_t end_i) {
|
||||
@@ -1306,15 +1319,19 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
end_idx = len - 1;
|
||||
}
|
||||
|
||||
// true if the line currently read must be added to the current line
|
||||
// info
|
||||
bool new_line_found =
|
||||
(t == 0) || is_line_ending(buf, start_idx - 1, end_idx);
|
||||
|
||||
size_t prev_pos = start_idx;
|
||||
for (size_t i = start_idx; i < end_idx; i++) {
|
||||
if (is_line_ending(buf, i, end_idx)) {
|
||||
if ((t > 0) && (prev_pos == start_idx) &&
|
||||
(!is_line_ending(buf, start_idx - 1, end_idx))) {
|
||||
if (!new_line_found) {
|
||||
// first linebreak found in (chunk > 0), and a line before this
|
||||
// linebreak belongs to previous chunk, so skip it.
|
||||
prev_pos = i + 1;
|
||||
continue;
|
||||
new_line_found = true;
|
||||
} else {
|
||||
LineInfo info;
|
||||
info.pos = prev_pos;
|
||||
@@ -1329,11 +1346,11 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
}
|
||||
}
|
||||
|
||||
// Find extra line which spand across chunk boundary.
|
||||
if ((t < num_threads) && (buf[end_idx - 1] != '\n')) {
|
||||
auto extra_span_idx = (std::min)(end_idx - 1 + chunk_size, len);
|
||||
for (size_t i = end_idx; i < extra_span_idx; i++) {
|
||||
if (is_line_ending(buf, i, extra_span_idx)) {
|
||||
// If at least one line started in this chunk, find where it ends in the
|
||||
// rest of the buffer
|
||||
if (new_line_found && (t < num_threads) && (buf[end_idx - 1] != '\n')) {
|
||||
for (size_t i = end_idx; i < len; i++) {
|
||||
if (is_line_ending(buf, i, len)) {
|
||||
LineInfo info;
|
||||
info.pos = prev_pos;
|
||||
info.len = i - prev_pos;
|
||||
@@ -1390,7 +1407,6 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
|
||||
for (size_t t = 0; t < num_threads; t++) {
|
||||
workers->push_back(std::thread([&, t]() {
|
||||
|
||||
for (size_t i = 0; i < line_infos[t].size(); i++) {
|
||||
Command command;
|
||||
bool ret = parseLine(&command, &buf[line_infos[t][i].pos],
|
||||
@@ -1415,7 +1431,6 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
commands[t].emplace_back(std::move(command));
|
||||
}
|
||||
}
|
||||
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -1493,7 +1508,7 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
attrib->texcoords.resize(num_vt * 2);
|
||||
attrib->indices.resize(num_f);
|
||||
attrib->face_num_verts.resize(num_indices);
|
||||
attrib->material_ids.resize(num_indices);
|
||||
attrib->material_ids.resize(num_indices, -1);
|
||||
|
||||
size_t v_offsets[kMaxThreads];
|
||||
size_t n_offsets[kMaxThreads];
|
||||
@@ -1524,22 +1539,46 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
size_t t_count = t_offsets[t];
|
||||
size_t f_count = f_offsets[t];
|
||||
size_t face_count = face_offsets[t];
|
||||
int material_id = -1; // -1 = default unknown material.
|
||||
|
||||
for (size_t i = 0; i < commands[t].size(); i++) {
|
||||
if (commands[t][i].type == COMMAND_EMPTY) {
|
||||
continue;
|
||||
} else if (commands[t][i].type == COMMAND_USEMTL) {
|
||||
if (commands[t][i].material_name &&
|
||||
commands[t][i].material_name_len > 0) {
|
||||
commands[t][i].material_name_len > 0 &&
|
||||
// check if there are still faces after this command
|
||||
face_count < num_indices) {
|
||||
// Find next face
|
||||
bool found = false;
|
||||
size_t i_start = i + 1, t_next, i_next;
|
||||
for (t_next = t; t_next < num_threads; t_next++) {
|
||||
for (i_next = i_start; i_next < commands[t_next].size();
|
||||
i_next++) {
|
||||
if (commands[t_next][i_next].type == COMMAND_F) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found) break;
|
||||
i_start = 0;
|
||||
}
|
||||
// Assign material to this face
|
||||
if (found) {
|
||||
std::string material_name(commands[t][i].material_name,
|
||||
commands[t][i].material_name_len);
|
||||
|
||||
for (size_t k = 0;
|
||||
k < commands[t_next][i_next].f_num_verts.size(); k++) {
|
||||
if (material_map.find(material_name) != material_map.end()) {
|
||||
material_id = material_map[material_name];
|
||||
attrib->material_ids[face_count + k] =
|
||||
material_map[material_name];
|
||||
} else {
|
||||
// Assign invalid material ID
|
||||
material_id = -1;
|
||||
// Set a different value than the default, to
|
||||
// prevent following faces from being assigned a valid
|
||||
// material
|
||||
attrib->material_ids[face_count + k] = -2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (commands[t][i].type == COMMAND_V) {
|
||||
@@ -1566,7 +1605,6 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
index_t(vertex_index, texcoord_index, normal_index);
|
||||
}
|
||||
for (size_t k = 0; k < commands[t][i].f_num_verts.size(); k++) {
|
||||
attrib->material_ids[face_count + k] = material_id;
|
||||
attrib->face_num_verts[face_count + k] =
|
||||
commands[t][i].f_num_verts[k];
|
||||
}
|
||||
@@ -1581,19 +1619,13 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
for (size_t t = 0; t < workers->size(); t++) {
|
||||
workers[t].join();
|
||||
}
|
||||
if(material_map.size()>1&& num_threads>1) {
|
||||
for (size_t t = 0; t < num_threads; t++) {
|
||||
size_t face_count = face_offsets[t];
|
||||
if (-1 == attrib->material_ids[face_count]) {
|
||||
int prev_material_id = attrib->material_ids[face_count - 1];
|
||||
size_t max_face_offset = (t == num_threads - 1) ? attrib->material_ids.size() : face_offsets[t + 1];
|
||||
for (int i = face_count; i<max_face_offset; ++i) {
|
||||
if (attrib->material_ids[i] != -1) break;
|
||||
attrib->material_ids[i] = prev_material_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// To each face with uninitialized material id,
|
||||
// assign the material id of the last face preceding it that has one
|
||||
for (size_t face_count = 1; face_count < num_indices; ++face_count)
|
||||
if (attrib->material_ids[face_count] == -1)
|
||||
attrib->material_ids[face_count] = attrib->material_ids[face_count - 1];
|
||||
|
||||
auto t_end = std::chrono::high_resolution_clock::now();
|
||||
ms_merge = t_end - t_start;
|
||||
}
|
||||
@@ -1653,7 +1685,8 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
}
|
||||
}
|
||||
if (commands[t][i].type == COMMAND_F) {
|
||||
face_count++;
|
||||
// Consider generation of multiple faces per `f` line by triangulation
|
||||
face_count += commands[t][i].f_num_verts.size();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -357,7 +357,7 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename, int n
|
||||
std::vector<float> vb; // pos(3float), normal(3float), color(3float)
|
||||
size_t face_offset = 0;
|
||||
for (size_t v = 0; v < attrib.face_num_verts.size(); v++) {
|
||||
assert(attrib.face_num_verts[v] % 3 == 0); // assume all triangle face.
|
||||
assert(attrib.face_num_verts[v] % 3 == 0); // assume all triangle face(multiple of 3).
|
||||
for (size_t f = 0; f < attrib.face_num_verts[v] / 3; f++) {
|
||||
tinyobj_opt::index_t idx0 = attrib.indices[face_offset+3*f+0];
|
||||
tinyobj_opt::index_t idx1 = attrib.indices[face_offset+3*f+1];
|
||||
|
||||
BIN
fuzzer/afl.tar.gz
Normal file
BIN
fuzzer/afl.tar.gz
Normal file
Binary file not shown.
20
fuzzer/runner.py
Normal file
20
fuzzer/runner.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import os, sys
|
||||
import glob
|
||||
import subprocess
|
||||
|
||||
def main():
|
||||
for g in glob.glob("../tests/afl/id*"):
|
||||
print(g)
|
||||
|
||||
cmd = ["../a.out", g]
|
||||
|
||||
proc = subprocess.Popen(cmd)
|
||||
try:
|
||||
outs, errs = proc.communicate(timeout=15)
|
||||
print(outs)
|
||||
except TimeoutExpired:
|
||||
proc.kill()
|
||||
outs, errs = proc.communicate()
|
||||
|
||||
|
||||
main()
|
||||
@@ -137,14 +137,19 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
|
||||
for (size_t i = 0; i < shapes.size(); i++) {
|
||||
printf("shape[%ld].name = %s\n", static_cast<long>(i),
|
||||
shapes[i].name.c_str());
|
||||
printf("Size of shape[%ld].indices: %lu\n", static_cast<long>(i),
|
||||
printf("Size of shape[%ld].mesh.indices: %lu\n", static_cast<long>(i),
|
||||
static_cast<unsigned long>(shapes[i].mesh.indices.size()));
|
||||
printf("Size of shape[%ld].path.indices: %lu\n", static_cast<long>(i),
|
||||
static_cast<unsigned long>(shapes[i].path.indices.size()));
|
||||
|
||||
size_t index_offset = 0;
|
||||
|
||||
assert(shapes[i].mesh.num_face_vertices.size() ==
|
||||
shapes[i].mesh.material_ids.size());
|
||||
|
||||
assert(shapes[i].mesh.num_face_vertices.size() ==
|
||||
shapes[i].mesh.smoothing_group_ids.size());
|
||||
|
||||
printf("shape[%ld].num_faces: %lu\n", static_cast<long>(i),
|
||||
static_cast<unsigned long>(shapes[i].mesh.num_face_vertices.size()));
|
||||
|
||||
@@ -165,6 +170,8 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
|
||||
|
||||
printf(" face[%ld].material_id = %d\n", static_cast<long>(f),
|
||||
shapes[i].mesh.material_ids[f]);
|
||||
printf(" face[%ld].smoothing_group_id = %d\n", static_cast<long>(f),
|
||||
shapes[i].mesh.smoothing_group_ids[f]);
|
||||
|
||||
index_offset += fnum;
|
||||
}
|
||||
@@ -278,14 +285,19 @@ static bool TestLoadObj(const char* filename, const char* basepath = NULL,
|
||||
|
||||
timerutil t;
|
||||
t.start();
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename,
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename,
|
||||
basepath, triangulate);
|
||||
t.end();
|
||||
printf("Parsing time: %lu [msecs]\n", t.msec());
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
@@ -369,16 +381,12 @@ static bool TestStreamLoadObj() {
|
||||
virtual bool operator()(const std::string& matId,
|
||||
std::vector<material_t>* materials,
|
||||
std::map<std::string, int>* matMap,
|
||||
std::string* warn,
|
||||
std::string* err) {
|
||||
(void)err;
|
||||
(void)matId;
|
||||
std::string warning;
|
||||
LoadMtl(matMap, materials, &m_matSStream, &warning);
|
||||
LoadMtl(matMap, materials, &m_matSStream, warn, err);
|
||||
|
||||
if (!warning.empty()) {
|
||||
if (err) {
|
||||
(*err) += warning;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -390,8 +398,9 @@ static bool TestStreamLoadObj() {
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, &objStream,
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, &objStream,
|
||||
&matSSReader);
|
||||
|
||||
if (!err.empty()) {
|
||||
|
||||
9
models/colorspace-issue-184.mtl
Normal file
9
models/colorspace-issue-184.mtl
Normal file
@@ -0,0 +1,9 @@
|
||||
newmtl default
|
||||
Ka 0 0 0
|
||||
Kd 0 0 0
|
||||
Ks 0 0 0
|
||||
Kt 0.1 0.2 0.3
|
||||
map_Kd -colorspace sRGB -o 0.1 diffuse.jpg
|
||||
map_Ks -s 0.1 0.2 specular.jpg
|
||||
map_bump -colorspace linear -bm 3 bumpmap.jpg
|
||||
|
||||
7
models/colorspace-issue-184.obj
Normal file
7
models/colorspace-issue-184.obj
Normal file
@@ -0,0 +1,7 @@
|
||||
mtllib colorspace-issue-184.mtl
|
||||
o Test
|
||||
v 1.864151 -1.219172 -5.532511
|
||||
v 0.575869 -0.666304 5.896140
|
||||
v 0.940448 1.000000 -1.971128
|
||||
usemtl default
|
||||
f 1 2 3
|
||||
@@ -13,6 +13,7 @@ v 2.000000 2.000000 0.000000
|
||||
g front cube
|
||||
usemtl white
|
||||
f 1 2 3 4
|
||||
# two white spaces between 'back' and 'cube'
|
||||
g back cube
|
||||
# expects white material
|
||||
f 8 7 6 5
|
||||
|
||||
24
models/invalid-face-definition.mtl
Normal file
24
models/invalid-face-definition.mtl
Normal file
@@ -0,0 +1,24 @@
|
||||
newmtl white
|
||||
Ka 0 0 0
|
||||
Kd 1 1 1
|
||||
Ks 0 0 0
|
||||
|
||||
newmtl red
|
||||
Ka 0 0 0
|
||||
Kd 1 0 0
|
||||
Ks 0 0 0
|
||||
|
||||
newmtl green
|
||||
Ka 0 0 0
|
||||
Kd 0 1 0
|
||||
Ks 0 0 0
|
||||
|
||||
newmtl blue
|
||||
Ka 0 0 0
|
||||
Kd 0 0 1
|
||||
Ks 0 0 0
|
||||
|
||||
newmtl light
|
||||
Ka 20 20 20
|
||||
Kd 1 1 1
|
||||
Ks 0 0 0
|
||||
18
models/invalid-face-definition.obj
Normal file
18
models/invalid-face-definition.obj
Normal file
@@ -0,0 +1,18 @@
|
||||
mtllib invalid-face-definition.mtl
|
||||
|
||||
v 0.000000 2.000000 2.000000
|
||||
v 0.000000 0.000000 2.000000
|
||||
v 2.000000 0.000000 2.000000
|
||||
v 2.000000 2.000000 2.000000
|
||||
v 0.000000 2.000000 0.000000
|
||||
v 0.000000 0.000000 0.000000
|
||||
v 2.000000 0.000000 0.000000
|
||||
v 2.000000 2.000000 0.000000
|
||||
# 8 vertices
|
||||
|
||||
g front cube
|
||||
usemtl white
|
||||
f 1
|
||||
g back cube
|
||||
# expects white material
|
||||
f 8 7
|
||||
16
models/line-prim.obj
Normal file
16
models/line-prim.obj
Normal file
@@ -0,0 +1,16 @@
|
||||
mtllib cube.mtl
|
||||
|
||||
v 0.000000 2.000000 2.000000
|
||||
v 0.000000 0.000000 2.000000
|
||||
v 2.000000 0.000000 2.000000
|
||||
v 2.000000 2.000000 2.000000
|
||||
v 0.000000 2.000000 0.000000
|
||||
v 0.000000 0.000000 0.000000
|
||||
v 2.000000 0.000000 0.000000
|
||||
v 2.000000 2.000000 0.000000
|
||||
# 8 vertices
|
||||
|
||||
g g0
|
||||
usemtl white
|
||||
l 1 2 3 4
|
||||
l 5 6 7
|
||||
24
tests/issue-177.mtl
Normal file
24
tests/issue-177.mtl
Normal file
@@ -0,0 +1,24 @@
|
||||
newmtl white
|
||||
Ka 0 0 0
|
||||
Kd 1 1 1
|
||||
Ks 0 0 0
|
||||
|
||||
newmtl red
|
||||
Ka 0 0 0
|
||||
Kd 1 0 0
|
||||
Ks 0 0 0
|
||||
|
||||
newmtl green
|
||||
Ka 0 0 0
|
||||
Kd 0 1 0
|
||||
Ks 0 0 0
|
||||
|
||||
newmtl blue
|
||||
Ka 0 0 0
|
||||
Kd 0 0 1
|
||||
Ks 0 0 0
|
||||
|
||||
newmtl light
|
||||
Ka 20 20 20
|
||||
Kd 1 1 1
|
||||
Ks 0 0 0
|
||||
31
tests/issue-177.obj
Normal file
31
tests/issue-177.obj
Normal file
@@ -0,0 +1,31 @@
|
||||
mtllib issue-177.mtl
|
||||
|
||||
v 0.000000 2.000000 2.000000
|
||||
v 0.000000 0.000000 2.000000
|
||||
v 2.000000 0.000000 2.000000
|
||||
v 2.000000 2.000000 2.000000
|
||||
v 0.000000 2.000000 0.000000
|
||||
v 0.000000 0.000000 0.000000
|
||||
v 2.000000 0.000000 0.000000
|
||||
v 2.000000 2.000000 0.000000
|
||||
# 8 vertices
|
||||
|
||||
g front cube
|
||||
usemtl white
|
||||
f 1 2 3 4
|
||||
g back cube
|
||||
# expects white material
|
||||
f 8 7 6 5
|
||||
g right cube
|
||||
usemtl red
|
||||
f 4 3 7 8
|
||||
g top cube
|
||||
usemtl white
|
||||
f 5 1 4 8
|
||||
g left cube
|
||||
usemtl green
|
||||
f 5 6 2 1
|
||||
g bottom cube
|
||||
usemtl white
|
||||
f 2 6 7 3
|
||||
# 6 elements
|
||||
543
tests/tester.cc
543
tests/tester.cc
@@ -1,21 +1,25 @@
|
||||
#define TINYOBJLOADER_IMPLEMENTATION
|
||||
#include "../tiny_obj_loader.h"
|
||||
|
||||
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
|
||||
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do
|
||||
// this in one cpp file
|
||||
#include "catch.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
||||
static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials, bool triangulate = true)
|
||||
{
|
||||
static void PrintInfo(const tinyobj::attrib_t& attrib,
|
||||
const std::vector<tinyobj::shape_t>& shapes,
|
||||
const std::vector<tinyobj::material_t>& materials,
|
||||
bool triangulate = true) {
|
||||
std::cout << "# of vertices : " << (attrib.vertices.size() / 3) << std::endl;
|
||||
std::cout << "# of normals : " << (attrib.normals.size() / 3) << std::endl;
|
||||
std::cout << "# of texcoords : " << (attrib.texcoords.size() / 2) << std::endl;
|
||||
std::cout << "# of texcoords : " << (attrib.texcoords.size() / 2)
|
||||
<< std::endl;
|
||||
|
||||
std::cout << "# of shapes : " << shapes.size() << std::endl;
|
||||
std::cout << "# of materials : " << materials.size() << std::endl;
|
||||
@@ -42,11 +46,12 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
|
||||
|
||||
for (size_t i = 0; i < shapes.size(); i++) {
|
||||
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());
|
||||
|
||||
if (triangulate)
|
||||
{
|
||||
printf("Size of shape[%ld].material_ids: %ld\n", i, shapes[i].mesh.material_ids.size());
|
||||
if (triangulate) {
|
||||
printf("Size of shape[%ld].material_ids: %ld\n", i,
|
||||
shapes[i].mesh.material_ids.size());
|
||||
assert((shapes[i].mesh.indices.size() % 3) == 0);
|
||||
for (size_t f = 0; f < shapes[i].mesh.indices.size() / 3; f++) {
|
||||
tinyobj::index_t i0 = shapes[i].mesh.indices[3 * f + 0];
|
||||
@@ -61,19 +66,21 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
|
||||
} else {
|
||||
for (size_t f = 0; f < shapes[i].mesh.indices.size(); f++) {
|
||||
tinyobj::index_t idx = shapes[i].mesh.indices[f];
|
||||
printf(" idx[%ld] = %d/%d/%d\n", f, idx.vertex_index, idx.normal_index, idx.texcoord_index);
|
||||
printf(" idx[%ld] = %d/%d/%d\n", f, idx.vertex_index, idx.normal_index,
|
||||
idx.texcoord_index);
|
||||
}
|
||||
|
||||
printf("Size of shape[%ld].material_ids: %ld\n", i, shapes[i].mesh.material_ids.size());
|
||||
assert(shapes[i].mesh.material_ids.size() == shapes[i].mesh.num_face_vertices.size());
|
||||
printf("Size of shape[%ld].material_ids: %ld\n", i,
|
||||
shapes[i].mesh.material_ids.size());
|
||||
assert(shapes[i].mesh.material_ids.size() ==
|
||||
shapes[i].mesh.num_face_vertices.size());
|
||||
for (size_t m = 0; m < shapes[i].mesh.material_ids.size(); m++) {
|
||||
printf(" material_id[%ld] = %d\n", m,
|
||||
shapes[i].mesh.material_ids[m]);
|
||||
printf(" material_id[%ld] = %d\n", m, shapes[i].mesh.material_ids[m]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
printf("shape[%ld].num_faces: %ld\n", i, shapes[i].mesh.num_face_vertices.size());
|
||||
printf("shape[%ld].num_faces: %ld\n", i,
|
||||
shapes[i].mesh.num_face_vertices.size());
|
||||
for (size_t v = 0; v < shapes[i].mesh.num_face_vertices.size(); v++) {
|
||||
printf(" num_vertices[%ld] = %ld\n", v,
|
||||
static_cast<long>(shapes[i].mesh.num_face_vertices[v]));
|
||||
@@ -92,33 +99,28 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
|
||||
for (size_t t = 0; t < shapes[i].mesh.tags.size(); t++) {
|
||||
printf(" tag[%ld] = %s ", t, shapes[i].mesh.tags[t].name.c_str());
|
||||
printf(" ints: [");
|
||||
for (size_t j = 0; j < shapes[i].mesh.tags[t].intValues.size(); ++j)
|
||||
{
|
||||
for (size_t j = 0; j < shapes[i].mesh.tags[t].intValues.size(); ++j) {
|
||||
printf("%ld", static_cast<long>(shapes[i].mesh.tags[t].intValues[j]));
|
||||
if (j < (shapes[i].mesh.tags[t].intValues.size()-1))
|
||||
{
|
||||
if (j < (shapes[i].mesh.tags[t].intValues.size() - 1)) {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
printf("]");
|
||||
|
||||
printf(" floats: [");
|
||||
for (size_t j = 0; j < shapes[i].mesh.tags[t].floatValues.size(); ++j)
|
||||
{
|
||||
printf("%f", static_cast<const double>(shapes[i].mesh.tags[t].floatValues[j]));
|
||||
if (j < (shapes[i].mesh.tags[t].floatValues.size()-1))
|
||||
{
|
||||
for (size_t j = 0; j < shapes[i].mesh.tags[t].floatValues.size(); ++j) {
|
||||
printf("%f", static_cast<const double>(
|
||||
shapes[i].mesh.tags[t].floatValues[j]));
|
||||
if (j < (shapes[i].mesh.tags[t].floatValues.size() - 1)) {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
printf("]");
|
||||
|
||||
printf(" strings: [");
|
||||
for (size_t j = 0; j < shapes[i].mesh.tags[t].stringValues.size(); ++j)
|
||||
{
|
||||
for (size_t j = 0; j < shapes[i].mesh.tags[t].stringValues.size(); ++j) {
|
||||
printf("%s", shapes[i].mesh.tags[t].stringValues[j].c_str());
|
||||
if (j < (shapes[i].mesh.tags[t].stringValues.size()-1))
|
||||
{
|
||||
if (j < (shapes[i].mesh.tags[t].stringValues.size() - 1)) {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
@@ -129,25 +131,45 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
|
||||
|
||||
for (size_t i = 0; i < materials.size(); i++) {
|
||||
printf("material[%ld].name = %s\n", i, materials[i].name.c_str());
|
||||
printf(" material.Ka = (%f, %f ,%f)\n", static_cast<const double>(materials[i].ambient[0]), static_cast<const double>(materials[i].ambient[1]), static_cast<const double>(materials[i].ambient[2]));
|
||||
printf(" material.Kd = (%f, %f ,%f)\n", static_cast<const double>(materials[i].diffuse[0]), static_cast<const double>(materials[i].diffuse[1]), static_cast<const double>(materials[i].diffuse[2]));
|
||||
printf(" material.Ks = (%f, %f ,%f)\n", static_cast<const double>(materials[i].specular[0]), static_cast<const double>(materials[i].specular[1]), static_cast<const double>(materials[i].specular[2]));
|
||||
printf(" material.Tr = (%f, %f ,%f)\n", static_cast<const double>(materials[i].transmittance[0]), static_cast<const double>(materials[i].transmittance[1]), static_cast<const double>(materials[i].transmittance[2]));
|
||||
printf(" material.Ke = (%f, %f ,%f)\n", static_cast<const double>(materials[i].emission[0]), static_cast<const double>(materials[i].emission[1]), static_cast<const double>(materials[i].emission[2]));
|
||||
printf(" material.Ns = %f\n", static_cast<const double>(materials[i].shininess));
|
||||
printf(" material.Ka = (%f, %f ,%f)\n",
|
||||
static_cast<const double>(materials[i].ambient[0]),
|
||||
static_cast<const double>(materials[i].ambient[1]),
|
||||
static_cast<const double>(materials[i].ambient[2]));
|
||||
printf(" material.Kd = (%f, %f ,%f)\n",
|
||||
static_cast<const double>(materials[i].diffuse[0]),
|
||||
static_cast<const double>(materials[i].diffuse[1]),
|
||||
static_cast<const double>(materials[i].diffuse[2]));
|
||||
printf(" material.Ks = (%f, %f ,%f)\n",
|
||||
static_cast<const double>(materials[i].specular[0]),
|
||||
static_cast<const double>(materials[i].specular[1]),
|
||||
static_cast<const double>(materials[i].specular[2]));
|
||||
printf(" material.Tr = (%f, %f ,%f)\n",
|
||||
static_cast<const double>(materials[i].transmittance[0]),
|
||||
static_cast<const double>(materials[i].transmittance[1]),
|
||||
static_cast<const double>(materials[i].transmittance[2]));
|
||||
printf(" material.Ke = (%f, %f ,%f)\n",
|
||||
static_cast<const double>(materials[i].emission[0]),
|
||||
static_cast<const double>(materials[i].emission[1]),
|
||||
static_cast<const double>(materials[i].emission[2]));
|
||||
printf(" material.Ns = %f\n",
|
||||
static_cast<const double>(materials[i].shininess));
|
||||
printf(" material.Ni = %f\n", static_cast<const double>(materials[i].ior));
|
||||
printf(" material.dissolve = %f\n", static_cast<const double>(materials[i].dissolve));
|
||||
printf(" material.dissolve = %f\n",
|
||||
static_cast<const double>(materials[i].dissolve));
|
||||
printf(" material.illum = %d\n", materials[i].illum);
|
||||
printf(" material.map_Ka = %s\n", materials[i].ambient_texname.c_str());
|
||||
printf(" material.map_Kd = %s\n", materials[i].diffuse_texname.c_str());
|
||||
printf(" material.map_Ks = %s\n", materials[i].specular_texname.c_str());
|
||||
printf(" material.map_Ns = %s\n", materials[i].specular_highlight_texname.c_str());
|
||||
printf(" material.map_Ns = %s\n",
|
||||
materials[i].specular_highlight_texname.c_str());
|
||||
printf(" material.map_bump = %s\n", materials[i].bump_texname.c_str());
|
||||
printf(" material.map_d = %s\n", materials[i].alpha_texname.c_str());
|
||||
printf(" material.disp = %s\n", materials[i].displacement_texname.c_str());
|
||||
printf(" material.refl = %s\n", materials[i].reflection_texname.c_str());
|
||||
std::map<std::string, std::string>::const_iterator it(materials[i].unknown_parameter.begin());
|
||||
std::map<std::string, std::string>::const_iterator itEnd(materials[i].unknown_parameter.end());
|
||||
std::map<std::string, std::string>::const_iterator it(
|
||||
materials[i].unknown_parameter.begin());
|
||||
std::map<std::string, std::string>::const_iterator itEnd(
|
||||
materials[i].unknown_parameter.end());
|
||||
|
||||
for (; it != itEnd; it++) {
|
||||
printf(" material.%s = %s\n", it->first.c_str(), it->second.c_str());
|
||||
@@ -156,23 +178,25 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
TestLoadObj(
|
||||
const char* filename,
|
||||
const char* basepath = NULL,
|
||||
bool triangulate = true)
|
||||
{
|
||||
static bool TestLoadObj(const char* filename, const char* basepath = NULL,
|
||||
bool triangulate = true) {
|
||||
std::cout << "Loading " << filename << std::endl;
|
||||
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename, basepath, triangulate);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
filename, basepath, triangulate);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
@@ -185,13 +209,10 @@ TestLoadObj(
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
TestLoadObjFromPreopenedFile(
|
||||
const char* filename,
|
||||
static bool TestLoadObjFromPreopenedFile(const char* filename,
|
||||
const char* basepath = NULL,
|
||||
bool readMaterials = true,
|
||||
bool triangulate = true)
|
||||
{
|
||||
bool triangulate = true) {
|
||||
std::string fullFilename = std::string(basepath) + filename;
|
||||
std::cout << "Loading " << fullFilename << std::endl;
|
||||
|
||||
@@ -203,19 +224,24 @@ TestLoadObjFromPreopenedFile(
|
||||
}
|
||||
|
||||
tinyobj::MaterialStreamReader materialStreamReader(fileStream);
|
||||
tinyobj::MaterialStreamReader* materialReader = readMaterials
|
||||
? &materialStreamReader
|
||||
: NULL;
|
||||
tinyobj::MaterialStreamReader* materialReader =
|
||||
readMaterials ? &materialStreamReader : NULL;
|
||||
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, &fileStream, materialReader);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
&fileStream, materialReader);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
@@ -228,14 +254,11 @@ TestLoadObjFromPreopenedFile(
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
TestStreamLoadObj()
|
||||
{
|
||||
static bool TestStreamLoadObj() {
|
||||
std::cout << "Stream Loading " << std::endl;
|
||||
|
||||
std::stringstream objStream;
|
||||
objStream
|
||||
<< "mtllib cube.mtl\n"
|
||||
objStream << "mtllib cube.mtl\n"
|
||||
"\n"
|
||||
"v 0.000000 2.000000 2.000000\n"
|
||||
"v 0.000000 0.000000 2.000000\n"
|
||||
@@ -294,22 +317,21 @@ std::string matStream(
|
||||
"Ks 0 0 0");
|
||||
|
||||
using namespace tinyobj;
|
||||
class MaterialStringStreamReader:
|
||||
public MaterialReader
|
||||
{
|
||||
class MaterialStringStreamReader : public MaterialReader {
|
||||
public:
|
||||
MaterialStringStreamReader(const std::string& matSStream): m_matSStream(matSStream) {}
|
||||
MaterialStringStreamReader(const std::string& matSStream)
|
||||
: m_matSStream(matSStream) {}
|
||||
virtual ~MaterialStringStreamReader() {}
|
||||
virtual bool operator() (
|
||||
const std::string& matId,
|
||||
virtual bool operator()(const std::string& matId,
|
||||
std::vector<material_t>* materials,
|
||||
std::map<std::string, int>* matMap,
|
||||
std::string* err)
|
||||
{
|
||||
std::string* warn, std::string* err) {
|
||||
(void)matId;
|
||||
(void)warn;
|
||||
(void)err;
|
||||
std::string warning;
|
||||
LoadMtl(matMap, materials, &m_matSStream, &warning);
|
||||
std::string error_msg;
|
||||
LoadMtl(matMap, materials, &m_matSStream, &warning, &error_msg);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -321,11 +343,17 @@ std::string matStream(
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, &objStream, &matSSReader);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
&objStream, &matSSReader);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
@@ -340,21 +368,26 @@ std::string matStream(
|
||||
const char* gMtlBasePath = "../models/";
|
||||
|
||||
TEST_CASE("cornell_box", "[Loader]") {
|
||||
|
||||
REQUIRE(true == TestLoadObj("../models/cornell_box.obj", gMtlBasePath));
|
||||
}
|
||||
|
||||
TEST_CASE("catmark_torus_creases0", "[Loader]") {
|
||||
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/catmark_torus_creases0.obj", gMtlBasePath, /*triangulate*/false);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/catmark_torus_creases0.obj",
|
||||
gMtlBasePath, /*triangulate*/ false);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
REQUIRE(true == ret);
|
||||
@@ -364,16 +397,22 @@ TEST_CASE("catmark_torus_creases0", "[Loader]") {
|
||||
}
|
||||
|
||||
TEST_CASE("pbr", "[Loader]") {
|
||||
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/pbr-mat-ext.obj", gMtlBasePath, /*triangulate*/false);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/pbr-mat-ext.obj", gMtlBasePath,
|
||||
/*triangulate*/ false);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(1 == materials.size());
|
||||
@@ -391,18 +430,18 @@ TEST_CASE("pbr", "[Loader]") {
|
||||
REQUIRE(0 == materials[0].normal_texname.compare("normalmap.tex"));
|
||||
}
|
||||
|
||||
TEST_CASE("stream_load", "[Stream]") {
|
||||
REQUIRE(true == TestStreamLoadObj());
|
||||
}
|
||||
TEST_CASE("stream_load", "[Stream]") { REQUIRE(true == TestStreamLoadObj()); }
|
||||
|
||||
TEST_CASE("stream_load_from_file_skipping_materials", "[Stream]") {
|
||||
REQUIRE(true == TestLoadObjFromPreopenedFile(
|
||||
"../models/pbr-mat-ext.obj", gMtlBasePath, /*readMaterials*/false, /*triangulate*/false));
|
||||
"../models/pbr-mat-ext.obj", gMtlBasePath,
|
||||
/*readMaterials*/ false, /*triangulate*/ false));
|
||||
}
|
||||
|
||||
TEST_CASE("stream_load_from_file_with_materials", "[Stream]") {
|
||||
REQUIRE(true == TestLoadObjFromPreopenedFile(
|
||||
"../models/pbr-mat-ext.obj", gMtlBasePath, /*readMaterials*/true, /*triangulate*/false));
|
||||
"../models/pbr-mat-ext.obj", gMtlBasePath,
|
||||
/*readMaterials*/ true, /*triangulate*/ false));
|
||||
}
|
||||
|
||||
TEST_CASE("trailing_whitespace_in_mtl", "[Issue92]") {
|
||||
@@ -410,11 +449,17 @@ TEST_CASE("trailing_whitespace_in_mtl", "[Issue92]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-92.obj", gMtlBasePath);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/issue-92.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(1 == materials.size());
|
||||
@@ -426,11 +471,17 @@ TEST_CASE("transmittance_filter", "[Issue95]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-95.obj", gMtlBasePath);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/issue-95.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(1 == materials.size());
|
||||
@@ -444,11 +495,17 @@ TEST_CASE("transmittance_filter_Tf", "[Issue95-Tf]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-95-2.obj", gMtlBasePath);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/issue-95-2.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(1 == materials.size());
|
||||
@@ -462,11 +519,17 @@ TEST_CASE("transmittance_filter_Kt", "[Issue95-Kt]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-95.obj", gMtlBasePath);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/issue-95.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(1 == materials.size());
|
||||
@@ -480,11 +543,17 @@ TEST_CASE("usemtl_at_last_line", "[Issue104]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/usemtl-issue-104.obj", gMtlBasePath);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/usemtl-issue-104.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(1 == shapes.size());
|
||||
@@ -495,11 +564,18 @@ TEST_CASE("texture_opts", "[Issue85]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/texture-options-issue-85.obj", gMtlBasePath);
|
||||
bool ret =
|
||||
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/texture-options-issue-85.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(1 == shapes.size());
|
||||
@@ -525,11 +601,13 @@ TEST_CASE("texture_opts", "[Issue85]") {
|
||||
|
||||
REQUIRE(tinyobj::TEXTURE_TYPE_SPHERE == materials[2].diffuse_texopt.type);
|
||||
REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_TOP == materials[2].specular_texopt.type);
|
||||
REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_BOTTOM == materials[2].specular_highlight_texopt.type);
|
||||
REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_BOTTOM ==
|
||||
materials[2].specular_highlight_texopt.type);
|
||||
REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_LEFT == materials[2].ambient_texopt.type);
|
||||
REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_RIGHT == materials[2].alpha_texopt.type);
|
||||
REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_FRONT == materials[2].bump_texopt.type);
|
||||
REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_BACK == materials[2].displacement_texopt.type);
|
||||
REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_BACK ==
|
||||
materials[2].displacement_texopt.type);
|
||||
}
|
||||
|
||||
TEST_CASE("mtllib_multiple_filenames", "[Issue112]") {
|
||||
@@ -537,11 +615,18 @@ TEST_CASE("mtllib_multiple_filenames", "[Issue112]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/mtllib-multiple-files-issue-112.obj", gMtlBasePath);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/mtllib-multiple-files-issue-112.obj",
|
||||
gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(1 == materials.size());
|
||||
@@ -552,11 +637,17 @@ TEST_CASE("tr_and_d", "[Issue43]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/tr-and-d-issue-43.obj", gMtlBasePath);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/tr-and-d-issue-43.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(2 == materials.size());
|
||||
@@ -570,11 +661,17 @@ TEST_CASE("refl", "[refl]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/refl.obj", gMtlBasePath);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/refl.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
PrintInfo(attrib, shapes, materials);
|
||||
@@ -590,11 +687,17 @@ TEST_CASE("map_Bump", "[bump]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/map-bump.obj", gMtlBasePath);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/map-bump.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
PrintInfo(attrib, shapes, materials);
|
||||
@@ -610,11 +713,17 @@ TEST_CASE("g_ignored", "[Issue138]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-138.obj", gMtlBasePath);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/issue-138.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
PrintInfo(attrib, shapes, materials);
|
||||
@@ -622,7 +731,6 @@ TEST_CASE("g_ignored", "[Issue138]") {
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(2 == shapes.size());
|
||||
REQUIRE(2 == materials.size());
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("vertex-col-ext", "[Issue144]") {
|
||||
@@ -630,12 +738,18 @@ TEST_CASE("vertex-col-ext", "[Issue144]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/cube-vertexcol.obj", gMtlBasePath);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/cube-vertexcol.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
// PrintInfo(attrib, shapes, materials);
|
||||
@@ -663,18 +777,23 @@ TEST_CASE("norm_texopts", "[norm]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/norm-texopt.obj", gMtlBasePath);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/norm-texopt.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(1 == shapes.size());
|
||||
REQUIRE(1 == materials.size());
|
||||
REQUIRE(3.0 == Approx(materials[0].normal_texopt.bump_multiplier));
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("zero-face-idx-value", "[Issue140]") {
|
||||
@@ -682,16 +801,21 @@ TEST_CASE("zero-face-idx-value", "[Issue140]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-140-zero-face-idx.obj", gMtlBasePath);
|
||||
bool ret =
|
||||
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/issue-140-zero-face-idx.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
REQUIRE(false == ret);
|
||||
REQUIRE(!err.empty());
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("texture-name-whitespace", "[Issue145]") {
|
||||
@@ -699,12 +823,18 @@ TEST_CASE("texture-name-whitespace", "[Issue145]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/texture-filename-with-whitespace.obj", gMtlBasePath);
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/texture-filename-with-whitespace.obj",
|
||||
gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << "[Issue145] " << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
REQUIRE(true == ret);
|
||||
@@ -714,7 +844,6 @@ TEST_CASE("texture-name-whitespace", "[Issue145]") {
|
||||
REQUIRE(0 == materials[0].diffuse_texname.compare("texture 01.png"));
|
||||
REQUIRE(0 == materials[1].bump_texname.compare("bump 01.png"));
|
||||
REQUIRE(2 == Approx(materials[1].bump_texopt.bump_multiplier));
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("smoothing-group", "[Issue162]") {
|
||||
@@ -722,12 +851,18 @@ TEST_CASE("smoothing-group", "[Issue162]") {
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-162-smoothing-group.obj", gMtlBasePath);
|
||||
bool ret =
|
||||
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/issue-162-smoothing-group.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << "[Issue162] " << err << std::endl;
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
REQUIRE(true == ret);
|
||||
@@ -748,9 +883,165 @@ TEST_CASE("smoothing-group", "[Issue162]") {
|
||||
REQUIRE(0 == shapes[1].mesh.smoothing_group_ids[7]);
|
||||
REQUIRE(6 == shapes[1].mesh.smoothing_group_ids[8]);
|
||||
REQUIRE(6 == shapes[1].mesh.smoothing_group_ids[9]);
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("invalid-face-definition", "[face]") {
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret =
|
||||
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/invalid-face-definition.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(1 == shapes.size());
|
||||
REQUIRE(0 == shapes[0].mesh.indices.size());
|
||||
}
|
||||
|
||||
TEST_CASE("Empty mtl basedir", "[Issue177]") {
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
|
||||
// A case where the user explicitly provides an empty string
|
||||
// Win32 specific?
|
||||
const char* userBaseDir = "";
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"issue-177.obj", userBaseDir);
|
||||
|
||||
// if mtl loading fails, we get an warning message here
|
||||
ret &= warn.empty();
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
REQUIRE(true == ret);
|
||||
}
|
||||
|
||||
TEST_CASE("line-primitive", "[line]") {
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/line-prim.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(1 == shapes.size());
|
||||
REQUIRE(6 == shapes[0].path.indices.size());
|
||||
}
|
||||
|
||||
TEST_CASE("multiple-group-names", "[group]") {
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/cube.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(6 == shapes.size());
|
||||
REQUIRE(0 == shapes[0].name.compare("front cube"));
|
||||
REQUIRE(0 == shapes[1].name.compare("back cube")); // multiple whitespaces
|
||||
// are aggregated as
|
||||
// single white space.
|
||||
}
|
||||
|
||||
TEST_CASE("colorspace", "[Issue184]") {
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string warn;
|
||||
std::string err;
|
||||
bool ret =
|
||||
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||
"../models/colorspace-issue-184.obj", gMtlBasePath);
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << "WARN: " << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << "ERR: " << err << std::endl;
|
||||
}
|
||||
|
||||
REQUIRE(true == ret);
|
||||
REQUIRE(1 == shapes.size());
|
||||
REQUIRE(1 == materials.size());
|
||||
REQUIRE(0 == materials[0].diffuse_texopt.colorspace.compare("sRGB"));
|
||||
REQUIRE(0 == materials[0].specular_texopt.colorspace.size());
|
||||
REQUIRE(0 == materials[0].bump_texopt.colorspace.compare("linear"));
|
||||
}
|
||||
|
||||
// Fuzzer test.
|
||||
// Just check if it does not crash.
|
||||
// Disable by default since Windows filesystem can't create filename of afl
|
||||
// testdata
|
||||
#if 0
|
||||
|
||||
TEST_CASE("afl000000", "[AFL]") {
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "./afl/id:000000,sig:11,src:000000,op:havoc,rep:128", gMtlBasePath);
|
||||
|
||||
REQUIRE(true == ret);
|
||||
}
|
||||
|
||||
TEST_CASE("afl000001", "[AFL]") {
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "./afl/id:000001,sig:11,src:000000,op:havoc,rep:64", gMtlBasePath);
|
||||
|
||||
REQUIRE(true == ret);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
int
|
||||
main(
|
||||
|
||||
@@ -23,6 +23,11 @@ THE SOFTWARE.
|
||||
*/
|
||||
|
||||
//
|
||||
// version 1.3.0 : Separate warning and error message(breaking API of LoadObj)
|
||||
// version 1.2.3 : Added color space extension('-colorspace') to tex opts.
|
||||
// version 1.2.2 : Parse multiple group names.
|
||||
// version 1.2.1 : Added initial support for line('l') primitive(PR #178)
|
||||
// version 1.2.0 : Hardened implementation(#175)
|
||||
// version 1.1.1 : Support smoothing groups(#162)
|
||||
// version 1.1.0 : Support parsing vertex color(#144)
|
||||
// version 1.0.8 : Fix parsing `g` tag just after `usemtl`(#138)
|
||||
@@ -57,6 +62,9 @@ namespace tinyobj {
|
||||
#if __has_warning("-Wzero-as-null-pointer-constant")
|
||||
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
|
||||
#endif
|
||||
|
||||
#pragma clang diagnostic ignored "-Wpadded"
|
||||
|
||||
#endif
|
||||
|
||||
// https://en.wikipedia.org/wiki/Wavefront_.obj_file says ...
|
||||
@@ -107,6 +115,12 @@ namespace tinyobj {
|
||||
// cube_front | cube_back | # side of the cube is specified
|
||||
// separately
|
||||
// cube_left | cube_right
|
||||
//
|
||||
// TinyObjLoader extension.
|
||||
//
|
||||
// -colorspace SPACE # Color space of the texture. e.g.
|
||||
// 'sRGB` or 'linear'
|
||||
//
|
||||
|
||||
#ifdef TINYOBJLOADER_USE_DOUBLE
|
||||
//#pragma message "using double"
|
||||
@@ -141,6 +155,10 @@ typedef struct {
|
||||
bool blendu; // -blendu (default on)
|
||||
bool blendv; // -blendv (default on)
|
||||
real_t bump_multiplier; // -bm (for bump maps only, default 1.0)
|
||||
|
||||
// extension
|
||||
std::string colorspace; // Explicitly specify color space of stored value.
|
||||
// Usually `sRGB` or `linear` (default empty).
|
||||
} texture_option_t;
|
||||
|
||||
typedef struct {
|
||||
@@ -232,9 +250,14 @@ typedef struct {
|
||||
std::vector<tag_t> tags; // SubD tag
|
||||
} mesh_t;
|
||||
|
||||
typedef struct {
|
||||
std::vector<int> indices; // pairs of indices for lines
|
||||
} path_t;
|
||||
|
||||
typedef struct {
|
||||
std::string name;
|
||||
mesh_t mesh;
|
||||
path_t path;
|
||||
} shape_t;
|
||||
|
||||
// Vertex attributes
|
||||
@@ -287,7 +310,7 @@ class MaterialReader {
|
||||
|
||||
virtual bool operator()(const std::string &matId,
|
||||
std::vector<material_t> *materials,
|
||||
std::map<std::string, int> *matMap,
|
||||
std::map<std::string, int> *matMap, std::string *warn,
|
||||
std::string *err) = 0;
|
||||
};
|
||||
|
||||
@@ -298,7 +321,8 @@ class MaterialFileReader : public MaterialReader {
|
||||
virtual ~MaterialFileReader() {}
|
||||
virtual bool operator()(const std::string &matId,
|
||||
std::vector<material_t> *materials,
|
||||
std::map<std::string, int> *matMap, std::string *err);
|
||||
std::map<std::string, int> *matMap, std::string *warn,
|
||||
std::string *err);
|
||||
|
||||
private:
|
||||
std::string m_mtlBaseDir;
|
||||
@@ -311,7 +335,8 @@ class MaterialStreamReader : public MaterialReader {
|
||||
virtual ~MaterialStreamReader() {}
|
||||
virtual bool operator()(const std::string &matId,
|
||||
std::vector<material_t> *materials,
|
||||
std::map<std::string, int> *matMap, std::string *err);
|
||||
std::map<std::string, int> *matMap, std::string *warn,
|
||||
std::string *err);
|
||||
|
||||
private:
|
||||
std::istream &m_inStream;
|
||||
@@ -321,41 +346,45 @@ class MaterialStreamReader : public MaterialReader {
|
||||
/// 'attrib', 'shapes' and 'materials' will be filled with parsed shape data
|
||||
/// 'shapes' will be filled with parsed shape data
|
||||
/// Returns true when loading .obj become success.
|
||||
/// Returns warning and error message into `err`
|
||||
/// Returns warning message into `warn`, and error message into `err`
|
||||
/// 'mtl_basedir' is optional, and used for base directory for .mtl file.
|
||||
/// In default(`NULL'), .mtl file is searched from an application's working
|
||||
/// directory.
|
||||
/// 'triangulate' is optional, and used whether triangulate polygon face in .obj
|
||||
/// or not.
|
||||
/// Option 'default_vcols_fallback' specifies whether vertex colors should
|
||||
/// always be defined, even if no colors are given (fallback to white).
|
||||
bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
std::vector<material_t> *materials, std::string *err,
|
||||
const char *filename, const char *mtl_basedir = NULL,
|
||||
bool triangulate = true);
|
||||
std::vector<material_t> *materials, std::string *warn,
|
||||
std::string *err, const char *filename,
|
||||
const char *mtl_basedir = NULL, bool triangulate = true,
|
||||
bool default_vcols_fallback = true);
|
||||
|
||||
/// Loads .obj from a file with custom user callback.
|
||||
/// .mtl is loaded as usual and parsed material_t data will be passed to
|
||||
/// `callback.mtllib_cb`.
|
||||
/// Returns true when loading .obj/.mtl become success.
|
||||
/// Returns warning and error message into `err`
|
||||
/// Returns warning message into `warn`, and error message into `err`
|
||||
/// See `examples/callback_api/` for how to use this function.
|
||||
bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
|
||||
void *user_data = NULL,
|
||||
MaterialReader *readMatFn = NULL,
|
||||
std::string *err = NULL);
|
||||
std::string *warn = NULL, std::string *err = NULL);
|
||||
|
||||
/// Loads object from a std::istream, uses GetMtlIStreamFn to retrieve
|
||||
/// std::istream for materials.
|
||||
/// Returns true when loading .obj become success.
|
||||
/// Returns warning and error message into `err`
|
||||
bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
std::vector<material_t> *materials, std::string *err,
|
||||
std::istream *inStream, MaterialReader *readMatFn = NULL,
|
||||
bool triangulate = true);
|
||||
std::vector<material_t> *materials, std::string *warn,
|
||||
std::string *err, std::istream *inStream,
|
||||
MaterialReader *readMatFn = NULL, bool triangulate = true,
|
||||
bool default_vcols_fallback = true);
|
||||
|
||||
/// Loads materials into std::map
|
||||
void LoadMtl(std::map<std::string, int> *material_map,
|
||||
std::vector<material_t> *materials, std::istream *inStream,
|
||||
std::string *warning);
|
||||
std::string *warning, std::string *err);
|
||||
|
||||
} // namespace tinyobj
|
||||
|
||||
@@ -368,6 +397,7 @@ void LoadMtl(std::map<std::string, int> *material_map,
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
|
||||
#include <fstream>
|
||||
@@ -396,6 +426,11 @@ struct face_t {
|
||||
face_t() : smoothing_group_id(0) {}
|
||||
};
|
||||
|
||||
struct line_t {
|
||||
int idx0;
|
||||
int idx1;
|
||||
};
|
||||
|
||||
struct tag_sizes {
|
||||
tag_sizes() : num_ints(0), num_reals(0), num_strings(0) {}
|
||||
int num_ints;
|
||||
@@ -693,11 +728,14 @@ static inline bool parseVertexWithColor(real_t *x, real_t *y, real_t *z,
|
||||
(*y) = parseReal(token, default_y);
|
||||
(*z) = parseReal(token, default_z);
|
||||
|
||||
(*r) = parseReal(token, 1.0);
|
||||
(*g) = parseReal(token, 1.0);
|
||||
(*b) = parseReal(token, 1.0);
|
||||
const bool found_color =
|
||||
parseReal(token, r) && parseReal(token, g) && parseReal(token, b);
|
||||
|
||||
return true;
|
||||
if (!found_color) {
|
||||
(*r) = (*g) = (*b) = 1.0;
|
||||
}
|
||||
|
||||
return found_color;
|
||||
}
|
||||
|
||||
static inline bool parseOnOff(const char **token, bool default_value = true) {
|
||||
@@ -866,22 +904,22 @@ static bool ParseTextureNameAndOption(std::string *texname,
|
||||
} else {
|
||||
texopt->imfchan = 'm';
|
||||
}
|
||||
texopt->bump_multiplier = 1.0f;
|
||||
texopt->bump_multiplier = static_cast<real_t>(1.0);
|
||||
texopt->clamp = false;
|
||||
texopt->blendu = true;
|
||||
texopt->blendv = true;
|
||||
texopt->sharpness = 1.0f;
|
||||
texopt->brightness = 0.0f;
|
||||
texopt->contrast = 1.0f;
|
||||
texopt->origin_offset[0] = 0.0f;
|
||||
texopt->origin_offset[1] = 0.0f;
|
||||
texopt->origin_offset[2] = 0.0f;
|
||||
texopt->scale[0] = 1.0f;
|
||||
texopt->scale[1] = 1.0f;
|
||||
texopt->scale[2] = 1.0f;
|
||||
texopt->turbulence[0] = 0.0f;
|
||||
texopt->turbulence[1] = 0.0f;
|
||||
texopt->turbulence[2] = 0.0f;
|
||||
texopt->sharpness = static_cast<real_t>(1.0);
|
||||
texopt->brightness = static_cast<real_t>(0.0);
|
||||
texopt->contrast = static_cast<real_t>(1.0);
|
||||
texopt->origin_offset[0] = static_cast<real_t>(0.0);
|
||||
texopt->origin_offset[1] = static_cast<real_t>(0.0);
|
||||
texopt->origin_offset[2] = static_cast<real_t>(0.0);
|
||||
texopt->scale[0] = static_cast<real_t>(1.0);
|
||||
texopt->scale[1] = static_cast<real_t>(1.0);
|
||||
texopt->scale[2] = static_cast<real_t>(1.0);
|
||||
texopt->turbulence[0] = static_cast<real_t>(0.0);
|
||||
texopt->turbulence[1] = static_cast<real_t>(0.0);
|
||||
texopt->turbulence[2] = static_cast<real_t>(0.0);
|
||||
texopt->type = TEXTURE_TYPE_NONE;
|
||||
|
||||
const char *token = linebuf; // Assume line ends with NULL
|
||||
@@ -929,6 +967,10 @@ static bool ParseTextureNameAndOption(std::string *texname,
|
||||
} else if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) {
|
||||
token += 4;
|
||||
parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0);
|
||||
} else if ((0 == strncmp(token, "-colorspace", 11)) &&
|
||||
IS_SPACE((token[11]))) {
|
||||
token += 12;
|
||||
texopt->colorspace = parseString(&token);
|
||||
} else {
|
||||
// Assume texture filename
|
||||
#if 0
|
||||
@@ -967,24 +1009,24 @@ static void InitMaterial(material_t *material) {
|
||||
material->reflection_texname = "";
|
||||
material->alpha_texname = "";
|
||||
for (int i = 0; i < 3; i++) {
|
||||
material->ambient[i] = 0.f;
|
||||
material->diffuse[i] = 0.f;
|
||||
material->specular[i] = 0.f;
|
||||
material->transmittance[i] = 0.f;
|
||||
material->emission[i] = 0.f;
|
||||
material->ambient[i] = static_cast<real_t>(0.0);
|
||||
material->diffuse[i] = static_cast<real_t>(0.0);
|
||||
material->specular[i] = static_cast<real_t>(0.0);
|
||||
material->transmittance[i] = static_cast<real_t>(0.0);
|
||||
material->emission[i] = static_cast<real_t>(0.0);
|
||||
}
|
||||
material->illum = 0;
|
||||
material->dissolve = 1.f;
|
||||
material->shininess = 1.f;
|
||||
material->ior = 1.f;
|
||||
material->dissolve = static_cast<real_t>(1.0);
|
||||
material->shininess = static_cast<real_t>(1.0);
|
||||
material->ior = static_cast<real_t>(1.0);
|
||||
|
||||
material->roughness = 0.f;
|
||||
material->metallic = 0.f;
|
||||
material->sheen = 0.f;
|
||||
material->clearcoat_thickness = 0.f;
|
||||
material->clearcoat_roughness = 0.f;
|
||||
material->anisotropy_rotation = 0.f;
|
||||
material->anisotropy = 0.f;
|
||||
material->roughness = static_cast<real_t>(0.0);
|
||||
material->metallic = static_cast<real_t>(0.0);
|
||||
material->sheen = static_cast<real_t>(0.0);
|
||||
material->clearcoat_thickness = static_cast<real_t>(0.0);
|
||||
material->clearcoat_roughness = static_cast<real_t>(0.0);
|
||||
material->anisotropy_rotation = static_cast<real_t>(0.0);
|
||||
material->anisotropy = static_cast<real_t>(0.0);
|
||||
material->roughness_texname = "";
|
||||
material->metallic_texname = "";
|
||||
material->sheen_texname = "";
|
||||
@@ -995,8 +1037,8 @@ static void InitMaterial(material_t *material) {
|
||||
}
|
||||
|
||||
// code from https://wrf.ecse.rpi.edu//Research/Short_Notes/pnpoly.html
|
||||
static int pnpoly(int nvert, float *vertx, float *verty, float testx,
|
||||
float testy) {
|
||||
template <typename T>
|
||||
static int pnpoly(int nvert, T *vertx, T *verty, T testx, T testy) {
|
||||
int i, j, c = 0;
|
||||
for (i = 0, j = nvert - 1; i < nvert; j = i++) {
|
||||
if (((verty[i] > testy) != (verty[j] > testy)) &&
|
||||
@@ -1009,26 +1051,33 @@ static int pnpoly(int nvert, float *vertx, float *verty, float testx,
|
||||
}
|
||||
|
||||
// TODO(syoyo): refactor function.
|
||||
static bool exportFaceGroupToShape(shape_t *shape,
|
||||
static bool exportGroupsToShape(shape_t *shape,
|
||||
const std::vector<face_t> &faceGroup,
|
||||
std::vector<int> &lineGroup,
|
||||
const std::vector<tag_t> &tags,
|
||||
const int material_id,
|
||||
const std::string &name, bool triangulate,
|
||||
const int material_id, const std::string &name,
|
||||
bool triangulate,
|
||||
const std::vector<real_t> &v) {
|
||||
if (faceGroup.empty()) {
|
||||
if (faceGroup.empty() && lineGroup.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!faceGroup.empty()) {
|
||||
// Flatten vertices and indices
|
||||
for (size_t i = 0; i < faceGroup.size(); i++) {
|
||||
const face_t &face = faceGroup[i];
|
||||
|
||||
size_t npolys = face.vertex_indices.size();
|
||||
|
||||
if (npolys < 3) {
|
||||
// Face must have 3+ vertices.
|
||||
continue;
|
||||
}
|
||||
|
||||
vertex_index_t i0 = face.vertex_indices[0];
|
||||
vertex_index_t i1(-1);
|
||||
vertex_index_t i2 = face.vertex_indices[1];
|
||||
|
||||
size_t npolys = face.vertex_indices.size();
|
||||
|
||||
if (triangulate) {
|
||||
// find the two axes to work in
|
||||
size_t axes[2] = {1, 2};
|
||||
@@ -1039,6 +1088,13 @@ static bool exportFaceGroupToShape(shape_t *shape,
|
||||
size_t vi0 = size_t(i0.v_idx);
|
||||
size_t vi1 = size_t(i1.v_idx);
|
||||
size_t vi2 = size_t(i2.v_idx);
|
||||
|
||||
if (((3 * vi0 + 2) >= v.size()) || ((3 * vi1 + 2) >= v.size()) ||
|
||||
((3 * vi2 + 2) >= v.size())) {
|
||||
// Invalid triangle.
|
||||
// FIXME(syoyo): Is it ok to simply skip this invalid triangle?
|
||||
continue;
|
||||
}
|
||||
real_t v0x = v[vi0 * 3 + 0];
|
||||
real_t v0y = v[vi0 * 3 + 1];
|
||||
real_t v0z = v[vi0 * 3 + 2];
|
||||
@@ -1054,10 +1110,10 @@ static bool exportFaceGroupToShape(shape_t *shape,
|
||||
real_t e1x = v2x - v1x;
|
||||
real_t e1y = v2y - v1y;
|
||||
real_t e1z = v2z - v1z;
|
||||
float cx = std::fabs(e0y * e1z - e0z * e1y);
|
||||
float cy = std::fabs(e0z * e1x - e0x * e1z);
|
||||
float cz = std::fabs(e0x * e1y - e0y * e1x);
|
||||
const float epsilon = 0.0001f;
|
||||
real_t cx = std::fabs(e0y * e1z - e0z * e1y);
|
||||
real_t cy = std::fabs(e0z * e1x - e0x * e1z);
|
||||
real_t cz = std::fabs(e0x * e1y - e0y * e1x);
|
||||
const real_t epsilon = std::numeric_limits<real_t>::epsilon();
|
||||
if (cx > epsilon || cy > epsilon || cz > epsilon) {
|
||||
// found a corner
|
||||
if (cx > cy && cx > cz) {
|
||||
@@ -1075,15 +1131,22 @@ static bool exportFaceGroupToShape(shape_t *shape,
|
||||
i1 = face.vertex_indices[(k + 1) % npolys];
|
||||
size_t vi0 = size_t(i0.v_idx);
|
||||
size_t vi1 = size_t(i1.v_idx);
|
||||
if (((vi0 * 3 + axes[0]) >= v.size()) ||
|
||||
((vi0 * 3 + axes[1]) >= v.size()) ||
|
||||
((vi1 * 3 + axes[0]) >= v.size()) ||
|
||||
((vi1 * 3 + axes[1]) >= v.size())) {
|
||||
// Invalid index.
|
||||
continue;
|
||||
}
|
||||
real_t v0x = v[vi0 * 3 + axes[0]];
|
||||
real_t v0y = v[vi0 * 3 + axes[1]];
|
||||
real_t v1x = v[vi1 * 3 + axes[0]];
|
||||
real_t v1y = v[vi1 * 3 + axes[1]];
|
||||
area += (v0x * v1y - v0y * v1x) * 0.5f;
|
||||
area += (v0x * v1y - v0y * v1x) * static_cast<real_t>(0.5);
|
||||
}
|
||||
|
||||
int maxRounds =
|
||||
10; // arbitrary max loop count to protect against unexpected errors
|
||||
int maxRounds = 10; // arbitrary max loop count to protect against
|
||||
// unexpected errors
|
||||
|
||||
face_t remainingFace = face; // copy
|
||||
size_t guess_vert = 0;
|
||||
@@ -1099,16 +1162,23 @@ static bool exportFaceGroupToShape(shape_t *shape,
|
||||
for (size_t k = 0; k < 3; k++) {
|
||||
ind[k] = remainingFace.vertex_indices[(guess_vert + k) % npolys];
|
||||
size_t vi = size_t(ind[k].v_idx);
|
||||
if (((vi * 3 + axes[0]) >= v.size()) ||
|
||||
((vi * 3 + axes[1]) >= v.size())) {
|
||||
// ???
|
||||
vx[k] = static_cast<real_t>(0.0);
|
||||
vy[k] = static_cast<real_t>(0.0);
|
||||
} else {
|
||||
vx[k] = v[vi * 3 + axes[0]];
|
||||
vy[k] = v[vi * 3 + axes[1]];
|
||||
}
|
||||
}
|
||||
real_t e0x = vx[1] - vx[0];
|
||||
real_t e0y = vy[1] - vy[0];
|
||||
real_t e1x = vx[2] - vx[1];
|
||||
real_t e1y = vy[2] - vy[1];
|
||||
real_t cross = e0x * e1y - e0y * e1x;
|
||||
// if an internal angle
|
||||
if (cross * area < 0.0f) {
|
||||
if (cross * area < static_cast<real_t>(0.0)) {
|
||||
guess_vert += 1;
|
||||
continue;
|
||||
}
|
||||
@@ -1116,9 +1186,20 @@ static bool exportFaceGroupToShape(shape_t *shape,
|
||||
// check all other verts in case they are inside this triangle
|
||||
bool overlap = false;
|
||||
for (size_t otherVert = 3; otherVert < npolys; ++otherVert) {
|
||||
size_t ovi = size_t(
|
||||
remainingFace.vertex_indices[(guess_vert + otherVert) % npolys]
|
||||
.v_idx);
|
||||
size_t idx = (guess_vert + otherVert) % npolys;
|
||||
|
||||
if (idx >= remainingFace.vertex_indices.size()) {
|
||||
// ???
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t ovi = size_t(remainingFace.vertex_indices[idx].v_idx);
|
||||
|
||||
if (((ovi * 3 + axes[0]) >= v.size()) ||
|
||||
((ovi * 3 + axes[1]) >= v.size())) {
|
||||
// ???
|
||||
continue;
|
||||
}
|
||||
real_t tx = v[ovi * 3 + axes[0]];
|
||||
real_t ty = v[ovi * 3 + axes[1]];
|
||||
if (pnpoly(3, vx, vy, tx, ty)) {
|
||||
@@ -1208,6 +1289,11 @@ static bool exportFaceGroupToShape(shape_t *shape,
|
||||
|
||||
shape->name = name;
|
||||
shape->mesh.tags = tags;
|
||||
}
|
||||
|
||||
if (!lineGroup.empty()) {
|
||||
shape->path.indices.swap(lineGroup);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1226,7 +1312,9 @@ static void SplitString(const std::string &s, char delim,
|
||||
|
||||
void LoadMtl(std::map<std::string, int> *material_map,
|
||||
std::vector<material_t> *materials, std::istream *inStream,
|
||||
std::string *warning) {
|
||||
std::string *warning, std::string *err) {
|
||||
(void)err;
|
||||
|
||||
// Create a default material anyway.
|
||||
material_t material;
|
||||
InitMaterial(&material);
|
||||
@@ -1235,11 +1323,13 @@ void LoadMtl(std::map<std::string, int> *material_map,
|
||||
bool has_d = false;
|
||||
bool has_tr = false;
|
||||
|
||||
std::stringstream ss;
|
||||
std::stringstream warn_ss;
|
||||
|
||||
size_t line_no = 0;
|
||||
std::string linebuf;
|
||||
while (inStream->peek() != -1) {
|
||||
safeGetline(*inStream, linebuf);
|
||||
line_no++;
|
||||
|
||||
// Trim trailing whitespace.
|
||||
if (linebuf.size() > 0) {
|
||||
@@ -1378,8 +1468,9 @@ void LoadMtl(std::map<std::string, int> *material_map,
|
||||
material.dissolve = parseReal(&token);
|
||||
|
||||
if (has_tr) {
|
||||
ss << "WARN: Both `d` and `Tr` parameters defined for \""
|
||||
<< material.name << "\". Use the value of `d` for dissolve."
|
||||
warn_ss << "Both `d` and `Tr` parameters defined for \""
|
||||
<< material.name << "\". Use the value of `d` for dissolve (line "
|
||||
<< line_no << " in .mtl.)"
|
||||
<< std::endl;
|
||||
}
|
||||
has_d = true;
|
||||
@@ -1389,14 +1480,15 @@ void LoadMtl(std::map<std::string, int> *material_map,
|
||||
token += 2;
|
||||
if (has_d) {
|
||||
// `d` wins. Ignore `Tr` value.
|
||||
ss << "WARN: Both `d` and `Tr` parameters defined for \""
|
||||
<< material.name << "\". Use the value of `d` for dissolve."
|
||||
warn_ss << "Both `d` and `Tr` parameters defined for \""
|
||||
<< material.name << "\". Use the value of `d` for dissolve (line "
|
||||
<< line_no << " in .mtl.)"
|
||||
<< std::endl;
|
||||
} else {
|
||||
// We invert value of Tr(assume Tr is in range [0, 1])
|
||||
// NOTE: Interpretation of Tr is application(exporter) dependent. For
|
||||
// some application(e.g. 3ds max obj exporter), Tr = d(Issue 43)
|
||||
material.dissolve = 1.0f - parseReal(&token);
|
||||
material.dissolve = static_cast<real_t>(1.0) - parseReal(&token);
|
||||
}
|
||||
has_tr = true;
|
||||
continue;
|
||||
@@ -1606,14 +1698,14 @@ void LoadMtl(std::map<std::string, int> *material_map,
|
||||
materials->push_back(material);
|
||||
|
||||
if (warning) {
|
||||
(*warning) = ss.str();
|
||||
(*warning) = warn_ss.str();
|
||||
}
|
||||
}
|
||||
|
||||
bool MaterialFileReader::operator()(const std::string &matId,
|
||||
std::vector<material_t> *materials,
|
||||
std::map<std::string, int> *matMap,
|
||||
std::string *err) {
|
||||
std::string *warn, std::string *err) {
|
||||
std::string filepath;
|
||||
|
||||
if (!m_mtlBaseDir.empty()) {
|
||||
@@ -1625,21 +1717,14 @@ bool MaterialFileReader::operator()(const std::string &matId,
|
||||
std::ifstream matIStream(filepath.c_str());
|
||||
if (!matIStream) {
|
||||
std::stringstream ss;
|
||||
ss << "WARN: Material file [ " << filepath << " ] not found." << std::endl;
|
||||
if (err) {
|
||||
(*err) += ss.str();
|
||||
ss << "Material file [ " << filepath << " ] not found." << std::endl;
|
||||
if (warn) {
|
||||
(*warn) += ss.str();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string warning;
|
||||
LoadMtl(matMap, materials, &matIStream, &warning);
|
||||
|
||||
if (!warning.empty()) {
|
||||
if (err) {
|
||||
(*err) += warning;
|
||||
}
|
||||
}
|
||||
LoadMtl(matMap, materials, &matIStream, warn, err);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1647,32 +1732,27 @@ bool MaterialFileReader::operator()(const std::string &matId,
|
||||
bool MaterialStreamReader::operator()(const std::string &matId,
|
||||
std::vector<material_t> *materials,
|
||||
std::map<std::string, int> *matMap,
|
||||
std::string *err) {
|
||||
std::string *warn, std::string *err) {
|
||||
(void)err;
|
||||
(void)matId;
|
||||
if (!m_inStream) {
|
||||
std::stringstream ss;
|
||||
ss << "WARN: Material stream in error state. " << std::endl;
|
||||
if (err) {
|
||||
(*err) += ss.str();
|
||||
ss << "Material stream in error state. " << std::endl;
|
||||
if (warn) {
|
||||
(*warn) += ss.str();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string warning;
|
||||
LoadMtl(matMap, materials, &m_inStream, &warning);
|
||||
|
||||
if (!warning.empty()) {
|
||||
if (err) {
|
||||
(*err) += warning;
|
||||
}
|
||||
}
|
||||
LoadMtl(matMap, materials, &m_inStream, warn, err);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
std::vector<material_t> *materials, std::string *err,
|
||||
const char *filename, const char *mtl_basedir, bool trianglulate) {
|
||||
std::vector<material_t> *materials, std::string *warn,
|
||||
std::string *err, const char *filename, const char *mtl_basedir,
|
||||
bool trianglulate, bool default_vcols_fallback) {
|
||||
attrib->vertices.clear();
|
||||
attrib->normals.clear();
|
||||
attrib->texcoords.clear();
|
||||
@@ -1690,20 +1770,26 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string baseDir;
|
||||
if (mtl_basedir) {
|
||||
baseDir = mtl_basedir;
|
||||
std::string baseDir = mtl_basedir ? mtl_basedir : "";
|
||||
if (!baseDir.empty()) {
|
||||
#ifndef _WIN32
|
||||
const char dirsep = '/';
|
||||
#else
|
||||
const char dirsep = '\\';
|
||||
#endif
|
||||
if (baseDir[baseDir.length() - 1] != dirsep) baseDir += dirsep;
|
||||
}
|
||||
MaterialFileReader matFileReader(baseDir);
|
||||
|
||||
return LoadObj(attrib, shapes, materials, err, &ifs, &matFileReader,
|
||||
trianglulate);
|
||||
return LoadObj(attrib, shapes, materials, warn, err, &ifs, &matFileReader,
|
||||
trianglulate, default_vcols_fallback);
|
||||
}
|
||||
|
||||
bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
std::vector<material_t> *materials, std::string *err,
|
||||
std::istream *inStream, MaterialReader *readMatFn /*= NULL*/,
|
||||
bool triangulate) {
|
||||
std::vector<material_t> *materials, std::string *warn,
|
||||
std::string *err, std::istream *inStream,
|
||||
MaterialReader *readMatFn /*= NULL*/, bool triangulate,
|
||||
bool default_vcols_fallback) {
|
||||
std::stringstream errss;
|
||||
|
||||
std::vector<real_t> v;
|
||||
@@ -1712,6 +1798,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
std::vector<real_t> vc;
|
||||
std::vector<tag_t> tags;
|
||||
std::vector<face_t> faceGroup;
|
||||
std::vector<int> lineGroup;
|
||||
std::string name;
|
||||
|
||||
// material
|
||||
@@ -1722,12 +1809,21 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
unsigned int current_smoothing_id =
|
||||
0; // Initial value. 0 means no smoothing.
|
||||
|
||||
int greatest_v_idx = -1;
|
||||
int greatest_vn_idx = -1;
|
||||
int greatest_vt_idx = -1;
|
||||
|
||||
shape_t shape;
|
||||
|
||||
bool found_all_colors = true;
|
||||
|
||||
size_t line_num = 0;
|
||||
std::string linebuf;
|
||||
while (inStream->peek() != -1) {
|
||||
safeGetline(*inStream, linebuf);
|
||||
|
||||
line_num++;
|
||||
|
||||
// Trim newline '\r\n' or '\n'
|
||||
if (linebuf.size() > 0) {
|
||||
if (linebuf[linebuf.size() - 1] == '\n')
|
||||
@@ -1757,14 +1853,19 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
token += 2;
|
||||
real_t x, y, z;
|
||||
real_t r, g, b;
|
||||
parseVertexWithColor(&x, &y, &z, &r, &g, &b, &token);
|
||||
|
||||
found_all_colors &= parseVertexWithColor(&x, &y, &z, &r, &g, &b, &token);
|
||||
|
||||
v.push_back(x);
|
||||
v.push_back(y);
|
||||
v.push_back(z);
|
||||
|
||||
if (found_all_colors || default_vcols_fallback) {
|
||||
vc.push_back(r);
|
||||
vc.push_back(g);
|
||||
vc.push_back(b);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1789,6 +1890,33 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
continue;
|
||||
}
|
||||
|
||||
// line
|
||||
if (token[0] == 'l' && IS_SPACE((token[1]))) {
|
||||
token += 2;
|
||||
|
||||
line_t line_cache;
|
||||
bool end_line_bit = 0;
|
||||
while (!IS_NEW_LINE(token[0])) {
|
||||
// get index from string
|
||||
int idx;
|
||||
fixIndex(parseInt(&token), 0, &idx);
|
||||
|
||||
size_t n = strspn(token, " \t\r");
|
||||
token += n;
|
||||
|
||||
if (!end_line_bit) {
|
||||
line_cache.idx0 = idx;
|
||||
} else {
|
||||
line_cache.idx1 = idx;
|
||||
lineGroup.push_back(line_cache.idx0);
|
||||
lineGroup.push_back(line_cache.idx1);
|
||||
line_cache = line_t();
|
||||
}
|
||||
end_line_bit = !end_line_bit;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
// face
|
||||
if (token[0] == 'f' && IS_SPACE((token[1]))) {
|
||||
token += 2;
|
||||
@@ -1805,11 +1933,19 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
static_cast<int>(vn.size() / 3),
|
||||
static_cast<int>(vt.size() / 2), &vi)) {
|
||||
if (err) {
|
||||
(*err) = "Failed parse `f' line(e.g. zero value for face index).\n";
|
||||
std::stringstream ss;
|
||||
ss << "Failed parse `f' line(e.g. zero value for face index. line " << line_num << ".)\n";
|
||||
(*err) += ss.str();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
greatest_v_idx = greatest_v_idx > vi.v_idx ? greatest_v_idx : vi.v_idx;
|
||||
greatest_vn_idx =
|
||||
greatest_vn_idx > vi.vn_idx ? greatest_vn_idx : vi.vn_idx;
|
||||
greatest_vt_idx =
|
||||
greatest_vt_idx > vi.vt_idx ? greatest_vt_idx : vi.vt_idx;
|
||||
|
||||
face.vertex_indices.push_back(vi);
|
||||
size_t n = strspn(token, " \t\r");
|
||||
token += n;
|
||||
@@ -1838,8 +1974,8 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
if (newMaterialId != material) {
|
||||
// Create per-face material. Thus we don't add `shape` to `shapes` at
|
||||
// this time.
|
||||
// just clear `faceGroup` after `exportFaceGroupToShape()` call.
|
||||
exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
|
||||
// just clear `faceGroup` after `exportGroupsToShape()` call.
|
||||
exportGroupsToShape(&shape, faceGroup, lineGroup, tags, material, name,
|
||||
triangulate, v);
|
||||
faceGroup.clear();
|
||||
material = newMaterialId;
|
||||
@@ -1857,19 +1993,26 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
SplitString(std::string(token), ' ', filenames);
|
||||
|
||||
if (filenames.empty()) {
|
||||
if (err) {
|
||||
(*err) +=
|
||||
"WARN: Looks like empty filename for mtllib. Use default "
|
||||
"material. \n";
|
||||
if (warn) {
|
||||
std::stringstream ss;
|
||||
ss << "Looks like empty filename for mtllib. Use default "
|
||||
"material (line " << line_num << ".)\n";
|
||||
|
||||
(*warn) += ss.str();
|
||||
}
|
||||
} else {
|
||||
bool found = false;
|
||||
for (size_t s = 0; s < filenames.size(); s++) {
|
||||
std::string warn_mtl;
|
||||
std::string err_mtl;
|
||||
bool ok = (*readMatFn)(filenames[s].c_str(), materials,
|
||||
&material_map, &err_mtl);
|
||||
&material_map, &warn_mtl, &err_mtl);
|
||||
if (warn && (!warn_mtl.empty())) {
|
||||
(*warn) += warn_mtl;
|
||||
}
|
||||
|
||||
if (err && (!err_mtl.empty())) {
|
||||
(*err) += err_mtl; // This should be warn message.
|
||||
(*err) += err_mtl;
|
||||
}
|
||||
|
||||
if (ok) {
|
||||
@@ -1879,9 +2022,9 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
if (err) {
|
||||
(*err) +=
|
||||
"WARN: Failed to load material file(s). Use default "
|
||||
if (warn) {
|
||||
(*warn) +=
|
||||
"Failed to load material file(s). Use default "
|
||||
"material.\n";
|
||||
}
|
||||
}
|
||||
@@ -1894,8 +2037,8 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
// group name
|
||||
if (token[0] == 'g' && IS_SPACE((token[1]))) {
|
||||
// flush previous face group.
|
||||
bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
|
||||
triangulate, v);
|
||||
bool ret = exportGroupsToShape(&shape, faceGroup, lineGroup, tags,
|
||||
material, name, triangulate, v);
|
||||
(void)ret; // return value not used.
|
||||
|
||||
if (shape.mesh.indices.size() > 0) {
|
||||
@@ -1908,7 +2051,6 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
faceGroup.clear();
|
||||
|
||||
std::vector<std::string> names;
|
||||
names.reserve(2);
|
||||
|
||||
while (!IS_NEW_LINE(token[0])) {
|
||||
std::string str = parseString(&token);
|
||||
@@ -1916,14 +2058,30 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
token += strspn(token, " \t\r"); // skip tag
|
||||
}
|
||||
|
||||
assert(names.size() > 0);
|
||||
// names[0] must be 'g'
|
||||
|
||||
// names[0] must be 'g', so skip the 0th element.
|
||||
if (names.size() > 1) {
|
||||
name = names[1];
|
||||
} else {
|
||||
if (names.size() < 2) {
|
||||
// 'g' with empty names
|
||||
if (warn) {
|
||||
std::stringstream ss;
|
||||
ss << "Empty group name. line: " << line_num << "\n";
|
||||
(*warn) += ss.str();
|
||||
name = "";
|
||||
}
|
||||
} else {
|
||||
std::stringstream ss;
|
||||
ss << names[1];
|
||||
|
||||
// tinyobjloader does not support multiple groups for a primitive.
|
||||
// Currently we concatinate multiple group names with a space to get
|
||||
// single group name.
|
||||
|
||||
for (size_t i = 2; i < names.size(); i++) {
|
||||
ss << " " << names[i];
|
||||
}
|
||||
|
||||
name = ss.str();
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
@@ -1931,8 +2089,8 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
// object name
|
||||
if (token[0] == 'o' && IS_SPACE((token[1]))) {
|
||||
// flush previous face group.
|
||||
bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
|
||||
triangulate, v);
|
||||
bool ret = exportGroupsToShape(&shape, faceGroup, lineGroup, tags,
|
||||
material, name, triangulate, v);
|
||||
if (ret) {
|
||||
shapes->push_back(shape);
|
||||
}
|
||||
@@ -1951,6 +2109,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
}
|
||||
|
||||
if (token[0] == 't' && IS_SPACE(token[1])) {
|
||||
const int max_tag_nums = 8192; // FIXME(syoyo): Parameterize.
|
||||
tag_t tag;
|
||||
|
||||
token += 2;
|
||||
@@ -1959,6 +2118,27 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
|
||||
tag_sizes ts = parseTagTriple(&token);
|
||||
|
||||
if (ts.num_ints < 0) {
|
||||
ts.num_ints = 0;
|
||||
}
|
||||
if (ts.num_ints > max_tag_nums) {
|
||||
ts.num_ints = max_tag_nums;
|
||||
}
|
||||
|
||||
if (ts.num_reals < 0) {
|
||||
ts.num_reals = 0;
|
||||
}
|
||||
if (ts.num_reals > max_tag_nums) {
|
||||
ts.num_reals = max_tag_nums;
|
||||
}
|
||||
|
||||
if (ts.num_strings < 0) {
|
||||
ts.num_strings = 0;
|
||||
}
|
||||
if (ts.num_strings > max_tag_nums) {
|
||||
ts.num_strings = max_tag_nums;
|
||||
}
|
||||
|
||||
tag.intValues.resize(static_cast<size_t>(ts.num_ints));
|
||||
|
||||
for (size_t i = 0; i < static_cast<size_t>(ts.num_ints); ++i) {
|
||||
@@ -2017,9 +2197,36 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
// Ignore unknown command.
|
||||
}
|
||||
|
||||
bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name,
|
||||
triangulate, v);
|
||||
// exportFaceGroupToShape return false when `usemtl` is called in the last
|
||||
// not all vertices have colors, no default colors desired? -> clear colors
|
||||
if (!found_all_colors && !default_vcols_fallback) {
|
||||
vc.clear();
|
||||
}
|
||||
|
||||
if (greatest_v_idx >= static_cast<int>(v.size() / 3)) {
|
||||
if (warn) {
|
||||
std::stringstream ss;
|
||||
ss << "Vertex indices out of bounds (line " << line_num << ".)\n" << std::endl;
|
||||
(*warn) += ss.str();
|
||||
}
|
||||
}
|
||||
if (greatest_vn_idx >= static_cast<int>(vn.size() / 3)) {
|
||||
if (warn) {
|
||||
std::stringstream ss;
|
||||
ss << "Vertex normal indices out of bounds (line " << line_num << ".)\n" << std::endl;
|
||||
(*warn) += ss.str();
|
||||
}
|
||||
}
|
||||
if (greatest_vt_idx >= static_cast<int>(vt.size() / 2)) {
|
||||
if (warn) {
|
||||
std::stringstream ss;
|
||||
ss << "Vertex texcoord indices out of bounds (line " << line_num << ".)\n" << std::endl;
|
||||
(*warn) += ss.str();
|
||||
}
|
||||
}
|
||||
|
||||
bool ret = exportGroupsToShape(&shape, faceGroup, lineGroup, tags, material,
|
||||
name, triangulate, v);
|
||||
// exportGroupsToShape return false when `usemtl` is called in the last
|
||||
// line.
|
||||
// we also add `shape` to `shapes` when `shape.mesh` has already some
|
||||
// faces(indices)
|
||||
@@ -2043,6 +2250,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
||||
bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
|
||||
void *user_data /*= NULL*/,
|
||||
MaterialReader *readMatFn /*= NULL*/,
|
||||
std::string *warn, /* = NULL*/
|
||||
std::string *err /*= NULL*/) {
|
||||
std::stringstream errss;
|
||||
|
||||
@@ -2054,7 +2262,6 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
|
||||
std::vector<material_t> materials;
|
||||
std::vector<std::string> names;
|
||||
names.reserve(2);
|
||||
std::string name;
|
||||
std::vector<const char *> names_out;
|
||||
|
||||
std::string linebuf;
|
||||
@@ -2180,19 +2387,25 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
|
||||
SplitString(std::string(token), ' ', filenames);
|
||||
|
||||
if (filenames.empty()) {
|
||||
if (err) {
|
||||
(*err) +=
|
||||
"WARN: Looks like empty filename for mtllib. Use default "
|
||||
if (warn) {
|
||||
(*warn) +=
|
||||
"Looks like empty filename for mtllib. Use default "
|
||||
"material. \n";
|
||||
}
|
||||
} else {
|
||||
bool found = false;
|
||||
for (size_t s = 0; s < filenames.size(); s++) {
|
||||
std::string warn_mtl;
|
||||
std::string err_mtl;
|
||||
bool ok = (*readMatFn)(filenames[s].c_str(), &materials,
|
||||
&material_map, &err_mtl);
|
||||
&material_map, &warn_mtl, &err_mtl);
|
||||
|
||||
if (warn && (!warn_mtl.empty())) {
|
||||
(*warn) += warn_mtl; // This should be warn message.
|
||||
}
|
||||
|
||||
if (err && (!err_mtl.empty())) {
|
||||
(*err) += err_mtl; // This should be warn message.
|
||||
(*err) += err_mtl;
|
||||
}
|
||||
|
||||
if (ok) {
|
||||
@@ -2202,9 +2415,9 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
if (err) {
|
||||
(*err) +=
|
||||
"WARN: Failed to load material file(s). Use default "
|
||||
if (warn) {
|
||||
(*warn) +=
|
||||
"Failed to load material file(s). Use default "
|
||||
"material.\n";
|
||||
}
|
||||
} else {
|
||||
@@ -2231,13 +2444,6 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
|
||||
|
||||
assert(names.size() > 0);
|
||||
|
||||
// names[0] must be 'g', so skip the 0th element.
|
||||
if (names.size() > 1) {
|
||||
name = names[1];
|
||||
} else {
|
||||
name.clear();
|
||||
}
|
||||
|
||||
if (callback.group_cb) {
|
||||
if (names.size() > 1) {
|
||||
// create const char* array.
|
||||
|
||||
Reference in New Issue
Block a user