69 Commits

Author SHA1 Message Date
Syoyo Fujita
a957ebe002 Fix some coments.
Bump version 1.4.0
2018-12-01 13:32:05 +09:00
Syoyo Fujita
d793bfb405 Merge pull request #191 from sinisterchipmunk/initialize_unparsed_texopts
Properly initialize all texture_option_t's, not just parsed ones
2018-12-01 13:25:41 +09:00
Colin MacKenzie IV
0ab0146296 Update function declaration 2018-11-30 15:27:38 -05:00
Colin MacKenzie IV
20d122f305 Properly initialize all texture_option_t's, not just parsed ones 2018-11-30 15:14:07 -05:00
Syoyo Fujita
850d0ffdfa Merge pull request #190 from ukabuer/patch-1
Update README.md
2018-11-30 21:29:21 +09:00
UKABUER
80058fdcb0 Update README.md 2018-11-30 20:24:14 +08:00
Syoyo Fujita
1a7aea4ac1 Merge pull request #189 from mathue/patch-1
Fix of important memory leak in Python module
2018-11-29 01:31:03 +09:00
Martin Thümmel
178ef391c7 Fix of important memory leak in Python module
Dear all,
As described in the issue https://github.com/syoyo/tinyobjloader/issues/188#issue-385341218 there is a memory leak in the function pyLoadObj(). The reference count for all created Python objects must be decreased after they are fed into the lists and dictionaries. The only  exception from this is the function PyTuple_SetItem() in pyTupleFromfloat3(), which decreases the reference counter of the Python object *tuple automatically by calling this function. In all other cases like PyList_Insert(), the references are only borrowed and not decreased. Therefore, each created Python object will remain in the heap, since there is one reference to it left in the counter.

These facts are explained in https://docs.python.org/2/c-api/intro.html#reference-counts in detail.

