24 Commits

Author SHA1 Message Date
Syoyo Fujita
609139f370 Fix compilation on recent clang. 2017-12-28 18:45:14 +09:00
Syoyo Fujita
cc73127bf0 Merge branch 'tigrazone' into devel 2017-12-13 18:37:37 +09:00
Syoyo Fujita
160d6be10f Format code. 2017-12-12 17:31:22 +09:00
Syoyo Fujita
583590767e Fix compilation.
Remove unused file.
2017-12-12 17:30:59 +09:00
tigrazone
dc542d6638 comment for export data and functions block with hashed keywords to other projects 2017-12-10 10:48:05 +02:00
tigrazone
c5976f931b comment for export data and functions block with hashed keywords to other projects 2017-12-10 10:47:32 +02:00
tigrazone
0511658e86 comment for export data and functions block with hashed keywords to other projects 2017-12-10 10:46:35 +02:00
tigrazone
54851f8ac7 comment for export data and functions block with hashed keywords to other projects 2017-12-10 10:46:08 +02:00
tigrazone
a6c1d07560 comment for export data and functions block with hashed keywords to other projects 2017-12-10 10:43:24 +02:00
tigrazone
4fe479453b smallish optimize 2017-12-10 09:34:10 +02:00
tigrazone
af1bcf1e11 errors fix 2017-12-10 00:43:50 +02:00
tigrazone
05f06d09d8 hashed tokens as keys of map. 5% speedup 2017-12-09 13:17:04 +02:00
tigrazone
3c0196bfb7 map vs unordered_map remake 2017-12-09 11:59:33 +02:00
tigrazone
dc4c970262 small speedups - up to 1-3% 2017-12-08 23:39:07 +02:00
tigrazone
aeb0f05c0e remove stringstream for simple string copy 2017-12-08 23:19:31 +02:00
tigrazone
c016910317 minimize token checks 2017-12-08 21:53:36 +02:00
tigrazone
5d7f6bf539 buffered file read 2017-12-08 13:21:45 +02:00
tigrazone
b818a34f1a small fixes2 2017-12-08 09:05:42 +02:00
tigrazone
baa62f4d89 small fixes 2017-12-08 08:43:33 +02:00
tigrazone
b2f07d10aa 1.1.2 : new hashed keywords 2017-12-07 08:38:44 +02:00
tigrazone
ee2c734c15 refactoring for new speedup release1 2017-12-06 22:44:04 +02:00
tigrazone
6e579f027f refactoring for new speedup release 2017-12-06 22:37:44 +02:00
Syoyo Fujita
7b6e33da52 Rename vector type.
Refactor file layout of extperimental code.
2017-11-12 16:46:25 +09:00
Richard Fabian
5896933508 Added ear clipping triangulation. Added a tiny vertex type to the library to facilitate the change. 2017-11-11 22:37:52 +00:00
40 changed files with 2030 additions and 2798 deletions

View File

@@ -46,13 +46,15 @@ set(TINYOBJLOADER_PKGCONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
set(TINYOBJLOADER_RUNTIME_DIR ${CMAKE_INSTALL_BINDIR})
option(TINYOBJLOADER_BUILD_TEST_LOADER "Build Example Loader Application" OFF)
option(TINYOBJLOADER_COMPILATION_SHARED "Build as shared library" OFF)
add_library(${LIBRARY_NAME} ${tinyobjloader-Source})
if(BUILD_SHARED_LIBS)
if(TINYOBJLOADER_COMPILATION_SHARED)
add_library(${LIBRARY_NAME} SHARED ${tinyobjloader-Source})
set_target_properties(${LIBRARY_NAME} PROPERTIES
SOVERSION ${TINYOBJLOADER_SOVERSION}
)
else()
add_library(${LIBRARY_NAME} STATIC ${tinyobjloader-Source})
endif()
set_target_properties(${LIBRARY_NAME} PROPERTIES VERSION ${TINYOBJLOADER_VERSION})
@@ -118,8 +120,6 @@ install(TARGETS
)
install(EXPORT
${PROJECT_NAME}-targets
NAMESPACE
tinyobjloader::
DESTINATION
${TINYOBJLOADER_CMAKE_DIR}
)

View File

@@ -26,11 +26,20 @@ Old version is available `v0.9.x` branch https://github.com/syoyo/tinyobjloader/
## What's new
### Version 2.x
* Refactor API
* Support triangulation for concave polygons(#151)
### Version 1.x
Avaiable in `v1.x.y` branch.
* 20 Aug, 2016 : Bump version v1.0.0. New data structure and API!
### Old version
### Older version
Previous old version is avaiable in `v0.9.x` branch.
Older version is avaiable in `v0.9.x` branch.
## Example
@@ -49,7 +58,11 @@ http://casual-effects.com/data/index.html
TinyObjLoader is successfully used in ...
### New version(v1.0.x)
### New version(v2.x)
* Your project here! (Letting us know via github issue is welcome!)
### Old version(v1.x)
* Double precision support through `TINYOBJLOADER_USE_DOUBLE` thanks to noma
* Loading models in Vulkan Tutorial https://vulkan-tutorial.com/Loading_models
@@ -60,7 +73,7 @@ TinyObjLoader is successfully used in ...
* VFPR - a Vulkan Forward Plus Renderer : https://github.com/WindyDarian/Vulkan-Forward-Plus-Renderer
* Your project here! (Letting us know via github issue is welcome!)
### Old version(v0.9.x)
### Older version(v0.9.x)
* bullet3 https://github.com/erwincoumans/bullet3
* pbrt-v2 https://github.com/mmp/pbrt-v2
@@ -98,7 +111,6 @@ TinyObjLoader is successfully used in ...
* PBR material extension for .MTL. Its proposed here: http://exocortex.com/blog/extending_wavefront_mtl_to_support_pbr
* Callback API for custom loading.
* Double precision support(for HPC application).
* Smoothing group
## TODO
@@ -106,6 +118,8 @@ TinyObjLoader is successfully used in ...
* [ ] Fix obj_sticker example.
* [ ] More unit test codes.
* [x] Texture options
* [ ] Normal vector generation
* [ ] Support smoothing groups
## License
@@ -205,9 +219,8 @@ 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, inputfile.c_str());
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, inputfile.c_str());
if (!err.empty()) { // `err` may contain warning message.
std::cerr << err << std::endl;
@@ -228,12 +241,12 @@ for (size_t s = 0; s < shapes.size(); s++) {
for (size_t v = 0; v < fv; v++) {
// access to vertex
tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
tinyobj::real_t vx = attrib.vertices[3*idx.vertex_index+0];
tinyobj::real_t vy = attrib.vertices[3*idx.vertex_index+1];
tinyobj::real_t vz = attrib.vertices[3*idx.vertex_index+2];
tinyobj::real_t nx = attrib.normals[3*idx.normal_index+0];
tinyobj::real_t ny = attrib.normals[3*idx.normal_index+1];
tinyobj::real_t nz = attrib.normals[3*idx.normal_index+2];
tinyobj::real_t vx = attrib.vertices[idx.vertex_index].x;
tinyobj::real_t vy = attrib.vertices[idx.vertex_index].y;
tinyobj::real_t vz = attrib.vertices[idx.vertex_index].z;
tinyobj::real_t nx = attrib.normals[idx.normal_index].x;
tinyobj::real_t ny = attrib.normals[idx.normal_index].y;
tinyobj::real_t nz = attrib.normals[idx.normal_index].z;
tinyobj::real_t tx = attrib.texcoords[2*idx.texcoord_index+0];
tinyobj::real_t ty = attrib.texcoords[2*idx.texcoord_index+1];
// Optional: vertex colors

View File

@@ -129,7 +129,6 @@ 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) {
@@ -144,11 +143,7 @@ int main(int argc, char **argv) {
tinyobj::MaterialFileReader mtlReader("../../models/");
bool ret = tinyobj::LoadObjWithCallback(ifs, cb, &mesh, &mtlReader, &warn, &err);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObjWithCallback(ifs, cb, &mesh, &mtlReader, &err);
if (!err.empty()) {
std::cerr << err << std::endl;

View File

@@ -1,9 +1,8 @@
//
// Stiches multiple .obj files into one .obj.
//
#include "obj_writer.h"
#include "../../tiny_obj_loader.h"
#include "obj_writer.h"
#include <cassert>
#include <iostream>
@@ -12,59 +11,27 @@
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)
{
// 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();
int numShapes = 0;
for (size_t i = 0; i < shapes.size(); i++) {
numShapes += (int)shapes[i].size();
}
// 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);
printf("Total # of shapes = %d\n", numShapes);
int materialIdOffset = 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()
size_t face_offset = 0;
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];
@@ -72,51 +39,36 @@ 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 material offset.
// Add offset.
for (size_t f = 0; f < new_shape.mesh.material_ids.size(); f++) {
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.mesh.material_ids[f] += materialIdOffset;
}
new_shape.name = new_name;
printf("shape[%ld][%ld].new_name = %s\n", i, k, new_shape.name.c_str());
out_shape[shape_id_offset++] = new_shape;
out_shape.push_back(new_shape);
}
// Copy materials
materialIdOffset += materials[i].size();
}
for (size_t i = 0; i < materials.size(); i++) {
for (size_t k = 0; k < materials[i].size(); 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();
out_material.push_back(materials[i][k]);
}
}
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");
@@ -126,16 +78,16 @@ int main(int argc, char **argv)
int num_objfiles = argc - 2;
std::string out_filename = std::string(argv[argc-1]); // last element
std::vector<Attribute> attributes(num_objfiles);
std::vector<Shape> shapes(num_objfiles);
std::vector<Material> materials(num_objfiles);
std::vector<Shape> shapes;
std::vector<Material> materials;
shapes.resize(num_objfiles);
materials.resize(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(&attributes[i], &shapes[i], &materials[i], &warn, &err, argv[i+1]);
bool ret = tinyobj::LoadObj(shapes[i], materials[i], err, argv[i+1]);
if (!err.empty()) {
std::cerr << err << std::endl;
}
@@ -146,13 +98,12 @@ int main(int argc, char **argv)
std::cout << "DONE." << std::endl;
}
Attribute out_attribute;
Shape out_shape;
Material out_material;
StichObjs(out_attribute, out_shape, out_material, attributes, shapes, materials);
std::vector<tinyobj::shape_t> out_shape;
std::vector<tinyobj::material_t> out_material;
StichObjs(out_shape, out_material, shapes, materials);
bool coordTransform = true;
bool ret = WriteObj(out_filename, out_attribute, out_shape, out_material, coordTransform);
bool ret = WriteObj(out_filename, out_shape, out_material, coordTransform);
assert(ret);
return 0;

View File

@@ -30,8 +30,6 @@ 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 }
}
@@ -40,7 +38,7 @@ bool WriteMat(const std::string& filename, const std::vector<tinyobj::material_t
return true;
}
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) {
bool WriteObj(const std::string& filename, 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());
@@ -50,53 +48,17 @@ bool WriteObj(const std::string& filename, const tinyobj::attrib_t& attributes,
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\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]);
}
fprintf(fp, "mtllib %s\n", material_filename.c_str());
for (size_t i = 0; i < shapes.size(); i++) {
fprintf(fp, "\n");
bool has_vn = false;
bool has_vt = false;
if (shapes[i].name.empty()) {
fprintf(fp, "g Unknown\n");
@@ -104,53 +66,101 @@ bool WriteObj(const std::string& filename, const tinyobj::attrib_t& attributes,
fprintf(fp, "g %s\n", shapes[i].name.c_str());
}
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;
//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]);
}
}
}
// 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
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];
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];
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) {
// v0/t0/vn0
fprintf(fp, " %d/%d/%d", ref.vertex_index + 1, ref.texcoord_index + 1, ref.normal_index + 1);
continue;
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);
}
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);
@@ -162,3 +172,5 @@ bool WriteObj(const std::string& filename, const tinyobj::attrib_t& attributes,
return ret;
}

View File

@@ -3,6 +3,7 @@
#include "../../tiny_obj_loader.h"
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);
extern bool WriteObj(const std::string& filename, const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials, bool coordTransform = false);
#endif // __OBJ_WRITER_H__

View File

@@ -191,101 +191,9 @@ static void CalcNormal(float N[3], float v0[3], float v1[3], float v2[3]) {
N[0] /= len;
N[1] /= len;
N[2] /= len;
}
}
namespace // Local utility functions
{
struct vec3 {
float v[3];
vec3() {
v[0] = 0.0f;
v[1] = 0.0f;
v[2] = 0.0f;
}
};
void normalizeVector(vec3 &v) {
float len2 = v.v[0] * v.v[0] + v.v[1] * v.v[1] + v.v[2] * v.v[2];
if (len2 > 0.0f) {
float len = sqrtf(len2);
v.v[0] /= len;
v.v[1] /= len;
v.v[2] /= len;
}
}
// Check if `mesh_t` contains smoothing group id.
bool hasSmoothingGroup(const tinyobj::shape_t& shape)
{
for (size_t i = 0; i < shape.mesh.smoothing_group_ids.size(); i++) {
if (shape.mesh.smoothing_group_ids[i] > 0) {
return true;
}
}
return false;
}
void computeSmoothingNormals(const tinyobj::attrib_t& attrib, const tinyobj::shape_t& shape,
std::map<int, vec3>& smoothVertexNormals) {
smoothVertexNormals.clear();
std::map<int, vec3>::iterator iter;
for (size_t f = 0; f < shape.mesh.indices.size() / 3; f++) {
// Get the three indexes of the face (all faces are triangular)
tinyobj::index_t idx0 = shape.mesh.indices[3 * f + 0];
tinyobj::index_t idx1 = shape.mesh.indices[3 * f + 1];
tinyobj::index_t idx2 = shape.mesh.indices[3 * f + 2];
// Get the three vertex indexes and coordinates
int vi[3]; // indexes
float v[3][3]; // coordinates
for (int k = 0; k < 3; k++) {
vi[0] = idx0.vertex_index;
vi[1] = idx1.vertex_index;
vi[2] = idx2.vertex_index;
assert(vi[0] >= 0);
assert(vi[1] >= 0);
assert(vi[2] >= 0);
v[0][k] = attrib.vertices[3 * vi[0] + k];
v[1][k] = attrib.vertices[3 * vi[1] + k];
v[2][k] = attrib.vertices[3 * vi[2] + k];
}
// Compute the normal of the face
float normal[3];
CalcNormal(normal, v[0], v[1], v[2]);
// Add the normal to the three vertexes
for (size_t i = 0; i < 3; ++i) {
iter = smoothVertexNormals.find(vi[i]);
if (iter != smoothVertexNormals.end()) {
// add
iter->second.v[0] += normal[0];
iter->second.v[1] += normal[1];
iter->second.v[2] += normal[2];
} else {
smoothVertexNormals[vi[i]].v[0] = normal[0];
smoothVertexNormals[vi[i]].v[1] = normal[1];
smoothVertexNormals[vi[i]].v[2] = normal[2];
}
}
} // f
// Normalize the normals, that is, make them unit vectors
for (iter = smoothVertexNormals.begin(); iter != smoothVertexNormals.end();
iter++) {
normalizeVector(iter->second);
}
} // computeSmoothingNormals
} // namespace
static bool LoadObjAndConvert(float bmin[3], float bmax[3],
std::vector<DrawObject>* drawObjects,
std::vector<tinyobj::material_t>& materials,
@@ -308,13 +216,9 @@ 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, &warn, &err, filename,
base_dir.c_str());
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret =
tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename, base_dir.c_str());
if (!err.empty()) {
std::cerr << err << std::endl;
}
@@ -338,8 +242,7 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
materials.push_back(tinyobj::material_t());
for (size_t i = 0; i < materials.size(); i++) {
printf("material[%d].diffuse_texname = %s\n", int(i),
materials[i].diffuse_texname.c_str());
printf("material[%d].diffuse_texname = %s\n", int(i), materials[i].diffuse_texname.c_str());
}
// Load diffuse textures
@@ -359,32 +262,27 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
// Append base dir.
texture_filename = base_dir + mp->diffuse_texname;
if (!FileExists(texture_filename)) {
std::cerr << "Unable to find file: " << mp->diffuse_texname
<< std::endl;
std::cerr << "Unable to find file: " << mp->diffuse_texname << std::endl;
exit(1);
}
}
unsigned char* image =
stbi_load(texture_filename.c_str(), &w, &h, &comp, STBI_default);
unsigned char* image = stbi_load(texture_filename.c_str(), &w, &h, &comp, STBI_default);
if (!image) {
std::cerr << "Unable to load texture: " << texture_filename
<< std::endl;
std::cerr << "Unable to load texture: " << texture_filename << std::endl;
exit(1);
}
std::cout << "Loaded texture: " << texture_filename << ", w = " << w
<< ", h = " << h << ", comp = " << comp << std::endl;
std::cout << "Loaded texture: " << texture_filename << ", w = " << w << ", h = " << h << ", comp = " << comp << std::endl;
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (comp == 3) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
GL_UNSIGNED_BYTE, image);
} else if (comp == 4) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, image);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
}
else if (comp == 4) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
} else {
assert(0); // TODO
}
@@ -403,14 +301,6 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
for (size_t s = 0; s < shapes.size(); s++) {
DrawObject o;
std::vector<float> buffer; // pos(3float), normal(3float), color(3float)
// Check for smoothing group and compute smoothing normals
std::map<int, vec3> smoothVertexNormals;
if (hasSmoothingGroup(shapes[s]) > 0) {
std::cout << "Compute smoothingNormal for shape [" << s << "]" << std::endl;
computeSmoothingNormals(attrib, shapes[s], smoothVertexNormals);
}
for (size_t f = 0; f < shapes[s].mesh.indices.size() / 3; f++) {
tinyobj::index_t idx0 = shapes[s].mesh.indices[3 * f + 0];
tinyobj::index_t idx1 = shapes[s].mesh.indices[3 * f + 1];
@@ -418,16 +308,12 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
int current_material_id = shapes[s].mesh.material_ids[f];
if ((current_material_id < 0) ||
(current_material_id >= static_cast<int>(materials.size()))) {
if ((current_material_id < 0) || (current_material_id >= static_cast<int>(materials.size()))) {
// Invaid material ID. Use default material.
current_material_id =
materials.size() -
1; // Default material is added to the last item in `materials`.
current_material_id = materials.size() - 1; // Default material is added to the last item in `materials`.
}
//if (current_material_id >= materials.size()) {
// std::cerr << "Invalid material index: " << current_material_id <<
// std::endl;
// std::cerr << "Invalid material index: " << current_material_id << std::endl;
//}
//
float diffuse[3];
@@ -436,22 +322,9 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
}
float tc[3][2];
if (attrib.texcoords.size() > 0) {
if ((idx0.texcoord_index < 0) || (idx1.texcoord_index < 0) ||
(idx2.texcoord_index < 0)) {
// face does not contain valid uv index.
tc[0][0] = 0.0f;
tc[0][1] = 0.0f;
tc[1][0] = 0.0f;
tc[1][1] = 0.0f;
tc[2][0] = 0.0f;
tc[2][1] = 0.0f;
} else {
assert(attrib.texcoords.size() >
size_t(2 * idx0.texcoord_index + 1));
assert(attrib.texcoords.size() >
size_t(2 * idx1.texcoord_index + 1));
assert(attrib.texcoords.size() >
size_t(2 * idx2.texcoord_index + 1));
assert(attrib.texcoords.size() > 2 * idx0.texcoord_index + 1);
assert(attrib.texcoords.size() > 2 * idx1.texcoord_index + 1);
assert(attrib.texcoords.size() > 2 * idx2.texcoord_index + 1);
// Flip Y coord.
tc[0][0] = attrib.texcoords[2 * idx0.texcoord_index];
@@ -460,7 +333,6 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
tc[1][1] = 1.0f - attrib.texcoords[2 * idx1.texcoord_index + 1];
tc[2][0] = attrib.texcoords[2 * idx2.texcoord_index];
tc[2][1] = 1.0f - attrib.texcoords[2 * idx2.texcoord_index + 1];
}
} else {
tc[0][0] = 0.0f;
tc[0][1] = 0.0f;
@@ -491,54 +363,19 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
}
float n[3][3];
{
bool invalid_normal_index = false;
if (attrib.normals.size() > 0) {
int nf0 = idx0.normal_index;
int nf1 = idx1.normal_index;
int nf2 = idx2.normal_index;
if ((nf0 < 0) || (nf1 < 0) || (nf2 < 0)) {
// normal index is missing from this face.
invalid_normal_index = true;
} else {
int f0 = idx0.normal_index;
int f1 = idx1.normal_index;
int f2 = idx2.normal_index;
assert(f0 >= 0);
assert(f1 >= 0);
assert(f2 >= 0);
for (int k = 0; k < 3; k++) {
assert(size_t(3 * nf0 + k) < attrib.normals.size());
assert(size_t(3 * nf1 + k) < attrib.normals.size());
assert(size_t(3 * nf2 + k) < attrib.normals.size());
n[0][k] = attrib.normals[3 * nf0 + k];
n[1][k] = attrib.normals[3 * nf1 + k];
n[2][k] = attrib.normals[3 * nf2 + k];
}
n[0][k] = attrib.normals[3 * f0 + k];
n[1][k] = attrib.normals[3 * f1 + k];
n[2][k] = attrib.normals[3 * f2 + k];
}
} else {
invalid_normal_index = true;
}
if (invalid_normal_index && !smoothVertexNormals.empty()) {
// Use smoothing normals
int f0 = idx0.vertex_index;
int f1 = idx1.vertex_index;
int f2 = idx2.vertex_index;
if (f0 >= 0 && f1 >= 0 && f2 >= 0) {
n[0][0] = smoothVertexNormals[f0].v[0];
n[0][1] = smoothVertexNormals[f0].v[1];
n[0][2] = smoothVertexNormals[f0].v[2];
n[1][0] = smoothVertexNormals[f1].v[0];
n[1][1] = smoothVertexNormals[f1].v[1];
n[1][2] = smoothVertexNormals[f1].v[2];
n[2][0] = smoothVertexNormals[f2].v[0];
n[2][1] = smoothVertexNormals[f2].v[1];
n[2][2] = smoothVertexNormals[f2].v[2];
invalid_normal_index = false;
}
}
if (invalid_normal_index) {
// compute geometric normal
CalcNormal(n[0], v[0], v[1], v[2]);
n[1][0] = n[0][0];
@@ -548,7 +385,6 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
n[2][1] = n[0][1];
n[2][2] = n[0][2];
}
}
for (int k = 0; k < 3; k++) {
buffer.push_back(v[k][0]);
@@ -560,9 +396,11 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
// Combine normal and diffuse to get color.
float normal_factor = 0.2;
float diffuse_factor = 1 - normal_factor;
float c[3] = {n[k][0] * normal_factor + diffuse[0] * diffuse_factor,
float c[3] = {
n[k][0] * normal_factor + diffuse[0] * diffuse_factor,
n[k][1] * normal_factor + diffuse[1] * diffuse_factor,
n[k][2] * normal_factor + diffuse[2] * diffuse_factor};
n[k][2] * normal_factor + diffuse[2] * diffuse_factor
};
float len2 = c[0] * c[0] + c[1] * c[1] + c[2] * c[2];
if (len2 > 0.0f) {
float len = sqrtf(len2);
@@ -584,10 +422,8 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
o.numTriangles = 0;
// OpenGL viewer does not support texturing with per-face material.
if (shapes[s].mesh.material_ids.size() > 0 &&
shapes[s].mesh.material_ids.size() > s) {
o.material_id = shapes[s].mesh.material_ids[0]; // use the material ID
// of the first face.
if (shapes[s].mesh.material_ids.size() > 0 && shapes[s].mesh.material_ids.size() > s) {
o.material_id = shapes[s].mesh.material_ids[0]; // use the material ID of the first face.
} else {
o.material_id = materials.size() - 1; // = ID for default material.
}
@@ -596,10 +432,9 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
if (buffer.size() > 0) {
glGenBuffers(1, &o.vb_id);
glBindBuffer(GL_ARRAY_BUFFER, o.vb_id);
glBufferData(GL_ARRAY_BUFFER, buffer.size() * sizeof(float),
&buffer.at(0), GL_STATIC_DRAW);
o.numTriangles = buffer.size() / (3 + 3 + 3 + 2) /
3; // 3:vtx, 3:normal, 3:col, 2:texcoord
glBufferData(GL_ARRAY_BUFFER, buffer.size() * sizeof(float), &buffer.at(0),
GL_STATIC_DRAW);
o.numTriangles = buffer.size() / (3 + 3 + 3 + 2) / 3; // 3:vtx, 3:normal, 3:col, 2:texcoord
printf("shape[%d] # of triangles = %d\n", static_cast<int>(s),
o.numTriangles);
@@ -714,9 +549,7 @@ static void motionFunc(GLFWwindow* window, double mouse_x, double mouse_y) {
prevMouseY = mouse_y;
}
static void Draw(const std::vector<DrawObject>& drawObjects,
std::vector<tinyobj::material_t>& materials,
std::map<std::string, GLuint>& textures) {
static void Draw(const std::vector<DrawObject>& drawObjects, std::vector<tinyobj::material_t>& materials, std::map<std::string, GLuint>& textures) {
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_FILL);
@@ -835,8 +668,7 @@ int main(int argc, char** argv) {
float bmin[3], bmax[3];
std::vector<tinyobj::material_t> materials;
std::map<std::string, GLuint> textures;
if (false == LoadObjAndConvert(bmin, bmax, &gDrawObjects, materials, textures,
argv[1])) {
if (false == LoadObjAndConvert(bmin, bmax, &gDrawObjects, materials, textures, argv[1])) {
return -1;
}

View File

@@ -9,9 +9,8 @@ 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, &warn, &err, filename);
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename);
if (!err.empty()) {
printf("err: %s\n", err.c_str());

View File

@@ -6,21 +6,11 @@
* 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.

View File

@@ -312,10 +312,6 @@ 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;
@@ -334,14 +330,7 @@ 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;
@@ -605,9 +594,19 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result) {
}
assemble :
*result = (sign == '+' ? 1 : -1) *
(exponent ? std::ldexp(mantissa * std::pow(5.0, exponent), exponent)
: mantissa);
{
// = pow(5.0, exponent);
double a = 1.0;
for (int i = 0; i < exponent; i++) {
a = a * 5.0;
}
*result =
//(sign == '+' ? 1 : -1) * ldexp(mantissa * pow(5.0, exponent), exponent);
(sign == '+' ? 1 : -1) * (mantissa * a) *
static_cast<double>(1ULL << exponent); // 5.0^exponent * 2^exponent
}
return true;
fail:
return false;
@@ -1239,8 +1238,6 @@ 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) {
@@ -1309,19 +1306,15 @@ 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 (!new_line_found) {
if ((t > 0) && (prev_pos == start_idx) &&
(!is_line_ending(buf, start_idx - 1, end_idx))) {
// first linebreak found in (chunk > 0), and a line before this
// linebreak belongs to previous chunk, so skip it.
prev_pos = i + 1;
new_line_found = true;
continue;
} else {
LineInfo info;
info.pos = prev_pos;
@@ -1336,11 +1329,11 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
}
}
// 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)) {
// 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)) {
LineInfo info;
info.pos = prev_pos;
info.len = i - prev_pos;
@@ -1397,6 +1390,7 @@ 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],
@@ -1414,14 +1408,14 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
}
if (command.type == COMMAND_MTLLIB) {
// Save the indices of the `mtllib` command in `commands` to easily find it later
mtllib_t_index = t;
mtllib_i_index = commands[t].size();
mtllib_i_index = commands->size();
}
commands[t].emplace_back(std::move(command));
}
}
}));
}
@@ -1446,9 +1440,7 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
// std::cout << "mtllib :" << material_filename << std::endl;
auto t1 = std::chrono::high_resolution_clock::now();
if (material_filename.back() == '\r') {
material_filename.pop_back();
}
std::ifstream ifs(material_filename);
if (ifs.good()) {
LoadMtl(&material_map, materials, &ifs);
@@ -1499,7 +1491,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, -1);
attrib->material_ids.resize(num_indices);
size_t v_offsets[kMaxThreads];
size_t n_offsets[kMaxThreads];
@@ -1524,6 +1516,7 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
StackVector<std::thread, 16> workers;
for (size_t t = 0; t < num_threads; t++) {
int material_id = -1; // -1 = default unknown material.
workers->push_back(std::thread([&, t]() {
size_t v_count = v_offsets[t];
size_t n_count = n_offsets[t];
@@ -1536,40 +1529,15 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
continue;
} else if (commands[t][i].type == COMMAND_USEMTL) {
if (commands[t][i].material_name &&
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) {
commands[t][i].material_name_len > 0) {
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()) {
attrib->material_ids[face_count + k] =
material_map[material_name];
material_id = material_map[material_name];
} else {
// Assign invalid material ID
// Set a different value than the default, to
// prevent following faces from being assigned a valid
// material
attrib->material_ids[face_count + k] = -2;
}
}
material_id = -1;
}
}
} else if (commands[t][i].type == COMMAND_V) {
@@ -1596,6 +1564,7 @@ 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];
}
@@ -1611,12 +1580,6 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
workers[t].join();
}
// 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;
}
@@ -1676,8 +1639,7 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
}
}
if (commands[t][i].type == COMMAND_F) {
// Consider generation of multiple faces per `f` line by triangulation
face_count += commands[t][i].f_num_verts.size();
face_count++;
}
}
}

View File

@@ -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(multiple of 3).
assert(attrib.face_num_verts[v] % 3 == 0); // assume all triangle face.
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];

Binary file not shown.

View File

@@ -1,20 +0,0 @@
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()

View File

@@ -30,7 +30,6 @@ extern "C" {
#endif
#ifdef __clang__
#pragma clang diagnostic push
#if __has_warning("-Wzero-as-null-pointer-constant")
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif
@@ -113,43 +112,38 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
std::cout << "# of shapes : " << shapes.size() << std::endl;
std::cout << "# of materials : " << materials.size() << std::endl;
for (size_t v = 0; v < attrib.vertices.size() / 3; v++) {
for (size_t v = 0; v < attrib.vertices.size(); v++) {
printf(" v[%ld] = (%f, %f, %f)\n", static_cast<long>(v),
static_cast<const double>(attrib.vertices[3 * v + 0]),
static_cast<const double>(attrib.vertices[3 * v + 1]),
static_cast<const double>(attrib.vertices[3 * v + 2]));
static_cast<const double>(attrib.vertices[v].x),
static_cast<const double>(attrib.vertices[v].y),
static_cast<const double>(attrib.vertices[v].z));
}
for (size_t v = 0; v < attrib.normals.size() / 3; v++) {
for (size_t v = 0; v < attrib.normals.size(); v++) {
printf(" n[%ld] = (%f, %f, %f)\n", static_cast<long>(v),
static_cast<const double>(attrib.normals[3 * v + 0]),
static_cast<const double>(attrib.normals[3 * v + 1]),
static_cast<const double>(attrib.normals[3 * v + 2]));
static_cast<const double>(attrib.normals[v].x),
static_cast<const double>(attrib.normals[v].y),
static_cast<const double>(attrib.normals[v].z));
}
for (size_t v = 0; v < attrib.texcoords.size() / 2; v++) {
for (size_t v = 0; v < attrib.texcoords.size(); v++) {
printf(" uv[%ld] = (%f, %f)\n", static_cast<long>(v),
static_cast<const double>(attrib.texcoords[2 * v + 0]),
static_cast<const double>(attrib.texcoords[2 * v + 1]));
static_cast<const double>(attrib.texcoords[v].x),
static_cast<const double>(attrib.texcoords[v].y));
}
// For each shape
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].mesh.indices: %lu\n", static_cast<long>(i),
printf("Size of shape[%ld].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()));
@@ -170,8 +164,6 @@ 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;
}
@@ -285,19 +277,14 @@ 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, &warn, &err, filename,
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &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: " << err << std::endl;
std::cerr << err << std::endl;
}
if (!ret) {
@@ -380,13 +367,17 @@ static bool TestStreamLoadObj() {
virtual ~MaterialStringStreamReader() {}
virtual bool operator()(const std::string& matId,
std::vector<material_t>* materials,
std::map<std::string, int>* matMap,
std::string* warn,
std::map<unsigned int, int>* matMap,
std::string* err) {
(void)err;
(void)matId;
LoadMtl(matMap, materials, &m_matSStream, warn, err);
std::string warning;
LoadMtl(matMap, materials, &m_matSStream, &warning);
if (!warning.empty()) {
if (err) {
(*err) += warning;
}
}
return true;
}
@@ -398,9 +389,8 @@ 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, &warn, &err, &objStream,
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, &objStream,
&matSSReader);
if (!err.empty()) {

View File

@@ -1,9 +0,0 @@
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

View File

@@ -1,7 +0,0 @@
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

View File

@@ -13,7 +13,6 @@ 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

View File

@@ -1,24 +0,0 @@
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

View File

@@ -1,18 +0,0 @@
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

View File

@@ -1,37 +0,0 @@
o cube
mtllib cube.mtl
v -0.500000 -0.500000 0.500000
v 0.500000 -0.500000 0.500000
v -0.500000 0.500000 0.500000
v 0.500000 0.500000 0.500000
v -0.500000 0.500000 -0.500000
v 0.500000 0.500000 -0.500000
v -0.500000 -0.500000 -0.500000
v 0.500000 -0.500000 -0.500000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
g cube
usemtl cube
s 1
f 1/1 2/2 3/3
f 3/3 2/2 4/4
s 2
f 3/1 4/2 5/3
f 5/3 4/2 6/4
s 3
f 5/4 6/3 7/2
f 7/2 6/3 8/1
s 4
f 7/1 8/2 1/3
f 1/3 8/2 2/4
s 5
f 2/1 8/2 4/3
f 4/3 8/2 6/4
s 6
f 7/1 1/2 5/3
f 5 1 3

View File

@@ -1,23 +0,0 @@
newmtl test1
Ns 10.0000
Ni 1.5000
d 1.0000
Tr 0.0000
Tf 1.0000 1.0000 1.0000
illum 2
Ka 0.0000 0.0000 0.0000
Kd 0.5 0.2 0.2
Ks 0.0000 0.0000 0.0000
Ke 0.0000 0.0000 0.0000
newmtl test2
Ns 10.0000
Ni 1.5000
d 1.0000
Tr 0.0000
Tf 1.0000 1.0000 1.0000
illum 2
Ka 0.0000 0.0000 0.0000
Kd 0.2 0.5 0.2
Ks 0.0000 0.0000 0.0000
Ke 0.0000 0.0000 0.0000

View File

@@ -1,51 +0,0 @@
# cube.obj
#
mtllib issue-162-smoothing-group.mtl
v -0.500000 -0.500000 0.500000
v 0.500000 -0.500000 0.500000
v -0.500000 0.500000 0.500000
v 0.500000 0.500000 0.500000
v -0.500000 0.500000 -0.500000
v 0.500000 0.500000 -0.500000
v -0.500000 -0.500000 -0.500000
v 0.500000 -0.500000 -0.500000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 -1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
usemtl test1
g test1
s 1
f 1/1/1 2/2/1 3/3/1
f 3/3/1 2/2/1 4/4/1
usemtl test2
g test2
s off
f 3/1/2 4/2/2 5/3/2
f 5/3/2 4/2/2 6/4/2
s 3
f 5/4/3 6/3/3 7/2/3
f 7/2/3 6/3/3 8/1/3
s 4
f 7/1/4 8/2/4 1/3/4
f 1/3/4 8/2/4 2/4/4
s 0
f 2/1/5 8/2/5 4/3/5
f 4/3/5 8/2/5 6/4/5
s 6
f 7/1/6 1/2/6 5/3/6
f 5/3/6 1/2/6 3/4/6

View File

@@ -1,16 +0,0 @@
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

View File

@@ -1,22 +0,0 @@
# from tinyobjloader issue #29
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
v 4.000000 0.000000 -3.255298
v 4.000000 2.000000 -3.255298
#vn 0.000000 0.000000 1.000000
#vn 0.000000 0.000000 1.000000
#vn 0.276597 0.000000 0.960986
#vn 0.276597 0.000000 0.960986
#vn 0.531611 0.000000 0.846988
#vn 0.531611 0.000000 0.846988
# 6 vertices
# 6 normals
g all
s 1
f 1//1 2//2 3//3 4//4
f 4//4 3//3 5//5

View File

@@ -1,24 +0,0 @@
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

View File

@@ -1,35 +0,0 @@
mtllib smoothing-normal.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
s 1
f 1 2 3 4
#g bottom cube
#usemtl white
s 1
f 2 6 7 3
g back cube
# expects white material
s off
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
# 6 elements

View File

@@ -18,9 +18,11 @@ typedef std::vector<int> vecti;
PyObject* pyTupleFromfloat3(float array[3]) {
int i;
PyObject* tuple = PyTuple_New(3);
for (i = 0; i <= 2; i++) {
PyTuple_SetItem(tuple, i, PyFloat_FromDouble(array[i]));
}
return tuple;
}
@@ -28,27 +30,32 @@ extern "C" {
static PyObject* pyLoadObj(PyObject* self, PyObject* args) {
PyObject *rtndict, *pyshapes, *pymaterials, *pymaterial_indices, *attribobj, *current, *meshobj;
char const* current_name;
char const* filename;
vectd vect;
std::vector<tinyobj::index_t> indices;
std::vector<unsigned char> face_verts;
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
if (!PyArg_ParseTuple(args, "s", &filename)) return NULL;
std::string err, warn;
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename);
std::string err;
tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename);
pyshapes = PyDict_New();
pymaterials = PyDict_New();
pymaterial_indices = PyList_New(0);
rtndict = PyDict_New();
attribobj = PyDict_New();
for (int i = 0; i <= 2; i++) {
current = PyList_New(0);
switch (i) {
case 0:
current_name = "vertices";
@@ -65,137 +72,124 @@ static PyObject* pyLoadObj(PyObject* self, PyObject* args) {
}
for (vectd::iterator it = vect.begin(); it != vect.end(); it++) {
PyObject* value = PyFloat_FromDouble(*it);
PyList_Insert(current, it - vect.begin(), value);
Py_DECREF(value);
}
PyDict_SetItemString(attribobj, current_name, current);
Py_DECREF(current);
PyList_Insert(current, it - vect.begin(), PyFloat_FromDouble(*it));
}
for (std::vector<tinyobj::shape_t>::iterator shape = shapes.begin(); shape != shapes.end(); shape++) {
PyDict_SetItemString(attribobj, current_name, current);
}
for (std::vector<tinyobj::shape_t>::iterator shape = shapes.begin();
shape != shapes.end(); shape++) {
meshobj = PyDict_New();
tinyobj::mesh_t cm = (*shape).mesh;
{
current = PyList_New(0);
for (size_t i = 0; i < cm.indices.size(); i++) {
// Flatten index array: v_idx, vn_idx, vt_idx, v_idx, vn_idx, vt_idx,
PyObject* value = PyLong_FromLong(cm.indices[i].vertex_index);
PyList_Insert(current, 3 * i + 0, value);
Py_DECREF(value);
value = PyLong_FromLong(cm.indices[i].normal_index);
PyList_Insert(current, 3 * i + 1, value);
Py_DECREF(value);
value = PyLong_FromLong(cm.indices[i].texcoord_index);
PyList_Insert(current, 3 * i + 2, value);
Py_DECREF(value);
// ...
PyList_Insert(current, 3 * i + 0,
PyLong_FromLong(cm.indices[i].vertex_index));
PyList_Insert(current, 3 * i + 1,
PyLong_FromLong(cm.indices[i].normal_index));
PyList_Insert(current, 3 * i + 2,
PyLong_FromLong(cm.indices[i].texcoord_index));
}
PyDict_SetItemString(meshobj, "indices", current);
Py_DECREF(current);
}
{
current = PyList_New(0);
for (size_t i = 0; i < cm.num_face_vertices.size(); i++) {
// Widen data type to long.
PyObject* value = PyLong_FromLong(cm.num_face_vertices[i]);
PyList_Insert(current, i, value);
Py_DECREF(value);
PyList_Insert(current, i, PyLong_FromLong(cm.num_face_vertices[i]));
}
PyDict_SetItemString(meshobj, "num_face_vertices", current);
Py_DECREF(current);
}
{
current = PyList_New(0);
for (size_t i = 0; i < cm.material_ids.size(); i++) {
PyObject* value = PyLong_FromLong(cm.material_ids[i]);
PyList_Insert(current, i, value);
Py_DECREF(value);
}
PyDict_SetItemString(meshobj, "material_ids", current);
Py_DECREF(current);
}
PyDict_SetItemString(pyshapes, (*shape).name.c_str(), meshobj);
Py_DECREF(meshobj);
PyList_Insert(current, i, PyLong_FromLong(cm.material_ids[i]));
}
for (std::vector<tinyobj::material_t>::iterator mat = materials.begin(); mat != materials.end(); mat++) {
PyDict_SetItemString(meshobj, "material_ids", current);
}
PyDict_SetItemString(pyshapes, (*shape).name.c_str(), meshobj);
}
for (std::vector<tinyobj::material_t>::iterator mat = materials.begin();
mat != materials.end(); mat++) {
PyObject* matobj = PyDict_New();
PyObject* unknown_parameter = PyDict_New();
for (std::map<std::string, std::string>::iterator p = mat->unknown_parameter.begin(); p != mat->unknown_parameter.end(); ++p) {
PyObject* value = PyUnicode_FromString(p->second.c_str());
PyDict_SetItemString(unknown_parameter, p->first.c_str(), value);
Py_DECREF(value);
for (std::map<std::string, std::string>::iterator p =
mat->unknown_parameter.begin();
p != mat->unknown_parameter.end(); ++p) {
PyDict_SetItemString(unknown_parameter, p->first.c_str(),
PyUnicode_FromString(p->second.c_str()));
}
PyObject* value = PyFloat_FromDouble(mat->shininess);
PyDict_SetItemString(matobj, "shininess", value);
Py_DECREF(value);
value = PyFloat_FromDouble(mat->ior);
PyDict_SetItemString(matobj, "ior", value);
Py_DECREF(value);
value = PyFloat_FromDouble(mat->dissolve);
PyDict_SetItemString(matobj, "dissolve", value);
Py_DECREF(value);
value = PyLong_FromLong(mat->illum);
PyDict_SetItemString(matobj, "illum", value);
Py_DECREF(value);
value = PyUnicode_FromString(mat->ambient_texname.c_str());
PyDict_SetItemString(matobj, "ambient_texname", value);
Py_DECREF(value);
value = PyUnicode_FromString(mat->diffuse_texname.c_str());
PyDict_SetItemString(matobj, "diffuse_texname", value);
Py_DECREF(value);
value = PyUnicode_FromString(mat->specular_texname.c_str());
PyDict_SetItemString(matobj, "specular_texname", value);
Py_DECREF(value);
value = PyUnicode_FromString(mat->specular_highlight_texname.c_str());
PyDict_SetItemString(matobj, "specular_highlight_texname", value);
Py_DECREF(value);
value = PyUnicode_FromString(mat->bump_texname.c_str());
PyDict_SetItemString(matobj, "bump_texname", value);
Py_DECREF(value);
value = PyUnicode_FromString(mat->displacement_texname.c_str());
PyDict_SetItemString(matobj, "displacement_texname", value);
Py_DECREF(value);
value = PyUnicode_FromString(mat->alpha_texname.c_str());
PyDict_SetItemString(matobj, "alpha_texname", value);
Py_DECREF(value);
PyDict_SetItemString(matobj, "shininess",
PyFloat_FromDouble(mat->shininess));
PyDict_SetItemString(matobj, "ior", PyFloat_FromDouble(mat->ior));
PyDict_SetItemString(matobj, "dissolve",
PyFloat_FromDouble(mat->dissolve));
PyDict_SetItemString(matobj, "illum", PyLong_FromLong(mat->illum));
PyDict_SetItemString(matobj, "ambient_texname",
PyUnicode_FromString(mat->ambient_texname.c_str()));
PyDict_SetItemString(matobj, "diffuse_texname",
PyUnicode_FromString(mat->diffuse_texname.c_str()));
PyDict_SetItemString(matobj, "specular_texname",
PyUnicode_FromString(mat->specular_texname.c_str()));
PyDict_SetItemString(
matobj, "specular_highlight_texname",
PyUnicode_FromString(mat->specular_highlight_texname.c_str()));
PyDict_SetItemString(matobj, "bump_texname",
PyUnicode_FromString(mat->bump_texname.c_str()));
PyDict_SetItemString(
matobj, "displacement_texname",
PyUnicode_FromString(mat->displacement_texname.c_str()));
PyDict_SetItemString(matobj, "alpha_texname",
PyUnicode_FromString(mat->alpha_texname.c_str()));
PyDict_SetItemString(matobj, "ambient", pyTupleFromfloat3(mat->ambient));
PyDict_SetItemString(matobj, "diffuse", pyTupleFromfloat3(mat->diffuse));
PyDict_SetItemString(matobj, "specular", pyTupleFromfloat3(mat->specular));
PyDict_SetItemString(matobj, "transmittance", pyTupleFromfloat3(mat->transmittance));
PyDict_SetItemString(matobj, "emission", pyTupleFromfloat3(mat->emission));
PyDict_SetItemString(matobj, "specular",
pyTupleFromfloat3(mat->specular));
PyDict_SetItemString(matobj, "transmittance",
pyTupleFromfloat3(mat->transmittance));
PyDict_SetItemString(matobj, "emission",
pyTupleFromfloat3(mat->emission));
PyDict_SetItemString(matobj, "unknown_parameter", unknown_parameter);
Py_DECREF(unknown_parameter);
PyDict_SetItemString(pymaterials, mat->name.c_str(), matobj);
Py_DECREF(matobj);
value = PyUnicode_FromString(mat->name.c_str());
PyList_Append(pymaterial_indices, value);
Py_DECREF(value);
PyList_Append(pymaterial_indices, PyUnicode_FromString(mat->name.c_str()));
}
PyDict_SetItemString(rtndict, "shapes", pyshapes);
Py_DECREF(pyshapes);
PyDict_SetItemString(rtndict, "materials", pymaterials);
Py_DECREF(pymaterials);
PyDict_SetItemString(rtndict, "material_indices", pymaterial_indices);
Py_DECREF(pymaterial_indices);
PyDict_SetItemString(rtndict, "attribs", attribobj);
Py_DECREF(attribobj);
return rtndict;
}
static PyMethodDef mMethods[] = {
{"LoadObj", pyLoadObj, METH_VARARGS}, {NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT, "tinyobjloader", NULL, -1, mMethods};
static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT, "tinyobjloader",
NULL, -1, mMethods};
PyMODINIT_FUNC PyInit_tinyobjloader(void) {
return PyModule_Create(&moduledef);

View File

@@ -1,24 +0,0 @@
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

View File

@@ -1,31 +0,0 @@
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

View File

@@ -1,57 +1,52 @@
#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 <fstream>
#include <cassert>
#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;
for (size_t v = 0; v < attrib.vertices.size() / 3; v++) {
printf(" v[%ld] = (%f, %f, %f)\n", v,
static_cast<const double>(attrib.vertices[3 * v + 0]),
static_cast<const double>(attrib.vertices[3 * v + 1]),
static_cast<const double>(attrib.vertices[3 * v + 2]));
static_cast<const double>(attrib.vertices[v].x),
static_cast<const double>(attrib.vertices[v].y),
static_cast<const double>(attrib.vertices[v].z));
}
for (size_t v = 0; v < attrib.normals.size() / 3; v++) {
printf(" n[%ld] = (%f, %f, %f)\n", v,
static_cast<const double>(attrib.normals[3 * v + 0]),
static_cast<const double>(attrib.normals[3 * v + 1]),
static_cast<const double>(attrib.normals[3 * v + 2]));
static_cast<const double>(attrib.normals[v].x),
static_cast<const double>(attrib.normals[v].y),
static_cast<const double>(attrib.normals[v].z));
}
for (size_t v = 0; v < attrib.texcoords.size() / 2; v++) {
printf(" uv[%ld] = (%f, %f)\n", v,
static_cast<const double>(attrib.texcoords[2 * v + 0]),
static_cast<const double>(attrib.texcoords[2 * v + 1]));
static_cast<const double>(attrib.texcoords[v].x),
static_cast<const double>(attrib.texcoords[v].y));
}
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];
@@ -66,21 +61,19 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
} 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]));
@@ -99,28 +92,33 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
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(", ");
}
}
@@ -131,45 +129,25 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
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());
@@ -178,25 +156,23 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
}
}
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, &warn, &err,
filename, basepath, triangulate);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename, basepath, triangulate);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
if (!ret) {
@@ -209,10 +185,13 @@ static bool TestLoadObj(const char* filename, const char* basepath = NULL,
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;
@@ -224,24 +203,19 @@ static bool TestLoadObjFromPreopenedFile(const char* filename,
}
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, &warn, &err,
&fileStream, materialReader);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, &fileStream, materialReader);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
if (!ret) {
@@ -254,11 +228,14 @@ static bool TestLoadObjFromPreopenedFile(const char* filename,
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"
@@ -317,21 +294,23 @@ static bool TestStreamLoadObj() {
"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* warn, std::string* err) {
//std::map<std::string, int>* matMap,
std::map<uint32_t, int>* matMap,
std::string* err)
{
(void)matId;
(void)warn;
(void)err;
std::string warning;
std::string error_msg;
LoadMtl(matMap, materials, &m_matSStream, &warning, &error_msg);
LoadMtl(matMap, materials, &m_matSStream, &warning);
return true;
}
@@ -343,17 +322,11 @@ 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, &warn, &err,
&objStream, &matSSReader);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, &objStream, &matSSReader);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
if (!ret) {
@@ -368,26 +341,21 @@ static bool TestStreamLoadObj() {
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, &warn, &err,
"../models/catmark_torus_creases0.obj",
gMtlBasePath, /*triangulate*/ false);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/catmark_torus_creases0.obj", gMtlBasePath, /*triangulate*/false);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
REQUIRE(true == ret);
@@ -397,22 +365,16 @@ 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, &warn, &err,
"../models/pbr-mat-ext.obj", gMtlBasePath,
/*triangulate*/ false);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/pbr-mat-ext.obj", gMtlBasePath, /*triangulate*/false);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
REQUIRE(true == ret);
REQUIRE(1 == materials.size());
@@ -430,18 +392,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]") {
@@ -449,17 +411,11 @@ 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, &warn, &err,
"../models/issue-92.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-92.obj", gMtlBasePath);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
REQUIRE(true == ret);
REQUIRE(1 == materials.size());
@@ -471,17 +427,11 @@ 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, &warn, &err,
"../models/issue-95.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-95.obj", gMtlBasePath);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
REQUIRE(true == ret);
REQUIRE(1 == materials.size());
@@ -495,17 +445,11 @@ 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, &warn, &err,
"../models/issue-95-2.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-95-2.obj", gMtlBasePath);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
REQUIRE(true == ret);
REQUIRE(1 == materials.size());
@@ -519,17 +463,11 @@ 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, &warn, &err,
"../models/issue-95.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-95.obj", gMtlBasePath);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
REQUIRE(true == ret);
REQUIRE(1 == materials.size());
@@ -543,17 +481,11 @@ 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, &warn, &err,
"../models/usemtl-issue-104.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/usemtl-issue-104.obj", gMtlBasePath);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
REQUIRE(true == ret);
REQUIRE(1 == shapes.size());
@@ -564,18 +496,11 @@ 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, &warn, &err,
"../models/texture-options-issue-85.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/texture-options-issue-85.obj", gMtlBasePath);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
REQUIRE(true == ret);
REQUIRE(1 == shapes.size());
@@ -601,13 +526,11 @@ 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]") {
@@ -615,18 +538,11 @@ 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, &warn, &err,
"../models/mtllib-multiple-files-issue-112.obj",
gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/mtllib-multiple-files-issue-112.obj", gMtlBasePath);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
REQUIRE(true == ret);
REQUIRE(1 == materials.size());
@@ -637,17 +553,11 @@ 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, &warn, &err,
"../models/tr-and-d-issue-43.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/tr-and-d-issue-43.obj", gMtlBasePath);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
REQUIRE(true == ret);
REQUIRE(2 == materials.size());
@@ -661,17 +571,11 @@ 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, &warn, &err,
"../models/refl.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/refl.obj", gMtlBasePath);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
PrintInfo(attrib, shapes, materials);
@@ -687,17 +591,11 @@ 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, &warn, &err,
"../models/map-bump.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/map-bump.obj", gMtlBasePath);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
PrintInfo(attrib, shapes, materials);
@@ -713,17 +611,11 @@ 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, &warn, &err,
"../models/issue-138.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-138.obj", gMtlBasePath);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
PrintInfo(attrib, shapes, materials);
@@ -731,6 +623,7 @@ TEST_CASE("g_ignored", "[Issue138]") {
REQUIRE(true == ret);
REQUIRE(2 == shapes.size());
REQUIRE(2 == materials.size());
}
TEST_CASE("vertex-col-ext", "[Issue144]") {
@@ -738,18 +631,12 @@ 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, &warn, &err,
"../models/cube-vertexcol.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/cube-vertexcol.obj", gMtlBasePath);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
//PrintInfo(attrib, shapes, materials);
@@ -777,23 +664,18 @@ 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, &warn, &err,
"../models/norm-texopt.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/norm-texopt.obj", gMtlBasePath);
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << 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]") {
@@ -801,21 +683,16 @@ 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, &warn, &err,
"../models/issue-140-zero-face-idx.obj", gMtlBasePath);
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-140-zero-face-idx.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << err << std::endl;
}
REQUIRE(false == ret);
REQUIRE(!err.empty());
}
TEST_CASE("texture-name-whitespace", "[Issue145]") {
@@ -823,18 +700,12 @@ 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, &warn, &err,
"../models/texture-filename-with-whitespace.obj",
gMtlBasePath);
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/texture-filename-with-whitespace.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
std::cerr << "[Issue145] " << err << std::endl;
}
REQUIRE(true == ret);
@@ -844,247 +715,9 @@ 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]") {
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/issue-162-smoothing-group.obj", gMtlBasePath);
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
}
REQUIRE(true == ret);
REQUIRE(2 == shapes.size());
REQUIRE(2 == shapes[0].mesh.smoothing_group_ids.size());
REQUIRE(1 == shapes[0].mesh.smoothing_group_ids[0]);
REQUIRE(1 == shapes[0].mesh.smoothing_group_ids[1]);
REQUIRE(10 == shapes[1].mesh.smoothing_group_ids.size());
REQUIRE(0 == shapes[1].mesh.smoothing_group_ids[0]);
REQUIRE(0 == shapes[1].mesh.smoothing_group_ids[1]);
REQUIRE(3 == shapes[1].mesh.smoothing_group_ids[2]);
REQUIRE(3 == shapes[1].mesh.smoothing_group_ids[3]);
REQUIRE(4 == shapes[1].mesh.smoothing_group_ids[4]);
REQUIRE(4 == shapes[1].mesh.smoothing_group_ids[5]);
REQUIRE(0 == shapes[1].mesh.smoothing_group_ids[6]);
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("initialize_all_texopts", "[ensure unparsed texopts are initialized to defaults]") {
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/cornell_box.obj", gMtlBasePath, false);
REQUIRE(0 < materials.size());
#define REQUIRE_DEFAULT_TEXOPT(texopt) \
REQUIRE(tinyobj::TEXTURE_TYPE_NONE == texopt.type); \
REQUIRE(0.0 == texopt.brightness); \
REQUIRE(1.0 == texopt.contrast); \
REQUIRE(false == texopt.clamp); \
REQUIRE(true == texopt.blendu); \
REQUIRE(true == texopt.blendv); \
REQUIRE(1.0 == texopt.bump_multiplier); \
for (int j = 0; j < 3; j++) { \
REQUIRE(0.0 == texopt.origin_offset[j]); \
REQUIRE(1.0 == texopt.scale[j]); \
REQUIRE(0.0 == texopt.turbulence[j]); \
}
for (size_t i = 0; i < materials.size(); i++) {
REQUIRE_DEFAULT_TEXOPT(materials[i].ambient_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].diffuse_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].specular_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].specular_highlight_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].bump_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].displacement_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].alpha_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].reflection_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].roughness_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].metallic_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].sheen_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].emissive_texopt);
REQUIRE_DEFAULT_TEXOPT(materials[i].normal_texopt);
}
#undef REQUIRE_DEFAULT_TEXOPT
}
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(

File diff suppressed because it is too large Load Diff