Best regards
Martin.
P.S. sorry, that i did not put that much effort into a more readable code and just inserted the Py_DECREF() function at every necessary position.
2018-11-28 17:16:12 +01:00
Syoyo Fujita
59b4d7ccef Merge pull request #187 from Neptilo/master
Fix parsing double with negative exponent
2018-11-06 00:28:10 +09:00
Victor Ripplinger
6f990e2b6c Fix parsing double with negative exponent 2018-11-05 15:17:34 +01:00
Syoyo Fujita
7fb5056a53 Make ParseTextureNameAndOption function public. 2018-10-19 15:29:31 +09:00
Syoyo Fujita
934788785e Show line number for some warning and error message. 2018-10-10 13:33:11 +09:00
Syoyo Fujita
e07a835f02 Separate error message into warning and error. Breaks API, so bump to version 1.3.0 2018-10-10 01:56:07 +09:00
Syoyo Fujita
1cdfd786d8 Add colorspace extension to texture options. Fixes #184 2018-09-14 13:42:27 +09:00
Syoyo Fujita
803b65b8a0 Merge pull request #183 from mlimper/master
Added option to disable default vertex colors
2018-09-01 00:31:29 +09:00
Max Limper
50518a515b added option to disable default vertex colors 2018-08-31 16:59:32 +02:00
Syoyo Fujita
c9b1bccf97 Fix mis-counting of faces when triangulation is enabled. 2018-08-20 15:10:37 +09:00
Syoyo Fujita
1f17833657 Add comments. 2018-08-20 14:24:49 +09:00
Syoyo Fujita
bb58a8f8c3 Revert assertion equation. 2018-08-20 14:19:34 +09:00
Syoyo Fujita
b1f594d682 Merge branch 'master' of github.com:syoyo/tinyobjloader 2018-08-20 14:10:17 +09:00
Syoyo Fujita
02df4943f9 Fix assert equation. 2018-08-20 14:09:56 +09:00
Syoyo Fujita
826a892d0b Merge pull request #181 from vincentdm05/missing-data
Produce an error and return when indices and data dont match.
2018-08-17 16:12:36 +09:00
Vincent de Marignac
68350e2fc7 give respective warnings but allow partially correct data to be returned 2018-08-16 23:34:47 +03:00
Vincent de Marignac
7d20e9b901 use ternary op instead of max from omitted lib 2018-08-16 00:00:22 +03:00
Vincent de Marignac
fdc70abdc6 fix index comparison wrt array size 2018-08-15 23:41:35 +03:00
Vincent de Marignac
8a885e14b8 Produce an error and return when indices and data dont match. 2018-08-15 23:13:00 +03:00
Syoyo Fujita
cd65de860b Merge branch 'master' of github.com:syoyo/tinyobjloader 2018-08-13 17:47:38 +09:00
Syoyo Fujita
4924857fd3 Add note on # of max threads. 2018-08-13 17:47:13 +09:00
Syoyo Fujita
1a7bdc6192 Parse multiple group names. Fixes #146 2018-08-11 15:21:17 +09:00
Syoyo Fujita
fd06fa49e4 Add unit test for parsing 'l'. 2018-07-25 21:42:23 +09:00
Syoyo Fujita
0d68262246 Fix buffer overrun when parsing 'l' line. 2018-07-25 16:24:40 +09:00
Syoyo Fujita
0e950513a3 Merge pull request #178 from ZKing1000/master
Added support for line paths.
2018-07-25 16:02:49 +09:00
Holden Green
a4b115a584 Hopefully fixed compiling error 2018-07-24 23:47:59 -07:00
Syoyo Fujita
bb3e27d4f3 Merge pull request #179 from mlimper/master
Fixed #177
2018-07-09 19:29:07 +09:00
Max Limper
c4e7e65acb fixed #177 (explicit empty mtl base dir given) 2018-07-09 12:21:46 +02:00
Max Limper
eba327b9c0 make tester fail for issue 177 failing (explicit MTL base dir given by API user) 2018-07-09 12:19:33 +02:00
Holden Green
3cbf45a572 Non const 2018-07-04 22:13:39 -07:00
Holden Green
6650dbf397 Used swap in favor of move 2018-07-04 22:10:45 -07:00
Holden Green
7befd59de4 Removed comment 2018-07-04 21:59:56 -07:00
Holden Green
c5b3139653 Fixed comments 2018-07-04 21:46:42 -07:00
Holden Green
adb2309110 Removed print message. 2018-07-04 21:44:09 -07:00
Holden Green
3edca81a75 Added line paths. 2018-07-04 21:21:56 -07:00
Syoyo Fujita
7a88cddefc Add unit test for issue 177. 2018-07-03 18:37:41 +02:00
Syoyo Fujita
d541711a79 Merge pull request #176 from silverweed/master
Ensure mtl_basedir ends with a directory separator
2018-06-02 00:18:54 +09:00
Syoyo Fujita
d1ce2082f6 Remove afl files since it fails to create file on Windows file system. 2018-06-02 00:15:18 +09:00
silverweed
8fd9f6e57b ensure mtl_basedir ends with a directory separator 2018-06-01 12:02:38 +02:00
Syoyo Fujita
24bd8b49ff Merge branch 'master' of github.com:syoyo/tinyobjloader 2018-05-31 16:34:01 +09:00
Syoyo Fujita
4a0e79985d Bump version. 2018-05-31 16:29:14 +09:00
Syoyo Fujita
06f6139d1f Limit # of tags when parsing SubD tag('t'). 2018-05-31 16:25:39 +09:00
Syoyo Fujita
0c8db8ee23 Hardened implementation(fix some seg faults, out-of-bound access) found by fuzzer. 2018-05-31 15:49:56 +09:00
Syoyo Fujita
64d1e3f883 Merge pull request #170 from Neptilo/master
Fix bug that discarded some chunks.
2018-03-27 00:57:19 +09:00
Victor Ripplinger
a39a6b481c Fix bug that discarded some chunks.
The OBJ loader was discarding the first line of a file
or a chunk following one that ends with a newline.
2018-03-26 16:28:13 +02:00
Syoyo Fujita
bce1bb8387 Merge branch 'master' of github.com:syoyo/tinyobjloader 2018-03-26 20:10:55 +09:00
Syoyo Fujita
7fcfafb39a Fix compilation for double precision mode(Fixes #167). 2018-03-26 20:09:56 +09:00
Syoyo Fujita
5eda671225 Merge pull request #169 from LZaw/master
Adapt 1.0.x format
2018-03-10 00:03:35 +09:00
LZaw
f95510b04b Removed multiplication for color_idx_offset 2018-03-09 13:57:18 +01:00
LZaw
94f1dc15b3 Update obj_sticher.cc 2018-03-09 13:48:05 +01:00
LZaw
a4eabf54b1 Adapt 1.0.x datastructure 2018-03-09 13:43:52 +01:00
Syoyo Fujita
72c38f64d5 Merge pull request #168 from LZaw/master
Adapt the v1.0.x data format to the WriteObj function
2018-03-09 00:57:27 +09:00
LZaw
d174e625a2 Update obj_writer.h 2018-03-08 15:58:39 +01:00
LZaw
707014f843 Update obj_writer.cc 2018-03-08 15:48:45 +01:00
LZaw
12837cc8b2 Adapt v 1.0.x
Added an attributes parameter
2018-03-08 15:47:06 +01:00
Syoyo Fujita
b85714b4cf Skip parsing incomplete or invalid face definition(e.g. f definition only contains 1 or 2 indices). 2018-02-23 20:25:13 +09:00
Syoyo Fujita
e060b4f4aa Remove unused variable. 2018-02-21 20:53:30 +09:00
Syoyo Fujita
2dca72724f Merge pull request #165 from Neptilo/master
Fix bug in not assigning material id to faces
2018-02-21 20:01:23 +09:00
Victor Ripplinger
72529f02fe Material ids were not assigned to faces if the
first face with this material was in another thread buffer than where
the usemtl command was
2018-02-21 11:36:36 +01:00
Syoyo Fujita
16f3041c76 Merge pull request #163 from Neptilo/master
Fix bug when line starts before a chunk and ends after it
2018-02-09 20:10:16 +09:00
Neptilo
73e9b4dc3a Fix bug when line starts before a chunk and ends after it
When a line started in a chunk, the algorithm only looked for its end in the following chunk, but the end of the line may actually be located even further. Instead, in this commit we look for the next new line until the end of the whole buffer.
2018-02-09 11:46:43 +01:00
Syoyo Fujita
5113cd65cf print smoothing group id. 2018-02-04 15:19:42 +09:00
24 changed files with 1668 additions and 865 deletions

View File

@@ -204,9 +204,10 @@ std::string inputfile = "cornell_box.obj";
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, inputfile.c_str());
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, inputfile.c_str());
if (!err.empty()) { // `err` may contain warning message.
std::cerr << err << std::endl;

View File

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

View File

@@ -1,9 +1,10 @@
//
// Stiches multiple .obj files into one .obj.
// Stiches multiple .obj files into one .obj.
//
#include "../../tiny_obj_loader.h"
#include "obj_writer.h"
#include "../../tiny_obj_loader.h"
#include <cassert>
#include <iostream>
#include <cstdlib>
@@ -11,27 +12,59 @@
typedef std::vector<tinyobj::shape_t> Shape;
typedef std::vector<tinyobj::material_t> Material;
typedef tinyobj::attrib_t Attribute;
void
StichObjs(
tinyobj::attrib_t& out_attribute,
std::vector<tinyobj::shape_t>& out_shape,
std::vector<tinyobj::material_t>& out_material,
const std::vector<Attribute>& attributes,
const std::vector<Shape>& shapes,
const std::vector<Material>& materials)
{
int numShapes = 0;
for (size_t i = 0; i < shapes.size(); i++) {
numShapes += (int)shapes[i].size();
// The amount of attributes, shape-vectors and material-vecotrs should be the same.
if(attributes.size() != shapes.size() && attributes.size() != materials.size()){
std::cerr << "Size of attributes, shapes and Materials don't fit!" << attributes.size() << " " << shapes.size() <<" " << materials.size() << std::endl;;
exit(1);
}
int num_shapes = 0;
// 4 values (vertices, normals, texcoords, colors)
std::vector<int> num_attributes(4, 0);
int num_materials = 0;
for(int i = 0; i < shapes.size(); i++){
num_shapes += shapes[i].size();
}
for(int i = 0; i < attributes.size(); i++){
num_attributes[0] += attributes[i].vertices.size();
num_attributes[1] += attributes[i].normals.size();
num_attributes[2] += attributes[i].texcoords.size();
num_attributes[3] += attributes[i].colors.size();
}
for(int i = 0; i < materials.size(); i++){
num_materials += materials[i].size();
}
printf("Total # of shapes = %d\n", numShapes);
int materialIdOffset = 0;
// More performant, than push_back
out_attribute.vertices.resize(num_attributes[0]);
out_attribute.normals.resize(num_attributes[1]);
out_attribute.texcoords.resize(num_attributes[2]);
out_attribute.colors.resize(num_attributes[3]);
out_shape.resize(num_shapes);
out_material.resize(num_materials);
size_t face_offset = 0;
int material_id_offset = 0;
int shape_id_offset = 0;
int vertex_idx_offset = 0;
int normal_idx_offset = 0;
int texcoord_idx_offset = 0;
int color_idx_offset = 0;
// shapes.size() = attributes.size() = materials.size()
for (size_t i = 0; i < shapes.size(); i++) {
// Copy shapes
for (size_t k = 0; k < shapes[i].size(); k++) {
std::string new_name = shapes[i][k].name;
// Add suffix
char buf[1024];
@@ -39,36 +72,51 @@ StichObjs(
new_name += std::string(buf);
printf("shape[%ld][%ld].name = %s\n", i, k, shapes[i][k].name.c_str());
assert((shapes[i][k].mesh.indices.size() % 3) == 0);
assert((shapes[i][k].mesh.positions.size() % 3) == 0);
tinyobj::shape_t new_shape = shapes[i][k];
// Add offset.
for (size_t f = 0; f < new_shape.mesh.material_ids.size(); f++) {
new_shape.mesh.material_ids[f] += materialIdOffset;
// Add material 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.name = new_name;
printf("shape[%ld][%ld].new_name = %s\n", i, k, new_shape.name.c_str());
out_shape.push_back(new_shape);
out_shape[shape_id_offset++] = new_shape;
}
materialIdOffset += materials[i].size();
}
for (size_t i = 0; i < materials.size(); i++) {
// Copy materials
for (size_t k = 0; k < materials[i].size(); k++) {
out_material.push_back(materials[i][k]);
out_material[material_id_offset++] = materials[i][k];
}
}
// Copy attributes (3 floats per vertex, 3 floats per normal, 2 floats per texture-coordinate, 3 floats per color)
// You could also include a check here, if the sizes are dividable by 3 (resp. 2), but it's safe to simply assume, they do.
std::copy(attributes[i].vertices.begin(), attributes[i].vertices.end(), out_attribute.vertices.begin() + vertex_idx_offset * 3);
vertex_idx_offset += attributes[i].vertices.size() / 3;
std::copy(attributes[i].normals.begin(), attributes[i].normals.end(), out_attribute.normals.begin() + normal_idx_offset * 3);
normal_idx_offset += attributes[i].normals.size() / 3;
std::copy(attributes[i].texcoords.begin(), attributes[i].texcoords.end(), out_attribute.texcoords.begin() + texcoord_idx_offset * 2);
texcoord_idx_offset += attributes[i].texcoords.size() / 2;
std::copy(attributes[i].colors.begin(), attributes[i].colors.end(), out_attribute.colors.begin() + color_idx_offset);
color_idx_offset += attributes[i].colors.size();
}
}
int
main(
int argc,
char **argv)
int main(int argc, char **argv)
{
if (argc < 3) {
printf("Usage: obj_sticher input0.obj input1.obj ... output.obj\n");
@@ -78,16 +126,16 @@ main(
int num_objfiles = argc - 2;
std::string out_filename = std::string(argv[argc-1]); // last element
std::vector<Shape> shapes;
std::vector<Material> materials;
shapes.resize(num_objfiles);
materials.resize(num_objfiles);
std::vector<Attribute> attributes(num_objfiles);
std::vector<Shape> shapes(num_objfiles);
std::vector<Material> materials(num_objfiles);
for (int i = 0; i < num_objfiles; i++) {
std::cout << "Loading " << argv[i+1] << " ... " << std::flush;
std::string warn;
std::string err;
bool ret = tinyobj::LoadObj(shapes[i], materials[i], err, argv[i+1]);
bool ret = tinyobj::LoadObj(&attributes[i], &shapes[i], &materials[i], &warn, &err, argv[i+1]);
if (!err.empty()) {
std::cerr << err << std::endl;
}
@@ -98,12 +146,13 @@ main(
std::cout << "DONE." << std::endl;
}
std::vector<tinyobj::shape_t> out_shape;
std::vector<tinyobj::material_t> out_material;
StichObjs(out_shape, out_material, shapes, materials);
Attribute out_attribute;
Shape out_shape;
Material out_material;
StichObjs(out_attribute, out_shape, out_material, attributes, shapes, materials);
bool coordTransform = true;
bool ret = WriteObj(out_filename, out_shape, out_material, coordTransform);
bool ret = WriteObj(out_filename, out_attribute, out_shape, out_material, coordTransform);
assert(ret);
return 0;

View File

@@ -30,6 +30,8 @@ bool WriteMat(const std::string& filename, const std::vector<tinyobj::material_t
fprintf(fp, "Ke %f %f %f\n", mat.emission[0], mat.emission[1], mat.emission[2]);
fprintf(fp, "Ns %f\n", mat.shininess);
fprintf(fp, "Ni %f\n", mat.ior);
fprintf(fp, "illum %d\n", mat.illum);
fprintf(fp, "\n");
// @todo { texture }
}
@@ -38,7 +40,7 @@ bool WriteMat(const std::string& filename, const std::vector<tinyobj::material_t
return true;
}
bool WriteObj(const std::string& filename, const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials, bool coordTransform) {
bool WriteObj(const std::string& filename, const tinyobj::attrib_t& attributes, const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials, bool coordTransform) {
FILE* fp = fopen(filename.c_str(), "w");
if (!fp) {
fprintf(stderr, "Failed to open file [ %s ] for write.\n", filename.c_str());
@@ -48,17 +50,53 @@ bool WriteObj(const std::string& filename, const std::vector<tinyobj::shape_t>&
std::string basename = GetFileBasename(filename);
std::string material_filename = basename + ".mtl";
int v_offset = 0;
int vn_offset = 0;
int vt_offset = 0;
int prev_material_id = -1;
fprintf(fp, "mtllib %s\n", material_filename.c_str());
fprintf(fp, "mtllib %s\n\n", material_filename.c_str());
// facevarying vtx
for (size_t k = 0; k < attributes.vertices.size(); k+=3) {
if (coordTransform) {
fprintf(fp, "v %f %f %f\n",
attributes.vertices[k + 0],
attributes.vertices[k + 2],
-attributes.vertices[k + 1]);
} else {
fprintf(fp, "v %f %f %f\n",
attributes.vertices[k + 0],
attributes.vertices[k + 1],
attributes.vertices[k + 2]);
}
}
fprintf(fp, "\n");
// facevarying normal
for (size_t k = 0; k < attributes.normals.size(); k += 3) {
if (coordTransform) {
fprintf(fp, "vn %f %f %f\n",
attributes.normals[k + 0],
attributes.normals[k + 2],
-attributes.normals[k + 1]);
} else {
fprintf(fp, "vn %f %f %f\n",
attributes.normals[k + 0],
attributes.normals[k + 1],
attributes.normals[k + 2]);
}
}
fprintf(fp, "\n");
// facevarying texcoord
for (size_t k = 0; k < attributes.texcoords.size(); k += 2) {
fprintf(fp, "vt %f %f\n",
attributes.texcoords[k + 0],
attributes.texcoords[k + 1]);
}
for (size_t i = 0; i < shapes.size(); i++) {
bool has_vn = false;
bool has_vt = false;
fprintf(fp, "\n");
if (shapes[i].name.empty()) {
fprintf(fp, "g Unknown\n");
@@ -66,101 +104,53 @@ bool WriteObj(const std::string& filename, const std::vector<tinyobj::shape_t>&
fprintf(fp, "g %s\n", shapes[i].name.c_str());
}
//if (!shapes[i].material.name.empty()) {
// fprintf(fp, "usemtl %s\n", shapes[i].material.name.c_str());
//}
// facevarying vtx
for (size_t k = 0; k < shapes[i].mesh.indices.size() / 3; k++) {
for (int j = 0; j < 3; j++) {
int idx = shapes[i].mesh.indices[3*k+j];
if (coordTransform) {
fprintf(fp, "v %f %f %f\n",
shapes[i].mesh.positions[3*idx+0],
shapes[i].mesh.positions[3*idx+2],
-shapes[i].mesh.positions[3*idx+1]);
} else {
fprintf(fp, "v %f %f %f\n",
shapes[i].mesh.positions[3*idx+0],
shapes[i].mesh.positions[3*idx+1],
shapes[i].mesh.positions[3*idx+2]);
}
}
bool has_vn = false;
bool has_vt = false;
// Assumes normals and textures are set shape-wise.
if(shapes[i].mesh.indices.size() > 0){
has_vn = shapes[i].mesh.indices[0].normal_index != -1;
has_vt = shapes[i].mesh.indices[0].texcoord_index != -1;
}
// facevarying normal
if (shapes[i].mesh.normals.size() > 0) {
for (size_t k = 0; k < shapes[i].mesh.indices.size() / 3; k++) {
for (int j = 0; j < 3; j++) {
int idx = shapes[i].mesh.indices[3*k+j];
if (coordTransform) {
fprintf(fp, "vn %f %f %f\n",
shapes[i].mesh.normals[3*idx+0],
shapes[i].mesh.normals[3*idx+2],
-shapes[i].mesh.normals[3*idx+1]);
} else {
fprintf(fp, "vn %f %f %f\n",
shapes[i].mesh.normals[3*idx+0],
shapes[i].mesh.normals[3*idx+1],
shapes[i].mesh.normals[3*idx+2]);
}
}
}
}
if (shapes[i].mesh.normals.size() > 0) has_vn = true;
// facevarying texcoord
if (shapes[i].mesh.texcoords.size() > 0) {
for (size_t k = 0; k < shapes[i].mesh.indices.size() / 3; k++) {
for (int j = 0; j < 3; j++) {
int idx = shapes[i].mesh.indices[3*k+j];
fprintf(fp, "vt %f %f\n",
shapes[i].mesh.texcoords[2*idx+0],
shapes[i].mesh.texcoords[2*idx+1]);
}
}
}
if (shapes[i].mesh.texcoords.size() > 0) has_vt = true;
// face
for (size_t k = 0; k < shapes[i].mesh.indices.size() / 3; k++) {
// Face index is 1-base.
//int v0 = shapes[i].mesh.indices[3*k+0] + 1 + v_offset;
//int v1 = shapes[i].mesh.indices[3*k+1] + 1 + v_offset;
//int v2 = shapes[i].mesh.indices[3*k+2] + 1 + v_offset;
int v0 = (3*k + 0) + 1 + v_offset;
int v1 = (3*k + 1) + 1 + v_offset;
int v2 = (3*k + 2) + 1 + v_offset;
int vt0 = (3*k + 0) + 1 + vt_offset;
int vt1 = (3*k + 1) + 1 + vt_offset;
int vt2 = (3*k + 2) + 1 + vt_offset;
int material_id = shapes[i].mesh.material_ids[k];
int face_index = 0;
for (size_t k = 0; k < shapes[i].mesh.indices.size(); k += shapes[i].mesh.num_face_vertices[face_index++]) {
// Check Materials
int material_id = shapes[i].mesh.material_ids[face_index];
if (material_id != prev_material_id) {
std::string material_name = materials[material_id].name;
fprintf(fp, "usemtl %s\n", material_name.c_str());
prev_material_id = material_id;
}
if (has_vn && has_vt) {
fprintf(fp, "f %d/%d/%d %d/%d/%d %d/%d/%d\n",
v0, vt0, v0, v1, vt1, v1, v2, vt2, v2);
} else if (has_vn && !has_vt) {
fprintf(fp, "f %d//%d %d//%d %d//%d\n", v0, v0, v1, v1, v2, v2);
} else if (!has_vn && has_vt) {
fprintf(fp, "f %d/%d %d/%d %d/%d\n", v0, v0, v1, v1, v2, v2);
} else {
fprintf(fp, "f %d %d %d\n", v0, v1, v2);
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;
}
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);

View File

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

View File

@@ -308,9 +308,13 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
base_dir += "/";
#endif
std::string warn;
std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename,
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename,
base_dir.c_str());
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) {
std::cerr << err << std::endl;
}

View File

@@ -9,8 +9,9 @@ bool Voxelize(const char* filename, float voxelsizex, float voxelsizey, float vo
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename);
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename);
if (!err.empty()) {
printf("err: %s\n", err.c_str());

View File

@@ -6,11 +6,21 @@
* C++-11 compiler
## How to build
```
$ premak5 gmake
```
## Compile options
* zstd compressed .obj support. `--with-zstd` premake option.
* gzip compressed .obj support. `--with-zlib` premake option.
## Notes on AMD GPU + Linux
You may need to link with libdrm(`-ldrm`).
## Licenses
* lfpAlloc : MIT license.

View File

@@ -312,6 +312,10 @@ typedef struct {
typedef struct {
std::string name; // group name or object name.
// Shape's corresponding faces are accessed by attrib.indices[face_offset,
// face_offset + length] NOTE: you'll need to sum up
// attrib.face_num_verts[face_offset, face_offset + length] to find actual
// number of faces.
unsigned int face_offset;
unsigned int length;
} shape_t;
@@ -330,7 +334,14 @@ typedef struct {
std::vector<float, lfpAlloc::lfpAllocator<float> > normals;
std::vector<float, lfpAlloc::lfpAllocator<float> > texcoords;
std::vector<index_t, lfpAlloc::lfpAllocator<index_t> > indices;
// # of vertices for each face.
// 3 for triangle, 4 for qual, ...
// If triangulation is enabled and the original face are quad,
// face_num_verts will be 6(3 + 3)
std::vector<int, lfpAlloc::lfpAllocator<int> > face_num_verts;
// Per-face material IDs.
std::vector<int, lfpAlloc::lfpAllocator<int> > material_ids;
} attrib_t;
@@ -594,19 +605,9 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result) {
}
assemble :
{
// = 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
}
*result = (sign == '+' ? 1 : -1) *
(exponent ? std::ldexp(mantissa * std::pow(5.0, exponent), exponent)
: mantissa);
return true;
fail:
return false;
@@ -1046,7 +1047,7 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
std::vector<material_t> *materials, const char *buf, size_t len,
const LoadOption &option);
} // namespace tinyobj_opt
} // namespace tinyobj_opt
#endif // TINOBJ_LOADER_OPT_H_
@@ -1238,6 +1239,8 @@ typedef struct {
// 3. Do parallel parsing for each line.
// 4. Reconstruct final mesh data structure.
// Raise # of max threads if you have more CPU cores...
// In 2018, 32 cores are getting common in high-end workstaion PC.
#define kMaxThreads (32)
static inline bool is_line_ending(const char *p, size_t i, size_t end_i) {
@@ -1306,15 +1309,19 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
end_idx = len - 1;
}
// true if the line currently read must be added to the current line
// info
bool new_line_found =
(t == 0) || is_line_ending(buf, start_idx - 1, end_idx);
size_t prev_pos = start_idx;
for (size_t i = start_idx; i < end_idx; i++) {
if (is_line_ending(buf, i, end_idx)) {
if ((t > 0) && (prev_pos == start_idx) &&
(!is_line_ending(buf, start_idx - 1, end_idx))) {
if (!new_line_found) {
// first linebreak found in (chunk > 0), and a line before this
// linebreak belongs to previous chunk, so skip it.
prev_pos = i + 1;
continue;
new_line_found = true;
} else {
LineInfo info;
info.pos = prev_pos;
@@ -1329,11 +1336,11 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
}
}
// Find extra line which spand across chunk boundary.
if ((t < num_threads) && (buf[end_idx - 1] != '\n')) {
auto extra_span_idx = (std::min)(end_idx - 1 + chunk_size, len);
for (size_t i = end_idx; i < extra_span_idx; i++) {
if (is_line_ending(buf, i, extra_span_idx)) {
// If at least one line started in this chunk, find where it ends in the
// rest of the buffer
if (new_line_found && (t < num_threads) && (buf[end_idx - 1] != '\n')) {
for (size_t i = end_idx; i < len; i++) {
if (is_line_ending(buf, i, len)) {
LineInfo info;
info.pos = prev_pos;
info.len = i - prev_pos;
@@ -1390,7 +1397,6 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
for (size_t t = 0; t < num_threads; t++) {
workers->push_back(std::thread([&, t]() {
for (size_t i = 0; i < line_infos[t].size(); i++) {
Command command;
bool ret = parseLine(&command, &buf[line_infos[t][i].pos],
@@ -1415,7 +1421,6 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
commands[t].emplace_back(std::move(command));
}
}
}));
}
@@ -1440,9 +1445,9 @@ 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();
}
if (material_filename.back() == '\r') {
material_filename.pop_back();
}
std::ifstream ifs(material_filename);
if (ifs.good()) {
LoadMtl(&material_map, materials, &ifs);
@@ -1493,7 +1498,7 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
attrib->texcoords.resize(num_vt * 2);
attrib->indices.resize(num_f);
attrib->face_num_verts.resize(num_indices);
attrib->material_ids.resize(num_indices);
attrib->material_ids.resize(num_indices, -1);
size_t v_offsets[kMaxThreads];
size_t n_offsets[kMaxThreads];
@@ -1524,22 +1529,46 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
size_t t_count = t_offsets[t];
size_t f_count = f_offsets[t];
size_t face_count = face_offsets[t];
int material_id = -1; // -1 = default unknown material.
for (size_t i = 0; i < commands[t].size(); i++) {
if (commands[t][i].type == COMMAND_EMPTY) {
continue;
} else if (commands[t][i].type == COMMAND_USEMTL) {
if (commands[t][i].material_name &&
commands[t][i].material_name_len > 0) {
std::string material_name(commands[t][i].material_name,
commands[t][i].material_name_len);
if (material_map.find(material_name) != material_map.end()) {
material_id = material_map[material_name];
} else {
// Assign invalid material ID
material_id = -1;
commands[t][i].material_name_len > 0 &&
// check if there are still faces after this command
face_count < num_indices) {
// Find next face
bool found = false;
size_t i_start = i + 1, t_next, i_next;
for (t_next = t; t_next < num_threads; t_next++) {
for (i_next = i_start; i_next < commands[t_next].size();
i_next++) {
if (commands[t_next][i_next].type == COMMAND_F) {
found = true;
break;
}
}
if (found) break;
i_start = 0;
}
// Assign material to this face
if (found) {
std::string material_name(commands[t][i].material_name,
commands[t][i].material_name_len);
for (size_t k = 0;
k < commands[t_next][i_next].f_num_verts.size(); k++) {
if (material_map.find(material_name) != material_map.end()) {
attrib->material_ids[face_count + k] =
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;
}
}
}
}
} else if (commands[t][i].type == COMMAND_V) {
@@ -1566,7 +1595,6 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
index_t(vertex_index, texcoord_index, normal_index);
}
for (size_t k = 0; k < commands[t][i].f_num_verts.size(); k++) {
attrib->material_ids[face_count + k] = material_id;
attrib->face_num_verts[face_count + k] =
commands[t][i].f_num_verts[k];
}
@@ -1581,19 +1609,13 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
for (size_t t = 0; t < workers->size(); t++) {
workers[t].join();
}
if(material_map.size()>1&& num_threads>1) {
for (size_t t = 0; t < num_threads; t++) {
size_t face_count = face_offsets[t];
if (-1 == attrib->material_ids[face_count]) {
int prev_material_id = attrib->material_ids[face_count - 1];
size_t max_face_offset = (t == num_threads - 1) ? attrib->material_ids.size() : face_offsets[t + 1];
for (int i = face_count; i<max_face_offset; ++i) {
if (attrib->material_ids[i] != -1) break;
attrib->material_ids[i] = prev_material_id;
}
}
}
}
// To each face with uninitialized material id,
// assign the material id of the last face preceding it that has one
for (size_t face_count = 1; face_count < num_indices; ++face_count)
if (attrib->material_ids[face_count] == -1)
attrib->material_ids[face_count] = attrib->material_ids[face_count - 1];
auto t_end = std::chrono::high_resolution_clock::now();
ms_merge = t_end - t_start;
}
@@ -1653,7 +1675,8 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
}
}
if (commands[t][i].type == COMMAND_F) {
face_count++;
// Consider generation of multiple faces per `f` line by triangulation
face_count += commands[t][i].f_num_verts.size();
}
}
}

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.
assert(attrib.face_num_verts[v] % 3 == 0); // assume all triangle face(multiple of 3).
for (size_t f = 0; f < attrib.face_num_verts[v] / 3; f++) {
tinyobj_opt::index_t idx0 = attrib.indices[face_offset+3*f+0];
tinyobj_opt::index_t idx1 = attrib.indices[face_offset+3*f+1];

BIN
fuzzer/afl.tar.gz Normal file

Binary file not shown.

20
fuzzer/runner.py Normal file
View File

@@ -0,0 +1,20 @@
import os, sys
import glob
import subprocess
def main():
for g in glob.glob("../tests/afl/id*"):
print(g)
cmd = ["../a.out", g]
proc = subprocess.Popen(cmd)
try:
outs, errs = proc.communicate(timeout=15)
print(outs)
except TimeoutExpired:
proc.kill()
outs, errs = proc.communicate()
main()

View File

@@ -137,14 +137,19 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
for (size_t i = 0; i < shapes.size(); i++) {
printf("shape[%ld].name = %s\n", static_cast<long>(i),
shapes[i].name.c_str());
printf("Size of shape[%ld].indices: %lu\n", static_cast<long>(i),
printf("Size of shape[%ld].mesh.indices: %lu\n", static_cast<long>(i),
static_cast<unsigned long>(shapes[i].mesh.indices.size()));
printf("Size of shape[%ld].path.indices: %lu\n", static_cast<long>(i),
static_cast<unsigned long>(shapes[i].path.indices.size()));
size_t index_offset = 0;
assert(shapes[i].mesh.num_face_vertices.size() ==
shapes[i].mesh.material_ids.size());
assert(shapes[i].mesh.num_face_vertices.size() ==
shapes[i].mesh.smoothing_group_ids.size());
printf("shape[%ld].num_faces: %lu\n", static_cast<long>(i),
static_cast<unsigned long>(shapes[i].mesh.num_face_vertices.size()));
@@ -165,6 +170,8 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
printf(" face[%ld].material_id = %d\n", static_cast<long>(f),
shapes[i].mesh.material_ids[f]);
printf(" face[%ld].smoothing_group_id = %d\n", static_cast<long>(f),
shapes[i].mesh.smoothing_group_ids[f]);
index_offset += fnum;
}
@@ -278,14 +285,19 @@ static bool TestLoadObj(const char* filename, const char* basepath = NULL,
timerutil t;
t.start();
std::string warn;
std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename,
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename,
basepath, triangulate);
t.end();
printf("Parsing time: %lu [msecs]\n", t.msec());
if (!warn.empty()) {
std::cout << "WARN: " << warn << std::endl;
}
if (!err.empty()) {
std::cerr << err << std::endl;
std::cerr << "ERR: " << err << std::endl;
}
if (!ret) {
@@ -369,16 +381,12 @@ static bool TestStreamLoadObj() {
virtual bool operator()(const std::string& matId,
std::vector<material_t>* materials,
std::map<std::string, int>* matMap,
std::string* warn,
std::string* err) {
(void)err;
(void)matId;
std::string warning;
LoadMtl(matMap, materials, &m_matSStream, &warning);
LoadMtl(matMap, materials, &m_matSStream, warn, err);
if (!warning.empty()) {
if (err) {
(*err) += warning;
}
}
return true;
}
@@ -390,8 +398,9 @@ static bool TestStreamLoadObj() {
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, &objStream,
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, &objStream,
&matSSReader);
if (!err.empty()) {

View File

@@ -0,0 +1,9 @@
newmtl default
Ka 0 0 0
Kd 0 0 0
Ks 0 0 0
Kt 0.1 0.2 0.3
map_Kd -colorspace sRGB -o 0.1 diffuse.jpg
map_Ks -s 0.1 0.2 specular.jpg
map_bump -colorspace linear -bm 3 bumpmap.jpg

View File

@@ -0,0 +1,7 @@
mtllib colorspace-issue-184.mtl
o Test
v 1.864151 -1.219172 -5.532511
v 0.575869 -0.666304 5.896140
v 0.940448 1.000000 -1.971128
usemtl default
f 1 2 3

View File

@@ -13,7 +13,8 @@ v 2.000000 2.000000 0.000000
g front cube
usemtl white
f 1 2 3 4
g back cube
# two white spaces between 'back' and 'cube'
g back cube
# expects white material
f 8 7 6 5
g right cube

View File

@@ -0,0 +1,24 @@
newmtl white
Ka 0 0 0
Kd 1 1 1
Ks 0 0 0
newmtl red
Ka 0 0 0
Kd 1 0 0
Ks 0 0 0
newmtl green
Ka 0 0 0
Kd 0 1 0
Ks 0 0 0
newmtl blue
Ka 0 0 0
Kd 0 0 1
Ks 0 0 0
newmtl light
Ka 20 20 20
Kd 1 1 1
Ks 0 0 0

View File

@@ -0,0 +1,18 @@
mtllib invalid-face-definition.mtl
v 0.000000 2.000000 2.000000
v 0.000000 0.000000 2.000000
v 2.000000 0.000000 2.000000
v 2.000000 2.000000 2.000000
v 0.000000 2.000000 0.000000
v 0.000000 0.000000 0.000000
v 2.000000 0.000000 0.000000
v 2.000000 2.000000 0.000000
# 8 vertices
g front cube
usemtl white
f 1
g back cube
# expects white material
f 8 7

16
models/line-prim.obj Normal file
View File

@@ -0,0 +1,16 @@
mtllib cube.mtl
v 0.000000 2.000000 2.000000
v 0.000000 0.000000 2.000000
v 2.000000 0.000000 2.000000
v 2.000000 2.000000 2.000000
v 0.000000 2.000000 0.000000
v 0.000000 0.000000 0.000000
v 2.000000 0.000000 0.000000
v 2.000000 2.000000 0.000000
# 8 vertices
g g0
usemtl white
l 1 2 3 4
l 5 6 7

View File

@@ -18,11 +18,9 @@ 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;
}
@@ -30,32 +28,27 @@ 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;
tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename);
std::string err, warn;
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &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";
@@ -72,124 +65,137 @@ static PyObject* pyLoadObj(PyObject* self, PyObject* args) {
}
for (vectd::iterator it = vect.begin(); it != vect.end(); it++) {
PyList_Insert(current, it - vect.begin(), PyFloat_FromDouble(*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);
}
for (std::vector<tinyobj::shape_t>::iterator shape = shapes.begin();
shape != shapes.end(); shape++) {
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,
// ...
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));
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);
}
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.
PyList_Insert(current, i, PyLong_FromLong(cm.num_face_vertices[i]));
PyObject* value = PyLong_FromLong(cm.num_face_vertices[i]);
PyList_Insert(current, i, value);
Py_DECREF(value);
}
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++) {
PyList_Insert(current, i, PyLong_FromLong(cm.material_ids[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);
}
for (std::vector<tinyobj::material_t>::iterator mat = materials.begin();
mat != materials.end(); mat++) {
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) {
PyDict_SetItemString(unknown_parameter, p->first.c_str(),
PyUnicode_FromString(p->second.c_str()));
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);
}
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()));
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, "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);
PyList_Append(pymaterial_indices, PyUnicode_FromString(mat->name.c_str()));
Py_DECREF(matobj);
value = PyUnicode_FromString(mat->name.c_str());
PyList_Append(pymaterial_indices, value);
Py_DECREF(value);
}
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);

24
tests/issue-177.mtl Normal file
View File

@@ -0,0 +1,24 @@
newmtl white
Ka 0 0 0
Kd 1 1 1
Ks 0 0 0
newmtl red
Ka 0 0 0
Kd 1 0 0
Ks 0 0 0
newmtl green
Ka 0 0 0
Kd 0 1 0
Ks 0 0 0
newmtl blue
Ka 0 0 0
Kd 0 0 1
Ks 0 0 0
newmtl light
Ka 20 20 20
Kd 1 1 1
Ks 0 0 0

31
tests/issue-177.obj Normal file
View File

@@ -0,0 +1,31 @@
mtllib issue-177.mtl
v 0.000000 2.000000 2.000000
v 0.000000 0.000000 2.000000
v 2.000000 0.000000 2.000000
v 2.000000 2.000000 2.000000
v 0.000000 2.000000 0.000000
v 0.000000 0.000000 0.000000
v 2.000000 0.000000 0.000000
v 2.000000 2.000000 0.000000
# 8 vertices
g front cube
usemtl white
f 1 2 3 4
g back cube
# expects white material
f 8 7 6 5
g right cube
usemtl red
f 4 3 7 8
g top cube
usemtl white
f 5 1 4 8
g left cube
usemtl green
f 5 6 2 1
g bottom cube
usemtl white
f 2 6 7 3
# 6 elements

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff