Compare commits
139 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f37fed32f3 | ||
|
|
85d92bb5cb | ||
|
|
40d00fd935 | ||
|
|
7712f1bebd | ||
|
|
e96e994355 | ||
|
|
8f16866c37 | ||
|
|
5e668b398a | ||
|
|
df3bb6f8e8 | ||
|
|
546aa09d8d | ||
|
|
aa90b5466f | ||
|
|
b404f3af67 | ||
|
|
a957ebe002 | ||
|
|
d793bfb405 | ||
|
|
0ab0146296 | ||
|
|
20d122f305 | ||
|
|
850d0ffdfa | ||
|
|
80058fdcb0 | ||
|
|
1a7aea4ac1 | ||
|
|
178ef391c7 | ||
|
|
59b4d7ccef | ||
|
|
6f990e2b6c | ||
|
|
7fb5056a53 | ||
|
|
934788785e | ||
|
|
e07a835f02 | ||
|
|
1cdfd786d8 | ||
|
|
803b65b8a0 | ||
|
|
50518a515b | ||
|
|
c9b1bccf97 | ||
|
|
1f17833657 | ||
|
|
bb58a8f8c3 | ||
|
|
b1f594d682 | ||
|
|
02df4943f9 | ||
|
|
826a892d0b | ||
|
|
68350e2fc7 | ||
|
|
7d20e9b901 | ||
|
|
fdc70abdc6 | ||
|
|
8a885e14b8 | ||
|
|
cd65de860b | ||
|
|
4924857fd3 | ||
|
|
1a7bdc6192 | ||
|
|
fd06fa49e4 | ||
|
|
0d68262246 | ||
|
|
0e950513a3 | ||
|
|
a4b115a584 | ||
|
|
bb3e27d4f3 | ||
|
|
c4e7e65acb | ||
|
|
eba327b9c0 | ||
|
|
3cbf45a572 | ||
|
|
6650dbf397 | ||
|
|
7befd59de4 | ||
|
|
c5b3139653 | ||
|
|
adb2309110 | ||
|
|
3edca81a75 | ||
|
|
7a88cddefc | ||
|
|
d541711a79 | ||
|
|
d1ce2082f6 | ||
|
|
8fd9f6e57b | ||
|
|
24bd8b49ff | ||
|
|
4a0e79985d | ||
|
|
06f6139d1f | ||
|
|
0c8db8ee23 | ||
|
|
64d1e3f883 | ||
|
|
a39a6b481c | ||
|
|
bce1bb8387 | ||
|
|
7fcfafb39a | ||
|
|
5eda671225 | ||
|
|
f95510b04b | ||
|
|
94f1dc15b3 | ||
|
|
a4eabf54b1 | ||
|
|
72c38f64d5 | ||
|
|
d174e625a2 | ||
|
|
707014f843 | ||
|
|
12837cc8b2 | ||
|
|
b85714b4cf | ||
|
|
e060b4f4aa | ||
|
|
2dca72724f | ||
|
|
72529f02fe | ||
|
|
16f3041c76 | ||
|
|
73e9b4dc3a | ||
|
|
5113cd65cf | ||
|
|
65df7c4794 | ||
|
|
eaf8623e61 | ||
|
|
dcad3e6c50 | ||
|
|
2dfc37a475 | ||
|
|
9a6390cdee | ||
|
|
78c7c9011a | ||
|
|
1754669b07 | ||
|
|
1f7b4a49c0 | ||
|
|
15f47e2e35 | ||
|
|
12bf4165be | ||
|
|
f206a56362 | ||
|
|
13951d6459 | ||
|
|
5383e3400a | ||
|
|
ac3c36ffda | ||
|
|
3b681805aa | ||
|
|
ca49183639 | ||
|
|
b29d34f2e1 | ||
|
|
cb085d1fb6 | ||
|
|
b38e97b7ec | ||
|
|
b434c2497f | ||
|
|
94fc413466 | ||
|
|
1c6dbf9bd8 | ||
|
|
88ad575f62 | ||
|
|
5cd30b70e0 | ||
|
|
eb1f395101 | ||
|
|
27bdd547f0 | ||
|
|
75a4bd1d35 | ||
|
|
7c7335c907 | ||
|
|
3a9483ca6f | ||
|
|
1065d7cfb2 | ||
|
|
303043f9ec | ||
|
|
981f7c5f99 | ||
|
|
dcbc8d51aa | ||
|
|
785af4491d | ||
|
|
be46318a52 | ||
|
|
b5961cd7b1 | ||
|
|
76632f80b3 | ||
|
|
f59f93d7dc | ||
|
|
1dfd117ccd | ||
|
|
a86b78386b | ||
|
|
e3508c3ca3 | ||
|
|
8e7da82852 | ||
|
|
6cde18eb55 | ||
|
|
2409832b24 | ||
|
|
95fba2ab32 | ||
|
|
99518b6d3e | ||
|
|
d8f702c6f7 | ||
|
|
6eca09f2bf | ||
|
|
a1324f17fd | ||
|
|
56fa047ba9 | ||
|
|
0f4a955e01 | ||
|
|
fb80e04212 | ||
|
|
cdb5c2d375 | ||
|
|
44bff466e5 | ||
|
|
47989b591f | ||
|
|
41acdc95bc | ||
|
|
cc948e4c44 | ||
|
|
889b2187c1 | ||
|
|
3e146c376c |
@@ -46,15 +46,13 @@ set(TINYOBJLOADER_PKGCONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
|
|||||||
set(TINYOBJLOADER_RUNTIME_DIR ${CMAKE_INSTALL_BINDIR})
|
set(TINYOBJLOADER_RUNTIME_DIR ${CMAKE_INSTALL_BINDIR})
|
||||||
|
|
||||||
option(TINYOBJLOADER_BUILD_TEST_LOADER "Build Example Loader Application" OFF)
|
option(TINYOBJLOADER_BUILD_TEST_LOADER "Build Example Loader Application" OFF)
|
||||||
option(TINYOBJLOADER_COMPILATION_SHARED "Build as shared library" OFF)
|
|
||||||
|
|
||||||
if(TINYOBJLOADER_COMPILATION_SHARED)
|
add_library(${LIBRARY_NAME} ${tinyobjloader-Source})
|
||||||
add_library(${LIBRARY_NAME} SHARED ${tinyobjloader-Source})
|
|
||||||
|
if(BUILD_SHARED_LIBS)
|
||||||
set_target_properties(${LIBRARY_NAME} PROPERTIES
|
set_target_properties(${LIBRARY_NAME} PROPERTIES
|
||||||
SOVERSION ${TINYOBJLOADER_SOVERSION}
|
SOVERSION ${TINYOBJLOADER_SOVERSION}
|
||||||
)
|
)
|
||||||
else()
|
|
||||||
add_library(${LIBRARY_NAME} STATIC ${tinyobjloader-Source})
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
set_target_properties(${LIBRARY_NAME} PROPERTIES VERSION ${TINYOBJLOADER_VERSION})
|
set_target_properties(${LIBRARY_NAME} PROPERTIES VERSION ${TINYOBJLOADER_VERSION})
|
||||||
@@ -120,6 +118,8 @@ install(TARGETS
|
|||||||
)
|
)
|
||||||
install(EXPORT
|
install(EXPORT
|
||||||
${PROJECT_NAME}-targets
|
${PROJECT_NAME}-targets
|
||||||
|
NAMESPACE
|
||||||
|
tinyobjloader::
|
||||||
DESTINATION
|
DESTINATION
|
||||||
${TINYOBJLOADER_CMAKE_DIR}
|
${TINYOBJLOADER_CMAKE_DIR}
|
||||||
)
|
)
|
||||||
|
|||||||
54
README.md
54
README.md
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
[](https://coveralls.io/github/syoyo/tinyobjloader?branch=master)
|
[](https://coveralls.io/github/syoyo/tinyobjloader?branch=master)
|
||||||
|
|
||||||
http://syoyo.github.io/tinyobjloader/
|
[https://github.com/syoyo/tinyobjloader](https://github.com/syoyo/tinyobjloader)
|
||||||
|
|
||||||
Tiny but powerful single file wavefront obj loader written in C++. No dependency except for C++ STL. It can parse over 10M polygons with moderate memory and time.
|
Tiny but powerful single file wavefront obj loader written in C++. No dependency except for C++ STL. It can parse over 10M polygons with moderate memory and time.
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ Old version is available `v0.9.x` branch https://github.com/syoyo/tinyobjloader/
|
|||||||
|
|
||||||
## What's new
|
## What's new
|
||||||
|
|
||||||
* 20 Aug, 2016 : Bump version v1.0.0. New data strcutre and API!
|
* 20 Aug, 2016 : Bump version v1.0.0. New data structure and API!
|
||||||
|
|
||||||
### Old version
|
### Old version
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ Previous old version is avaiable in `v0.9.x` branch.
|
|||||||

|

|
||||||
|
|
||||||
tinyobjloader can successfully load 6M triangles Rungholt scene.
|
tinyobjloader can successfully load 6M triangles Rungholt scene.
|
||||||
http://graphics.cs.williams.edu/data/meshes.xml
|
http://casual-effects.com/data/index.html
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
@@ -51,9 +51,14 @@ TinyObjLoader is successfully used in ...
|
|||||||
|
|
||||||
### New version(v1.0.x)
|
### New version(v1.0.x)
|
||||||
|
|
||||||
|
* Double precision support through `TINYOBJLOADER_USE_DOUBLE` thanks to noma
|
||||||
* Loading models in Vulkan Tutorial https://vulkan-tutorial.com/Loading_models
|
* Loading models in Vulkan Tutorial https://vulkan-tutorial.com/Loading_models
|
||||||
* .obj viewer with Metal https://github.com/middlefeng/NuoModelViewer/tree/master
|
* .obj viewer with Metal https://github.com/middlefeng/NuoModelViewer/tree/master
|
||||||
* Your project here!
|
* Vulkan Cookbook https://github.com/PacktPublishing/Vulkan-Cookbook
|
||||||
|
* cudabox: CUDA Solid Voxelizer Engine https://github.com/gaspardzoss/cudavox
|
||||||
|
* Drake: A planning, control, and analysis toolbox for nonlinear dynamical systems https://github.com/RobotLocomotion/drake
|
||||||
|
* 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)
|
### Old version(v0.9.x)
|
||||||
|
|
||||||
@@ -84,6 +89,7 @@ TinyObjLoader is successfully used in ...
|
|||||||
|
|
||||||
* Group(parse multiple group name)
|
* Group(parse multiple group name)
|
||||||
* Vertex
|
* Vertex
|
||||||
|
* Vertex color(as an extension: https://blender.stackexchange.com/questions/31997/how-can-i-get-vertex-painted-obj-files-to-import-into-blender)
|
||||||
* Texcoord
|
* Texcoord
|
||||||
* Normal
|
* Normal
|
||||||
* Material
|
* Material
|
||||||
@@ -91,6 +97,8 @@ TinyObjLoader is successfully used in ...
|
|||||||
* Crease tag('t'). This is OpenSubdiv specific(not in wavefront .obj specification)
|
* Crease tag('t'). This is OpenSubdiv specific(not in wavefront .obj specification)
|
||||||
* PBR material extension for .MTL. Its proposed here: http://exocortex.com/blog/extending_wavefront_mtl_to_support_pbr
|
* PBR material extension for .MTL. Its proposed here: http://exocortex.com/blog/extending_wavefront_mtl_to_support_pbr
|
||||||
* Callback API for custom loading.
|
* Callback API for custom loading.
|
||||||
|
* Double precision support(for HPC application).
|
||||||
|
* Smoothing group
|
||||||
|
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
@@ -98,8 +106,6 @@ TinyObjLoader is successfully used in ...
|
|||||||
* [ ] Fix obj_sticker example.
|
* [ ] Fix obj_sticker example.
|
||||||
* [ ] More unit test codes.
|
* [ ] More unit test codes.
|
||||||
* [x] Texture options
|
* [x] Texture options
|
||||||
* [ ] Normal vector generation
|
|
||||||
* [ ] Support smoothing groups
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
@@ -133,6 +139,13 @@ attrib_t::texcoords => 2 floats per vertex
|
|||||||
| u | v | u | v | u | v | u | v | .... | u | v |
|
| u | v | u | v | u | v | u | v | .... | u | v |
|
||||||
+-----------+-----------+-----------+-----------+ +-----------+
|
+-----------+-----------+-----------+-----------+ +-----------+
|
||||||
|
|
||||||
|
attrib_t::colors => 3 floats per vertex(vertex color. optional)
|
||||||
|
|
||||||
|
c[0] c[1] c[2] c[3] c[n-1]
|
||||||
|
+-----------+-----------+-----------+-----------+ +-----------+
|
||||||
|
| x | y | z | x | y | z | x | y | z | x | y | z | .... | x | y | z |
|
||||||
|
+-----------+-----------+-----------+-----------+ +-----------+
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Each `shape_t::mesh_t` does not contain vertex data but contains array index to `attrib_t`.
|
Each `shape_t::mesh_t` does not contain vertex data but contains array index to `attrib_t`.
|
||||||
@@ -175,6 +188,12 @@ mesh_t::num_face_vertices => array of the number of vertices per face(e.g. 3 = t
|
|||||||
|
|
||||||
Note that when `triangulate` flas is true in `tinyobj::LoadObj()` argument, `num_face_vertices` are all filled with 3(triangle).
|
Note that when `triangulate` flas is true in `tinyobj::LoadObj()` argument, `num_face_vertices` are all filled with 3(triangle).
|
||||||
|
|
||||||
|
### float data type
|
||||||
|
|
||||||
|
TinyObjLoader now use `real_t` for floating point data type.
|
||||||
|
Default is `float(32bit)`.
|
||||||
|
You can enable `double(64bit)` precision by using `TINYOBJLOADER_USE_DOUBLE` define.
|
||||||
|
|
||||||
#### Example code
|
#### Example code
|
||||||
|
|
||||||
```c++
|
```c++
|
||||||
@@ -186,8 +205,9 @@ tinyobj::attrib_t attrib;
|
|||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
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.
|
if (!err.empty()) { // `err` may contain warning message.
|
||||||
std::cerr << err << std::endl;
|
std::cerr << err << std::endl;
|
||||||
@@ -208,14 +228,18 @@ for (size_t s = 0; s < shapes.size(); s++) {
|
|||||||
for (size_t v = 0; v < fv; v++) {
|
for (size_t v = 0; v < fv; v++) {
|
||||||
// access to vertex
|
// access to vertex
|
||||||
tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
|
tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
|
||||||
float vx = attrib.vertices[3*idx.vertex_index+0];
|
tinyobj::real_t vx = attrib.vertices[3*idx.vertex_index+0];
|
||||||
float vy = attrib.vertices[3*idx.vertex_index+1];
|
tinyobj::real_t vy = attrib.vertices[3*idx.vertex_index+1];
|
||||||
float vz = attrib.vertices[3*idx.vertex_index+2];
|
tinyobj::real_t vz = attrib.vertices[3*idx.vertex_index+2];
|
||||||
float nx = attrib.normals[3*idx.normal_index+0];
|
tinyobj::real_t nx = attrib.normals[3*idx.normal_index+0];
|
||||||
float ny = attrib.normals[3*idx.normal_index+1];
|
tinyobj::real_t ny = attrib.normals[3*idx.normal_index+1];
|
||||||
float nz = attrib.normals[3*idx.normal_index+2];
|
tinyobj::real_t nz = attrib.normals[3*idx.normal_index+2];
|
||||||
float tx = attrib.texcoords[2*idx.texcoord_index+0];
|
tinyobj::real_t tx = attrib.texcoords[2*idx.texcoord_index+0];
|
||||||
float ty = attrib.texcoords[2*idx.texcoord_index+1];
|
tinyobj::real_t ty = attrib.texcoords[2*idx.texcoord_index+1];
|
||||||
|
// Optional: vertex colors
|
||||||
|
// tinyobj::real_t red = attrib.colors[3*idx.vertex_index+0];
|
||||||
|
// tinyobj::real_t green = attrib.colors[3*idx.vertex_index+1];
|
||||||
|
// tinyobj::real_t blue = attrib.colors[3*idx.vertex_index+2];
|
||||||
}
|
}
|
||||||
index_offset += fv;
|
index_offset += fv;
|
||||||
|
|
||||||
|
|||||||
@@ -129,6 +129,7 @@ int main(int argc, char **argv) {
|
|||||||
cb.object_cb = object_cb;
|
cb.object_cb = object_cb;
|
||||||
|
|
||||||
MyMesh mesh;
|
MyMesh mesh;
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
std::string filename = "../../models/cornell_box.obj";
|
std::string filename = "../../models/cornell_box.obj";
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
@@ -143,7 +144,11 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
tinyobj::MaterialFileReader mtlReader("../../models/");
|
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()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << err << std::endl;
|
||||||
|
|||||||
@@ -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 "obj_writer.h"
|
||||||
|
|
||||||
|
#include "../../tiny_obj_loader.h"
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
@@ -11,27 +12,59 @@
|
|||||||
|
|
||||||
typedef std::vector<tinyobj::shape_t> Shape;
|
typedef std::vector<tinyobj::shape_t> Shape;
|
||||||
typedef std::vector<tinyobj::material_t> Material;
|
typedef std::vector<tinyobj::material_t> Material;
|
||||||
|
typedef tinyobj::attrib_t Attribute;
|
||||||
|
|
||||||
void
|
void
|
||||||
StichObjs(
|
StichObjs(
|
||||||
|
tinyobj::attrib_t& out_attribute,
|
||||||
std::vector<tinyobj::shape_t>& out_shape,
|
std::vector<tinyobj::shape_t>& out_shape,
|
||||||
std::vector<tinyobj::material_t>& out_material,
|
std::vector<tinyobj::material_t>& out_material,
|
||||||
|
const std::vector<Attribute>& attributes,
|
||||||
const std::vector<Shape>& shapes,
|
const std::vector<Shape>& shapes,
|
||||||
const std::vector<Material>& materials)
|
const std::vector<Material>& materials)
|
||||||
{
|
{
|
||||||
int numShapes = 0;
|
// The amount of attributes, shape-vectors and material-vecotrs should be the same.
|
||||||
for (size_t i = 0; i < shapes.size(); i++) {
|
if(attributes.size() != shapes.size() && attributes.size() != materials.size()){
|
||||||
numShapes += (int)shapes[i].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);
|
// More performant, than push_back
|
||||||
int materialIdOffset = 0;
|
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++) {
|
for (size_t i = 0; i < shapes.size(); i++) {
|
||||||
|
|
||||||
|
// Copy shapes
|
||||||
for (size_t k = 0; k < shapes[i].size(); k++) {
|
for (size_t k = 0; k < shapes[i].size(); k++) {
|
||||||
|
|
||||||
std::string new_name = shapes[i][k].name;
|
std::string new_name = shapes[i][k].name;
|
||||||
// Add suffix
|
// Add suffix
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
@@ -39,36 +72,51 @@ StichObjs(
|
|||||||
new_name += std::string(buf);
|
new_name += std::string(buf);
|
||||||
|
|
||||||
printf("shape[%ld][%ld].name = %s\n", i, k, shapes[i][k].name.c_str());
|
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];
|
tinyobj::shape_t new_shape = shapes[i][k];
|
||||||
// Add offset.
|
// Add material offset.
|
||||||
for(size_t f = 0; f < new_shape.mesh.material_ids.size(); f++) {
|
for(size_t f = 0; f < new_shape.mesh.material_ids.size(); f++) {
|
||||||
new_shape.mesh.material_ids[f] += materialIdOffset;
|
new_shape.mesh.material_ids[f] += material_id_offset;
|
||||||
|
}
|
||||||
|
// Add indices offset.
|
||||||
|
for(size_t f = 0; f < new_shape.mesh.indices.size(); f++){
|
||||||
|
tinyobj::index_t& ref = new_shape.mesh.indices[f];
|
||||||
|
if(ref.vertex_index > -1){
|
||||||
|
ref.vertex_index += vertex_idx_offset;
|
||||||
|
}
|
||||||
|
if(ref.normal_index > -1){
|
||||||
|
ref.normal_index += normal_idx_offset;
|
||||||
|
}
|
||||||
|
if(ref.texcoord_index > -1){
|
||||||
|
ref.texcoord_index += texcoord_idx_offset;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
new_shape.name = new_name;
|
new_shape.name = new_name;
|
||||||
printf("shape[%ld][%ld].new_name = %s\n", i, k, new_shape.name.c_str());
|
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();
|
// Copy materials
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t i = 0; i < materials.size(); i++) {
|
|
||||||
for (size_t k = 0; k < materials[i].size(); k++) {
|
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) {
|
if (argc < 3) {
|
||||||
printf("Usage: obj_sticher input0.obj input1.obj ... output.obj\n");
|
printf("Usage: obj_sticher input0.obj input1.obj ... output.obj\n");
|
||||||
@@ -78,16 +126,16 @@ main(
|
|||||||
int num_objfiles = argc - 2;
|
int num_objfiles = argc - 2;
|
||||||
std::string out_filename = std::string(argv[argc-1]); // last element
|
std::string out_filename = std::string(argv[argc-1]); // last element
|
||||||
|
|
||||||
std::vector<Shape> shapes;
|
std::vector<Attribute> attributes(num_objfiles);
|
||||||
std::vector<Material> materials;
|
std::vector<Shape> shapes(num_objfiles);
|
||||||
shapes.resize(num_objfiles);
|
std::vector<Material> materials(num_objfiles);
|
||||||
materials.resize(num_objfiles);
|
|
||||||
|
|
||||||
for (int i = 0; i < num_objfiles; i++) {
|
for (int i = 0; i < num_objfiles; i++) {
|
||||||
std::cout << "Loading " << argv[i+1] << " ... " << std::flush;
|
std::cout << "Loading " << argv[i+1] << " ... " << std::flush;
|
||||||
|
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
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()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << err << std::endl;
|
||||||
}
|
}
|
||||||
@@ -98,12 +146,13 @@ main(
|
|||||||
std::cout << "DONE." << std::endl;
|
std::cout << "DONE." << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<tinyobj::shape_t> out_shape;
|
Attribute out_attribute;
|
||||||
std::vector<tinyobj::material_t> out_material;
|
Shape out_shape;
|
||||||
StichObjs(out_shape, out_material, shapes, materials);
|
Material out_material;
|
||||||
|
StichObjs(out_attribute, out_shape, out_material, attributes, shapes, materials);
|
||||||
|
|
||||||
bool coordTransform = true;
|
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);
|
assert(ret);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ bool WriteMat(const std::string& filename, const std::vector<tinyobj::material_t
|
|||||||
fprintf(fp, "Ke %f %f %f\n", mat.emission[0], mat.emission[1], mat.emission[2]);
|
fprintf(fp, "Ke %f %f %f\n", mat.emission[0], mat.emission[1], mat.emission[2]);
|
||||||
fprintf(fp, "Ns %f\n", mat.shininess);
|
fprintf(fp, "Ns %f\n", mat.shininess);
|
||||||
fprintf(fp, "Ni %f\n", mat.ior);
|
fprintf(fp, "Ni %f\n", mat.ior);
|
||||||
|
fprintf(fp, "illum %d\n", mat.illum);
|
||||||
|
fprintf(fp, "\n");
|
||||||
// @todo { texture }
|
// @todo { texture }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,7 +40,7 @@ bool WriteMat(const std::string& filename, const std::vector<tinyobj::material_t
|
|||||||
return true;
|
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");
|
FILE* fp = fopen(filename.c_str(), "w");
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
fprintf(stderr, "Failed to open file [ %s ] for write.\n", filename.c_str());
|
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 basename = GetFileBasename(filename);
|
||||||
std::string material_filename = basename + ".mtl";
|
std::string material_filename = basename + ".mtl";
|
||||||
|
|
||||||
int v_offset = 0;
|
|
||||||
int vn_offset = 0;
|
|
||||||
int vt_offset = 0;
|
|
||||||
int prev_material_id = -1;
|
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++) {
|
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()) {
|
if (shapes[i].name.empty()) {
|
||||||
fprintf(fp, "g Unknown\n");
|
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());
|
fprintf(fp, "g %s\n", shapes[i].name.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
//if (!shapes[i].material.name.empty()) {
|
bool has_vn = false;
|
||||||
// fprintf(fp, "usemtl %s\n", shapes[i].material.name.c_str());
|
bool has_vt = false;
|
||||||
//}
|
// Assumes normals and textures are set shape-wise.
|
||||||
|
if(shapes[i].mesh.indices.size() > 0){
|
||||||
// facevarying vtx
|
has_vn = shapes[i].mesh.indices[0].normal_index != -1;
|
||||||
for (size_t k = 0; k < shapes[i].mesh.indices.size() / 3; k++) {
|
has_vt = shapes[i].mesh.indices[0].texcoord_index != -1;
|
||||||
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
|
// face
|
||||||
for (size_t k = 0; k < shapes[i].mesh.indices.size() / 3; 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++]) {
|
||||||
// Face index is 1-base.
|
// Check Materials
|
||||||
//int v0 = shapes[i].mesh.indices[3*k+0] + 1 + v_offset;
|
int material_id = shapes[i].mesh.material_ids[face_index];
|
||||||
//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) {
|
if (material_id != prev_material_id) {
|
||||||
std::string material_name = materials[material_id].name;
|
std::string material_name = materials[material_id].name;
|
||||||
fprintf(fp, "usemtl %s\n", material_name.c_str());
|
fprintf(fp, "usemtl %s\n", material_name.c_str());
|
||||||
prev_material_id = material_id;
|
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){
|
if(has_vn && has_vt){
|
||||||
fprintf(fp, "f %d/%d/%d %d/%d/%d %d/%d/%d\n",
|
// v0/t0/vn0
|
||||||
v0, vt0, v0, v1, vt1, v1, v2, vt2, v2);
|
fprintf(fp, " %d/%d/%d", ref.vertex_index + 1, ref.texcoord_index + 1, ref.normal_index + 1);
|
||||||
} else if (has_vn && !has_vt) {
|
continue;
|
||||||
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);
|
fclose(fp);
|
||||||
@@ -172,5 +162,3 @@ bool WriteObj(const std::string& filename, const std::vector<tinyobj::shape_t>&
|
|||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
#include "../../tiny_obj_loader.h"
|
#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__
|
#endif // __OBJ_WRITER_H__
|
||||||
|
|||||||
@@ -6,7 +6,6 @@
|
|||||||
* glfw3
|
* glfw3
|
||||||
* glew
|
* glew
|
||||||
|
|
||||||
|
|
||||||
## Build on MaCOSX
|
## Build on MaCOSX
|
||||||
|
|
||||||
Install glfw3 and glew using brew.
|
Install glfw3 and glew using brew.
|
||||||
@@ -35,3 +34,9 @@ Put glfw3 and glew library somewhere and replace include and lib path in `premak
|
|||||||
Then,
|
Then,
|
||||||
|
|
||||||
> premake5.exe vs2013
|
> premake5.exe vs2013
|
||||||
|
|
||||||
|
## TODO
|
||||||
|
|
||||||
|
* [ ] Support per-face material.
|
||||||
|
* [ ] Use shader-based GL rendering.
|
||||||
|
* [ ] PBR shader support.
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ class timerutil {
|
|||||||
};
|
};
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
GLuint vb; // vertex buffer
|
GLuint vb_id; // vertex buffer id
|
||||||
int numTriangles;
|
int numTriangles;
|
||||||
size_t material_id;
|
size_t material_id;
|
||||||
} DrawObject;
|
} DrawObject;
|
||||||
@@ -191,9 +191,101 @@ static void CalcNormal(float N[3], float v0[3], float v1[3], float v2[3]) {
|
|||||||
|
|
||||||
N[0] /= len;
|
N[0] /= len;
|
||||||
N[1] /= 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],
|
static bool LoadObjAndConvert(float bmin[3], float bmax[3],
|
||||||
std::vector<DrawObject>* drawObjects,
|
std::vector<DrawObject>* drawObjects,
|
||||||
std::vector<tinyobj::material_t>& materials,
|
std::vector<tinyobj::material_t>& materials,
|
||||||
@@ -207,15 +299,22 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
|
|||||||
tm.start();
|
tm.start();
|
||||||
|
|
||||||
std::string base_dir = GetBaseDir(filename);
|
std::string base_dir = GetBaseDir(filename);
|
||||||
|
if (base_dir.empty()) {
|
||||||
|
base_dir = ".";
|
||||||
|
}
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
base_dir += "\\";
|
base_dir += "\\";
|
||||||
#else
|
#else
|
||||||
base_dir += "/";
|
base_dir += "/";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret =
|
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename,
|
||||||
tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename, base_dir.c_str());
|
base_dir.c_str());
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << err << std::endl;
|
||||||
}
|
}
|
||||||
@@ -238,6 +337,11 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
|
|||||||
// Append `default` material
|
// Append `default` material
|
||||||
materials.push_back(tinyobj::material_t());
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
// Load diffuse textures
|
// Load diffuse textures
|
||||||
{
|
{
|
||||||
for (size_t m = 0; m < materials.size(); m++) {
|
for (size_t m = 0; m < materials.size(); m++) {
|
||||||
@@ -255,25 +359,34 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
|
|||||||
// Append base dir.
|
// Append base dir.
|
||||||
texture_filename = base_dir + mp->diffuse_texname;
|
texture_filename = base_dir + mp->diffuse_texname;
|
||||||
if (!FileExists(texture_filename)) {
|
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);
|
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) {
|
if (!image) {
|
||||||
std::cerr << "Unable to load texture: " << texture_filename << std::endl;
|
std::cerr << "Unable to load texture: " << texture_filename
|
||||||
|
<< std::endl;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
std::cout << "Loaded texture: " << texture_filename << ", w = " << w
|
||||||
|
<< ", h = " << h << ", comp = " << comp << std::endl;
|
||||||
|
|
||||||
glGenTextures(1, &texture_id);
|
glGenTextures(1, &texture_id);
|
||||||
glBindTexture(GL_TEXTURE_2D, texture_id);
|
glBindTexture(GL_TEXTURE_2D, texture_id);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
if (comp == 3) {
|
if (comp == 3) {
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
|
||||||
}
|
GL_UNSIGNED_BYTE, image);
|
||||||
else if (comp == 4) {
|
} 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_RGBA, w, h, 0, GL_RGBA,
|
||||||
|
GL_UNSIGNED_BYTE, image);
|
||||||
|
} else {
|
||||||
|
assert(0); // TODO
|
||||||
}
|
}
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
stbi_image_free(image);
|
stbi_image_free(image);
|
||||||
@@ -289,7 +402,15 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
|
|||||||
{
|
{
|
||||||
for (size_t s = 0; s < shapes.size(); s++) {
|
for (size_t s = 0; s < shapes.size(); s++) {
|
||||||
DrawObject o;
|
DrawObject o;
|
||||||
std::vector<float> vb; // pos(3float), normal(3float), color(3float)
|
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++) {
|
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 idx0 = shapes[s].mesh.indices[3 * f + 0];
|
||||||
tinyobj::index_t idx1 = shapes[s].mesh.indices[3 * f + 1];
|
tinyobj::index_t idx1 = shapes[s].mesh.indices[3 * f + 1];
|
||||||
@@ -297,12 +418,16 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
|
|||||||
|
|
||||||
int current_material_id = shapes[s].mesh.material_ids[f];
|
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.
|
// 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()) {
|
// 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];
|
float diffuse[3];
|
||||||
@@ -311,15 +436,31 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
|
|||||||
}
|
}
|
||||||
float tc[3][2];
|
float tc[3][2];
|
||||||
if (attrib.texcoords.size() > 0) {
|
if (attrib.texcoords.size() > 0) {
|
||||||
assert(attrib.texcoords.size() > 2 * idx0.texcoord_index + 1);
|
if ((idx0.texcoord_index < 0) || (idx1.texcoord_index < 0) ||
|
||||||
assert(attrib.texcoords.size() > 2 * idx1.texcoord_index + 1);
|
(idx2.texcoord_index < 0)) {
|
||||||
assert(attrib.texcoords.size() > 2 * idx2.texcoord_index + 1);
|
// 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));
|
||||||
|
|
||||||
|
// Flip Y coord.
|
||||||
tc[0][0] = attrib.texcoords[2 * idx0.texcoord_index];
|
tc[0][0] = attrib.texcoords[2 * idx0.texcoord_index];
|
||||||
tc[0][1] = 1.0f - attrib.texcoords[2 * idx0.texcoord_index + 1];
|
tc[0][1] = 1.0f - attrib.texcoords[2 * idx0.texcoord_index + 1];
|
||||||
tc[1][0] = attrib.texcoords[2 * idx1.texcoord_index];
|
tc[1][0] = attrib.texcoords[2 * idx1.texcoord_index];
|
||||||
tc[1][1] = 1.0f - attrib.texcoords[2 * idx1.texcoord_index + 1];
|
tc[1][1] = 1.0f - attrib.texcoords[2 * idx1.texcoord_index + 1];
|
||||||
tc[2][0] = attrib.texcoords[2 * idx2.texcoord_index];
|
tc[2][0] = attrib.texcoords[2 * idx2.texcoord_index];
|
||||||
tc[2][1] = 1.0f - attrib.texcoords[2 * idx2.texcoord_index + 1];
|
tc[2][1] = 1.0f - attrib.texcoords[2 * idx2.texcoord_index + 1];
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
tc[0][0] = 0.0f;
|
tc[0][0] = 0.0f;
|
||||||
tc[0][1] = 0.0f;
|
tc[0][1] = 0.0f;
|
||||||
@@ -350,19 +491,54 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
|
|||||||
}
|
}
|
||||||
|
|
||||||
float n[3][3];
|
float n[3][3];
|
||||||
|
{
|
||||||
|
bool invalid_normal_index = false;
|
||||||
if (attrib.normals.size() > 0) {
|
if (attrib.normals.size() > 0) {
|
||||||
int f0 = idx0.normal_index;
|
int nf0 = idx0.normal_index;
|
||||||
int f1 = idx1.normal_index;
|
int nf1 = idx1.normal_index;
|
||||||
int f2 = idx2.normal_index;
|
int nf2 = idx2.normal_index;
|
||||||
assert(f0 >= 0);
|
|
||||||
assert(f1 >= 0);
|
if ((nf0 < 0) || (nf1 < 0) || (nf2 < 0)) {
|
||||||
assert(f2 >= 0);
|
// normal index is missing from this face.
|
||||||
|
invalid_normal_index = true;
|
||||||
|
} else {
|
||||||
for (int k = 0; k < 3; k++) {
|
for (int k = 0; k < 3; k++) {
|
||||||
n[0][k] = attrib.normals[3 * f0 + k];
|
assert(size_t(3 * nf0 + k) < attrib.normals.size());
|
||||||
n[1][k] = attrib.normals[3 * f1 + k];
|
assert(size_t(3 * nf1 + k) < attrib.normals.size());
|
||||||
n[2][k] = attrib.normals[3 * f2 + k];
|
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];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} 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
|
// compute geometric normal
|
||||||
CalcNormal(n[0], v[0], v[1], v[2]);
|
CalcNormal(n[0], v[0], v[1], v[2]);
|
||||||
n[1][0] = n[0][0];
|
n[1][0] = n[0][0];
|
||||||
@@ -372,22 +548,21 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
|
|||||||
n[2][1] = n[0][1];
|
n[2][1] = n[0][1];
|
||||||
n[2][2] = n[0][2];
|
n[2][2] = n[0][2];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (int k = 0; k < 3; k++) {
|
for (int k = 0; k < 3; k++) {
|
||||||
vb.push_back(v[k][0]);
|
buffer.push_back(v[k][0]);
|
||||||
vb.push_back(v[k][1]);
|
buffer.push_back(v[k][1]);
|
||||||
vb.push_back(v[k][2]);
|
buffer.push_back(v[k][2]);
|
||||||
vb.push_back(n[k][0]);
|
buffer.push_back(n[k][0]);
|
||||||
vb.push_back(n[k][1]);
|
buffer.push_back(n[k][1]);
|
||||||
vb.push_back(n[k][2]);
|
buffer.push_back(n[k][2]);
|
||||||
// Combine normal and diffuse to get color.
|
// Combine normal and diffuse to get color.
|
||||||
float normal_factor = 0.2;
|
float normal_factor = 0.2;
|
||||||
float diffuse_factor = 1 - normal_factor;
|
float diffuse_factor = 1 - normal_factor;
|
||||||
float c[3] = {
|
float c[3] = {n[k][0] * normal_factor + diffuse[0] * diffuse_factor,
|
||||||
n[k][0] * normal_factor + diffuse[0] * diffuse_factor,
|
|
||||||
n[k][1] * normal_factor + diffuse[1] * 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];
|
float len2 = c[0] * c[0] + c[1] * c[1] + c[2] * c[2];
|
||||||
if (len2 > 0.0f) {
|
if (len2 > 0.0f) {
|
||||||
float len = sqrtf(len2);
|
float len = sqrtf(len2);
|
||||||
@@ -396,32 +571,36 @@ static bool LoadObjAndConvert(float bmin[3], float bmax[3],
|
|||||||
c[1] /= len;
|
c[1] /= len;
|
||||||
c[2] /= len;
|
c[2] /= len;
|
||||||
}
|
}
|
||||||
vb.push_back(c[0] * 0.5 + 0.5);
|
buffer.push_back(c[0] * 0.5 + 0.5);
|
||||||
vb.push_back(c[1] * 0.5 + 0.5);
|
buffer.push_back(c[1] * 0.5 + 0.5);
|
||||||
vb.push_back(c[2] * 0.5 + 0.5);
|
buffer.push_back(c[2] * 0.5 + 0.5);
|
||||||
|
|
||||||
vb.push_back(tc[k][0]);
|
buffer.push_back(tc[k][0]);
|
||||||
vb.push_back(tc[k][1]);
|
buffer.push_back(tc[k][1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
o.vb = 0;
|
o.vb_id = 0;
|
||||||
o.numTriangles = 0;
|
o.numTriangles = 0;
|
||||||
|
|
||||||
// OpenGL viewer does not support texturing with per-face material.
|
// 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) {
|
if (shapes[s].mesh.material_ids.size() > 0 &&
|
||||||
// Base case
|
shapes[s].mesh.material_ids.size() > s) {
|
||||||
o.material_id = shapes[s].mesh.material_ids[s];
|
o.material_id = shapes[s].mesh.material_ids[0]; // use the material ID
|
||||||
|
// of the first face.
|
||||||
} else {
|
} else {
|
||||||
o.material_id = materials.size() - 1; // = ID for default material.
|
o.material_id = materials.size() - 1; // = ID for default material.
|
||||||
}
|
}
|
||||||
|
printf("shape[%d] material_id %d\n", int(s), int(o.material_id));
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
if (vb.size() > 0) {
|
|
||||||
glGenBuffers(1, &o.vb);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, o.vb);
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, vb.size() * sizeof(float), &vb.at(0),
|
|
||||||
GL_STATIC_DRAW);
|
|
||||||
o.numTriangles = vb.size() / (3 + 3 + 3 + 2) * 3;
|
|
||||||
printf("shape[%d] # of triangles = %d\n", static_cast<int>(s),
|
printf("shape[%d] # of triangles = %d\n", static_cast<int>(s),
|
||||||
o.numTriangles);
|
o.numTriangles);
|
||||||
}
|
}
|
||||||
@@ -535,7 +714,9 @@ static void motionFunc(GLFWwindow* window, double mouse_x, double mouse_y) {
|
|||||||
prevMouseY = 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_FRONT, GL_FILL);
|
||||||
glPolygonMode(GL_BACK, GL_FILL);
|
glPolygonMode(GL_BACK, GL_FILL);
|
||||||
|
|
||||||
@@ -544,16 +725,17 @@ static void Draw(const std::vector<DrawObject>& drawObjects, std::vector<tinyobj
|
|||||||
GLsizei stride = (3 + 3 + 3 + 2) * sizeof(float);
|
GLsizei stride = (3 + 3 + 3 + 2) * sizeof(float);
|
||||||
for (size_t i = 0; i < drawObjects.size(); i++) {
|
for (size_t i = 0; i < drawObjects.size(); i++) {
|
||||||
DrawObject o = drawObjects[i];
|
DrawObject o = drawObjects[i];
|
||||||
if (o.vb < 1) {
|
if (o.vb_id < 1) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, o.vb);
|
glBindBuffer(GL_ARRAY_BUFFER, o.vb_id);
|
||||||
glEnableClientState(GL_VERTEX_ARRAY);
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
glEnableClientState(GL_NORMAL_ARRAY);
|
glEnableClientState(GL_NORMAL_ARRAY);
|
||||||
glEnableClientState(GL_COLOR_ARRAY);
|
glEnableClientState(GL_COLOR_ARRAY);
|
||||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
if ((o.material_id < materials.size())) {
|
if ((o.material_id < materials.size())) {
|
||||||
std::string diffuse_texname = materials[o.material_id].diffuse_texname;
|
std::string diffuse_texname = materials[o.material_id].diffuse_texname;
|
||||||
if (textures.find(diffuse_texname) != textures.end()) {
|
if (textures.find(diffuse_texname) != textures.end()) {
|
||||||
@@ -578,11 +760,11 @@ static void Draw(const std::vector<DrawObject>& drawObjects, std::vector<tinyobj
|
|||||||
glColor3f(0.0f, 0.0f, 0.4f);
|
glColor3f(0.0f, 0.0f, 0.4f);
|
||||||
for (size_t i = 0; i < drawObjects.size(); i++) {
|
for (size_t i = 0; i < drawObjects.size(); i++) {
|
||||||
DrawObject o = drawObjects[i];
|
DrawObject o = drawObjects[i];
|
||||||
if (o.vb < 1) {
|
if (o.vb_id < 1) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, o.vb);
|
glBindBuffer(GL_ARRAY_BUFFER, o.vb_id);
|
||||||
glEnableClientState(GL_VERTEX_ARRAY);
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
glEnableClientState(GL_NORMAL_ARRAY);
|
glEnableClientState(GL_NORMAL_ARRAY);
|
||||||
glDisableClientState(GL_COLOR_ARRAY);
|
glDisableClientState(GL_COLOR_ARRAY);
|
||||||
@@ -653,7 +835,8 @@ int main(int argc, char** argv) {
|
|||||||
float bmin[3], bmax[3];
|
float bmin[3], bmax[3];
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
std::map<std::string, GLuint> textures;
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ bool Voxelize(const char* filename, float voxelsizex, float voxelsizey, float vo
|
|||||||
tinyobj::attrib_t attrib;
|
tinyobj::attrib_t attrib;
|
||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
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()) {
|
if (!err.empty()) {
|
||||||
printf("err: %s\n", err.c_str());
|
printf("err: %s\n", err.c_str());
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
Copyright (c) 2013, Alexander Tretyak
|
|
||||||
Copyright (c) 2015, r-lyeh
|
|
||||||
All rights reserved.
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions
|
|
||||||
are met:
|
|
||||||
* Redistributions of source code must retain the above copyright
|
|
||||||
notice, this list of conditions and the following disclaimer.
|
|
||||||
* Redistributions in binary form must reproduce the above
|
|
||||||
copyright notice, this list of conditions and the following disclaimer
|
|
||||||
in the documentation and/or other materials provided with the
|
|
||||||
distribution.
|
|
||||||
* Neither the name of the author nor the names of its
|
|
||||||
contributors may be used to endorse or promote products derived
|
|
||||||
from this software without specific prior written permission.
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
||||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
||||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
||||||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
||||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
||||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
||||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
||||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
@@ -1,5 +1,26 @@
|
|||||||
Experimental code for .obj loader.
|
# Experimental code for .obj loader.
|
||||||
|
|
||||||
* Multi-threaded optimized parser : tinyobj_loader_opt.h
|
* Multi-threaded optimized parser : tinyobj_loader_opt.h
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
* C++-11 compiler
|
||||||
|
|
||||||
|
## How to build
|
||||||
|
|
||||||
|
```
|
||||||
|
$ premak5 gmake
|
||||||
|
```
|
||||||
|
|
||||||
|
## Compile options
|
||||||
|
|
||||||
* zstd compressed .obj support. `--with-zstd` premake option.
|
* zstd compressed .obj support. `--with-zstd` premake option.
|
||||||
* gzip compressed .obj support. `--with-zlib` 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.
|
||||||
|
|||||||
89
experimental/lfpAlloc/Allocator.hpp
Normal file
89
experimental/lfpAlloc/Allocator.hpp
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
#ifndef LF_POOL_ALLOCATOR
|
||||||
|
#define LF_POOL_ALLOCATOR
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <thread>
|
||||||
|
#include <lfpAlloc/PoolDispatcher.hpp>
|
||||||
|
|
||||||
|
namespace lfpAlloc {
|
||||||
|
template <typename T, std::size_t NumPools = 70>
|
||||||
|
class lfpAllocator {
|
||||||
|
public:
|
||||||
|
using value_type = T;
|
||||||
|
using pointer = T*;
|
||||||
|
using const_pointer = const T*;
|
||||||
|
using reference = T&;
|
||||||
|
using const_reference = T const&;
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
struct rebind {
|
||||||
|
typedef lfpAllocator<U, NumPools> other;
|
||||||
|
};
|
||||||
|
|
||||||
|
lfpAllocator() {}
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
lfpAllocator(lfpAllocator<U, NumPools>&&) noexcept {}
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
lfpAllocator(const lfpAllocator<U, NumPools>&) noexcept {}
|
||||||
|
|
||||||
|
T* allocate(std::size_t count) {
|
||||||
|
if (sizeof(T) * count <=
|
||||||
|
alignof(std::max_align_t) * NumPools - sizeof(void*)) {
|
||||||
|
return reinterpret_cast<T*>(
|
||||||
|
dispatcher_.allocate(sizeof(T) * count));
|
||||||
|
} else {
|
||||||
|
return new T[count];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void deallocate(T* p, std::size_t count) noexcept {
|
||||||
|
if (sizeof(T) * count <=
|
||||||
|
alignof(std::max_align_t) * NumPools - sizeof(void*)) {
|
||||||
|
dispatcher_.deallocate(p, sizeof(T) * count);
|
||||||
|
} else {
|
||||||
|
delete[] p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should not be required, but allocator_traits is not complete in
|
||||||
|
// gcc 4.9.1
|
||||||
|
template <typename U>
|
||||||
|
void destroy(U* p) {
|
||||||
|
p->~U();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U, typename... Args>
|
||||||
|
void construct(U* p, Args&&... args) {
|
||||||
|
new (p) U(std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Ty, typename U, std::size_t N, std::size_t M>
|
||||||
|
friend bool operator==(const lfpAllocator<Ty, N>&,
|
||||||
|
const lfpAllocator<U, M>&) noexcept;
|
||||||
|
|
||||||
|
template <typename U, std::size_t M>
|
||||||
|
friend class lfpAllocator;
|
||||||
|
|
||||||
|
private:
|
||||||
|
static PoolDispatcher<NumPools> dispatcher_;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, std::size_t N>
|
||||||
|
PoolDispatcher<N> lfpAllocator<T, N>::dispatcher_;
|
||||||
|
|
||||||
|
template <typename T, typename U, std::size_t N, std::size_t M>
|
||||||
|
inline bool operator==(const lfpAllocator<T, N>&,
|
||||||
|
const lfpAllocator<U, M>&) noexcept {
|
||||||
|
return N == M;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename U, std::size_t N, std::size_t M>
|
||||||
|
inline bool operator!=(const lfpAllocator<T, N>& left,
|
||||||
|
const lfpAllocator<U, M>& right) noexcept {
|
||||||
|
return !(left == right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
116
experimental/lfpAlloc/ChunkList.hpp
Normal file
116
experimental/lfpAlloc/ChunkList.hpp
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
#ifndef LF_POOL_ALLOC_CHUNK_LIST
|
||||||
|
#define LF_POOL_ALLOC_CHUNK_LIST
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <atomic>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
#ifndef LFP_ALLOW_BLOCKING
|
||||||
|
static_assert(ATOMIC_POINTER_LOCK_FREE == 2,
|
||||||
|
"Atomic pointer is not lock-free.");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace lfpAlloc {
|
||||||
|
|
||||||
|
template <std::size_t Size>
|
||||||
|
struct Cell {
|
||||||
|
uint8_t val_[Size];
|
||||||
|
Cell* next_ = this + 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
// For small types (less than the size of void*), no additional
|
||||||
|
// space is needed, so union val_ with next_ to avoid overhead.
|
||||||
|
template <>
|
||||||
|
struct Cell<0> {
|
||||||
|
Cell() : next_{this + 1} {}
|
||||||
|
union {
|
||||||
|
uint8_t val_[sizeof(Cell*)];
|
||||||
|
Cell* next_;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
template <std::size_t Size, std::size_t AllocationsPerChunk>
|
||||||
|
struct Chunk {
|
||||||
|
Chunk() noexcept {
|
||||||
|
auto& last = memBlock_[AllocationsPerChunk - 1];
|
||||||
|
last.next_ = nullptr;
|
||||||
|
}
|
||||||
|
Cell<Size> memBlock_[AllocationsPerChunk];
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct Node {
|
||||||
|
Node() : val_(), next_(nullptr) {}
|
||||||
|
Node(const T& val) : val_(val), next_(nullptr) {}
|
||||||
|
T val_;
|
||||||
|
std::atomic<Node<T>*> next_;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <std::size_t Size, std::size_t AllocationsPerChunk>
|
||||||
|
class ChunkList {
|
||||||
|
static constexpr auto CellSize =
|
||||||
|
(Size > sizeof(void*)) ? Size - sizeof(void*) : 0;
|
||||||
|
using Chunk_t = Chunk<CellSize, AllocationsPerChunk>;
|
||||||
|
using Cell_t = Cell<CellSize>;
|
||||||
|
|
||||||
|
using ChunkNode = Node<Chunk_t>;
|
||||||
|
using CellNode = Node<Cell_t*>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static ChunkList& getInstance() {
|
||||||
|
static ChunkList c;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cell_t* allocateChain() {
|
||||||
|
CellNode* recentHead = head_.load();
|
||||||
|
CellNode* currentNext = nullptr;
|
||||||
|
do {
|
||||||
|
// If there are no available chains, allocate a new chunk
|
||||||
|
if (!recentHead) {
|
||||||
|
ChunkNode* currentHandle;
|
||||||
|
|
||||||
|
// Make a new node
|
||||||
|
auto newChunk = new ChunkNode();
|
||||||
|
|
||||||
|
// Add the chunk to the chain
|
||||||
|
do {
|
||||||
|
currentHandle = handle_.load();
|
||||||
|
newChunk->next_ = currentHandle;
|
||||||
|
} while (
|
||||||
|
!handle_.compare_exchange_weak(currentHandle, newChunk));
|
||||||
|
return &newChunk->val_.memBlock_[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
currentNext = recentHead->next_;
|
||||||
|
} while (!head_.compare_exchange_weak(recentHead, currentNext));
|
||||||
|
|
||||||
|
auto retnValue = recentHead->val_;
|
||||||
|
delete recentHead;
|
||||||
|
return retnValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
void deallocateChain(Cell_t* newCell) {
|
||||||
|
if (!newCell) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
CellNode* currentHead = head_.load();
|
||||||
|
|
||||||
|
// Construct a new node to be added to the linked list
|
||||||
|
CellNode* newHead = new CellNode(newCell);
|
||||||
|
|
||||||
|
// Add the chain to the linked list
|
||||||
|
do {
|
||||||
|
newHead->next_.store(currentHead, std::memory_order_release);
|
||||||
|
} while (!head_.compare_exchange_weak(currentHead, newHead));
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
ChunkList() : handle_(nullptr), head_(nullptr) {}
|
||||||
|
|
||||||
|
std::atomic<ChunkNode*> handle_;
|
||||||
|
std::atomic<CellNode*> head_;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
21
experimental/lfpAlloc/LICENSE
Normal file
21
experimental/lfpAlloc/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 Adam Schwalm
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
48
experimental/lfpAlloc/Pool.hpp
Normal file
48
experimental/lfpAlloc/Pool.hpp
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#ifndef LF_POOL_ALLOC_POOL
|
||||||
|
#define LF_POOL_ALLOC_POOL
|
||||||
|
|
||||||
|
#include <lfpAlloc/Utils.hpp>
|
||||||
|
#include <lfpAlloc/ChunkList.hpp>
|
||||||
|
|
||||||
|
namespace lfpAlloc {
|
||||||
|
template <std::size_t Size, std::size_t AllocationsPerChunk>
|
||||||
|
class Pool {
|
||||||
|
using ChunkList_t = ChunkList<Size, AllocationsPerChunk>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static constexpr auto CellSize =
|
||||||
|
(Size > sizeof(void*)) ? Size - sizeof(void*) : 0;
|
||||||
|
using Cell_t = Cell<CellSize>;
|
||||||
|
|
||||||
|
Pool() : head_(nullptr) {}
|
||||||
|
|
||||||
|
~Pool() { ChunkList_t::getInstance().deallocateChain(head_); }
|
||||||
|
|
||||||
|
void* allocate() {
|
||||||
|
// Head loaded from head_
|
||||||
|
Cell_t* currentHead = head_;
|
||||||
|
Cell_t* next;
|
||||||
|
|
||||||
|
// Out of cells to allocate
|
||||||
|
if (!currentHead) {
|
||||||
|
currentHead = ChunkList_t::getInstance().allocateChain();
|
||||||
|
}
|
||||||
|
|
||||||
|
next = currentHead->next_;
|
||||||
|
head_ = next;
|
||||||
|
return ¤tHead->val_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void deallocate(void* p) noexcept {
|
||||||
|
auto newHead = reinterpret_cast<Cell_t*>(p);
|
||||||
|
Cell_t* currentHead = head_;
|
||||||
|
newHead->next_ = currentHead;
|
||||||
|
head_ = newHead;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Cell_t* head_;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
79
experimental/lfpAlloc/PoolDispatcher.hpp
Normal file
79
experimental/lfpAlloc/PoolDispatcher.hpp
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
#ifndef LF_POOL_DISPATCHER
|
||||||
|
#define LF_POOL_DISPATCHER
|
||||||
|
|
||||||
|
#include <tuple>
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <lfpAlloc/Pool.hpp>
|
||||||
|
|
||||||
|
#ifndef LFP_ALLOCATIONS_PER_CHUNK
|
||||||
|
#define LFP_ALLOCATIONS_PER_CHUNK 64 * 100
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace lfpAlloc {
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
template <std::size_t Num, uint16_t... Ts>
|
||||||
|
struct Pools : Pools<Num - 1, alignof(std::max_align_t) * Num, Ts...> {};
|
||||||
|
|
||||||
|
template <uint16_t... Size>
|
||||||
|
struct Pools<0, Size...> {
|
||||||
|
using type = std::tuple<Pool<Size, LFP_ALLOCATIONS_PER_CHUNK>...>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::size_t NumPools>
|
||||||
|
class PoolDispatcher {
|
||||||
|
public:
|
||||||
|
void* allocate(std::size_t size) { return dispatchAllocate<0>(size); }
|
||||||
|
|
||||||
|
void deallocate(void* p, std::size_t size) noexcept {
|
||||||
|
dispatchDeallocate<0>(p, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
thread_local static typename detail::Pools<NumPools>::type pools_;
|
||||||
|
static_assert(NumPools > 0, "Invalid number of pools");
|
||||||
|
|
||||||
|
template <std::size_t Index>
|
||||||
|
typename std::enable_if <
|
||||||
|
Index<NumPools, void*>::type
|
||||||
|
dispatchAllocate(std::size_t const& requestSize) {
|
||||||
|
if (requestSize <= std::get<Index>(pools_).CellSize) {
|
||||||
|
return std::get<Index>(pools_).allocate();
|
||||||
|
} else {
|
||||||
|
return dispatchAllocate<Index + 1>(requestSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::size_t Index>
|
||||||
|
typename std::enable_if<!(Index < NumPools), void*>::type
|
||||||
|
dispatchAllocate(std::size_t const&) {
|
||||||
|
assert(false && "Invalid allocation size.");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::size_t Index>
|
||||||
|
typename std::enable_if <
|
||||||
|
Index<NumPools>::type
|
||||||
|
dispatchDeallocate(void* p, std::size_t const& requestSize) noexcept {
|
||||||
|
if (requestSize <= std::get<Index>(pools_).CellSize) {
|
||||||
|
std::get<Index>(pools_).deallocate(p);
|
||||||
|
} else {
|
||||||
|
dispatchDeallocate<Index + 1>(p, requestSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::size_t Index>
|
||||||
|
typename std::enable_if<!(Index < NumPools)>::type
|
||||||
|
dispatchDeallocate(void*, std::size_t const&) noexcept {
|
||||||
|
assert(false && "Invalid deallocation size.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <std::size_t NumPools>
|
||||||
|
thread_local typename detail::Pools<NumPools>::type
|
||||||
|
PoolDispatcher<NumPools>::pools_;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
20
experimental/lfpAlloc/Utils.hpp
Normal file
20
experimental/lfpAlloc/Utils.hpp
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace lfpAlloc {
|
||||||
|
namespace detail {
|
||||||
|
template <std::size_t Val, std::size_t base = 2>
|
||||||
|
struct Log {
|
||||||
|
enum { value = 1 + Log<Val / base, base>::value };
|
||||||
|
};
|
||||||
|
|
||||||
|
template <std::size_t base>
|
||||||
|
struct Log<1, base> {
|
||||||
|
enum { value = 0 };
|
||||||
|
};
|
||||||
|
|
||||||
|
template <std::size_t base>
|
||||||
|
struct Log<0, base> {
|
||||||
|
enum { value = 0 };
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,973 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright (c) 2013, Alexander Tretyak
|
|
||||||
Copyright (c) 2015, r-lyeh
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions
|
|
||||||
are met:
|
|
||||||
|
|
||||||
* Redistributions of source code must retain the above copyright
|
|
||||||
notice, this list of conditions and the following disclaimer.
|
|
||||||
* Redistributions in binary form must reproduce the above
|
|
||||||
copyright notice, this list of conditions and the following disclaimer
|
|
||||||
in the documentation and/or other materials provided with the
|
|
||||||
distribution.
|
|
||||||
* Neither the name of the author nor the names of its
|
|
||||||
contributors may be used to endorse or promote products derived
|
|
||||||
from this software without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
||||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
||||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
||||||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
||||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
||||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
||||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
||||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
|
|
||||||
|
|
||||||
Note: The latest version of this memory allocator obtainable at
|
|
||||||
http://ltalloc.googlecode.com/hg/ltalloc.cc
|
|
||||||
|
|
||||||
Project URL: http://code.google.com/p/ltalloc
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "ltalloc.h"
|
|
||||||
|
|
||||||
#define LTALLOC_VERSION "2.0.0" /* (2015/06/16) - ltcalloc(), ltmsize(), ltrealloc(), ltmemalign(), LTALLOC_AUTO_GC_INTERVAL
|
|
||||||
#define LTALLOC_VERSION "1.0.0" /* (2015/06/16) - standard STL allocator provided [see ltalloc.hpp file](ltalloc.hpp)
|
|
||||||
#define LTALLOC_VERSION "0.0.0" /* (2013/xx/xx) - fork from public repository */
|
|
||||||
|
|
||||||
//Customizable constants
|
|
||||||
//#define LTALLOC_DISABLE_OPERATOR_NEW_OVERRIDE
|
|
||||||
//#define LTALLOC_AUTO_GC_INTERVAL 3.0
|
|
||||||
#ifndef LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO
|
|
||||||
#define LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO 2//determines how accurately size classes are spaced (i.e. when = 0, allocation requests are rounded up to the nearest power of two (2^n), when = 1, rounded to 2^n, or (2^n)*1.5, when = 2, rounded to 2^n, (2^n)*1.25, (2^n)*1.5, or (2^n)*1.75, and so on); this parameter have direct influence on memory fragmentation - bigger values lead to reducing internal fragmentation (which can be approximately estimated as pow(0.5, VALUE)*100%), but at the same time increasing external fragmentation
|
|
||||||
#endif
|
|
||||||
#define CHUNK_SIZE (64*1024)//size of chunk (basic allocation unit for all allocations of size <= MAX_BLOCK_SIZE); must be a power of two (as well as all following parameters), also should not be less than allocation granularity on Windows (which is always 64K by design of NT kernel)
|
|
||||||
#define CACHE_LINE_SIZE 64
|
|
||||||
#define MAX_NUM_OF_BLOCKS_IN_BATCH 256//maximum number of blocks to move between a thread cache and a central cache in one shot
|
|
||||||
static const unsigned int MAX_BATCH_SIZE = 64*1024;//maximum total size of blocks to move between a thread cache and a central cache in one shot (corresponds to half size of thread cache of each size class)
|
|
||||||
static const unsigned int MAX_BLOCK_SIZE = CHUNK_SIZE;//requesting memory of any size greater than this value will lead to direct call of system virtual memory allocation routine
|
|
||||||
|
|
||||||
//Platform-specific stuff
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
#define CPPCODE(code) code
|
|
||||||
#include <new>
|
|
||||||
#else
|
|
||||||
#define CPPCODE(code)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef LTALLOC_AUTO_GC_INTERVAL
|
|
||||||
#include <time.h>
|
|
||||||
# if LTALLOC_AUTO_GC_INTERVAL <= 0
|
|
||||||
# undef LTALLOC_AUTO_GC_INTERVAL
|
|
||||||
# define LTALLOC_AUTO_GC_INTERVAL 3.00
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef __GNUC__
|
|
||||||
|
|
||||||
#define __STDC_LIMIT_MACROS
|
|
||||||
#include <stdint.h> //for SIZE_MAX
|
|
||||||
#include <limits.h> //for UINT_MAX
|
|
||||||
#define alignas(a) __attribute__((aligned(a)))
|
|
||||||
#define thread_local __thread
|
|
||||||
#define NOINLINE __attribute__((noinline))
|
|
||||||
#define CAS_LOCK(lock) __sync_lock_test_and_set(lock, 1)
|
|
||||||
#define SPINLOCK_RELEASE(lock) __sync_lock_release(lock)
|
|
||||||
#define PAUSE __asm__ __volatile__("pause" ::: "memory")
|
|
||||||
#define BSR(r, v) r = CODE3264(__builtin_clz(v) ^ 31, __builtin_clzll(v) ^ 63)//x ^ 31 = 31 - x, but gcc does not optimize 31 - __builtin_clz(x) to bsr(x), but generates 31 - (bsr(x) ^ 31)
|
|
||||||
|
|
||||||
#elif _MSC_VER
|
|
||||||
|
|
||||||
#define _ALLOW_KEYWORD_MACROS
|
|
||||||
#include <limits.h> //for SIZE_MAX and UINT_MAX
|
|
||||||
#define alignas(a) __declspec(align(a))
|
|
||||||
#define thread_local __declspec(thread)
|
|
||||||
#define NOINLINE __declspec(noinline)
|
|
||||||
#define CAS_LOCK(lock) _InterlockedExchange((long*)lock, 1)
|
|
||||||
#define SPINLOCK_RELEASE(lock) _InterlockedExchange((long*)lock, 0)
|
|
||||||
#define PAUSE _mm_pause()
|
|
||||||
#define BSR(r, v) CODE3264(_BitScanReverse, _BitScanReverse64)((unsigned long*)&r, v)
|
|
||||||
CPPCODE(extern "C") long _InterlockedExchange(long volatile *, long);
|
|
||||||
CPPCODE(extern "C") void _mm_pause();
|
|
||||||
#pragma warning(disable: 4127 4201 4324 4290)//"conditional expression is constant", "nonstandard extension used : nameless struct/union", and "structure was padded due to __declspec(align())"
|
|
||||||
|
|
||||||
#else
|
|
||||||
#error Unsupported compiler
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if __GNUC__ || __INTEL_COMPILER
|
|
||||||
#define likely(x) __builtin_expect(!!(x), 1)
|
|
||||||
#define unlikely(x) __builtin_expect(!!(x), 0)
|
|
||||||
#else
|
|
||||||
#define likely(x) (x)
|
|
||||||
#define unlikely(x) (x)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static void SPINLOCK_ACQUIRE(volatile int *lock) {if (CAS_LOCK(lock)) while (*lock || CAS_LOCK(lock)) PAUSE;}
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <string.h> //for memset
|
|
||||||
|
|
||||||
#if SIZE_MAX == UINT_MAX
|
|
||||||
#define CODE3264(c32, c64) c32
|
|
||||||
#else
|
|
||||||
#define CODE3264(c32, c64) c64
|
|
||||||
#endif
|
|
||||||
typedef char CODE3264_check[sizeof(void*) == CODE3264(4, 8) ? 1 : -1];
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#include <windows.h>
|
|
||||||
|
|
||||||
#define VMALLOC(size) VirtualAlloc(NULL, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE)
|
|
||||||
#define VMFREE(p, size) VirtualFree(p, 0, MEM_RELEASE)
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
#include <sys/mman.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#define VMALLOC(size) (void*)(((uintptr_t)mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0)+1)&~1)//with the conversion of MAP_FAILED to 0
|
|
||||||
#define VMFREE(p, size) munmap(p, size)
|
|
||||||
|
|
||||||
static size_t page_size()
|
|
||||||
{
|
|
||||||
assert((uintptr_t)MAP_FAILED+1 == 0);//have to use dynamic check somewhere, because some gcc versions (e.g. 4.4.5) won't compile typedef char MAP_FAILED_value_static_check[(uintptr_t)MAP_FAILED+1 == 0 ? 1 : -1];
|
|
||||||
static size_t pagesize = 0;
|
|
||||||
if (!pagesize) pagesize = sysconf(_SC_PAGE_SIZE);//assuming that writing of size_t value is atomic, so this can be done safely in different simultaneously running threads
|
|
||||||
return pagesize;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct PTrieNode // Compressed radix tree (patricia trie) for storing sizes of system allocations
|
|
||||||
{ // This implementation have some specific properties:
|
|
||||||
uintptr_t keys[2]; // 1. There are no separate leaf nodes (with both null children), as leaf's value is stored directly in place of corresponding child node pointer. Least significant bit of that pointer used to determine its meaning (i.e., is it a value, or child node pointer).
|
|
||||||
struct PTrieNode *childNodes[2];// 2. Inserting a new element (key/value) into this tree require to create always an exactly one new node (and similarly for remove key/node operation).
|
|
||||||
} PTrieNode; // 3. Tree always contains just one node with null child (i.e. all but one nodes in any possible tree are always have two children).
|
|
||||||
#define PTRIE_NULL_NODE (PTrieNode*)(uintptr_t)1
|
|
||||||
static PTrieNode *ptrieRoot = PTRIE_NULL_NODE, *ptrieFreeNodesList = NULL, *ptrieNewAllocatedPage = NULL;
|
|
||||||
static volatile int ptrieLock = 0;
|
|
||||||
|
|
||||||
static uintptr_t ptrie_lookup(uintptr_t key)
|
|
||||||
{
|
|
||||||
PTrieNode *node = ptrieRoot;
|
|
||||||
uintptr_t *lastKey = NULL;
|
|
||||||
while (!((uintptr_t)node & 1))
|
|
||||||
{
|
|
||||||
int branch = (key >> (node->keys[0] & 0xFF)) & 1;
|
|
||||||
lastKey = &node->keys[branch];
|
|
||||||
node = node->childNodes[branch];
|
|
||||||
}
|
|
||||||
assert(lastKey && (*lastKey & ~0xFF) == key);
|
|
||||||
return (uintptr_t)node & ~1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ptrie_insert(uintptr_t key, uintptr_t value, PTrieNode *newNode/* = (PTrieNode*)malloc(sizeof(PTrieNode))*/)
|
|
||||||
{
|
|
||||||
PTrieNode **node = &ptrieRoot, *n;
|
|
||||||
uintptr_t *prevKey = NULL, x, pkey;
|
|
||||||
unsigned int index, b;
|
|
||||||
assert(!((value & 1) | (key & 0xFF)));//check constraints for key/value
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
n = *node;
|
|
||||||
if (!((uintptr_t)n & 1))//not a leaf
|
|
||||||
{
|
|
||||||
int prefixEnd = n->keys[0] & 0xFF;
|
|
||||||
x = key ^ n->keys[0];// & ~0xFF;
|
|
||||||
if (!(x & (~(uintptr_t)1 << prefixEnd))) {//prefix matches, so go on
|
|
||||||
int branch = (key >> prefixEnd) & 1;
|
|
||||||
node = &n->childNodes[branch];
|
|
||||||
prevKey = &n->keys[branch];
|
|
||||||
} else {//insert a new node before current
|
|
||||||
pkey = n->keys[0] & ~0xFF;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {//leaf
|
|
||||||
if (*node == PTRIE_NULL_NODE) {
|
|
||||||
*node = newNode;
|
|
||||||
newNode->keys[0] = key;//left prefixEnd = 0, so all following insertions will be before this node
|
|
||||||
newNode->childNodes[0] = (PTrieNode*)(value | 1);
|
|
||||||
newNode->childNodes[1] = PTRIE_NULL_NODE;
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
pkey = *prevKey & ~0xFF;
|
|
||||||
x = key ^ pkey;
|
|
||||||
assert(x/*key != pkey*/ && "key already inserted");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
BSR(index, x);
|
|
||||||
b = (key >> index) & 1;
|
|
||||||
newNode->keys[b] = key;
|
|
||||||
newNode->keys[b^1] = pkey;
|
|
||||||
newNode->keys[0] |= index;
|
|
||||||
newNode->childNodes[b] = (PTrieNode*)(value | 1);
|
|
||||||
newNode->childNodes[b^1] = n;
|
|
||||||
*node = newNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uintptr_t ptrie_remove(uintptr_t key)
|
|
||||||
{
|
|
||||||
PTrieNode **node = &ptrieRoot;
|
|
||||||
uintptr_t *pkey = NULL;
|
|
||||||
assert(ptrieRoot != PTRIE_NULL_NODE && "trie is empty!");
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
PTrieNode *n = *node;
|
|
||||||
int branch = (key >> (n->keys[0] & 0xFF)) & 1;
|
|
||||||
PTrieNode *cn = n->childNodes[branch];//current child node
|
|
||||||
if ((uintptr_t)cn & 1)//leaf
|
|
||||||
{
|
|
||||||
PTrieNode *other = n->childNodes[branch^1];
|
|
||||||
assert((n->keys[branch] & ~0xFF) == key);
|
|
||||||
assert(cn != PTRIE_NULL_NODE && "node's key is probably broken");
|
|
||||||
// if (other == PTRIE_NULL_NODE) *node = PTRIE_NULL_NODE; else//special handling for null child nodes is not necessary
|
|
||||||
if (((uintptr_t)other & 1) && other != PTRIE_NULL_NODE)//if other node is not a pointer
|
|
||||||
*pkey = (n->keys[branch^1] & ~0xFF) | ((*pkey) & 0xFF);
|
|
||||||
*node = other;
|
|
||||||
*(PTrieNode**)n = ptrieFreeNodesList; ptrieFreeNodesList = n;//free(n);
|
|
||||||
return (uintptr_t)cn & ~1;
|
|
||||||
}
|
|
||||||
pkey = &n->keys[branch];
|
|
||||||
node = &n->childNodes[branch];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static void *sys_aligned_alloc(size_t alignment, size_t size)
|
|
||||||
{
|
|
||||||
void *p = VMALLOC(size);//optimistically try mapping precisely the right amount before falling back to the slow method
|
|
||||||
assert(!(alignment & (alignment-1)) && "alignment must be a power of two");
|
|
||||||
if ((uintptr_t)p & (alignment-1)/* && p != MAP_FAILED*/)
|
|
||||||
{
|
|
||||||
VMFREE(p, size);
|
|
||||||
#ifdef _WIN32
|
|
||||||
{static DWORD allocationGranularity = 0;
|
|
||||||
if (!allocationGranularity) {
|
|
||||||
SYSTEM_INFO si;
|
|
||||||
GetSystemInfo(&si);
|
|
||||||
allocationGranularity = si.dwAllocationGranularity;
|
|
||||||
}
|
|
||||||
if ((uintptr_t)p < 16*1024*1024)//fill "bubbles" (reserve unaligned regions) at the beginning of virtual address space, otherwise there will be always falling back to the slow method
|
|
||||||
VirtualAlloc(p, alignment - ((uintptr_t)p & (alignment-1)), MEM_RESERVE, PAGE_NOACCESS);
|
|
||||||
do
|
|
||||||
{
|
|
||||||
p = VirtualAlloc(NULL, size + alignment - allocationGranularity, MEM_RESERVE, PAGE_NOACCESS);
|
|
||||||
if (p == NULL) return NULL;
|
|
||||||
VirtualFree(p, 0, MEM_RELEASE);//unfortunately, WinAPI doesn't support release a part of allocated region, so release a whole region
|
|
||||||
p = VirtualAlloc((void*)(((uintptr_t)p + (alignment-1)) & ~(alignment-1)), size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
|
|
||||||
} while (p == NULL);}
|
|
||||||
#else
|
|
||||||
p = VMALLOC(size + alignment - page_size());
|
|
||||||
if (p/* != MAP_FAILED*/)
|
|
||||||
{
|
|
||||||
uintptr_t ap = ((uintptr_t)p + (alignment-1)) & ~(alignment-1);
|
|
||||||
uintptr_t diff = ap - (uintptr_t)p;
|
|
||||||
if (diff) VMFREE(p, diff);
|
|
||||||
diff = alignment - page_size() - diff;
|
|
||||||
assert((intptr_t)diff >= 0);
|
|
||||||
if (diff) VMFREE((void*)(ap + size), diff);
|
|
||||||
return (void*)ap;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
//if (p == 0) p = sys_aligned_alloc(alignment, size);//just in case (because 0 pointer is handled specially elsewhere)
|
|
||||||
//if (p == MAP_FAILED) p = NULL;
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
static NOINLINE void sys_free(void *p)
|
|
||||||
{
|
|
||||||
if (p == NULL) return;
|
|
||||||
#ifdef _WIN32
|
|
||||||
VirtualFree(p, 0, MEM_RELEASE);
|
|
||||||
#else
|
|
||||||
SPINLOCK_ACQUIRE(&ptrieLock);
|
|
||||||
size_t size = ptrie_remove((uintptr_t)p);
|
|
||||||
SPINLOCK_RELEASE(&ptrieLock);
|
|
||||||
munmap(p, size);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static void release_thread_cache(void*);
|
|
||||||
#ifdef __GNUC__
|
|
||||||
#include <pthread.h>
|
|
||||||
#pragma weak pthread_once
|
|
||||||
#pragma weak pthread_key_create
|
|
||||||
#pragma weak pthread_setspecific
|
|
||||||
static pthread_key_t pthread_key;
|
|
||||||
static pthread_once_t init_once = PTHREAD_ONCE_INIT;
|
|
||||||
static void init_pthread_key() { pthread_key_create(&pthread_key, release_thread_cache); }
|
|
||||||
static thread_local int thread_initialized = 0;
|
|
||||||
static void init_pthread_destructor()//must be called only when some block placed into a thread cache's free list
|
|
||||||
{
|
|
||||||
if (unlikely(!thread_initialized))
|
|
||||||
{
|
|
||||||
thread_initialized = 1;
|
|
||||||
if (pthread_once)
|
|
||||||
{
|
|
||||||
pthread_once(&init_once, init_pthread_key);
|
|
||||||
pthread_setspecific(pthread_key, (void*)1);//set nonzero value to force calling of release_thread_cache() on thread terminate
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
static void NTAPI on_tls_callback(PVOID h, DWORD reason, PVOID pv) { h; pv; if (reason == DLL_THREAD_DETACH) release_thread_cache(0); }
|
|
||||||
#pragma comment(linker, "/INCLUDE:" CODE3264("_","") "p_thread_callback_ltalloc")
|
|
||||||
#pragma const_seg(".CRT$XLL")
|
|
||||||
extern CPPCODE("C") const PIMAGE_TLS_CALLBACK p_thread_callback_ltalloc = on_tls_callback;
|
|
||||||
#pragma const_seg()
|
|
||||||
#define init_pthread_destructor()
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//End of platform-specific stuff
|
|
||||||
|
|
||||||
#define MAX_BLOCK_SIZE (MAX_BLOCK_SIZE < CHUNK_SIZE - (CHUNK_SIZE >> (1 + LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO)) ? \
|
|
||||||
MAX_BLOCK_SIZE : CHUNK_SIZE - (CHUNK_SIZE >> (1 + LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO)))
|
|
||||||
#define NUMBER_OF_SIZE_CLASSES ((sizeof(void*)*8 + 1) << LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO)
|
|
||||||
|
|
||||||
typedef struct FreeBlock
|
|
||||||
{
|
|
||||||
struct FreeBlock *next,
|
|
||||||
*nextBatch;//in the central cache blocks are organized into batches to allow fast moving blocks from thread cache and back
|
|
||||||
} FreeBlock;
|
|
||||||
|
|
||||||
typedef struct alignas(CACHE_LINE_SIZE) ChunkBase//force sizeof(Chunk) = cache line size to avoid false sharing
|
|
||||||
{
|
|
||||||
unsigned int sizeClass;
|
|
||||||
} Chunk;
|
|
||||||
|
|
||||||
typedef struct alignas(CACHE_LINE_SIZE) ChunkSm//chunk of smallest blocks of size = sizeof(void*)
|
|
||||||
{
|
|
||||||
unsigned int sizeClass;//struct ChunkBase chunk;
|
|
||||||
struct ChunkSm *prev, *next;
|
|
||||||
int numBatches;
|
|
||||||
#define NUM_OF_BATCHES_IN_CHUNK_SM CHUNK_SIZE/(sizeof(void*)*MAX_NUM_OF_BLOCKS_IN_BATCH)
|
|
||||||
FreeBlock *batches[NUM_OF_BATCHES_IN_CHUNK_SM];//batches of blocks inside ChunkSm have to be stored separately (as smallest blocks of size = sizeof(void*) do not have enough space to store second pointer for the batch)
|
|
||||||
} ChunkSm;
|
|
||||||
|
|
||||||
typedef struct alignas(CACHE_LINE_SIZE)//align needed to prevent cache line sharing between adjacent classes accessed from different threads
|
|
||||||
{
|
|
||||||
volatile int lock;
|
|
||||||
unsigned int freeBlocksInLastChunk;
|
|
||||||
char *lastChunk;//Chunk or ChunkSm
|
|
||||||
union {
|
|
||||||
FreeBlock *firstBatch;
|
|
||||||
ChunkSm *chunkWithFreeBatches;
|
|
||||||
};
|
|
||||||
FreeBlock *freeList;//short list of free blocks that for some reason are not organized into batches
|
|
||||||
unsigned int freeListSize;//should be less than batch size
|
|
||||||
uintptr_t minChunkAddr, maxChunkAddr;
|
|
||||||
} CentralCache;
|
|
||||||
static CentralCache centralCache[NUMBER_OF_SIZE_CLASSES];// = {{0}};
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
FreeBlock *freeList;
|
|
||||||
FreeBlock *tempList;//intermediate list providing a hysteresis in order to avoid a corner case of too frequent moving free blocks to the central cache and back from
|
|
||||||
int counter;//number of blocks in freeList (used to determine when to move free blocks list to the central cache)
|
|
||||||
} ThreadCache;
|
|
||||||
static thread_local ThreadCache threadCache[NUMBER_OF_SIZE_CLASSES];// = {{0}};
|
|
||||||
|
|
||||||
static struct
|
|
||||||
{
|
|
||||||
volatile int lock;
|
|
||||||
void *freeChunk;
|
|
||||||
size_t size;
|
|
||||||
} pad = {0, NULL, 0};
|
|
||||||
|
|
||||||
static CPPCODE(inline) unsigned int get_size_class(size_t size)
|
|
||||||
{
|
|
||||||
unsigned int index;
|
|
||||||
#if _MSC_VER && LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO == 2
|
|
||||||
static const unsigned char small_size_classes[256] = {//have to use a const array here, because MS compiler unfortunately does not evaluate _BitScanReverse with a constant argument at compile time (as gcc does for __builtin_clz)
|
|
||||||
#if CODE3264(1, 0)
|
|
||||||
131, 4, 15, 17, 19, 20, 21, 22, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32, 33, 33, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43
|
|
||||||
#else
|
|
||||||
131, 15, 19, 21, 23, 24, 25, 26, 27, 28, 28, 29, 29, 30, 30, 31, 31, 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
if (size < 256 * sizeof(void*) - (sizeof(void*)-1))
|
|
||||||
return small_size_classes[(size + (sizeof(void*)-1)) / sizeof(void*)];
|
|
||||||
#endif
|
|
||||||
|
|
||||||
size = (size + (sizeof(void*)-1)) & ~(sizeof(void*)-1);//minimum block size is sizeof(void*), doing this is better than just "size = max(size, sizeof(void*))"
|
|
||||||
|
|
||||||
BSR(index, (size-1)|1);//"|1" needed because result of BSR is undefined for zero input
|
|
||||||
#if LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO == 0
|
|
||||||
return index;
|
|
||||||
#else
|
|
||||||
return (index<<LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO) + (unsigned int)((size-1) >> (index-LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO));
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned int class_to_size(unsigned int c)
|
|
||||||
{
|
|
||||||
#if LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO == 0
|
|
||||||
return 2 << c;
|
|
||||||
#else
|
|
||||||
#if LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO >= CODE3264(2, 3)
|
|
||||||
if (unlikely(c < (LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO<<LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO)))//for block sizes less than or equal to pow(2, LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO)
|
|
||||||
return 2 << (c>>LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO);
|
|
||||||
else
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
c -= (1<<LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO)-1;
|
|
||||||
return ((c & ((1<<LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO)-1)) | (1<<LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO)) << ((c>>LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO)-LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned int batch_size(unsigned int sizeClass)//calculates a number of blocks to move between a thread cache and a central cache in one shot
|
|
||||||
{
|
|
||||||
return ((MAX_BATCH_SIZE-1) >> (sizeClass >> LTALLOC_SIZE_CLASSES_SUBPOWER_OF_TWO)) & (MAX_NUM_OF_BLOCKS_IN_BATCH-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
CPPCODE(template <bool> static) void *ltmalloc(size_t size);
|
|
||||||
|
|
||||||
CPPCODE(template <bool throw_>) static void *fetch_from_central_cache(size_t size, ThreadCache *tc, unsigned int sizeClass)
|
|
||||||
{
|
|
||||||
void *p;
|
|
||||||
if (likely(size-1u <= MAX_BLOCK_SIZE-1u))//<=> if (size <= MAX_BLOCK_SIZE && size != 0)
|
|
||||||
{
|
|
||||||
FreeBlock *fb = tc->tempList;
|
|
||||||
if (fb)
|
|
||||||
{
|
|
||||||
assert(tc->counter == (int)batch_size(sizeClass)+1);
|
|
||||||
tc->counter = 1;
|
|
||||||
tc->freeList = fb->next;
|
|
||||||
tc->tempList = NULL;
|
|
||||||
return fb;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(tc->counter == 0 || tc->counter == (int)batch_size(sizeClass)+1);
|
|
||||||
tc->counter = 1;
|
|
||||||
|
|
||||||
{CentralCache *cc = ¢ralCache[sizeClass];
|
|
||||||
SPINLOCK_ACQUIRE(&cc->lock);
|
|
||||||
if (unlikely(!cc->firstBatch))//no free batch
|
|
||||||
{no_free_batch:{
|
|
||||||
unsigned int batchSize = batch_size(sizeClass)+1;
|
|
||||||
|
|
||||||
if (cc->freeList)
|
|
||||||
{
|
|
||||||
assert(cc->freeListSize);
|
|
||||||
if (likely(cc->freeListSize <= batchSize + 1))
|
|
||||||
{
|
|
||||||
tc->counter = batchSize - cc->freeListSize + 1;
|
|
||||||
// batchSize = cc->freeListSize;
|
|
||||||
cc->freeListSize = 0;
|
|
||||||
fb = cc->freeList;
|
|
||||||
cc->freeList = NULL;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
cc->freeListSize -= batchSize;
|
|
||||||
fb = cc->freeList;
|
|
||||||
{FreeBlock *b = cc->freeList;
|
|
||||||
while (--batchSize) b = b->next;
|
|
||||||
cc->freeList = b->next;
|
|
||||||
b->next = NULL;}
|
|
||||||
}
|
|
||||||
SPINLOCK_RELEASE(&cc->lock);
|
|
||||||
tc->freeList = fb->next;
|
|
||||||
init_pthread_destructor();//this call must be placed carefully to allow recursive memory allocation from pthread_key_create (in case when ltalloc replaces the system malloc)
|
|
||||||
return fb;
|
|
||||||
}
|
|
||||||
|
|
||||||
{unsigned int blockSize = class_to_size(sizeClass);
|
|
||||||
if (cc->freeBlocksInLastChunk)
|
|
||||||
{
|
|
||||||
char *firstFree = cc->lastChunk;
|
|
||||||
assert(cc->lastChunk && cc->freeBlocksInLastChunk == (CHUNK_SIZE - ((uintptr_t)cc->lastChunk & (CHUNK_SIZE-1)))/blockSize);
|
|
||||||
if (cc->freeBlocksInLastChunk < batchSize) {
|
|
||||||
tc->counter = batchSize - cc->freeBlocksInLastChunk + 1;
|
|
||||||
batchSize = cc->freeBlocksInLastChunk;
|
|
||||||
}
|
|
||||||
cc->freeBlocksInLastChunk -= batchSize;
|
|
||||||
cc->lastChunk += blockSize * batchSize;
|
|
||||||
if (cc->freeBlocksInLastChunk == 0) {
|
|
||||||
assert(((uintptr_t)cc->lastChunk & (CHUNK_SIZE-1)) == 0);
|
|
||||||
cc->lastChunk = ((char**)cc->lastChunk)[-1];
|
|
||||||
if (cc->lastChunk)
|
|
||||||
cc->freeBlocksInLastChunk = (CHUNK_SIZE - ((uintptr_t)cc->lastChunk & (CHUNK_SIZE-1)))/blockSize;
|
|
||||||
}
|
|
||||||
SPINLOCK_RELEASE(&cc->lock);
|
|
||||||
fb = (FreeBlock*)firstFree;
|
|
||||||
while (--batchSize)
|
|
||||||
firstFree = (char*)(((FreeBlock*)firstFree)->next = (FreeBlock*)(firstFree + blockSize));
|
|
||||||
((FreeBlock*)firstFree)->next = NULL;
|
|
||||||
tc->freeList = fb->next;
|
|
||||||
init_pthread_destructor();
|
|
||||||
return fb;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Allocate new chunk
|
|
||||||
SPINLOCK_RELEASE(&cc->lock);//release lock for a while
|
|
||||||
|
|
||||||
SPINLOCK_ACQUIRE(&pad.lock);
|
|
||||||
if (pad.freeChunk)
|
|
||||||
{
|
|
||||||
p = pad.freeChunk;
|
|
||||||
pad.freeChunk = *(void**)p;
|
|
||||||
pad.size -= CHUNK_SIZE;
|
|
||||||
SPINLOCK_RELEASE(&pad.lock);
|
|
||||||
((char**)((char*)p + CHUNK_SIZE))[-1] = 0;
|
|
||||||
} else {
|
|
||||||
SPINLOCK_RELEASE(&pad.lock);
|
|
||||||
p = sys_aligned_alloc(CHUNK_SIZE, CHUNK_SIZE);
|
|
||||||
if (unlikely(!p)) { CPPCODE(if (throw_) throw std::bad_alloc(); else) return NULL; }
|
|
||||||
}
|
|
||||||
|
|
||||||
#define CHUNK_IS_SMALL unlikely(sizeClass < get_size_class(2*sizeof(void*)))
|
|
||||||
{unsigned int numBlocksInChunk = (CHUNK_SIZE - (CHUNK_IS_SMALL ? sizeof(ChunkSm) : sizeof(Chunk)))/blockSize;
|
|
||||||
#ifndef _WIN32
|
|
||||||
//intptr_t sz = ((CHUNK_SIZE - numBlocksInChunk*blockSize) & ~(page_size()-1)) - page_size();
|
|
||||||
//if (sz > 0) mprotect((char*)p + page_size(), sz, PROT_NONE);//munmap((char*)p + page_size(), sz);//to make possible unmapping, we need to be more careful when returning memory to the system, not simply VMFREE(firstFreeChunk, CHUNK_SIZE), so let there be just mprotect
|
|
||||||
#endif
|
|
||||||
assert(((char**)((char*)p + CHUNK_SIZE))[-1] == 0);//assume that allocated memory is always zero filled (on first access); it is better not to zero it explicitly because it will lead to allocation of physical page which may never needed otherwise
|
|
||||||
if (numBlocksInChunk < batchSize) {
|
|
||||||
tc->counter = batchSize - numBlocksInChunk + 1;
|
|
||||||
batchSize = numBlocksInChunk;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Prepare chunk
|
|
||||||
((Chunk*)p)->sizeClass = sizeClass;
|
|
||||||
{char *firstFree = (char*)p + CHUNK_SIZE - numBlocksInChunk*blockSize;//blocks in chunk are located in such way to achieve a maximum possible alignment
|
|
||||||
fb = (FreeBlock*)firstFree;
|
|
||||||
{int n = batchSize; while (--n)
|
|
||||||
firstFree = (char*)(((FreeBlock*)firstFree)->next = (FreeBlock*)(firstFree + blockSize));}
|
|
||||||
((FreeBlock*)firstFree)->next = NULL;
|
|
||||||
firstFree += blockSize;
|
|
||||||
|
|
||||||
SPINLOCK_ACQUIRE(&cc->lock);
|
|
||||||
if ((uintptr_t)p < cc->minChunkAddr || !cc->minChunkAddr) cc->minChunkAddr = (uintptr_t)p;
|
|
||||||
if ((uintptr_t)p > cc->maxChunkAddr ) cc->maxChunkAddr = (uintptr_t)p;
|
|
||||||
|
|
||||||
if (CHUNK_IS_SMALL)//special handling for smallest blocks of size = sizeof(void*)
|
|
||||||
{
|
|
||||||
ChunkSm *cs = (ChunkSm*)p;
|
|
||||||
cs->numBatches = 0;
|
|
||||||
//Insert new chunk right after chunkWithFreeBatches
|
|
||||||
cs->prev = cc->chunkWithFreeBatches;
|
|
||||||
if (cc->chunkWithFreeBatches) {
|
|
||||||
cs->next = cc->chunkWithFreeBatches->next;
|
|
||||||
if (cc->chunkWithFreeBatches->next) cc->chunkWithFreeBatches->next->prev = cs;
|
|
||||||
cc->chunkWithFreeBatches->next = cs;
|
|
||||||
} else {
|
|
||||||
cs->next = NULL;
|
|
||||||
cc->chunkWithFreeBatches = cs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (unlikely(cc->freeBlocksInLastChunk))//so happened that other thread have already allocated chunk for the same size class while the lock was released
|
|
||||||
{
|
|
||||||
//Hook pointer to the current lastChunk at the end of new chunk (another way is just put all blocks to cc->freeList which is much less effecient)
|
|
||||||
((char**)(((uintptr_t)firstFree & ~(CHUNK_SIZE-1)) + CHUNK_SIZE))[-1] = cc->lastChunk;
|
|
||||||
}
|
|
||||||
cc->freeBlocksInLastChunk = numBlocksInChunk - batchSize;
|
|
||||||
cc->lastChunk = firstFree;
|
|
||||||
}}}}}
|
|
||||||
else {
|
|
||||||
if (!CHUNK_IS_SMALL)//smallest blocks of size = sizeof(void*) are handled specially
|
|
||||||
{
|
|
||||||
fb = cc->firstBatch;
|
|
||||||
cc->firstBatch = fb->nextBatch;
|
|
||||||
}
|
|
||||||
else//size of block = sizeof(void*)
|
|
||||||
{
|
|
||||||
ChunkSm *cs = cc->chunkWithFreeBatches;
|
|
||||||
if (unlikely(cs->numBatches == 0))
|
|
||||||
{
|
|
||||||
if (unlikely(cs->prev == NULL)) goto no_free_batch;
|
|
||||||
cs = cc->chunkWithFreeBatches = cs->prev;
|
|
||||||
assert(cs->numBatches == NUM_OF_BATCHES_IN_CHUNK_SM);
|
|
||||||
}
|
|
||||||
fb = cs->batches[--cs->numBatches];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
SPINLOCK_RELEASE(&cc->lock);}
|
|
||||||
tc->freeList = fb->next;
|
|
||||||
init_pthread_destructor();
|
|
||||||
return fb;
|
|
||||||
}
|
|
||||||
else//allocate block directly from the system
|
|
||||||
{
|
|
||||||
if (unlikely(size == 0)) return ltmalloc CPPCODE(<throw_>)(1);//return NULL;//doing this check here is better than on the top level
|
|
||||||
|
|
||||||
size = (size + CHUNK_SIZE-1) & ~(CHUNK_SIZE-1);
|
|
||||||
p = sys_aligned_alloc(CHUNK_SIZE, size);
|
|
||||||
#ifndef _WIN32
|
|
||||||
if (p) {
|
|
||||||
SPINLOCK_ACQUIRE(&ptrieLock);
|
|
||||||
PTrieNode *newNode;
|
|
||||||
if (ptrieFreeNodesList)
|
|
||||||
ptrieFreeNodesList = *(PTrieNode**)(newNode = ptrieFreeNodesList);
|
|
||||||
else if (ptrieNewAllocatedPage) {
|
|
||||||
newNode = ptrieNewAllocatedPage;
|
|
||||||
if (!((uintptr_t)++ptrieNewAllocatedPage & (page_size()-1)))
|
|
||||||
ptrieNewAllocatedPage = ((PTrieNode**)ptrieNewAllocatedPage)[-1];
|
|
||||||
} else {
|
|
||||||
SPINLOCK_RELEASE(&ptrieLock);
|
|
||||||
newNode = (PTrieNode*)VMALLOC(page_size());
|
|
||||||
if (unlikely(!newNode)) { CPPCODE(if (throw_) throw std::bad_alloc(); else) return NULL; }
|
|
||||||
assert(((char**)((char*)newNode + page_size()))[-1] == 0);
|
|
||||||
SPINLOCK_ACQUIRE(&ptrieLock);
|
|
||||||
((PTrieNode**)((char*)newNode + page_size()))[-1] = ptrieNewAllocatedPage;//in case if other thread also have just allocated a new page
|
|
||||||
ptrieNewAllocatedPage = newNode + 1;
|
|
||||||
}
|
|
||||||
ptrie_insert((uintptr_t)p, size, newNode);
|
|
||||||
SPINLOCK_RELEASE(&ptrieLock);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
CPPCODE(if (throw_) if (unlikely(!p)) throw std::bad_alloc();)
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CPPCODE(template <bool throw_> static) void *ltmalloc(size_t size)
|
|
||||||
{
|
|
||||||
unsigned int sizeClass = get_size_class(size);
|
|
||||||
ThreadCache *tc = &threadCache[sizeClass];
|
|
||||||
FreeBlock *fb = tc->freeList;
|
|
||||||
if (likely(fb))
|
|
||||||
{
|
|
||||||
tc->freeList = fb->next;
|
|
||||||
tc->counter++;
|
|
||||||
return fb;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return fetch_from_central_cache CPPCODE(<throw_>)(size, tc, sizeClass);
|
|
||||||
}
|
|
||||||
CPPCODE(void *ltmalloc(size_t size) {return ltmalloc<false>(size);})//for possible external usage
|
|
||||||
|
|
||||||
static void add_batch_to_central_cache(CentralCache *cc, unsigned int sizeClass, FreeBlock *batch)
|
|
||||||
{
|
|
||||||
if (!CHUNK_IS_SMALL)
|
|
||||||
{
|
|
||||||
batch->nextBatch = cc->firstBatch;
|
|
||||||
cc->firstBatch = batch;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ChunkSm *cs = cc->chunkWithFreeBatches;
|
|
||||||
if (unlikely(cs->numBatches == NUM_OF_BATCHES_IN_CHUNK_SM))
|
|
||||||
{
|
|
||||||
cs = cc->chunkWithFreeBatches = cc->chunkWithFreeBatches->next;
|
|
||||||
assert(cs && cs->numBatches == 0);
|
|
||||||
}
|
|
||||||
cs->batches[cs->numBatches++] = batch;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static NOINLINE void move_to_central_cache(ThreadCache *tc, unsigned int sizeClass)
|
|
||||||
{
|
|
||||||
init_pthread_destructor();//needed for cases when freed memory was allocated in the other thread and no alloc was called in this thread till its termination
|
|
||||||
|
|
||||||
tc->counter = batch_size(sizeClass);
|
|
||||||
if (tc->tempList)//move temp list to the central cache
|
|
||||||
{
|
|
||||||
CentralCache *cc = ¢ralCache[sizeClass];
|
|
||||||
SPINLOCK_ACQUIRE(&cc->lock);
|
|
||||||
add_batch_to_central_cache(cc, sizeClass, tc->tempList);
|
|
||||||
SPINLOCK_RELEASE(&cc->lock);
|
|
||||||
}
|
|
||||||
// else if (unlikely(!tc->freeList))//this is a first call (i.e. when counter = 0) - just initialization of counter needed
|
|
||||||
// {
|
|
||||||
// tc->counter--;
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
tc->tempList = tc->freeList;
|
|
||||||
tc->freeList = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ltfree(void *p)
|
|
||||||
{
|
|
||||||
if (likely((uintptr_t)p & (CHUNK_SIZE-1)))
|
|
||||||
{
|
|
||||||
unsigned int sizeClass = ((Chunk*)((uintptr_t)p & ~(CHUNK_SIZE-1)))->sizeClass;
|
|
||||||
ThreadCache *tc = &threadCache[sizeClass];
|
|
||||||
|
|
||||||
if (unlikely(--tc->counter < 0))
|
|
||||||
move_to_central_cache(tc, sizeClass);
|
|
||||||
|
|
||||||
((FreeBlock*)p)->next = tc->freeList;
|
|
||||||
tc->freeList = (FreeBlock*)p;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
sys_free(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t ltmsize(void *p)
|
|
||||||
{
|
|
||||||
if (likely((uintptr_t)p & (CHUNK_SIZE-1)))
|
|
||||||
{
|
|
||||||
return class_to_size(((Chunk*)((uintptr_t)p & ~(CHUNK_SIZE-1)))->sizeClass);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (p == NULL) return 0;
|
|
||||||
#ifdef _WIN32
|
|
||||||
{MEMORY_BASIC_INFORMATION mi;
|
|
||||||
VirtualQuery(p, &mi, sizeof(mi));
|
|
||||||
return mi.RegionSize;}
|
|
||||||
#else
|
|
||||||
SPINLOCK_ACQUIRE(&ptrieLock);
|
|
||||||
size_t size = ptrie_lookup((uintptr_t)p);
|
|
||||||
SPINLOCK_RELEASE(&ptrieLock);
|
|
||||||
return size;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void release_thread_cache(void *p)
|
|
||||||
{
|
|
||||||
unsigned int sizeClass = 0; (void)p;
|
|
||||||
for (;sizeClass < NUMBER_OF_SIZE_CLASSES; sizeClass++)
|
|
||||||
{
|
|
||||||
ThreadCache *tc = &threadCache[sizeClass];
|
|
||||||
if (tc->freeList || tc->tempList)
|
|
||||||
{
|
|
||||||
FreeBlock *tail = tc->freeList;
|
|
||||||
unsigned int freeListSize = 1;
|
|
||||||
CentralCache *cc = ¢ralCache[sizeClass];
|
|
||||||
|
|
||||||
if (tail)
|
|
||||||
while (tail->next)//search for end of list
|
|
||||||
tail = tail->next, freeListSize++;
|
|
||||||
|
|
||||||
SPINLOCK_ACQUIRE(&cc->lock);
|
|
||||||
if (tc->tempList)
|
|
||||||
add_batch_to_central_cache(cc, sizeClass, tc->tempList);
|
|
||||||
if (tc->freeList) {//append tc->freeList to cc->freeList
|
|
||||||
tail->next = cc->freeList;
|
|
||||||
cc->freeList = tc->freeList;
|
|
||||||
assert(freeListSize == batch_size(sizeClass)+1 - tc->counter);
|
|
||||||
cc->freeListSize += freeListSize;
|
|
||||||
}
|
|
||||||
SPINLOCK_RELEASE(&cc->lock);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ltsqueeze(size_t padsz)
|
|
||||||
{
|
|
||||||
unsigned int sizeClass = get_size_class(2*sizeof(void*));//skip small chunks because corresponding batches can not be efficiently detached from the central cache (if that becomes relevant, may be it worths to reimplement batches for small chunks from array to linked lists)
|
|
||||||
for (;sizeClass < NUMBER_OF_SIZE_CLASSES; sizeClass++)
|
|
||||||
{
|
|
||||||
CentralCache *cc = ¢ralCache[sizeClass];
|
|
||||||
if (cc->maxChunkAddr - cc->minChunkAddr <= CHUNK_SIZE)//preliminary check without lock (assume that writing to minChunkAddr/maxChunkAddr is atomic)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
SPINLOCK_ACQUIRE(&cc->lock);
|
|
||||||
if (cc->maxChunkAddr - cc->minChunkAddr <= CHUNK_SIZE) {//quick check for theoretical possibility that at least one chunk is totally free
|
|
||||||
SPINLOCK_RELEASE(&cc->lock);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
{uintptr_t minChunkAddr = cc->minChunkAddr;
|
|
||||||
size_t bufferSize = ((cc->maxChunkAddr - minChunkAddr) / CHUNK_SIZE + 1) * sizeof(short);
|
|
||||||
//Quickly detach all batches of the current size class from the central cache
|
|
||||||
unsigned int freeListSize = cc->freeListSize;
|
|
||||||
FreeBlock *firstBatch = cc->firstBatch, *freeList = cc->freeList;
|
|
||||||
cc->firstBatch = NULL;
|
|
||||||
cc->freeList = NULL;
|
|
||||||
cc->freeListSize = 0;
|
|
||||||
SPINLOCK_RELEASE(&cc->lock);
|
|
||||||
|
|
||||||
//1. Find out chunks with only free blocks via a simple counting the number of free blocks in each chunk
|
|
||||||
{char buffer[32*1024];//enough for 1GB address space
|
|
||||||
unsigned short *inChunkFreeBlocks = (unsigned short*)(bufferSize <= sizeof(buffer) ? memset(buffer, 0, bufferSize) : VMALLOC(bufferSize));
|
|
||||||
unsigned int numBlocksInChunk = (CHUNK_SIZE - (/*CHUNK_IS_SMALL ? sizeof(ChunkSm) : */sizeof(Chunk)))/class_to_size(sizeClass);
|
|
||||||
FreeBlock **pbatch, *block, **pblock;
|
|
||||||
Chunk *firstFreeChunk = NULL;
|
|
||||||
assert(numBlocksInChunk < (1U<<(sizeof(short)*8)));//in case if CHUNK_SIZE is too big that total count of blocks in it doesn't fit at short type (...may be use static_assert instead?)
|
|
||||||
if (inChunkFreeBlocks)//consider VMALLOC can fail
|
|
||||||
{
|
|
||||||
for (pbatch = &firstBatch; *pbatch; pbatch = &(*pbatch)->nextBatch)
|
|
||||||
for (block = *pbatch; block; block = block->next)
|
|
||||||
#define FREE_BLOCK(block) \
|
|
||||||
if (++inChunkFreeBlocks[((uintptr_t)block - minChunkAddr) / CHUNK_SIZE] == numBlocksInChunk)/*chunk is totally free*/\
|
|
||||||
{\
|
|
||||||
Chunk *chunk = (Chunk*)((uintptr_t)block & ~(CHUNK_SIZE-1));\
|
|
||||||
assert(chunk->sizeClass == sizeClass);/*just in case check before overwriting this info*/\
|
|
||||||
*(Chunk**)chunk = firstFreeChunk;/*put nextFreeChunk pointer right at the beginning of Chunk as there are always must be a space for one pointer before first memory block*/\
|
|
||||||
firstFreeChunk = chunk;\
|
|
||||||
}
|
|
||||||
FREE_BLOCK(block)
|
|
||||||
for (pblock = &freeList; *pblock; pblock = &(*pblock)->next)
|
|
||||||
FREE_BLOCK(*pblock)
|
|
||||||
#undef FREE_BLOCK
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
for (pbatch = &firstBatch; *pbatch; pbatch = &(*pbatch)->nextBatch);
|
|
||||||
for (pblock = &freeList; *pblock; pblock = &(*pblock)->next);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (firstFreeChunk)//is anything to release
|
|
||||||
{
|
|
||||||
//2. Unlink all matching blocks from the corresponding free lists
|
|
||||||
FreeBlock *additionalBatchesList = NULL, *additionalBlocksList = NULL, **abatch = &additionalBatchesList, **ablock = &additionalBlocksList;
|
|
||||||
unsigned int additionalBlocksListSize = 0, batchSize = batch_size(sizeClass)+1;
|
|
||||||
for (pbatch = &firstBatch; *pbatch;)
|
|
||||||
{
|
|
||||||
for (block = *pbatch; block; block = block->next)
|
|
||||||
if (inChunkFreeBlocks[((uintptr_t)block - minChunkAddr) / CHUNK_SIZE] == numBlocksInChunk)//if at least one block belongs to a releasable chunk, then this batch should be handled specially
|
|
||||||
{
|
|
||||||
FreeBlock *nextBatch = (*pbatch)->nextBatch;
|
|
||||||
for (block = *pbatch; block;)//re-add blocks of not-for-release chunks and organize them into another batches' list (to join it with the main later)
|
|
||||||
if (inChunkFreeBlocks[((uintptr_t)block - minChunkAddr) / CHUNK_SIZE] != numBlocksInChunk)//skip matching-for-release blocks
|
|
||||||
{
|
|
||||||
*ablock = block;
|
|
||||||
do//this loop needed only to minimize memory write operations, otherwise a simpler approach could be used (like in the next loop below)
|
|
||||||
{
|
|
||||||
ablock = &block->next;
|
|
||||||
block = block->next;
|
|
||||||
if (++additionalBlocksListSize == batchSize)
|
|
||||||
{
|
|
||||||
abatch = &(*abatch = additionalBlocksList)->nextBatch;
|
|
||||||
*abatch = NULL;
|
|
||||||
*ablock = NULL;
|
|
||||||
ablock = &additionalBlocksList;
|
|
||||||
additionalBlocksList = NULL;
|
|
||||||
additionalBlocksListSize = 0;
|
|
||||||
break;//to force *ablock = block; for starting a new batch
|
|
||||||
}
|
|
||||||
} while (block && inChunkFreeBlocks[((uintptr_t)block - minChunkAddr) / CHUNK_SIZE] != numBlocksInChunk);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
block = block->next;
|
|
||||||
*ablock = NULL;
|
|
||||||
*pbatch = nextBatch;//unlink batch
|
|
||||||
goto continue_;
|
|
||||||
}
|
|
||||||
pbatch = &(*pbatch)->nextBatch;
|
|
||||||
continue_:;
|
|
||||||
}
|
|
||||||
for (block = freeList; block;)
|
|
||||||
if (inChunkFreeBlocks[((uintptr_t)block - minChunkAddr) / CHUNK_SIZE] != numBlocksInChunk)
|
|
||||||
{
|
|
||||||
//*pblock = (*pblock)->next, freeListSize--;//unlink block
|
|
||||||
ablock = &(*ablock = block)->next;
|
|
||||||
block = block->next;
|
|
||||||
*ablock = NULL;
|
|
||||||
if (++additionalBlocksListSize == batchSize)
|
|
||||||
{
|
|
||||||
abatch = &(*abatch = additionalBlocksList)->nextBatch;
|
|
||||||
*abatch = NULL;
|
|
||||||
ablock = &additionalBlocksList;
|
|
||||||
additionalBlocksList = NULL;
|
|
||||||
additionalBlocksListSize = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
block = block->next;
|
|
||||||
//Add additional lists
|
|
||||||
*abatch = *pbatch;
|
|
||||||
*pbatch = additionalBatchesList;
|
|
||||||
pblock = ablock;
|
|
||||||
freeList = additionalBlocksList;
|
|
||||||
freeListSize = additionalBlocksListSize;
|
|
||||||
|
|
||||||
//Return back all left not-for-release blocks to the central cache as quickly as possible (as other threads may want to allocate a new memory)
|
|
||||||
#define GIVE_LISTS_BACK_TO_CC \
|
|
||||||
SPINLOCK_ACQUIRE(&cc->lock);\
|
|
||||||
*pbatch = cc->firstBatch;\
|
|
||||||
cc->firstBatch = firstBatch;\
|
|
||||||
*pblock = cc->freeList;\
|
|
||||||
cc->freeList = freeList;\
|
|
||||||
cc->freeListSize += freeListSize;\
|
|
||||||
SPINLOCK_RELEASE(&cc->lock);\
|
|
||||||
if (bufferSize > sizeof(buffer)) VMFREE(inChunkFreeBlocks, bufferSize);//this better to do before 3. as kernel is likely optimized for release of just allocated range
|
|
||||||
GIVE_LISTS_BACK_TO_CC
|
|
||||||
|
|
||||||
if (padsz)
|
|
||||||
{
|
|
||||||
SPINLOCK_ACQUIRE(&pad.lock);
|
|
||||||
if (pad.size < padsz)
|
|
||||||
{
|
|
||||||
Chunk *first = firstFreeChunk, **c;
|
|
||||||
do//put off free chunks up to a specified pad size
|
|
||||||
{
|
|
||||||
c = (Chunk**)firstFreeChunk;
|
|
||||||
firstFreeChunk = *c;
|
|
||||||
pad.size += CHUNK_SIZE;
|
|
||||||
} while (pad.size < padsz && firstFreeChunk);
|
|
||||||
*c = (Chunk*)pad.freeChunk;
|
|
||||||
pad.freeChunk = first;
|
|
||||||
}
|
|
||||||
SPINLOCK_RELEASE(&pad.lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
//3. Return memory to the system
|
|
||||||
while (firstFreeChunk)
|
|
||||||
{
|
|
||||||
Chunk *nextFreeChunk = *(Chunk**)firstFreeChunk;
|
|
||||||
VMFREE(firstFreeChunk, CHUNK_SIZE);
|
|
||||||
firstFreeChunk = nextFreeChunk;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else//nothing to release - just return batches back to the central cache
|
|
||||||
{
|
|
||||||
GIVE_LISTS_BACK_TO_CC
|
|
||||||
#undef GIVE_LISTS_BACK_TO_CC
|
|
||||||
}}}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(__cplusplus) && !defined(LTALLOC_DISABLE_OPERATOR_NEW_OVERRIDE)
|
|
||||||
void *operator new (size_t size) throw(std::bad_alloc) {return ltmalloc<true> (size);}
|
|
||||||
void *operator new (size_t size, const std::nothrow_t&) throw() {return ltmalloc<false>(size);}
|
|
||||||
void *operator new[](size_t size) throw(std::bad_alloc) {return ltmalloc<true> (size);}
|
|
||||||
void *operator new[](size_t size, const std::nothrow_t&) throw() {return ltmalloc<false>(size);}
|
|
||||||
|
|
||||||
void operator delete (void* p) throw() {ltfree(p);}
|
|
||||||
void operator delete (void* p, const std::nothrow_t&) throw() {ltfree(p);}
|
|
||||||
void operator delete[](void* p) throw() {ltfree(p);}
|
|
||||||
void operator delete[](void* p, const std::nothrow_t&) throw() {ltfree(p);}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* @r-lyeh's { */
|
|
||||||
#include <string.h>
|
|
||||||
void *ltcalloc(size_t elems, size_t size) {
|
|
||||||
size *= elems;
|
|
||||||
return memset( ltmalloc( size ), 0, size );
|
|
||||||
}
|
|
||||||
void *ltmemalign( size_t align, size_t size ) {
|
|
||||||
return --align, ltmalloc( (size+align)&~align );
|
|
||||||
}
|
|
||||||
void *ltrealloc( void *ptr, size_t sz ) {
|
|
||||||
if( !ptr ) return ltmalloc( sz );
|
|
||||||
if( !sz ) return ltfree( ptr ), (void *)0;
|
|
||||||
size_t osz = ltmsize( ptr );
|
|
||||||
if( sz <= osz ) {
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
void *nptr = memcpy( ltmalloc(sz), ptr, osz );
|
|
||||||
ltfree( ptr );
|
|
||||||
|
|
||||||
#ifdef LTALLOC_AUTO_GC_INTERVAL
|
|
||||||
/* this is kind of compromise; the following timer is to guarantee
|
|
||||||
that memory gets wiped out at least every given seconds between consecutive
|
|
||||||
ltrealloc() calls (I am assuming frequency usage for ltrealloc() is smaller
|
|
||||||
than ltmalloc() or ltfree() too) - @r-lyeh */
|
|
||||||
clock_t now = clock();
|
|
||||||
static clock_t then = now;
|
|
||||||
if( ( double(now - then) / CLOCKS_PER_SEC ) > LTALLOC_AUTO_GC_INTERVAL ) {
|
|
||||||
ltsqueeze(0);
|
|
||||||
}
|
|
||||||
then = now;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return nptr;
|
|
||||||
}
|
|
||||||
/* } */
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
#include <stdlib.h> /*a more portable size_t definition than stddef.h itself*/
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
void* ltmalloc(size_t);
|
|
||||||
void ltfree(void*);
|
|
||||||
void* ltrealloc( void *, size_t );
|
|
||||||
void* ltcalloc( size_t, size_t );
|
|
||||||
void* ltmemalign( size_t, size_t );
|
|
||||||
void ltsqueeze(size_t pad); /*return memory to system (see README.md)*/
|
|
||||||
size_t ltmsize(void*);
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
// based on code by Jerry Coffin (most likely Public Domain)
|
|
||||||
// - rlyeh
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <new>
|
|
||||||
#include <limits>
|
|
||||||
|
|
||||||
#include "ltalloc.h"
|
|
||||||
|
|
||||||
namespace lt {
|
|
||||||
template <class T>
|
|
||||||
struct allocator {
|
|
||||||
typedef size_t size_type;
|
|
||||||
typedef ptrdiff_t difference_type;
|
|
||||||
typedef T* pointer;
|
|
||||||
typedef const T* const_pointer;
|
|
||||||
typedef T& reference;
|
|
||||||
typedef const T& const_reference;
|
|
||||||
typedef T value_type;
|
|
||||||
|
|
||||||
template <class U> struct rebind { typedef allocator<U> other; };
|
|
||||||
allocator() throw() {}
|
|
||||||
allocator(const allocator&) throw() {}
|
|
||||||
|
|
||||||
template <class U> allocator(const allocator<U>&) throw(){}
|
|
||||||
|
|
||||||
~allocator() throw() {}
|
|
||||||
|
|
||||||
pointer address(reference x) const { return &x; }
|
|
||||||
const_pointer address(const_reference x) const { return &x; }
|
|
||||||
|
|
||||||
pointer allocate(size_type s, void const * = 0) {
|
|
||||||
if (0 == s)
|
|
||||||
return NULL;
|
|
||||||
pointer temp = (pointer)ltmalloc(s * sizeof(T));
|
|
||||||
if (temp == NULL)
|
|
||||||
throw std::bad_alloc();
|
|
||||||
return temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
void deallocate(pointer p, size_type) {
|
|
||||||
ltfree(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_type max_size() const throw() {
|
|
||||||
return std::numeric_limits<size_t>::max() / sizeof(T);
|
|
||||||
}
|
|
||||||
|
|
||||||
void construct(pointer p, const T& val) {
|
|
||||||
new((void *)p) T(val);
|
|
||||||
}
|
|
||||||
|
|
||||||
void destroy(pointer p) {
|
|
||||||
p->~T();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -27,12 +27,11 @@ solution "objview"
|
|||||||
|
|
||||||
kind "ConsoleApp"
|
kind "ConsoleApp"
|
||||||
language "C++"
|
language "C++"
|
||||||
files { "viewer.cc", "trackball.cc", "ltalloc.cc" }
|
files { "viewer.cc", "trackball.cc" }
|
||||||
includedirs { "./" }
|
includedirs { "./" }
|
||||||
includedirs { "../../" }
|
includedirs { "../../" }
|
||||||
|
|
||||||
flags { "c++11" }
|
flags { "c++11" }
|
||||||
--buildoptions { "-std=c++11" }
|
|
||||||
|
|
||||||
if _OPTIONS['clang'] then
|
if _OPTIONS['clang'] then
|
||||||
toolset "clang"
|
toolset "clang"
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
//
|
//
|
||||||
// Optimized wavefront .obj loader.
|
// Optimized wavefront .obj loader.
|
||||||
// Requires ltalloc and C++11
|
// Requires lfpAlloc and C++11
|
||||||
//
|
//
|
||||||
|
|
||||||
/*
|
/*
|
||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) 2012-2016 Syoyo Fujita and many contributors.
|
Copyright (c) 2012-2017 Syoyo Fujita and many contributors.
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@@ -38,7 +38,6 @@ THE SOFTWARE.
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <unistd.h>
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
@@ -55,7 +54,7 @@ THE SOFTWARE.
|
|||||||
#include <chrono> // C++11
|
#include <chrono> // C++11
|
||||||
#include <thread> // C++11
|
#include <thread> // C++11
|
||||||
|
|
||||||
#include "ltalloc.hpp"
|
#include "lfpAlloc/Allocator.hpp"
|
||||||
|
|
||||||
namespace tinyobj_opt {
|
namespace tinyobj_opt {
|
||||||
|
|
||||||
@@ -313,6 +312,10 @@ typedef struct {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
std::string name; // group name or object name.
|
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 face_offset;
|
||||||
unsigned int length;
|
unsigned int length;
|
||||||
} shape_t;
|
} shape_t;
|
||||||
@@ -327,12 +330,19 @@ struct index_t {
|
|||||||
};
|
};
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
std::vector<float, lt::allocator<float> > vertices;
|
std::vector<float, lfpAlloc::lfpAllocator<float> > vertices;
|
||||||
std::vector<float, lt::allocator<float> > normals;
|
std::vector<float, lfpAlloc::lfpAllocator<float> > normals;
|
||||||
std::vector<float, lt::allocator<float> > texcoords;
|
std::vector<float, lfpAlloc::lfpAllocator<float> > texcoords;
|
||||||
std::vector<index_t, lt::allocator<index_t> > indices;
|
std::vector<index_t, lfpAlloc::lfpAllocator<index_t> > indices;
|
||||||
std::vector<int, lt::allocator<int> > face_num_verts;
|
|
||||||
std::vector<int, lt::allocator<int> > material_ids;
|
// # 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;
|
} attrib_t;
|
||||||
|
|
||||||
typedef StackVector<char, 256> ShortString;
|
typedef StackVector<char, 256> ShortString;
|
||||||
@@ -595,19 +605,9 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assemble :
|
assemble :
|
||||||
|
*result = (sign == '+' ? 1 : -1) *
|
||||||
{
|
(exponent ? std::ldexp(mantissa * std::pow(5.0, exponent), exponent)
|
||||||
// = pow(5.0, exponent);
|
: mantissa);
|
||||||
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;
|
return true;
|
||||||
fail:
|
fail:
|
||||||
return false;
|
return false;
|
||||||
@@ -999,9 +999,9 @@ typedef struct {
|
|||||||
float tx, ty;
|
float tx, ty;
|
||||||
|
|
||||||
// for f
|
// for f
|
||||||
std::vector<index_t, lt::allocator<index_t> > f;
|
std::vector<index_t, lfpAlloc::lfpAllocator<index_t> > f;
|
||||||
// std::vector<index_t> f;
|
// std::vector<index_t> f;
|
||||||
std::vector<int, lt::allocator<int> > f_num_verts;
|
std::vector<int, lfpAlloc::lfpAllocator<int> > f_num_verts;
|
||||||
|
|
||||||
const char *group_name;
|
const char *group_name;
|
||||||
unsigned int group_name_len;
|
unsigned int group_name_len;
|
||||||
@@ -1047,8 +1047,14 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
|||||||
std::vector<material_t> *materials, const char *buf, size_t len,
|
std::vector<material_t> *materials, const char *buf, size_t len,
|
||||||
const LoadOption &option);
|
const LoadOption &option);
|
||||||
|
|
||||||
|
} // namespace tinyobj_opt
|
||||||
|
|
||||||
|
#endif // TINOBJ_LOADER_OPT_H_
|
||||||
|
|
||||||
#ifdef TINYOBJ_LOADER_OPT_IMPLEMENTATION
|
#ifdef TINYOBJ_LOADER_OPT_IMPLEMENTATION
|
||||||
|
|
||||||
|
namespace tinyobj_opt {
|
||||||
|
|
||||||
static bool parseLine(Command *command, const char *p, size_t p_len,
|
static bool parseLine(Command *command, const char *p, size_t p_len,
|
||||||
bool triangulate = true) {
|
bool triangulate = true) {
|
||||||
// @todo { operate directly on pointer `p'. to do that, add range check for
|
// @todo { operate directly on pointer `p'. to do that, add range check for
|
||||||
@@ -1233,6 +1239,8 @@ typedef struct {
|
|||||||
// 3. Do parallel parsing for each line.
|
// 3. Do parallel parsing for each line.
|
||||||
// 4. Reconstruct final mesh data structure.
|
// 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)
|
#define kMaxThreads (32)
|
||||||
|
|
||||||
static inline bool is_line_ending(const char *p, size_t i, size_t end_i) {
|
static inline bool is_line_ending(const char *p, size_t i, size_t end_i) {
|
||||||
@@ -1271,7 +1279,8 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
|||||||
|
|
||||||
auto t1 = std::chrono::high_resolution_clock::now();
|
auto t1 = std::chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
std::vector<LineInfo, lt::allocator<LineInfo> > line_infos[kMaxThreads];
|
std::vector<LineInfo, lfpAlloc::lfpAllocator<LineInfo> >
|
||||||
|
line_infos[kMaxThreads];
|
||||||
for (size_t t = 0; t < static_cast<size_t>(num_threads); t++) {
|
for (size_t t = 0; t < static_cast<size_t>(num_threads); t++) {
|
||||||
// Pre allocate enough memory. len / 128 / num_threads is just a heuristic
|
// Pre allocate enough memory. len / 128 / num_threads is just a heuristic
|
||||||
// value.
|
// value.
|
||||||
@@ -1300,15 +1309,19 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
|||||||
end_idx = len - 1;
|
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;
|
size_t prev_pos = start_idx;
|
||||||
for (size_t i = start_idx; i < end_idx; i++) {
|
for (size_t i = start_idx; i < end_idx; i++) {
|
||||||
if (is_line_ending(buf, i, end_idx)) {
|
if (is_line_ending(buf, i, end_idx)) {
|
||||||
if ((t > 0) && (prev_pos == start_idx) &&
|
if (!new_line_found) {
|
||||||
(!is_line_ending(buf, start_idx - 1, end_idx))) {
|
|
||||||
// first linebreak found in (chunk > 0), and a line before this
|
// first linebreak found in (chunk > 0), and a line before this
|
||||||
// linebreak belongs to previous chunk, so skip it.
|
// linebreak belongs to previous chunk, so skip it.
|
||||||
prev_pos = i + 1;
|
prev_pos = i + 1;
|
||||||
continue;
|
new_line_found = true;
|
||||||
} else {
|
} else {
|
||||||
LineInfo info;
|
LineInfo info;
|
||||||
info.pos = prev_pos;
|
info.pos = prev_pos;
|
||||||
@@ -1323,11 +1336,11 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find extra line which spand across chunk boundary.
|
// If at least one line started in this chunk, find where it ends in the
|
||||||
if ((t < num_threads) && (buf[end_idx - 1] != '\n')) {
|
// rest of the buffer
|
||||||
auto extra_span_idx = (std::min)(end_idx - 1 + chunk_size, len - 1);
|
if (new_line_found && (t < num_threads) && (buf[end_idx - 1] != '\n')) {
|
||||||
for (size_t i = end_idx; i < extra_span_idx; i++) {
|
for (size_t i = end_idx; i < len; i++) {
|
||||||
if (is_line_ending(buf, i, extra_span_idx)) {
|
if (is_line_ending(buf, i, len)) {
|
||||||
LineInfo info;
|
LineInfo info;
|
||||||
info.pos = prev_pos;
|
info.pos = prev_pos;
|
||||||
info.len = i - prev_pos;
|
info.len = i - prev_pos;
|
||||||
@@ -1384,7 +1397,6 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
|||||||
|
|
||||||
for (size_t t = 0; t < num_threads; t++) {
|
for (size_t t = 0; t < num_threads; t++) {
|
||||||
workers->push_back(std::thread([&, t]() {
|
workers->push_back(std::thread([&, t]() {
|
||||||
|
|
||||||
for (size_t i = 0; i < line_infos[t].size(); i++) {
|
for (size_t i = 0; i < line_infos[t].size(); i++) {
|
||||||
Command command;
|
Command command;
|
||||||
bool ret = parseLine(&command, &buf[line_infos[t][i].pos],
|
bool ret = parseLine(&command, &buf[line_infos[t][i].pos],
|
||||||
@@ -1402,14 +1414,14 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (command.type == COMMAND_MTLLIB) {
|
if (command.type == COMMAND_MTLLIB) {
|
||||||
|
// Save the indices of the `mtllib` command in `commands` to easily find it later
|
||||||
mtllib_t_index = t;
|
mtllib_t_index = t;
|
||||||
mtllib_i_index = commands->size();
|
mtllib_i_index = commands[t].size();
|
||||||
}
|
}
|
||||||
|
|
||||||
commands[t].emplace_back(std::move(command));
|
commands[t].emplace_back(std::move(command));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1434,7 +1446,9 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
|||||||
// std::cout << "mtllib :" << material_filename << std::endl;
|
// std::cout << "mtllib :" << material_filename << std::endl;
|
||||||
|
|
||||||
auto t1 = std::chrono::high_resolution_clock::now();
|
auto t1 = std::chrono::high_resolution_clock::now();
|
||||||
|
if (material_filename.back() == '\r') {
|
||||||
|
material_filename.pop_back();
|
||||||
|
}
|
||||||
std::ifstream ifs(material_filename);
|
std::ifstream ifs(material_filename);
|
||||||
if (ifs.good()) {
|
if (ifs.good()) {
|
||||||
LoadMtl(&material_map, materials, &ifs);
|
LoadMtl(&material_map, materials, &ifs);
|
||||||
@@ -1485,7 +1499,7 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
|||||||
attrib->texcoords.resize(num_vt * 2);
|
attrib->texcoords.resize(num_vt * 2);
|
||||||
attrib->indices.resize(num_f);
|
attrib->indices.resize(num_f);
|
||||||
attrib->face_num_verts.resize(num_indices);
|
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 v_offsets[kMaxThreads];
|
||||||
size_t n_offsets[kMaxThreads];
|
size_t n_offsets[kMaxThreads];
|
||||||
@@ -1510,7 +1524,6 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
|||||||
StackVector<std::thread, 16> workers;
|
StackVector<std::thread, 16> workers;
|
||||||
|
|
||||||
for (size_t t = 0; t < num_threads; t++) {
|
for (size_t t = 0; t < num_threads; t++) {
|
||||||
int material_id = -1; // -1 = default unknown material.
|
|
||||||
workers->push_back(std::thread([&, t]() {
|
workers->push_back(std::thread([&, t]() {
|
||||||
size_t v_count = v_offsets[t];
|
size_t v_count = v_offsets[t];
|
||||||
size_t n_count = n_offsets[t];
|
size_t n_count = n_offsets[t];
|
||||||
@@ -1523,15 +1536,40 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
|||||||
continue;
|
continue;
|
||||||
} else if (commands[t][i].type == COMMAND_USEMTL) {
|
} else if (commands[t][i].type == COMMAND_USEMTL) {
|
||||||
if (commands[t][i].material_name &&
|
if (commands[t][i].material_name &&
|
||||||
commands[t][i].material_name_len > 0) {
|
commands[t][i].material_name_len > 0 &&
|
||||||
|
// check if there are still faces after this command
|
||||||
|
face_count < num_indices) {
|
||||||
|
// Find next face
|
||||||
|
bool found = false;
|
||||||
|
size_t i_start = i + 1, t_next, i_next;
|
||||||
|
for (t_next = t; t_next < num_threads; t_next++) {
|
||||||
|
for (i_next = i_start; i_next < commands[t_next].size();
|
||||||
|
i_next++) {
|
||||||
|
if (commands[t_next][i_next].type == COMMAND_F) {
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (found) break;
|
||||||
|
i_start = 0;
|
||||||
|
}
|
||||||
|
// Assign material to this face
|
||||||
|
if (found) {
|
||||||
std::string material_name(commands[t][i].material_name,
|
std::string material_name(commands[t][i].material_name,
|
||||||
commands[t][i].material_name_len);
|
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()) {
|
if (material_map.find(material_name) != material_map.end()) {
|
||||||
material_id = material_map[material_name];
|
attrib->material_ids[face_count + k] =
|
||||||
|
material_map[material_name];
|
||||||
} else {
|
} else {
|
||||||
// Assign invalid material ID
|
// Assign invalid material ID
|
||||||
material_id = -1;
|
// Set a different value than the default, to
|
||||||
|
// prevent following faces from being assigned a valid
|
||||||
|
// material
|
||||||
|
attrib->material_ids[face_count + k] = -2;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (commands[t][i].type == COMMAND_V) {
|
} else if (commands[t][i].type == COMMAND_V) {
|
||||||
@@ -1558,8 +1596,8 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
|||||||
index_t(vertex_index, texcoord_index, normal_index);
|
index_t(vertex_index, texcoord_index, normal_index);
|
||||||
}
|
}
|
||||||
for (size_t k = 0; k < commands[t][i].f_num_verts.size(); k++) {
|
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] =
|
||||||
attrib->face_num_verts[face_count + k] = commands[t][i].f_num_verts[k];
|
commands[t][i].f_num_verts[k];
|
||||||
}
|
}
|
||||||
|
|
||||||
f_count += commands[t][i].f.size();
|
f_count += commands[t][i].f.size();
|
||||||
@@ -1573,6 +1611,12 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
|||||||
workers[t].join();
|
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();
|
auto t_end = std::chrono::high_resolution_clock::now();
|
||||||
ms_merge = t_end - t_start;
|
ms_merge = t_end - t_start;
|
||||||
}
|
}
|
||||||
@@ -1632,7 +1676,8 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (commands[t][i].type == COMMAND_F) {
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1671,8 +1716,7 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
#endif // TINYOBJ_LOADER_OPT_IMPLEMENTATION
|
|
||||||
|
|
||||||
} // namespace tinyobj_opt
|
} // namespace tinyobj_opt
|
||||||
|
|
||||||
#endif // TINOBJ_LOADER_OPT_H_
|
#endif // TINYOBJ_LOADER_OPT_IMPLEMENTATION
|
||||||
|
|||||||
@@ -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)
|
std::vector<float> vb; // pos(3float), normal(3float), color(3float)
|
||||||
size_t face_offset = 0;
|
size_t face_offset = 0;
|
||||||
for (size_t v = 0; v < attrib.face_num_verts.size(); v++) {
|
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++) {
|
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 idx0 = attrib.indices[face_offset+3*f+0];
|
||||||
tinyobj_opt::index_t idx1 = attrib.indices[face_offset+3*f+1];
|
tinyobj_opt::index_t idx1 = attrib.indices[face_offset+3*f+1];
|
||||||
|
|||||||
BIN
fuzzer/afl.tar.gz
Normal file
BIN
fuzzer/afl.tar.gz
Normal file
Binary file not shown.
20
fuzzer/runner.py
Normal file
20
fuzzer/runner.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import os, sys
|
||||||
|
import glob
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
def main():
|
||||||
|
for g in glob.glob("../tests/afl/id*"):
|
||||||
|
print(g)
|
||||||
|
|
||||||
|
cmd = ["../a.out", g]
|
||||||
|
|
||||||
|
proc = subprocess.Popen(cmd)
|
||||||
|
try:
|
||||||
|
outs, errs = proc.communicate(timeout=15)
|
||||||
|
print(outs)
|
||||||
|
except TimeoutExpired:
|
||||||
|
proc.kill()
|
||||||
|
outs, errs = proc.communicate()
|
||||||
|
|
||||||
|
|
||||||
|
main()
|
||||||
@@ -29,6 +29,13 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#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
|
||||||
|
#endif
|
||||||
|
|
||||||
class timerutil {
|
class timerutil {
|
||||||
public:
|
public:
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@@ -130,14 +137,19 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
|
|||||||
for (size_t i = 0; i < shapes.size(); i++) {
|
for (size_t i = 0; i < shapes.size(); i++) {
|
||||||
printf("shape[%ld].name = %s\n", static_cast<long>(i),
|
printf("shape[%ld].name = %s\n", static_cast<long>(i),
|
||||||
shapes[i].name.c_str());
|
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()));
|
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;
|
size_t index_offset = 0;
|
||||||
|
|
||||||
assert(shapes[i].mesh.num_face_vertices.size() ==
|
assert(shapes[i].mesh.num_face_vertices.size() ==
|
||||||
shapes[i].mesh.material_ids.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),
|
printf("shape[%ld].num_faces: %lu\n", static_cast<long>(i),
|
||||||
static_cast<unsigned long>(shapes[i].mesh.num_face_vertices.size()));
|
static_cast<unsigned long>(shapes[i].mesh.num_face_vertices.size()));
|
||||||
|
|
||||||
@@ -158,6 +170,8 @@ static void PrintInfo(const tinyobj::attrib_t& attrib,
|
|||||||
|
|
||||||
printf(" face[%ld].material_id = %d\n", static_cast<long>(f),
|
printf(" face[%ld].material_id = %d\n", static_cast<long>(f),
|
||||||
shapes[i].mesh.material_ids[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;
|
index_offset += fnum;
|
||||||
}
|
}
|
||||||
@@ -271,14 +285,19 @@ static bool TestLoadObj(const char* filename, const char* basepath = NULL,
|
|||||||
|
|
||||||
timerutil t;
|
timerutil t;
|
||||||
t.start();
|
t.start();
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename,
|
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename,
|
||||||
basepath, triangulate);
|
basepath, triangulate);
|
||||||
t.end();
|
t.end();
|
||||||
printf("Parsing time: %lu [msecs]\n", t.msec());
|
printf("Parsing time: %lu [msecs]\n", t.msec());
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
@@ -362,16 +381,12 @@ static bool TestStreamLoadObj() {
|
|||||||
virtual bool operator()(const std::string& matId,
|
virtual bool operator()(const std::string& matId,
|
||||||
std::vector<material_t>* materials,
|
std::vector<material_t>* materials,
|
||||||
std::map<std::string, int>* matMap,
|
std::map<std::string, int>* matMap,
|
||||||
|
std::string* warn,
|
||||||
std::string* err) {
|
std::string* err) {
|
||||||
|
(void)err;
|
||||||
(void)matId;
|
(void)matId;
|
||||||
std::string warning;
|
LoadMtl(matMap, materials, &m_matSStream, warn, err);
|
||||||
LoadMtl(matMap, materials, &m_matSStream, &warning);
|
|
||||||
|
|
||||||
if (!warning.empty()) {
|
|
||||||
if (err) {
|
|
||||||
(*err) += warning;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -383,8 +398,9 @@ static bool TestStreamLoadObj() {
|
|||||||
tinyobj::attrib_t attrib;
|
tinyobj::attrib_t attrib;
|
||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, &objStream,
|
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, &objStream,
|
||||||
&matSSReader);
|
&matSSReader);
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
|
|||||||
9
models/colorspace-issue-184.mtl
Normal file
9
models/colorspace-issue-184.mtl
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
newmtl default
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 0 0 0
|
||||||
|
Ks 0 0 0
|
||||||
|
Kt 0.1 0.2 0.3
|
||||||
|
map_Kd -colorspace sRGB -o 0.1 diffuse.jpg
|
||||||
|
map_Ks -s 0.1 0.2 specular.jpg
|
||||||
|
map_bump -colorspace linear -bm 3 bumpmap.jpg
|
||||||
|
|
||||||
7
models/colorspace-issue-184.obj
Normal file
7
models/colorspace-issue-184.obj
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
mtllib colorspace-issue-184.mtl
|
||||||
|
o Test
|
||||||
|
v 1.864151 -1.219172 -5.532511
|
||||||
|
v 0.575869 -0.666304 5.896140
|
||||||
|
v 0.940448 1.000000 -1.971128
|
||||||
|
usemtl default
|
||||||
|
f 1 2 3
|
||||||
31
models/cube-vertexcol.obj
Normal file
31
models/cube-vertexcol.obj
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
mtllib cube.mtl
|
||||||
|
|
||||||
|
v 0.000000 2.000000 2.000000 0 0 0
|
||||||
|
v 0.000000 0.000000 2.000000 0 0 1
|
||||||
|
v 2.000000 0.000000 2.000000 0 1 0
|
||||||
|
v 2.000000 2.000000 2.000000 0 1 1
|
||||||
|
v 0.000000 2.000000 0.000000 1 0 0
|
||||||
|
v 0.000000 0.000000 0.000000 1 0 1
|
||||||
|
v 2.000000 0.000000 0.000000 1 1 0
|
||||||
|
v 2.000000 2.000000 0.000000 1 1 1
|
||||||
|
# 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
|
||||||
@@ -13,6 +13,7 @@ v 2.000000 2.000000 0.000000
|
|||||||
g front cube
|
g front cube
|
||||||
usemtl white
|
usemtl white
|
||||||
f 1 2 3 4
|
f 1 2 3 4
|
||||||
|
# two white spaces between 'back' and 'cube'
|
||||||
g back cube
|
g back cube
|
||||||
# expects white material
|
# expects white material
|
||||||
f 8 7 6 5
|
f 8 7 6 5
|
||||||
|
|||||||
24
models/invalid-face-definition.mtl
Normal file
24
models/invalid-face-definition.mtl
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
newmtl white
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 1 1 1
|
||||||
|
Ks 0 0 0
|
||||||
|
|
||||||
|
newmtl red
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 1 0 0
|
||||||
|
Ks 0 0 0
|
||||||
|
|
||||||
|
newmtl green
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 0 1 0
|
||||||
|
Ks 0 0 0
|
||||||
|
|
||||||
|
newmtl blue
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 0 0 1
|
||||||
|
Ks 0 0 0
|
||||||
|
|
||||||
|
newmtl light
|
||||||
|
Ka 20 20 20
|
||||||
|
Kd 1 1 1
|
||||||
|
Ks 0 0 0
|
||||||
18
models/invalid-face-definition.obj
Normal file
18
models/invalid-face-definition.obj
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
mtllib invalid-face-definition.mtl
|
||||||
|
|
||||||
|
v 0.000000 2.000000 2.000000
|
||||||
|
v 0.000000 0.000000 2.000000
|
||||||
|
v 2.000000 0.000000 2.000000
|
||||||
|
v 2.000000 2.000000 2.000000
|
||||||
|
v 0.000000 2.000000 0.000000
|
||||||
|
v 0.000000 0.000000 0.000000
|
||||||
|
v 2.000000 0.000000 0.000000
|
||||||
|
v 2.000000 2.000000 0.000000
|
||||||
|
# 8 vertices
|
||||||
|
|
||||||
|
g front cube
|
||||||
|
usemtl white
|
||||||
|
f 1
|
||||||
|
g back cube
|
||||||
|
# expects white material
|
||||||
|
f 8 7
|
||||||
23
models/issue-138.mtl
Normal file
23
models/issue-138.mtl
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
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
|
||||||
51
models/issue-138.obj
Normal file
51
models/issue-138.obj
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
|
||||||
|
# cube.obj
|
||||||
|
#
|
||||||
|
|
||||||
|
mtllib issue-138.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 2
|
||||||
|
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 5
|
||||||
|
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
|
||||||
2
models/issue-140-zero-face-idx.mtl
Normal file
2
models/issue-140-zero-face-idx.mtl
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
newmtl main
|
||||||
|
Kd 1 1 1
|
||||||
17
models/issue-140-zero-face-idx.obj
Normal file
17
models/issue-140-zero-face-idx.obj
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
mtllib issue-140-zero-face-idx.mtl
|
||||||
|
|
||||||
|
v -0.5 -0.5 0
|
||||||
|
v 0.5 -0.5 0
|
||||||
|
v 0.5 0.5 0
|
||||||
|
v -0.5 0.5 0
|
||||||
|
|
||||||
|
vt 0 0 0
|
||||||
|
vt 1 0 0
|
||||||
|
vt 1 1 0
|
||||||
|
vt 0 1 0
|
||||||
|
|
||||||
|
vn 0 0 -1
|
||||||
|
|
||||||
|
usemtl main
|
||||||
|
f 0/0/0 1/1/0 3/3/0
|
||||||
|
f 1/1/0 3/3/0 2/2/0
|
||||||
37
models/issue-161-inconsistent-f.obj
Normal file
37
models/issue-161-inconsistent-f.obj
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
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
|
||||||
23
models/issue-162-smoothing-group.mtl
Normal file
23
models/issue-162-smoothing-group.mtl
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
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
|
||||||
51
models/issue-162-smoothing-group.obj
Normal file
51
models/issue-162-smoothing-group.obj
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
|
||||||
|
# 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
|
||||||
16
models/line-prim.obj
Normal file
16
models/line-prim.obj
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
mtllib cube.mtl
|
||||||
|
|
||||||
|
v 0.000000 2.000000 2.000000
|
||||||
|
v 0.000000 0.000000 2.000000
|
||||||
|
v 2.000000 0.000000 2.000000
|
||||||
|
v 2.000000 2.000000 2.000000
|
||||||
|
v 0.000000 2.000000 0.000000
|
||||||
|
v 0.000000 0.000000 0.000000
|
||||||
|
v 2.000000 0.000000 0.000000
|
||||||
|
v 2.000000 2.000000 0.000000
|
||||||
|
# 8 vertices
|
||||||
|
|
||||||
|
g g0
|
||||||
|
usemtl white
|
||||||
|
l 1 2 3 4
|
||||||
|
l 5 6 7
|
||||||
10
models/map-bump.mtl
Normal file
10
models/map-bump.mtl
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
newmtl Material.001
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 0 0 0
|
||||||
|
Ks 0 0 0
|
||||||
|
map_Bump bump.jpg
|
||||||
|
|
||||||
|
newmtl Material.003
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 1 1 1
|
||||||
|
Ks 0 0 0
|
||||||
817
models/map-bump.obj
Normal file
817
models/map-bump.obj
Normal file
@@ -0,0 +1,817 @@
|
|||||||
|
# https://github.com/syoyo/tinyobjloader/issues/68
|
||||||
|
# Blender v2.73 (sub 0) OBJ File: 'enemy.blend'
|
||||||
|
# www.blender.org
|
||||||
|
mtllib map-bump.mtl
|
||||||
|
o Cube
|
||||||
|
v 1.864151 -1.219172 -5.532511
|
||||||
|
v 0.575869 -0.666304 5.896140
|
||||||
|
v 0.940448 1.000000 -1.971128
|
||||||
|
v 1.620345 1.000000 -5.815706
|
||||||
|
v 1.864152 1.000000 -6.334323
|
||||||
|
v 0.575869 -0.129842 5.896143
|
||||||
|
v 5.440438 -1.462153 -5.818601
|
||||||
|
v 4.896782 -1.462153 -2.744413
|
||||||
|
v 1.000825 -0.677484 1.899605
|
||||||
|
v 5.440438 -1.246362 -5.818600
|
||||||
|
v 1.000825 0.852342 1.899608
|
||||||
|
v 4.896782 -1.246362 -2.744412
|
||||||
|
v 1.160660 -0.450871 -2.356325
|
||||||
|
v 1.704316 -0.450871 -5.430513
|
||||||
|
v 1.000825 -0.351920 -1.293797
|
||||||
|
v 1.000825 1.000000 -1.293794
|
||||||
|
v 1.160660 -0.877888 -2.356326
|
||||||
|
v 1.704316 -0.877888 -5.430514
|
||||||
|
v 1.000825 -1.219172 -1.452514
|
||||||
|
v 1.000825 1.000000 -1.452511
|
||||||
|
v 1.000825 -0.351920 1.759410
|
||||||
|
v 1.000825 1.000000 1.759413
|
||||||
|
v 9.097919 1.221145 -6.212147
|
||||||
|
v 8.356775 1.221145 -2.021231
|
||||||
|
v 1.864151 -0.109586 -6.334325
|
||||||
|
v 0.575869 -0.398073 5.896141
|
||||||
|
v 9.097919 0.943958 -6.212148
|
||||||
|
v 8.356775 0.943958 -2.021233
|
||||||
|
v 1.061916 0.113661 -1.797961
|
||||||
|
v 1.000825 0.161258 1.899606
|
||||||
|
v 1.000825 0.324040 -1.293795
|
||||||
|
v 1.803060 0.113661 -5.988876
|
||||||
|
v 1.000825 -0.109586 -1.452513
|
||||||
|
v 1.061916 0.776753 -1.797960
|
||||||
|
v 1.803061 0.776753 -5.988875
|
||||||
|
v 1.000825 0.324040 1.759412
|
||||||
|
v 0.000825 -1.219172 -5.532512
|
||||||
|
v 0.000825 -0.666304 5.896139
|
||||||
|
v 0.000826 1.000000 -6.334325
|
||||||
|
v 0.000825 -0.129842 5.896140
|
||||||
|
v 0.000825 0.852342 1.899606
|
||||||
|
v 0.000825 -0.677484 1.899604
|
||||||
|
v 0.000825 -0.351920 -1.293797
|
||||||
|
v 0.000825 1.000000 -1.293796
|
||||||
|
v 0.000825 1.000000 -1.452513
|
||||||
|
v 0.000825 -1.219172 -1.452515
|
||||||
|
v 0.000825 -0.351920 1.759409
|
||||||
|
v 0.000825 1.000000 1.759411
|
||||||
|
v 0.000826 -0.109586 -6.334326
|
||||||
|
v 0.000825 -0.398073 5.896140
|
||||||
|
v 0.152918 1.000000 -5.815708
|
||||||
|
v 0.152917 1.000000 -1.971130
|
||||||
|
v 0.940448 1.168419 -1.971128
|
||||||
|
v 1.620345 1.168419 -5.815706
|
||||||
|
v 0.152918 1.168419 -5.815708
|
||||||
|
v 0.152917 1.168419 -1.971130
|
||||||
|
v 0.921118 1.091883 -1.050430
|
||||||
|
v 0.921118 1.091883 1.516050
|
||||||
|
v 0.080533 1.091883 -1.050432
|
||||||
|
v 0.080533 1.091883 1.516048
|
||||||
|
v 0.613003 -0.553430 5.546911
|
||||||
|
v 0.963691 -0.559956 2.248834
|
||||||
|
v 0.613003 -0.396857 5.546912
|
||||||
|
v 0.963691 -0.070362 2.248835
|
||||||
|
v 1.499370 -0.994317 3.966028
|
||||||
|
v 1.850058 -0.997914 0.667950
|
||||||
|
v 1.499370 -0.908021 3.966029
|
||||||
|
v 1.850058 -0.728071 0.667951
|
||||||
|
v 1.601022 0.760960 -6.334324
|
||||||
|
v 1.601021 0.129454 -6.334325
|
||||||
|
v 0.263955 0.760960 -6.334325
|
||||||
|
v 0.263955 0.129454 -6.334325
|
||||||
|
v 1.334809 0.760960 -7.515329
|
||||||
|
v 1.334809 0.129455 -7.515330
|
||||||
|
v 0.530168 0.760960 -7.515330
|
||||||
|
v 0.530168 0.129455 -7.515330
|
||||||
|
v 1.192720 0.649445 -7.515329
|
||||||
|
v 1.192720 0.240971 -7.515330
|
||||||
|
v 0.672258 0.649445 -7.515330
|
||||||
|
v 0.672258 0.240971 -7.515330
|
||||||
|
v 1.192719 0.649444 -6.524630
|
||||||
|
v 1.192719 0.240970 -6.524631
|
||||||
|
v 0.672257 0.649444 -6.524631
|
||||||
|
v 0.672257 0.240970 -6.524631
|
||||||
|
v 3.851026 0.431116 -1.883326
|
||||||
|
v 3.851026 0.946662 -1.883325
|
||||||
|
v 4.592170 0.946662 -6.074241
|
||||||
|
v 4.592169 0.431116 -6.074242
|
||||||
|
v 4.995714 0.561404 -1.918362
|
||||||
|
v 4.995714 1.016394 -1.918360
|
||||||
|
v 5.736857 1.016394 -6.109276
|
||||||
|
v 5.736857 0.561404 -6.109277
|
||||||
|
v 3.975454 0.471731 -2.162156
|
||||||
|
v 3.975454 0.919244 -2.162155
|
||||||
|
v 4.618796 0.919244 -5.800034
|
||||||
|
v 4.618795 0.471730 -5.800035
|
||||||
|
v 4.969088 0.584825 -2.192568
|
||||||
|
v 4.969088 0.979775 -2.192567
|
||||||
|
v 5.612430 0.979775 -5.830446
|
||||||
|
v 5.612429 0.584825 -5.830447
|
||||||
|
v 0.864214 -0.673890 3.184381
|
||||||
|
v 0.864213 0.489129 3.184384
|
||||||
|
v 0.864213 -0.018552 3.184383
|
||||||
|
v 0.000825 0.489129 3.184382
|
||||||
|
v 0.000825 -0.673890 3.184381
|
||||||
|
v 0.850955 -0.557858 3.309075
|
||||||
|
v 0.850955 -0.175321 3.309076
|
||||||
|
v 1.737321 -0.996758 1.728192
|
||||||
|
v 1.737321 -0.785920 1.728193
|
||||||
|
v -1.864151 -1.219172 -5.532511
|
||||||
|
v -0.575869 -0.666304 5.896140
|
||||||
|
v -0.940448 1.000000 -1.971128
|
||||||
|
v -1.620345 1.000000 -5.815706
|
||||||
|
v -1.864152 1.000000 -6.334323
|
||||||
|
v -0.575869 -0.129842 5.896143
|
||||||
|
v -5.440438 -1.462153 -5.818601
|
||||||
|
v -4.896782 -1.462153 -2.744413
|
||||||
|
v -1.000825 -0.677484 1.899605
|
||||||
|
v -5.440438 -1.246362 -5.818600
|
||||||
|
v -1.000825 0.852342 1.899608
|
||||||
|
v -4.896782 -1.246362 -2.744412
|
||||||
|
v -1.160660 -0.450871 -2.356325
|
||||||
|
v -1.704316 -0.450871 -5.430513
|
||||||
|
v -1.000825 -0.351920 -1.293797
|
||||||
|
v -1.000825 1.000000 -1.293794
|
||||||
|
v -1.160660 -0.877888 -2.356326
|
||||||
|
v -1.704316 -0.877888 -5.430514
|
||||||
|
v -1.000825 -1.219172 -1.452514
|
||||||
|
v -1.000825 1.000000 -1.452511
|
||||||
|
v -1.000825 -0.351920 1.759410
|
||||||
|
v -1.000825 1.000000 1.759413
|
||||||
|
v -9.097919 1.221145 -6.212147
|
||||||
|
v -8.356775 1.221145 -2.021231
|
||||||
|
v -1.864151 -0.109586 -6.334325
|
||||||
|
v -0.575869 -0.398073 5.896141
|
||||||
|
v -9.097919 0.943958 -6.212148
|
||||||
|
v -8.356775 0.943958 -2.021233
|
||||||
|
v -1.061916 0.113661 -1.797961
|
||||||
|
v -1.000825 0.161258 1.899606
|
||||||
|
v -1.000825 0.324040 -1.293795
|
||||||
|
v -1.803060 0.113661 -5.988876
|
||||||
|
v -1.000825 -0.109586 -1.452513
|
||||||
|
v -1.061916 0.776753 -1.797960
|
||||||
|
v -1.803061 0.776753 -5.988875
|
||||||
|
v -1.000825 0.324040 1.759412
|
||||||
|
v -0.000825 -1.219172 -5.532512
|
||||||
|
v -0.000825 -0.666304 5.896139
|
||||||
|
v -0.000826 1.000000 -6.334325
|
||||||
|
v -0.000825 -0.129842 5.896140
|
||||||
|
v -0.000825 0.852342 1.899606
|
||||||
|
v -0.000825 -0.677484 1.899604
|
||||||
|
v -0.000825 -0.351920 -1.293797
|
||||||
|
v -0.000825 1.000000 -1.293796
|
||||||
|
v -0.000825 1.000000 -1.452513
|
||||||
|
v -0.000825 -1.219172 -1.452515
|
||||||
|
v -0.000825 -0.351920 1.759409
|
||||||
|
v -0.000825 1.000000 1.759411
|
||||||
|
v -0.000826 -0.109586 -6.334326
|
||||||
|
v -0.000825 -0.398073 5.896140
|
||||||
|
v -0.152918 1.000000 -5.815708
|
||||||
|
v -0.152917 1.000000 -1.971130
|
||||||
|
v -0.940448 1.168419 -1.971128
|
||||||
|
v -1.620345 1.168419 -5.815706
|
||||||
|
v -0.152918 1.168419 -5.815708
|
||||||
|
v -0.152917 1.168419 -1.971130
|
||||||
|
v -0.921118 1.091883 -1.050430
|
||||||
|
v -0.921118 1.091883 1.516050
|
||||||
|
v -0.080533 1.091883 -1.050432
|
||||||
|
v -0.080533 1.091883 1.516048
|
||||||
|
v -0.613003 -0.553430 5.546911
|
||||||
|
v -0.963691 -0.559956 2.248834
|
||||||
|
v -0.613003 -0.396857 5.546912
|
||||||
|
v -0.963691 -0.070362 2.248835
|
||||||
|
v -1.499370 -0.994317 3.966028
|
||||||
|
v -1.850058 -0.997914 0.667950
|
||||||
|
v -1.499370 -0.908021 3.966029
|
||||||
|
v -1.850058 -0.728071 0.667951
|
||||||
|
v -1.601022 0.760960 -6.334324
|
||||||
|
v -1.601021 0.129454 -6.334325
|
||||||
|
v -0.263955 0.760960 -6.334325
|
||||||
|
v -0.263955 0.129454 -6.334325
|
||||||
|
v -1.334809 0.760960 -7.515329
|
||||||
|
v -1.334809 0.129455 -7.515330
|
||||||
|
v -0.530168 0.760960 -7.515330
|
||||||
|
v -0.530168 0.129455 -7.515330
|
||||||
|
v -1.192720 0.649445 -7.515329
|
||||||
|
v -1.192720 0.240971 -7.515330
|
||||||
|
v -0.672258 0.649445 -7.515330
|
||||||
|
v -0.672258 0.240971 -7.515330
|
||||||
|
v -1.192719 0.649444 -6.524630
|
||||||
|
v -1.192719 0.240970 -6.524631
|
||||||
|
v -0.672257 0.649444 -6.524631
|
||||||
|
v -0.672257 0.240970 -6.524631
|
||||||
|
v -3.851026 0.431116 -1.883326
|
||||||
|
v -3.851026 0.946662 -1.883325
|
||||||
|
v -4.592170 0.946662 -6.074241
|
||||||
|
v -4.592169 0.431116 -6.074242
|
||||||
|
v -4.995714 0.561404 -1.918362
|
||||||
|
v -4.995714 1.016394 -1.918360
|
||||||
|
v -5.736857 1.016394 -6.109276
|
||||||
|
v -5.736857 0.561404 -6.109277
|
||||||
|
v -3.975454 0.471731 -2.162156
|
||||||
|
v -3.975454 0.919244 -2.162155
|
||||||
|
v -4.618796 0.919244 -5.800034
|
||||||
|
v -4.618795 0.471730 -5.800035
|
||||||
|
v -4.969088 0.584825 -2.192568
|
||||||
|
v -4.969088 0.979775 -2.192567
|
||||||
|
v -5.612430 0.979775 -5.830446
|
||||||
|
v -5.612429 0.584825 -5.830447
|
||||||
|
v -0.864214 -0.673890 3.184381
|
||||||
|
v -0.864213 0.489129 3.184384
|
||||||
|
v -0.864213 -0.018552 3.184383
|
||||||
|
v -0.000825 0.489129 3.184382
|
||||||
|
v -0.000825 -0.673890 3.184381
|
||||||
|
v -0.850955 -0.557858 3.309075
|
||||||
|
v -0.850955 -0.175321 3.309076
|
||||||
|
v -1.737321 -0.996758 1.728192
|
||||||
|
v -1.737321 -0.785920 1.728193
|
||||||
|
vt 0.135351 -0.558072
|
||||||
|
vt 0.003035 -0.363507
|
||||||
|
vt 0.092282 -0.976844
|
||||||
|
vt -0.081322 0.947351
|
||||||
|
vt 0.100058 1.958891
|
||||||
|
vt 0.050091 1.852185
|
||||||
|
vt -0.092752 1.055565
|
||||||
|
vt -0.251711 1.059474
|
||||||
|
vt 0.075587 0.041384
|
||||||
|
vt -0.086008 0.279003
|
||||||
|
vt -0.086212 0.249830
|
||||||
|
vt -0.276044 1.968137
|
||||||
|
vt -0.246101 1.859467
|
||||||
|
vt 0.009828 1.911388
|
||||||
|
vt -0.133014 1.114769
|
||||||
|
vt 0.413322 1.261595
|
||||||
|
vt 0.299103 0.624605
|
||||||
|
vt 1.243955 0.407183
|
||||||
|
vt 0.515404 1.111487
|
||||||
|
vt 1.358173 1.044173
|
||||||
|
vt -0.081553 0.914324
|
||||||
|
vt 0.080042 0.676706
|
||||||
|
vt 0.401185 0.474498
|
||||||
|
vt 1.295541 0.331328
|
||||||
|
vt 0.365315 1.568841
|
||||||
|
vt 0.299111 1.575740
|
||||||
|
vt 0.143401 0.707357
|
||||||
|
vt 0.629403 1.011947
|
||||||
|
vt 0.449192 0.167251
|
||||||
|
vt 1.409760 0.968317
|
||||||
|
vt 0.986264 1.738667
|
||||||
|
vt 1.573373 1.877873
|
||||||
|
vt 1.417663 1.009490
|
||||||
|
vt 0.237182 -0.196235
|
||||||
|
vt 0.721785 1.030226
|
||||||
|
vt 0.830554 0.870285
|
||||||
|
vt 0.877494 1.898608
|
||||||
|
vt 1.351399 1.106930
|
||||||
|
vt 0.183935 0.557301
|
||||||
|
vt 1.507109 1.975312
|
||||||
|
vt 0.241636 0.439088
|
||||||
|
vt 0.114297 -0.045011
|
||||||
|
vt 0.140593 1.808834
|
||||||
|
vt -0.015118 0.940452
|
||||||
|
vt 0.156405 -1.071134
|
||||||
|
vt 0.164119 -0.998223
|
||||||
|
vt 0.040336 -1.068281
|
||||||
|
vt 0.104459 -1.162571
|
||||||
|
vt -0.165787 1.882802
|
||||||
|
vt -0.014821 1.660811
|
||||||
|
vt -0.287852 0.283965
|
||||||
|
vt -0.293374 0.366508
|
||||||
|
vt -0.289630 0.900550
|
||||||
|
vt 0.035337 -0.191272
|
||||||
|
vt 0.247348 0.172213
|
||||||
|
vt 0.253300 1.021193
|
||||||
|
vt -0.283166 0.952313
|
||||||
|
vt -0.283398 0.919286
|
||||||
|
vt 0.039792 0.444050
|
||||||
|
vt 0.314806 -0.339851
|
||||||
|
vt 0.112962 -0.334889
|
||||||
|
vt -0.288056 0.254793
|
||||||
|
vt -0.023788 -0.973990
|
||||||
|
vt -0.155922 -0.359599
|
||||||
|
vt 0.220528 -1.165425
|
||||||
|
vt 0.108710 -0.748730
|
||||||
|
vt -0.286364 1.918670
|
||||||
|
vt -0.291973 1.118678
|
||||||
|
vt -0.119962 0.896379
|
||||||
|
vt -0.123707 0.362337
|
||||||
|
vt 0.162891 -0.598569
|
||||||
|
vt 0.467532 -0.853353
|
||||||
|
vt 0.201549 -1.053262
|
||||||
|
vt 0.161663 -0.198915
|
||||||
|
vt 0.267667 -0.752638
|
||||||
|
vt 0.278705 -0.371021
|
||||||
|
vt 0.526390 -0.542053
|
||||||
|
vt 0.483821 -0.479457
|
||||||
|
vt 0.488162 -0.883689
|
||||||
|
vt 0.500110 -0.105561
|
||||||
|
vt 0.564618 -0.200418
|
||||||
|
vt -0.110331 2.127229
|
||||||
|
vt 0.040636 1.905238
|
||||||
|
vt -0.010786 1.578087
|
||||||
|
vt 0.104092 1.876168
|
||||||
|
vt 0.255058 1.654176
|
||||||
|
vt -0.054992 2.087323
|
||||||
|
vt 0.203048 1.901245
|
||||||
|
vt 0.052081 2.123235
|
||||||
|
vt 0.042658 1.943733
|
||||||
|
vt -0.056437 1.881175
|
||||||
|
vt 0.147710 1.941151
|
||||||
|
vt 0.050060 2.084741
|
||||||
|
vt 0.146264 1.735002
|
||||||
|
vt 0.041212 1.737584
|
||||||
|
vt 0.048615 1.878591
|
||||||
|
vt 0.663065 1.872485
|
||||||
|
vt 0.786311 1.691257
|
||||||
|
vt 0.507355 1.004102
|
||||||
|
vt 0.630601 0.822874
|
||||||
|
vt 0.955144 1.689498
|
||||||
|
vt 0.860727 1.828333
|
||||||
|
vt 0.725565 1.074543
|
||||||
|
vt 0.819981 0.935708
|
||||||
|
vt 0.674594 1.805657
|
||||||
|
vt 0.539432 1.051867
|
||||||
|
vt 0.646413 0.894554
|
||||||
|
vt 0.781576 1.648344
|
||||||
|
vt 0.240127 -0.712141
|
||||||
|
vn 0.994400 0.000000 0.105700
|
||||||
|
vn 0.000000 1.000000 0.000000
|
||||||
|
vn 1.000000 0.000000 0.000000
|
||||||
|
vn 0.984700 0.000000 0.174100
|
||||||
|
vn 0.211800 0.976600 0.037500
|
||||||
|
vn -0.103300 0.000000 -0.994600
|
||||||
|
vn 0.103300 -0.000000 0.994600
|
||||||
|
vn 0.911400 0.378700 0.161200
|
||||||
|
vn -0.157300 -0.987200 -0.027800
|
||||||
|
vn 0.113700 -0.993300 0.020100
|
||||||
|
vn 0.030600 -0.000000 0.999500
|
||||||
|
vn -0.061100 0.998100 -0.010800
|
||||||
|
vn -0.030600 0.000000 -0.999500
|
||||||
|
vn -0.000000 -0.000000 1.000000
|
||||||
|
vn 0.000000 0.000000 -1.000000
|
||||||
|
vn -0.755400 0.655300 0.000000
|
||||||
|
vn 0.000000 -1.000000 0.000000
|
||||||
|
vn -0.000000 -0.180000 0.983700
|
||||||
|
vn 0.000000 -0.395500 -0.918500
|
||||||
|
vn -0.000000 0.688500 0.725200
|
||||||
|
vn 0.000000 -0.585700 -0.810500
|
||||||
|
vn -0.000000 0.974900 0.222500
|
||||||
|
vn -0.000000 -1.000000 0.002800
|
||||||
|
vn -1.000000 0.000000 -0.000000
|
||||||
|
vn -0.000000 0.935500 0.353200
|
||||||
|
vn 0.755400 0.655300 0.000000
|
||||||
|
vn 0.000000 0.935500 -0.353200
|
||||||
|
vn 0.673800 0.724900 0.143400
|
||||||
|
vn 0.872300 -0.000000 0.489100
|
||||||
|
vn -0.872300 0.000000 -0.489100
|
||||||
|
vn -0.518300 -0.853500 -0.054200
|
||||||
|
vn -0.975500 0.000000 -0.219900
|
||||||
|
vn 0.975500 0.000000 -0.219900
|
||||||
|
vn -0.913200 0.000000 -0.407500
|
||||||
|
vn -0.436900 0.896200 -0.077300
|
||||||
|
vn -0.995300 -0.000000 0.096600
|
||||||
|
vn -0.297300 -0.953400 -0.052600
|
||||||
|
vn 0.473900 -0.876600 0.083800
|
||||||
|
vn 0.913200 0.000000 0.407500
|
||||||
|
vn 0.342200 0.937700 0.060500
|
||||||
|
vn 0.995300 -0.000000 -0.096600
|
||||||
|
vn -0.519200 -0.853000 -0.054300
|
||||||
|
vn 0.722400 0.676400 0.143800
|
||||||
|
vn -0.994400 0.000000 0.105700
|
||||||
|
vn -0.984700 0.000000 0.174100
|
||||||
|
vn -0.211800 0.976600 0.037500
|
||||||
|
vn 0.103300 0.000000 -0.994600
|
||||||
|
vn -0.103300 -0.000000 0.994600
|
||||||
|
vn -0.911400 0.378700 0.161200
|
||||||
|
vn 0.157300 -0.987200 -0.027800
|
||||||
|
vn -0.113700 -0.993300 0.020100
|
||||||
|
vn -0.030600 -0.000000 0.999500
|
||||||
|
vn 0.061100 0.998100 -0.010800
|
||||||
|
vn 0.030600 0.000000 -0.999500
|
||||||
|
vn -0.691900 0.713200 0.112500
|
||||||
|
vn -0.872300 -0.000000 0.489100
|
||||||
|
vn 0.872300 0.000000 -0.489100
|
||||||
|
vn 0.518300 -0.853500 -0.054200
|
||||||
|
vn 0.913200 0.000000 -0.407500
|
||||||
|
vn 0.436900 0.896200 -0.077300
|
||||||
|
vn 0.995300 0.000000 0.096600
|
||||||
|
vn 0.297300 -0.953300 -0.052600
|
||||||
|
vn -0.473900 -0.876600 0.083800
|
||||||
|
vn -0.913200 -0.000000 0.407500
|
||||||
|
vn -0.342200 0.937700 0.060500
|
||||||
|
vn -0.995300 -0.000000 -0.096600
|
||||||
|
vn 0.519200 -0.853000 -0.054300
|
||||||
|
vn -0.714800 0.690100 0.113700
|
||||||
|
vn 0.974400 0.089700 0.206200
|
||||||
|
vn 0.870400 0.288400 0.399100
|
||||||
|
vn 0.691900 0.713200 0.112500
|
||||||
|
vn -0.518000 -0.853700 -0.053400
|
||||||
|
vn -0.519700 -0.852700 -0.053600
|
||||||
|
vn 0.714800 0.690100 0.113700
|
||||||
|
vn -0.974400 0.089700 0.206200
|
||||||
|
vn -0.870400 0.288400 0.399100
|
||||||
|
vn -0.673800 0.724900 0.143400
|
||||||
|
vn 0.518000 -0.853700 -0.053400
|
||||||
|
vn 0.297300 -0.953400 -0.052600
|
||||||
|
vn 0.519700 -0.852700 -0.053600
|
||||||
|
vn -0.722400 0.676400 0.143800
|
||||||
|
vn -0.000000 0.962300 0.272000
|
||||||
|
usemtl Material.001
|
||||||
|
s off
|
||||||
|
f 103/1/1 102/2/1 6/3/1
|
||||||
|
f 20/4/2 5/5/2 4/6/2
|
||||||
|
f 20/4/2 3/7/2 52/8/2
|
||||||
|
f 36/9/3 22/10/3 11/11/3
|
||||||
|
f 39/12/2 51/13/2 4/6/2
|
||||||
|
f 4/6/4 54/14/4 53/15/4
|
||||||
|
f 14/16/5 13/17/5 12/18/5
|
||||||
|
f 18/19/6 14/16/6 10/20/6
|
||||||
|
f 20/4/3 16/21/3 31/22/3
|
||||||
|
f 17/23/7 8/24/7 12/18/7
|
||||||
|
f 25/25/4 32/26/4 29/27/4
|
||||||
|
f 10/20/4 12/18/4 8/24/4
|
||||||
|
f 1/28/8 18/19/8 17/23/8
|
||||||
|
f 19/29/4 17/23/4 13/17/4
|
||||||
|
f 25/25/4 14/16/4 18/19/4
|
||||||
|
f 18/19/9 7/30/9 8/24/9
|
||||||
|
f 92/31/10 27/32/10 28/33/10
|
||||||
|
f 16/21/3 22/10/3 36/9/3
|
||||||
|
f 31/22/3 36/9/3 21/34/3
|
||||||
|
f 90/35/11 89/36/11 28/33/11
|
||||||
|
f 91/37/12 90/35/12 24/38/12
|
||||||
|
f 33/39/4 13/17/4 14/16/4
|
||||||
|
f 23/40/4 24/38/4 28/33/4
|
||||||
|
f 33/39/3 31/22/3 15/41/3
|
||||||
|
f 21/34/3 36/9/3 30/42/3
|
||||||
|
f 5/5/4 35/43/4 32/26/4
|
||||||
|
f 5/5/4 20/4/4 34/44/4
|
||||||
|
f 33/39/4 29/27/4 34/44/4
|
||||||
|
f 91/37/13 23/40/13 27/32/13
|
||||||
|
f 103/1/1 26/45/1 63/46/1
|
||||||
|
f 26/45/14 50/47/14 38/48/14
|
||||||
|
f 39/12/15 71/49/15 72/50/15
|
||||||
|
f 48/51/16 60/52/16 59/53/16
|
||||||
|
f 15/41/17 21/34/17 47/54/17
|
||||||
|
f 19/29/17 46/55/17 37/56/17
|
||||||
|
f 39/12/2 45/57/2 52/8/2
|
||||||
|
f 20/4/2 45/57/2 44/58/2
|
||||||
|
f 19/29/18 15/41/18 43/59/18
|
||||||
|
f 9/60/19 42/61/19 47/54/19
|
||||||
|
f 22/10/20 48/51/20 41/62/20
|
||||||
|
f 25/25/21 1/28/21 37/56/21
|
||||||
|
f 6/3/14 40/63/14 50/47/14
|
||||||
|
f 104/64/22 40/63/22 6/3/22
|
||||||
|
f 2/65/23 38/48/23 105/66/23
|
||||||
|
f 55/67/2 56/68/2 53/15/2
|
||||||
|
f 3/7/14 53/15/14 56/68/14
|
||||||
|
f 51/13/15 55/67/15 54/14/15
|
||||||
|
f 52/8/24 56/68/24 55/67/24
|
||||||
|
f 57/69/2 59/53/2 60/52/2
|
||||||
|
f 48/51/25 22/10/25 58/70/25
|
||||||
|
f 16/21/26 57/69/26 58/70/26
|
||||||
|
f 16/21/27 44/58/27 59/53/27
|
||||||
|
f 107/71/28 63/46/28 67/72/28
|
||||||
|
f 26/45/1 2/65/1 61/73/1
|
||||||
|
f 9/60/1 30/42/1 64/74/1
|
||||||
|
f 101/75/1 9/60/1 62/76/1
|
||||||
|
f 108/77/1 109/78/1 67/72/1
|
||||||
|
f 61/73/29 65/79/29 67/72/29
|
||||||
|
f 62/76/30 64/74/30 68/80/30
|
||||||
|
f 62/76/31 66/81/31 108/77/31
|
||||||
|
f 71/49/32 75/82/32 76/83/32
|
||||||
|
f 25/25/15 49/84/15 72/50/15
|
||||||
|
f 5/5/15 69/85/15 71/49/15
|
||||||
|
f 25/25/15 70/86/15 69/85/15
|
||||||
|
f 76/83/15 75/82/15 79/87/15
|
||||||
|
f 72/50/17 76/83/17 74/88/17
|
||||||
|
f 71/49/2 69/85/2 73/89/2
|
||||||
|
f 70/86/33 74/88/33 73/89/33
|
||||||
|
f 80/90/3 79/87/3 83/91/3
|
||||||
|
f 76/83/15 80/90/15 78/92/15
|
||||||
|
f 75/82/15 73/89/15 77/93/15
|
||||||
|
f 74/88/15 78/92/15 77/93/15
|
||||||
|
f 82/94/15 84/95/15 83/91/15
|
||||||
|
f 80/90/2 84/95/2 82/94/2
|
||||||
|
f 77/93/17 81/96/17 83/91/17
|
||||||
|
f 77/93/24 78/92/24 82/94/24
|
||||||
|
f 35/43/13 87/97/13 88/98/13
|
||||||
|
f 35/43/12 34/44/12 86/99/12
|
||||||
|
f 34/44/11 29/27/11 85/100/11
|
||||||
|
f 32/26/10 88/98/10 85/100/10
|
||||||
|
f 92/31/34 100/101/34 99/102/34
|
||||||
|
f 90/35/35 91/37/35 99/102/35
|
||||||
|
f 89/36/36 90/35/36 98/103/36
|
||||||
|
f 89/36/37 97/104/37 100/101/37
|
||||||
|
f 95/105/13 99/102/13 100/101/13
|
||||||
|
f 95/105/12 94/106/12 98/103/12
|
||||||
|
f 94/106/11 93/107/11 97/104/11
|
||||||
|
f 96/108/10 100/101/10 97/104/10
|
||||||
|
f 88/98/38 96/108/38 93/107/38
|
||||||
|
f 86/99/39 85/100/39 93/107/39
|
||||||
|
f 87/97/40 86/99/40 94/106/40
|
||||||
|
f 87/97/41 95/105/41 96/108/41
|
||||||
|
f 106/109/42 108/77/42 65/79/42
|
||||||
|
f 66/81/1 68/80/1 109/78/1
|
||||||
|
f 101/75/1 106/109/1 61/73/1
|
||||||
|
f 64/74/43 107/71/43 109/78/43
|
||||||
|
f 101/75/23 105/66/23 42/61/23
|
||||||
|
f 103/1/1 107/71/1 64/74/1
|
||||||
|
f 30/42/1 11/11/1 102/2/1
|
||||||
|
f 212/1/44 135/45/44 115/3/44
|
||||||
|
f 129/4/2 112/7/2 113/6/2
|
||||||
|
f 161/8/2 112/7/2 129/4/2
|
||||||
|
f 145/9/24 139/42/24 120/11/24
|
||||||
|
f 113/6/2 160/13/2 148/12/2
|
||||||
|
f 162/15/45 163/14/45 113/6/45
|
||||||
|
f 123/16/46 119/20/46 121/18/46
|
||||||
|
f 127/19/47 116/30/47 119/20/47
|
||||||
|
f 140/22/24 125/21/24 129/4/24
|
||||||
|
f 121/18/48 117/24/48 126/23/48
|
||||||
|
f 138/27/45 141/26/45 134/25/45
|
||||||
|
f 117/24/45 121/18/45 119/20/45
|
||||||
|
f 126/23/49 127/19/49 110/28/49
|
||||||
|
f 122/17/45 126/23/45 128/29/45
|
||||||
|
f 127/19/45 123/16/45 134/25/45
|
||||||
|
f 117/24/50 116/30/50 127/19/50
|
||||||
|
f 137/33/51 136/32/51 201/31/51
|
||||||
|
f 145/9/24 131/10/24 125/21/24
|
||||||
|
f 130/34/24 145/9/24 140/22/24
|
||||||
|
f 199/35/52 133/38/52 137/33/52
|
||||||
|
f 200/37/53 132/40/53 133/38/53
|
||||||
|
f 123/16/45 122/17/45 142/39/45
|
||||||
|
f 137/33/45 133/38/45 132/40/45
|
||||||
|
f 124/41/24 140/22/24 142/39/24
|
||||||
|
f 130/34/24 118/60/24 139/42/24
|
||||||
|
f 141/26/45 144/43/45 114/5/45
|
||||||
|
f 114/5/45 144/43/45 143/44/45
|
||||||
|
f 143/44/45 138/27/45 142/39/45
|
||||||
|
f 136/32/54 132/40/54 200/37/54
|
||||||
|
f 212/1/44 216/71/44 172/46/44
|
||||||
|
f 147/48/14 159/47/14 135/45/14
|
||||||
|
f 181/50/15 180/49/15 148/12/15
|
||||||
|
f 168/53/26 169/52/26 157/51/26
|
||||||
|
f 124/41/17 152/59/17 156/54/17
|
||||||
|
f 146/56/17 155/55/17 128/29/17
|
||||||
|
f 148/12/2 160/13/2 161/8/2
|
||||||
|
f 129/4/2 125/21/2 153/58/2
|
||||||
|
f 155/55/18 152/59/18 124/41/18
|
||||||
|
f 130/34/19 156/54/19 151/61/19
|
||||||
|
f 131/10/20 120/11/20 150/62/20
|
||||||
|
f 134/25/21 158/84/21 146/56/21
|
||||||
|
f 159/47/14 149/63/14 115/3/14
|
||||||
|
f 115/3/22 149/63/22 213/64/22
|
||||||
|
f 214/66/23 147/48/23 111/65/23
|
||||||
|
f 162/15/2 165/68/2 164/67/2
|
||||||
|
f 165/68/14 162/15/14 112/7/14
|
||||||
|
f 163/14/15 164/67/15 160/13/15
|
||||||
|
f 164/67/3 165/68/3 161/8/3
|
||||||
|
f 166/69/2 167/70/2 169/52/2
|
||||||
|
f 157/51/25 169/52/25 167/70/25
|
||||||
|
f 167/70/16 166/69/16 125/21/16
|
||||||
|
f 125/21/27 166/69/27 168/53/27
|
||||||
|
f 216/71/55 218/78/55 176/72/55
|
||||||
|
f 135/45/44 172/46/44 170/73/44
|
||||||
|
f 118/60/44 171/76/44 173/74/44
|
||||||
|
f 210/75/44 215/109/44 171/76/44
|
||||||
|
f 217/77/44 174/79/44 176/72/44
|
||||||
|
f 176/72/56 174/79/56 170/73/56
|
||||||
|
f 171/76/57 175/81/57 177/80/57
|
||||||
|
f 217/77/58 175/81/58 171/76/58
|
||||||
|
f 185/83/33 184/82/33 180/49/33
|
||||||
|
f 134/25/15 179/86/15 181/50/15
|
||||||
|
f 180/49/15 178/85/15 114/5/15
|
||||||
|
f 178/85/15 179/86/15 134/25/15
|
||||||
|
f 189/90/15 188/87/15 184/82/15
|
||||||
|
f 183/88/17 185/83/17 181/50/17
|
||||||
|
f 180/49/2 184/82/2 182/89/2
|
||||||
|
f 182/89/32 183/88/32 179/86/32
|
||||||
|
f 189/90/24 193/95/24 192/91/24
|
||||||
|
f 187/92/15 189/90/15 185/83/15
|
||||||
|
f 184/82/15 188/87/15 186/93/15
|
||||||
|
f 186/93/15 187/92/15 183/88/15
|
||||||
|
f 192/91/15 193/95/15 191/94/15
|
||||||
|
f 191/94/2 193/95/2 189/90/2
|
||||||
|
f 192/91/17 190/96/17 186/93/17
|
||||||
|
f 186/93/3 190/96/3 191/94/3
|
||||||
|
f 197/98/54 196/97/54 144/43/54
|
||||||
|
f 144/43/53 196/97/53 195/99/53
|
||||||
|
f 143/44/52 195/99/52 194/100/52
|
||||||
|
f 194/100/51 197/98/51 141/26/51
|
||||||
|
f 208/102/59 209/101/59 201/31/59
|
||||||
|
f 199/35/60 207/103/60 208/102/60
|
||||||
|
f 198/36/61 206/104/61 207/103/61
|
||||||
|
f 209/101/62 206/104/62 198/36/62
|
||||||
|
f 209/101/54 208/102/54 204/105/54
|
||||||
|
f 204/105/53 208/102/53 207/103/53
|
||||||
|
f 203/106/52 207/103/52 206/104/52
|
||||||
|
f 206/104/51 209/101/51 205/108/51
|
||||||
|
f 202/107/63 205/108/63 197/98/63
|
||||||
|
f 195/99/64 203/106/64 202/107/64
|
||||||
|
f 196/97/65 204/105/65 203/106/65
|
||||||
|
f 205/108/66 204/105/66 196/97/66
|
||||||
|
f 174/79/67 217/77/67 215/109/67
|
||||||
|
f 175/81/44 217/77/44 218/78/44
|
||||||
|
f 170/73/44 215/109/44 210/75/44
|
||||||
|
f 173/74/68 177/80/68 218/78/68
|
||||||
|
f 151/61/23 214/66/23 210/75/23
|
||||||
|
f 173/74/44 216/71/44 212/1/44
|
||||||
|
f 139/42/44 212/1/44 211/2/44
|
||||||
|
f 26/45/1 103/1/1 6/3/1
|
||||||
|
f 3/7/2 20/4/2 4/6/2
|
||||||
|
f 45/57/2 20/4/2 52/8/2
|
||||||
|
f 30/42/3 36/9/3 11/11/3
|
||||||
|
f 5/5/2 39/12/2 4/6/2
|
||||||
|
f 3/7/4 4/6/4 53/15/4
|
||||||
|
f 10/20/5 14/16/5 12/18/5
|
||||||
|
f 7/30/6 18/19/6 10/20/6
|
||||||
|
f 33/39/3 20/4/3 31/22/3
|
||||||
|
f 13/17/7 17/23/7 12/18/7
|
||||||
|
f 33/39/4 25/25/4 29/27/4
|
||||||
|
f 7/30/4 10/20/4 8/24/4
|
||||||
|
f 19/29/69 1/28/69 17/23/69
|
||||||
|
f 33/39/4 19/29/4 13/17/4
|
||||||
|
f 1/28/70 25/25/70 18/19/70
|
||||||
|
f 17/23/9 18/19/9 8/24/9
|
||||||
|
f 89/36/10 92/31/10 28/33/10
|
||||||
|
f 31/22/3 16/21/3 36/9/3
|
||||||
|
f 15/41/3 31/22/3 21/34/3
|
||||||
|
f 24/38/11 90/35/11 28/33/11
|
||||||
|
f 23/40/12 91/37/12 24/38/12
|
||||||
|
f 25/25/4 33/39/4 14/16/4
|
||||||
|
f 27/32/4 23/40/4 28/33/4
|
||||||
|
f 19/29/3 33/39/3 15/41/3
|
||||||
|
f 9/60/3 21/34/3 30/42/3
|
||||||
|
f 25/25/4 5/5/4 32/26/4
|
||||||
|
f 35/43/4 5/5/4 34/44/4
|
||||||
|
f 20/4/4 33/39/4 34/44/4
|
||||||
|
f 92/31/13 91/37/13 27/32/13
|
||||||
|
f 107/71/1 103/1/1 63/46/1
|
||||||
|
f 2/65/14 26/45/14 38/48/14
|
||||||
|
f 49/84/15 39/12/15 72/50/15
|
||||||
|
f 44/58/16 48/51/16 59/53/16
|
||||||
|
f 43/59/17 15/41/17 47/54/17
|
||||||
|
f 1/28/17 19/29/17 37/56/17
|
||||||
|
f 51/13/2 39/12/2 52/8/2
|
||||||
|
f 16/21/2 20/4/2 44/58/2
|
||||||
|
f 46/55/18 19/29/18 43/59/18
|
||||||
|
f 21/34/19 9/60/19 47/54/19
|
||||||
|
f 11/11/20 22/10/20 41/62/20
|
||||||
|
f 49/84/21 25/25/21 37/56/21
|
||||||
|
f 26/45/14 6/3/14 50/47/14
|
||||||
|
f 102/2/22 104/64/22 6/3/22
|
||||||
|
f 101/75/23 2/65/23 105/66/23
|
||||||
|
f 54/14/2 55/67/2 53/15/2
|
||||||
|
f 52/8/14 3/7/14 56/68/14
|
||||||
|
f 4/6/15 51/13/15 54/14/15
|
||||||
|
f 51/13/24 52/8/24 55/67/24
|
||||||
|
f 58/70/2 57/69/2 60/52/2
|
||||||
|
f 60/52/25 48/51/25 58/70/25
|
||||||
|
f 22/10/26 16/21/26 58/70/26
|
||||||
|
f 57/69/27 16/21/27 59/53/27
|
||||||
|
f 109/78/71 107/71/71 67/72/71
|
||||||
|
f 63/46/1 26/45/1 61/73/1
|
||||||
|
f 62/76/1 9/60/1 64/74/1
|
||||||
|
f 106/109/1 101/75/1 62/76/1
|
||||||
|
f 65/79/1 108/77/1 67/72/1
|
||||||
|
f 63/46/29 61/73/29 67/72/29
|
||||||
|
f 66/81/30 62/76/30 68/80/30
|
||||||
|
f 106/109/72 62/76/72 108/77/72
|
||||||
|
f 72/50/32 71/49/32 76/83/32
|
||||||
|
f 70/86/15 25/25/15 72/50/15
|
||||||
|
f 39/12/15 5/5/15 71/49/15
|
||||||
|
f 5/5/15 25/25/15 69/85/15
|
||||||
|
f 80/90/15 76/83/15 79/87/15
|
||||||
|
f 70/86/17 72/50/17 74/88/17
|
||||||
|
f 75/82/2 71/49/2 73/89/2
|
||||||
|
f 69/85/33 70/86/33 73/89/33
|
||||||
|
f 84/95/3 80/90/3 83/91/3
|
||||||
|
f 74/88/15 76/83/15 78/92/15
|
||||||
|
f 79/87/15 75/82/15 77/93/15
|
||||||
|
f 73/89/15 74/88/15 77/93/15
|
||||||
|
f 81/96/15 82/94/15 83/91/15
|
||||||
|
f 78/92/2 80/90/2 82/94/2
|
||||||
|
f 79/87/17 77/93/17 83/91/17
|
||||||
|
f 81/96/24 77/93/24 82/94/24
|
||||||
|
f 32/26/13 35/43/13 88/98/13
|
||||||
|
f 87/97/12 35/43/12 86/99/12
|
||||||
|
f 86/99/11 34/44/11 85/100/11
|
||||||
|
f 29/27/10 32/26/10 85/100/10
|
||||||
|
f 91/37/34 92/31/34 99/102/34
|
||||||
|
f 98/103/35 90/35/35 99/102/35
|
||||||
|
f 97/104/36 89/36/36 98/103/36
|
||||||
|
f 92/31/37 89/36/37 100/101/37
|
||||||
|
f 96/108/13 95/105/13 100/101/13
|
||||||
|
f 99/102/12 95/105/12 98/103/12
|
||||||
|
f 98/103/11 94/106/11 97/104/11
|
||||||
|
f 93/107/10 96/108/10 97/104/10
|
||||||
|
f 85/100/38 88/98/38 93/107/38
|
||||||
|
f 94/106/39 86/99/39 93/107/39
|
||||||
|
f 95/105/40 87/97/40 94/106/40
|
||||||
|
f 88/98/41 87/97/41 96/108/41
|
||||||
|
f 61/73/73 106/109/73 65/79/73
|
||||||
|
f 108/77/1 66/81/1 109/78/1
|
||||||
|
f 2/65/1 101/75/1 61/73/1
|
||||||
|
f 68/80/74 64/74/74 109/78/74
|
||||||
|
f 9/60/23 101/75/23 42/61/23
|
||||||
|
f 30/42/1 103/1/1 64/74/1
|
||||||
|
f 103/1/1 30/42/1 102/2/1
|
||||||
|
f 211/2/44 212/1/44 115/3/44
|
||||||
|
f 114/5/2 129/4/2 113/6/2
|
||||||
|
f 154/57/2 161/8/2 129/4/2
|
||||||
|
f 131/10/24 145/9/24 120/11/24
|
||||||
|
f 114/5/2 113/6/2 148/12/2
|
||||||
|
f 112/7/45 162/15/45 113/6/45
|
||||||
|
f 122/17/46 123/16/46 121/18/46
|
||||||
|
f 123/16/47 127/19/47 119/20/47
|
||||||
|
f 142/39/24 140/22/24 129/4/24
|
||||||
|
f 122/17/48 121/18/48 126/23/48
|
||||||
|
f 142/39/45 138/27/45 134/25/45
|
||||||
|
f 116/30/45 117/24/45 119/20/45
|
||||||
|
f 128/29/75 126/23/75 110/28/75
|
||||||
|
f 142/39/45 122/17/45 128/29/45
|
||||||
|
f 110/28/76 127/19/76 134/25/76
|
||||||
|
f 126/23/50 117/24/50 127/19/50
|
||||||
|
f 198/36/51 137/33/51 201/31/51
|
||||||
|
f 140/22/24 145/9/24 125/21/24
|
||||||
|
f 124/41/24 130/34/24 140/22/24
|
||||||
|
f 198/36/52 199/35/52 137/33/52
|
||||||
|
f 199/35/53 200/37/53 133/38/53
|
||||||
|
f 134/25/45 123/16/45 142/39/45
|
||||||
|
f 136/32/45 137/33/45 132/40/45
|
||||||
|
f 128/29/24 124/41/24 142/39/24
|
||||||
|
f 145/9/24 130/34/24 139/42/24
|
||||||
|
f 134/25/45 141/26/45 114/5/45
|
||||||
|
f 129/4/45 114/5/45 143/44/45
|
||||||
|
f 129/4/45 143/44/45 142/39/45
|
||||||
|
f 201/31/54 136/32/54 200/37/54
|
||||||
|
f 135/45/44 212/1/44 172/46/44
|
||||||
|
f 111/65/14 147/48/14 135/45/14
|
||||||
|
f 158/84/15 181/50/15 148/12/15
|
||||||
|
f 153/58/26 168/53/26 157/51/26
|
||||||
|
f 130/34/17 124/41/17 156/54/17
|
||||||
|
f 110/28/17 146/56/17 128/29/17
|
||||||
|
f 154/57/2 148/12/2 161/8/2
|
||||||
|
f 154/57/2 129/4/2 153/58/2
|
||||||
|
f 128/29/18 155/55/18 124/41/18
|
||||||
|
f 118/60/19 130/34/19 151/61/19
|
||||||
|
f 157/51/20 131/10/20 150/62/20
|
||||||
|
f 110/28/21 134/25/21 146/56/21
|
||||||
|
f 135/45/14 159/47/14 115/3/14
|
||||||
|
f 211/2/22 115/3/22 213/64/22
|
||||||
|
f 210/75/23 214/66/23 111/65/23
|
||||||
|
f 163/14/2 162/15/2 164/67/2
|
||||||
|
f 161/8/14 165/68/14 112/7/14
|
||||||
|
f 113/6/15 163/14/15 160/13/15
|
||||||
|
f 160/13/3 164/67/3 161/8/3
|
||||||
|
f 168/53/2 166/69/2 169/52/2
|
||||||
|
f 131/10/25 157/51/25 167/70/25
|
||||||
|
f 131/10/16 167/70/16 125/21/16
|
||||||
|
f 153/58/27 125/21/27 168/53/27
|
||||||
|
f 172/46/77 216/71/77 176/72/77
|
||||||
|
f 111/65/44 135/45/44 170/73/44
|
||||||
|
f 139/42/44 118/60/44 173/74/44
|
||||||
|
f 118/60/44 210/75/44 171/76/44
|
||||||
|
f 218/78/44 217/77/44 176/72/44
|
||||||
|
f 172/46/56 176/72/56 170/73/56
|
||||||
|
f 173/74/57 171/76/57 177/80/57
|
||||||
|
f 215/109/78 217/77/78 171/76/78
|
||||||
|
f 181/50/33 185/83/33 180/49/33
|
||||||
|
f 158/84/15 134/25/15 181/50/15
|
||||||
|
f 148/12/15 180/49/15 114/5/15
|
||||||
|
f 114/5/15 178/85/15 134/25/15
|
||||||
|
f 185/83/15 189/90/15 184/82/15
|
||||||
|
f 179/86/17 183/88/17 181/50/17
|
||||||
|
f 178/85/2 180/49/2 182/89/2
|
||||||
|
f 178/85/32 182/89/32 179/86/32
|
||||||
|
f 188/87/24 189/90/24 192/91/24
|
||||||
|
f 183/88/15 187/92/15 185/83/15
|
||||||
|
f 182/89/15 184/82/15 186/93/15
|
||||||
|
f 182/89/15 186/93/15 183/88/15
|
||||||
|
f 190/96/15 192/91/15 191/94/15
|
||||||
|
f 187/92/2 191/94/2 189/90/2
|
||||||
|
f 188/87/17 192/91/17 186/93/17
|
||||||
|
f 187/92/3 186/93/3 191/94/3
|
||||||
|
f 141/26/54 197/98/54 144/43/54
|
||||||
|
f 143/44/53 144/43/53 195/99/53
|
||||||
|
f 138/27/52 143/44/52 194/100/52
|
||||||
|
f 138/27/51 194/100/51 141/26/51
|
||||||
|
f 200/37/59 208/102/59 201/31/59
|
||||||
|
f 200/37/60 199/35/60 208/102/60
|
||||||
|
f 199/35/61 198/36/61 207/103/61
|
||||||
|
f 201/31/79 209/101/79 198/36/79
|
||||||
|
f 205/108/54 209/101/54 204/105/54
|
||||||
|
f 203/106/53 204/105/53 207/103/53
|
||||||
|
f 202/107/52 203/106/52 206/104/52
|
||||||
|
f 202/107/51 206/104/51 205/108/51
|
||||||
|
f 194/100/63 202/107/63 197/98/63
|
||||||
|
f 194/100/64 195/99/64 202/107/64
|
||||||
|
f 195/99/65 196/97/65 203/106/65
|
||||||
|
f 197/98/66 205/108/66 196/97/66
|
||||||
|
f 170/73/80 174/79/80 215/109/80
|
||||||
|
f 177/80/44 175/81/44 218/78/44
|
||||||
|
f 111/65/44 170/73/44 210/75/44
|
||||||
|
f 216/71/81 173/74/81 218/78/81
|
||||||
|
f 118/60/23 151/61/23 210/75/23
|
||||||
|
f 139/42/44 173/74/44 212/1/44
|
||||||
|
f 120/11/44 139/42/44 211/2/44
|
||||||
|
usemtl Material.003
|
||||||
|
f 41/62/82 104/64/82 102/2/82
|
||||||
|
f 211/2/82 213/64/82 150/62/82
|
||||||
|
f 11/11/82 41/62/82 102/2/82
|
||||||
|
f 120/11/82 211/2/82 150/62/82
|
||||||
7
models/norm-texopt.mtl
Normal file
7
models/norm-texopt.mtl
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
newmtl default
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 0 0 0
|
||||||
|
Ks 0 0 0
|
||||||
|
Kt 0.1 0.2 0.3
|
||||||
|
norm -bm 3 normalmap.jpg
|
||||||
|
|
||||||
7
models/norm-texopt.obj
Normal file
7
models/norm-texopt.obj
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
mtllib norm-texopt.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
|
||||||
25
models/refl.mtl
Normal file
25
models/refl.mtl
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
newmtl white
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 1 1 1
|
||||||
|
Ks 0 0 0
|
||||||
|
refl reflection.tga
|
||||||
|
|
||||||
|
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
|
||||||
32
models/refl.obj
Normal file
32
models/refl.obj
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# Test for `refl` material parameter
|
||||||
|
mtllib refl.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
|
||||||
22
models/smoothing-group-two-squares.obj
Normal file
22
models/smoothing-group-two-squares.obj
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
# 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
|
||||||
24
models/smoothing-normal.mtl
Normal file
24
models/smoothing-normal.mtl
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
newmtl white
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 1 1 1
|
||||||
|
Ks 0 0 0
|
||||||
|
|
||||||
|
newmtl red
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 1 0 0
|
||||||
|
Ks 0 0 0
|
||||||
|
|
||||||
|
newmtl green
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 0 1 0
|
||||||
|
Ks 0 0 0
|
||||||
|
|
||||||
|
newmtl blue
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 0 0 1
|
||||||
|
Ks 0 0 0
|
||||||
|
|
||||||
|
newmtl light
|
||||||
|
Ka 20 20 20
|
||||||
|
Kd 1 1 1
|
||||||
|
Ks 0 0 0
|
||||||
35
models/smoothing-normal.obj
Normal file
35
models/smoothing-normal.obj
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
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
|
||||||
28
models/texture-filename-with-whitespace.mtl
Normal file
28
models/texture-filename-with-whitespace.mtl
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
newmtl white
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 1 1 1
|
||||||
|
Ks 0 0 0
|
||||||
|
# filename with white space.
|
||||||
|
map_Kd texture 01.png
|
||||||
|
|
||||||
|
newmtl red
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 1 0 0
|
||||||
|
Ks 0 0 0
|
||||||
|
# texture option + filename with white space.
|
||||||
|
bump -bm 2 bump 01.png
|
||||||
|
|
||||||
|
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
models/texture-filename-with-whitespace.obj
Normal file
31
models/texture-filename-with-whitespace.obj
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
mtllib texture-filename-with-whitespace.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
|
||||||
157
python/main.cpp
157
python/main.cpp
@@ -18,43 +18,37 @@ typedef std::vector<int> vecti;
|
|||||||
PyObject* pyTupleFromfloat3(float array[3]) {
|
PyObject* pyTupleFromfloat3(float array[3]) {
|
||||||
int i;
|
int i;
|
||||||
PyObject* tuple = PyTuple_New(3);
|
PyObject* tuple = PyTuple_New(3);
|
||||||
|
|
||||||
for (i = 0; i <= 2; i++) {
|
for (i = 0; i <= 2; i++) {
|
||||||
PyTuple_SetItem(tuple, i, PyFloat_FromDouble(array[i]));
|
PyTuple_SetItem(tuple, i, PyFloat_FromDouble(array[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
return tuple;
|
return tuple;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
static PyObject* pyLoadObj(PyObject* self, PyObject* args) {
|
static PyObject* pyLoadObj(PyObject* self, PyObject* args) {
|
||||||
PyObject *rtndict, *pyshapes, *pymaterials, *attribobj, *current, *meshobj;
|
PyObject *rtndict, *pyshapes, *pymaterials, *pymaterial_indices, *attribobj, *current, *meshobj;
|
||||||
|
|
||||||
char const* current_name;
|
char const* current_name;
|
||||||
char const* filename;
|
char const* filename;
|
||||||
vectd vect;
|
vectd vect;
|
||||||
std::vector<tinyobj::index_t> indices;
|
std::vector<tinyobj::index_t> indices;
|
||||||
std::vector<unsigned char> face_verts;
|
std::vector<unsigned char> face_verts;
|
||||||
|
|
||||||
tinyobj::attrib_t attrib;
|
tinyobj::attrib_t attrib;
|
||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "s", &filename)) return NULL;
|
if (!PyArg_ParseTuple(args, "s", &filename)) return NULL;
|
||||||
|
|
||||||
std::string err;
|
std::string err, warn;
|
||||||
tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename);
|
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename);
|
||||||
|
|
||||||
pyshapes = PyDict_New();
|
pyshapes = PyDict_New();
|
||||||
pymaterials = PyDict_New();
|
pymaterials = PyDict_New();
|
||||||
|
pymaterial_indices = PyList_New(0);
|
||||||
rtndict = PyDict_New();
|
rtndict = PyDict_New();
|
||||||
|
|
||||||
attribobj = PyDict_New();
|
attribobj = PyDict_New();
|
||||||
|
|
||||||
for (int i = 0; i <= 2; i++) {
|
for (int i = 0; i <= 2; i++) {
|
||||||
current = PyList_New(0);
|
current = PyList_New(0);
|
||||||
|
|
||||||
switch (i) {
|
switch (i) {
|
||||||
case 0:
|
case 0:
|
||||||
current_name = "vertices";
|
current_name = "vertices";
|
||||||
@@ -71,122 +65,137 @@ static PyObject* pyLoadObj(PyObject* self, PyObject* args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (vectd::iterator it = vect.begin(); it != vect.end(); it++) {
|
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);
|
PyDict_SetItemString(attribobj, current_name, current);
|
||||||
|
Py_DECREF(current);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (std::vector<tinyobj::shape_t>::iterator shape = shapes.begin();
|
for (std::vector<tinyobj::shape_t>::iterator shape = shapes.begin(); shape != shapes.end(); shape++) {
|
||||||
shape != shapes.end(); shape++) {
|
|
||||||
meshobj = PyDict_New();
|
meshobj = PyDict_New();
|
||||||
tinyobj::mesh_t cm = (*shape).mesh;
|
tinyobj::mesh_t cm = (*shape).mesh;
|
||||||
|
|
||||||
{
|
{
|
||||||
current = PyList_New(0);
|
current = PyList_New(0);
|
||||||
|
|
||||||
for (size_t i = 0; i < cm.indices.size(); i++) {
|
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,
|
// 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,
|
PyList_Insert(current, 3 * i + 0, value);
|
||||||
PyLong_FromLong(cm.indices[i].vertex_index));
|
Py_DECREF(value);
|
||||||
PyList_Insert(current, 3 * i + 1,
|
value = PyLong_FromLong(cm.indices[i].normal_index);
|
||||||
PyLong_FromLong(cm.indices[i].normal_index));
|
PyList_Insert(current, 3 * i + 1, value);
|
||||||
PyList_Insert(current, 3 * i + 2,
|
Py_DECREF(value);
|
||||||
PyLong_FromLong(cm.indices[i].texcoord_index));
|
value = PyLong_FromLong(cm.indices[i].texcoord_index);
|
||||||
|
PyList_Insert(current, 3 * i + 2, value);
|
||||||
|
Py_DECREF(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDict_SetItemString(meshobj, "indices", current);
|
PyDict_SetItemString(meshobj, "indices", current);
|
||||||
|
Py_DECREF(current);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
current = PyList_New(0);
|
current = PyList_New(0);
|
||||||
|
|
||||||
for (size_t i = 0; i < cm.num_face_vertices.size(); i++) {
|
for (size_t i = 0; i < cm.num_face_vertices.size(); i++) {
|
||||||
// Widen data type to long.
|
// 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);
|
PyDict_SetItemString(meshobj, "num_face_vertices", current);
|
||||||
|
Py_DECREF(current);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
current = PyList_New(0);
|
current = PyList_New(0);
|
||||||
|
|
||||||
for (size_t i = 0; i < cm.material_ids.size(); i++) {
|
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);
|
PyDict_SetItemString(meshobj, "material_ids", current);
|
||||||
|
Py_DECREF(current);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDict_SetItemString(pyshapes, (*shape).name.c_str(), meshobj);
|
PyDict_SetItemString(pyshapes, (*shape).name.c_str(), meshobj);
|
||||||
|
Py_DECREF(meshobj);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (std::vector<tinyobj::material_t>::iterator mat = materials.begin();
|
for (std::vector<tinyobj::material_t>::iterator mat = materials.begin(); mat != materials.end(); mat++) {
|
||||||
mat != materials.end(); mat++) {
|
|
||||||
PyObject* matobj = PyDict_New();
|
PyObject* matobj = PyDict_New();
|
||||||
PyObject* unknown_parameter = PyDict_New();
|
PyObject* unknown_parameter = PyDict_New();
|
||||||
|
|
||||||
for (std::map<std::string, std::string>::iterator p =
|
for (std::map<std::string, std::string>::iterator p = mat->unknown_parameter.begin(); p != mat->unknown_parameter.end(); ++p) {
|
||||||
(*mat).unknown_parameter.begin();
|
PyObject* value = PyUnicode_FromString(p->second.c_str());
|
||||||
p != (*mat).unknown_parameter.end(); ++p) {
|
PyDict_SetItemString(unknown_parameter, p->first.c_str(), value);
|
||||||
PyDict_SetItemString(unknown_parameter, p->first.c_str(),
|
Py_DECREF(value);
|
||||||
PyUnicode_FromString(p->second.c_str()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDict_SetItemString(matobj, "shininess",
|
PyObject* value = PyFloat_FromDouble(mat->shininess);
|
||||||
PyFloat_FromDouble((*mat).shininess));
|
PyDict_SetItemString(matobj, "shininess", value);
|
||||||
PyDict_SetItemString(matobj, "ior", PyFloat_FromDouble((*mat).ior));
|
Py_DECREF(value);
|
||||||
PyDict_SetItemString(matobj, "dissolve",
|
value = PyFloat_FromDouble(mat->ior);
|
||||||
PyFloat_FromDouble((*mat).dissolve));
|
PyDict_SetItemString(matobj, "ior", value);
|
||||||
PyDict_SetItemString(matobj, "illum", PyLong_FromLong((*mat).illum));
|
Py_DECREF(value);
|
||||||
PyDict_SetItemString(matobj, "ambient_texname",
|
value = PyFloat_FromDouble(mat->dissolve);
|
||||||
PyUnicode_FromString((*mat).ambient_texname.c_str()));
|
PyDict_SetItemString(matobj, "dissolve", value);
|
||||||
PyDict_SetItemString(matobj, "diffuse_texname",
|
Py_DECREF(value);
|
||||||
PyUnicode_FromString((*mat).diffuse_texname.c_str()));
|
value = PyLong_FromLong(mat->illum);
|
||||||
PyDict_SetItemString(matobj, "specular_texname",
|
PyDict_SetItemString(matobj, "illum", value);
|
||||||
PyUnicode_FromString((*mat).specular_texname.c_str()));
|
Py_DECREF(value);
|
||||||
PyDict_SetItemString(
|
value = PyUnicode_FromString(mat->ambient_texname.c_str());
|
||||||
matobj, "specular_highlight_texname",
|
PyDict_SetItemString(matobj, "ambient_texname", value);
|
||||||
PyUnicode_FromString((*mat).specular_highlight_texname.c_str()));
|
Py_DECREF(value);
|
||||||
PyDict_SetItemString(matobj, "bump_texname",
|
value = PyUnicode_FromString(mat->diffuse_texname.c_str());
|
||||||
PyUnicode_FromString((*mat).bump_texname.c_str()));
|
PyDict_SetItemString(matobj, "diffuse_texname", value);
|
||||||
PyDict_SetItemString(
|
Py_DECREF(value);
|
||||||
matobj, "displacement_texname",
|
value = PyUnicode_FromString(mat->specular_texname.c_str());
|
||||||
PyUnicode_FromString((*mat).displacement_texname.c_str()));
|
PyDict_SetItemString(matobj, "specular_texname", value);
|
||||||
PyDict_SetItemString(matobj, "alpha_texname",
|
Py_DECREF(value);
|
||||||
PyUnicode_FromString((*mat).alpha_texname.c_str()));
|
value = PyUnicode_FromString(mat->specular_highlight_texname.c_str());
|
||||||
PyDict_SetItemString(matobj, "ambient", pyTupleFromfloat3((*mat).ambient));
|
PyDict_SetItemString(matobj, "specular_highlight_texname", value);
|
||||||
PyDict_SetItemString(matobj, "diffuse", pyTupleFromfloat3((*mat).diffuse));
|
Py_DECREF(value);
|
||||||
PyDict_SetItemString(matobj, "specular",
|
value = PyUnicode_FromString(mat->bump_texname.c_str());
|
||||||
pyTupleFromfloat3((*mat).specular));
|
PyDict_SetItemString(matobj, "bump_texname", value);
|
||||||
PyDict_SetItemString(matobj, "transmittance",
|
Py_DECREF(value);
|
||||||
pyTupleFromfloat3((*mat).transmittance));
|
value = PyUnicode_FromString(mat->displacement_texname.c_str());
|
||||||
PyDict_SetItemString(matobj, "emission",
|
PyDict_SetItemString(matobj, "displacement_texname", value);
|
||||||
pyTupleFromfloat3((*mat).emission));
|
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, "unknown_parameter", unknown_parameter);
|
PyDict_SetItemString(matobj, "unknown_parameter", unknown_parameter);
|
||||||
|
Py_DECREF(unknown_parameter);
|
||||||
PyDict_SetItemString(pymaterials, (*mat).name.c_str(), matobj);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDict_SetItemString(rtndict, "shapes", pyshapes);
|
PyDict_SetItemString(rtndict, "shapes", pyshapes);
|
||||||
|
Py_DECREF(pyshapes);
|
||||||
PyDict_SetItemString(rtndict, "materials", pymaterials);
|
PyDict_SetItemString(rtndict, "materials", pymaterials);
|
||||||
|
Py_DECREF(pymaterials);
|
||||||
|
PyDict_SetItemString(rtndict, "material_indices", pymaterial_indices);
|
||||||
|
Py_DECREF(pymaterial_indices);
|
||||||
PyDict_SetItemString(rtndict, "attribs", attribobj);
|
PyDict_SetItemString(rtndict, "attribs", attribobj);
|
||||||
|
Py_DECREF(attribobj);
|
||||||
return rtndict;
|
return rtndict;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyMethodDef mMethods[] = {
|
static PyMethodDef mMethods[] = {
|
||||||
|
|
||||||
{"LoadObj", pyLoadObj, METH_VARARGS}, {NULL, NULL, 0, NULL}
|
{"LoadObj", pyLoadObj, METH_VARARGS}, {NULL, NULL, 0, NULL}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#if PY_MAJOR_VERSION >= 3
|
#if PY_MAJOR_VERSION >= 3
|
||||||
|
|
||||||
static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT, "tinyobjloader",
|
static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT, "tinyobjloader", NULL, -1, mMethods};
|
||||||
NULL, -1, mMethods};
|
|
||||||
|
|
||||||
PyMODINIT_FUNC PyInit_tinyobjloader(void) {
|
PyMODINIT_FUNC PyInit_tinyobjloader(void) {
|
||||||
return PyModule_Create(&moduledef);
|
return PyModule_Create(&moduledef);
|
||||||
|
|||||||
24
tests/issue-177.mtl
Normal file
24
tests/issue-177.mtl
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
newmtl white
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 1 1 1
|
||||||
|
Ks 0 0 0
|
||||||
|
|
||||||
|
newmtl red
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 1 0 0
|
||||||
|
Ks 0 0 0
|
||||||
|
|
||||||
|
newmtl green
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 0 1 0
|
||||||
|
Ks 0 0 0
|
||||||
|
|
||||||
|
newmtl blue
|
||||||
|
Ka 0 0 0
|
||||||
|
Kd 0 0 1
|
||||||
|
Ks 0 0 0
|
||||||
|
|
||||||
|
newmtl light
|
||||||
|
Ka 20 20 20
|
||||||
|
Kd 1 1 1
|
||||||
|
Ks 0 0 0
|
||||||
31
tests/issue-177.obj
Normal file
31
tests/issue-177.obj
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
mtllib issue-177.mtl
|
||||||
|
|
||||||
|
v 0.000000 2.000000 2.000000
|
||||||
|
v 0.000000 0.000000 2.000000
|
||||||
|
v 2.000000 0.000000 2.000000
|
||||||
|
v 2.000000 2.000000 2.000000
|
||||||
|
v 0.000000 2.000000 0.000000
|
||||||
|
v 0.000000 0.000000 0.000000
|
||||||
|
v 2.000000 0.000000 0.000000
|
||||||
|
v 2.000000 2.000000 0.000000
|
||||||
|
# 8 vertices
|
||||||
|
|
||||||
|
g front cube
|
||||||
|
usemtl white
|
||||||
|
f 1 2 3 4
|
||||||
|
g back cube
|
||||||
|
# expects white material
|
||||||
|
f 8 7 6 5
|
||||||
|
g right cube
|
||||||
|
usemtl red
|
||||||
|
f 4 3 7 8
|
||||||
|
g top cube
|
||||||
|
usemtl white
|
||||||
|
f 5 1 4 8
|
||||||
|
g left cube
|
||||||
|
usemtl green
|
||||||
|
f 5 6 2 1
|
||||||
|
g bottom cube
|
||||||
|
usemtl white
|
||||||
|
f 2 6 7 3
|
||||||
|
# 6 elements
|
||||||
731
tests/tester.cc
731
tests/tester.cc
@@ -1,21 +1,25 @@
|
|||||||
#define TINYOBJLOADER_IMPLEMENTATION
|
#define TINYOBJLOADER_IMPLEMENTATION
|
||||||
#include "../tiny_obj_loader.h"
|
#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 "catch.hpp"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cassert>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#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 vertices : " << (attrib.vertices.size() / 3) << std::endl;
|
||||||
std::cout << "# of normals : " << (attrib.normals.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 shapes : " << shapes.size() << std::endl;
|
||||||
std::cout << "# of materials : " << materials.size() << std::endl;
|
std::cout << "# of materials : " << materials.size() << std::endl;
|
||||||
@@ -42,11 +46,12 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
|
|||||||
|
|
||||||
for (size_t i = 0; i < shapes.size(); i++) {
|
for (size_t i = 0; i < shapes.size(); i++) {
|
||||||
printf("shape[%ld].name = %s\n", i, shapes[i].name.c_str());
|
printf("shape[%ld].name = %s\n", i, shapes[i].name.c_str());
|
||||||
printf("Size of shape[%ld].indices: %ld\n", i, shapes[i].mesh.indices.size());
|
printf("Size of shape[%ld].indices: %ld\n", i,
|
||||||
|
shapes[i].mesh.indices.size());
|
||||||
|
|
||||||
if (triangulate)
|
if (triangulate) {
|
||||||
{
|
printf("Size of shape[%ld].material_ids: %ld\n", i,
|
||||||
printf("Size of shape[%ld].material_ids: %ld\n", i, shapes[i].mesh.material_ids.size());
|
shapes[i].mesh.material_ids.size());
|
||||||
assert((shapes[i].mesh.indices.size() % 3) == 0);
|
assert((shapes[i].mesh.indices.size() % 3) == 0);
|
||||||
for (size_t f = 0; f < shapes[i].mesh.indices.size() / 3; f++) {
|
for (size_t f = 0; f < shapes[i].mesh.indices.size() / 3; f++) {
|
||||||
tinyobj::index_t i0 = shapes[i].mesh.indices[3 * f + 0];
|
tinyobj::index_t i0 = shapes[i].mesh.indices[3 * f + 0];
|
||||||
@@ -61,19 +66,21 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
|
|||||||
} else {
|
} else {
|
||||||
for (size_t f = 0; f < shapes[i].mesh.indices.size(); f++) {
|
for (size_t f = 0; f < shapes[i].mesh.indices.size(); f++) {
|
||||||
tinyobj::index_t idx = shapes[i].mesh.indices[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());
|
printf("Size of shape[%ld].material_ids: %ld\n", i,
|
||||||
assert(shapes[i].mesh.material_ids.size() == shapes[i].mesh.num_face_vertices.size());
|
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++) {
|
for (size_t m = 0; m < shapes[i].mesh.material_ids.size(); m++) {
|
||||||
printf(" material_id[%ld] = %d\n", m,
|
printf(" material_id[%ld] = %d\n", m, shapes[i].mesh.material_ids[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++) {
|
for (size_t v = 0; v < shapes[i].mesh.num_face_vertices.size(); v++) {
|
||||||
printf(" num_vertices[%ld] = %ld\n", v,
|
printf(" num_vertices[%ld] = %ld\n", v,
|
||||||
static_cast<long>(shapes[i].mesh.num_face_vertices[v]));
|
static_cast<long>(shapes[i].mesh.num_face_vertices[v]));
|
||||||
@@ -92,33 +99,28 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
|
|||||||
for (size_t t = 0; t < shapes[i].mesh.tags.size(); t++) {
|
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(" tag[%ld] = %s ", t, shapes[i].mesh.tags[t].name.c_str());
|
||||||
printf(" ints: [");
|
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]));
|
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("]");
|
printf("]");
|
||||||
|
|
||||||
printf(" floats: [");
|
printf(" floats: [");
|
||||||
for (size_t j = 0; j < shapes[i].mesh.tags[t].floatValues.size(); ++j)
|
for (size_t j = 0; j < shapes[i].mesh.tags[t].floatValues.size(); ++j) {
|
||||||
{
|
printf("%f", static_cast<const double>(
|
||||||
printf("%f", static_cast<const double>(shapes[i].mesh.tags[t].floatValues[j]));
|
shapes[i].mesh.tags[t].floatValues[j]));
|
||||||
if (j < (shapes[i].mesh.tags[t].floatValues.size()-1))
|
if (j < (shapes[i].mesh.tags[t].floatValues.size() - 1)) {
|
||||||
{
|
|
||||||
printf(", ");
|
printf(", ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("]");
|
printf("]");
|
||||||
|
|
||||||
printf(" strings: [");
|
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());
|
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(", ");
|
printf(", ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -129,24 +131,45 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
|
|||||||
|
|
||||||
for (size_t i = 0; i < materials.size(); i++) {
|
for (size_t i = 0; i < materials.size(); i++) {
|
||||||
printf("material[%ld].name = %s\n", i, materials[i].name.c_str());
|
printf("material[%ld].name = %s\n", i, materials[i].name.c_str());
|
||||||
printf(" material.Ka = (%f, %f ,%f)\n", 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.Ka = (%f, %f ,%f)\n",
|
||||||
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]));
|
static_cast<const double>(materials[i].ambient[0]),
|
||||||
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]));
|
static_cast<const double>(materials[i].ambient[1]),
|
||||||
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]));
|
static_cast<const double>(materials[i].ambient[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.Kd = (%f, %f ,%f)\n",
|
||||||
printf(" material.Ns = %f\n", static_cast<const double>(materials[i].shininess));
|
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.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.illum = %d\n", materials[i].illum);
|
||||||
printf(" material.map_Ka = %s\n", materials[i].ambient_texname.c_str());
|
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_Kd = %s\n", materials[i].diffuse_texname.c_str());
|
||||||
printf(" material.map_Ks = %s\n", materials[i].specular_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_bump = %s\n", materials[i].bump_texname.c_str());
|
||||||
printf(" material.map_d = %s\n", materials[i].alpha_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.disp = %s\n", materials[i].displacement_texname.c_str());
|
||||||
std::map<std::string, std::string>::const_iterator it(materials[i].unknown_parameter.begin());
|
printf(" material.refl = %s\n", materials[i].reflection_texname.c_str());
|
||||||
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++) {
|
for (; it != itEnd; it++) {
|
||||||
printf(" material.%s = %s\n", it->first.c_str(), it->second.c_str());
|
printf(" material.%s = %s\n", it->first.c_str(), it->second.c_str());
|
||||||
@@ -155,23 +178,25 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool TestLoadObj(const char* filename, const char* basepath = NULL,
|
||||||
TestLoadObj(
|
bool triangulate = true) {
|
||||||
const char* filename,
|
|
||||||
const char* basepath = NULL,
|
|
||||||
bool triangulate = true)
|
|
||||||
{
|
|
||||||
std::cout << "Loading " << filename << std::endl;
|
std::cout << "Loading " << filename << std::endl;
|
||||||
|
|
||||||
tinyobj::attrib_t attrib;
|
tinyobj::attrib_t attrib;
|
||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename, basepath, triangulate);
|
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||||
|
filename, basepath, triangulate);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
@@ -184,13 +209,10 @@ TestLoadObj(
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool TestLoadObjFromPreopenedFile(const char* filename,
|
||||||
TestLoadObjFromPreopenedFile(
|
|
||||||
const char* filename,
|
|
||||||
const char* basepath = NULL,
|
const char* basepath = NULL,
|
||||||
bool readMaterials = true,
|
bool readMaterials = true,
|
||||||
bool triangulate = true)
|
bool triangulate = true) {
|
||||||
{
|
|
||||||
std::string fullFilename = std::string(basepath) + filename;
|
std::string fullFilename = std::string(basepath) + filename;
|
||||||
std::cout << "Loading " << fullFilename << std::endl;
|
std::cout << "Loading " << fullFilename << std::endl;
|
||||||
|
|
||||||
@@ -202,19 +224,24 @@ TestLoadObjFromPreopenedFile(
|
|||||||
}
|
}
|
||||||
|
|
||||||
tinyobj::MaterialStreamReader materialStreamReader(fileStream);
|
tinyobj::MaterialStreamReader materialStreamReader(fileStream);
|
||||||
tinyobj::MaterialStreamReader* materialReader = readMaterials
|
tinyobj::MaterialStreamReader* materialReader =
|
||||||
? &materialStreamReader
|
readMaterials ? &materialStreamReader : NULL;
|
||||||
: NULL;
|
|
||||||
|
|
||||||
tinyobj::attrib_t attrib;
|
tinyobj::attrib_t attrib;
|
||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, &fileStream, materialReader);
|
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||||
|
&fileStream, materialReader);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
@@ -227,14 +254,11 @@ TestLoadObjFromPreopenedFile(
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool TestStreamLoadObj() {
|
||||||
TestStreamLoadObj()
|
|
||||||
{
|
|
||||||
std::cout << "Stream Loading " << std::endl;
|
std::cout << "Stream Loading " << std::endl;
|
||||||
|
|
||||||
std::stringstream objStream;
|
std::stringstream objStream;
|
||||||
objStream
|
objStream << "mtllib cube.mtl\n"
|
||||||
<< "mtllib cube.mtl\n"
|
|
||||||
"\n"
|
"\n"
|
||||||
"v 0.000000 2.000000 2.000000\n"
|
"v 0.000000 2.000000 2.000000\n"
|
||||||
"v 0.000000 0.000000 2.000000\n"
|
"v 0.000000 0.000000 2.000000\n"
|
||||||
@@ -293,22 +317,21 @@ std::string matStream(
|
|||||||
"Ks 0 0 0");
|
"Ks 0 0 0");
|
||||||
|
|
||||||
using namespace tinyobj;
|
using namespace tinyobj;
|
||||||
class MaterialStringStreamReader:
|
class MaterialStringStreamReader : public MaterialReader {
|
||||||
public MaterialReader
|
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
MaterialStringStreamReader(const std::string& matSStream): m_matSStream(matSStream) {}
|
MaterialStringStreamReader(const std::string& matSStream)
|
||||||
|
: m_matSStream(matSStream) {}
|
||||||
virtual ~MaterialStringStreamReader() {}
|
virtual ~MaterialStringStreamReader() {}
|
||||||
virtual bool operator() (
|
virtual bool operator()(const std::string& matId,
|
||||||
const std::string& matId,
|
|
||||||
std::vector<material_t>* materials,
|
std::vector<material_t>* materials,
|
||||||
std::map<std::string, int>* matMap,
|
std::map<std::string, int>* matMap,
|
||||||
std::string* err)
|
std::string* warn, std::string* err) {
|
||||||
{
|
|
||||||
(void)matId;
|
(void)matId;
|
||||||
|
(void)warn;
|
||||||
(void)err;
|
(void)err;
|
||||||
std::string warning;
|
std::string warning;
|
||||||
LoadMtl(matMap, materials, &m_matSStream, &warning);
|
std::string error_msg;
|
||||||
|
LoadMtl(matMap, materials, &m_matSStream, &warning, &error_msg);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -320,11 +343,17 @@ std::string matStream(
|
|||||||
tinyobj::attrib_t attrib;
|
tinyobj::attrib_t attrib;
|
||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, &objStream, &matSSReader);
|
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||||
|
&objStream, &matSSReader);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
@@ -339,21 +368,26 @@ std::string matStream(
|
|||||||
const char* gMtlBasePath = "../models/";
|
const char* gMtlBasePath = "../models/";
|
||||||
|
|
||||||
TEST_CASE("cornell_box", "[Loader]") {
|
TEST_CASE("cornell_box", "[Loader]") {
|
||||||
|
|
||||||
REQUIRE(true == TestLoadObj("../models/cornell_box.obj", gMtlBasePath));
|
REQUIRE(true == TestLoadObj("../models/cornell_box.obj", gMtlBasePath));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("catmark_torus_creases0", "[Loader]") {
|
TEST_CASE("catmark_torus_creases0", "[Loader]") {
|
||||||
|
|
||||||
tinyobj::attrib_t attrib;
|
tinyobj::attrib_t attrib;
|
||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/catmark_torus_creases0.obj", gMtlBasePath, /*triangulate*/false);
|
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||||
|
"../models/catmark_torus_creases0.obj",
|
||||||
|
gMtlBasePath, /*triangulate*/ false);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
REQUIRE(true == ret);
|
REQUIRE(true == ret);
|
||||||
@@ -363,16 +397,22 @@ TEST_CASE("catmark_torus_creases0", "[Loader]") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("pbr", "[Loader]") {
|
TEST_CASE("pbr", "[Loader]") {
|
||||||
|
|
||||||
tinyobj::attrib_t attrib;
|
tinyobj::attrib_t attrib;
|
||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/pbr-mat-ext.obj", gMtlBasePath, /*triangulate*/false);
|
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||||
|
"../models/pbr-mat-ext.obj", gMtlBasePath,
|
||||||
|
/*triangulate*/ false);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
}
|
}
|
||||||
REQUIRE(true == ret);
|
REQUIRE(true == ret);
|
||||||
REQUIRE(1 == materials.size());
|
REQUIRE(1 == materials.size());
|
||||||
@@ -390,18 +430,18 @@ TEST_CASE("pbr", "[Loader]") {
|
|||||||
REQUIRE(0 == materials[0].normal_texname.compare("normalmap.tex"));
|
REQUIRE(0 == materials[0].normal_texname.compare("normalmap.tex"));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("stream_load", "[Stream]") {
|
TEST_CASE("stream_load", "[Stream]") { REQUIRE(true == TestStreamLoadObj()); }
|
||||||
REQUIRE(true == TestStreamLoadObj());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("stream_load_from_file_skipping_materials", "[Stream]") {
|
TEST_CASE("stream_load_from_file_skipping_materials", "[Stream]") {
|
||||||
REQUIRE(true == TestLoadObjFromPreopenedFile(
|
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]") {
|
TEST_CASE("stream_load_from_file_with_materials", "[Stream]") {
|
||||||
REQUIRE(true == TestLoadObjFromPreopenedFile(
|
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]") {
|
TEST_CASE("trailing_whitespace_in_mtl", "[Issue92]") {
|
||||||
@@ -409,11 +449,17 @@ TEST_CASE("trailing_whitespace_in_mtl", "[Issue92]") {
|
|||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-92.obj", gMtlBasePath);
|
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||||
|
"../models/issue-92.obj", gMtlBasePath);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
}
|
}
|
||||||
REQUIRE(true == ret);
|
REQUIRE(true == ret);
|
||||||
REQUIRE(1 == materials.size());
|
REQUIRE(1 == materials.size());
|
||||||
@@ -425,11 +471,17 @@ TEST_CASE("transmittance_filter", "[Issue95]") {
|
|||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-95.obj", gMtlBasePath);
|
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||||
|
"../models/issue-95.obj", gMtlBasePath);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
}
|
}
|
||||||
REQUIRE(true == ret);
|
REQUIRE(true == ret);
|
||||||
REQUIRE(1 == materials.size());
|
REQUIRE(1 == materials.size());
|
||||||
@@ -443,11 +495,17 @@ TEST_CASE("transmittance_filter_Tf", "[Issue95-Tf]") {
|
|||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-95-2.obj", gMtlBasePath);
|
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||||
|
"../models/issue-95-2.obj", gMtlBasePath);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
}
|
}
|
||||||
REQUIRE(true == ret);
|
REQUIRE(true == ret);
|
||||||
REQUIRE(1 == materials.size());
|
REQUIRE(1 == materials.size());
|
||||||
@@ -461,11 +519,17 @@ TEST_CASE("transmittance_filter_Kt", "[Issue95-Kt]") {
|
|||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-95.obj", gMtlBasePath);
|
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||||
|
"../models/issue-95.obj", gMtlBasePath);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
}
|
}
|
||||||
REQUIRE(true == ret);
|
REQUIRE(true == ret);
|
||||||
REQUIRE(1 == materials.size());
|
REQUIRE(1 == materials.size());
|
||||||
@@ -479,11 +543,17 @@ TEST_CASE("usemtl_at_last_line", "[Issue104]") {
|
|||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/usemtl-issue-104.obj", gMtlBasePath);
|
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||||
|
"../models/usemtl-issue-104.obj", gMtlBasePath);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
}
|
}
|
||||||
REQUIRE(true == ret);
|
REQUIRE(true == ret);
|
||||||
REQUIRE(1 == shapes.size());
|
REQUIRE(1 == shapes.size());
|
||||||
@@ -494,11 +564,18 @@ TEST_CASE("texture_opts", "[Issue85]") {
|
|||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/texture-options-issue-85.obj", gMtlBasePath);
|
bool ret =
|
||||||
|
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||||
|
"../models/texture-options-issue-85.obj", gMtlBasePath);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
}
|
}
|
||||||
REQUIRE(true == ret);
|
REQUIRE(true == ret);
|
||||||
REQUIRE(1 == shapes.size());
|
REQUIRE(1 == shapes.size());
|
||||||
@@ -524,11 +601,13 @@ TEST_CASE("texture_opts", "[Issue85]") {
|
|||||||
|
|
||||||
REQUIRE(tinyobj::TEXTURE_TYPE_SPHERE == materials[2].diffuse_texopt.type);
|
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_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_LEFT == materials[2].ambient_texopt.type);
|
||||||
REQUIRE(tinyobj::TEXTURE_TYPE_CUBE_RIGHT == materials[2].alpha_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_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]") {
|
TEST_CASE("mtllib_multiple_filenames", "[Issue112]") {
|
||||||
@@ -536,11 +615,18 @@ TEST_CASE("mtllib_multiple_filenames", "[Issue112]") {
|
|||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/mtllib-multiple-files-issue-112.obj", gMtlBasePath);
|
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||||
|
"../models/mtllib-multiple-files-issue-112.obj",
|
||||||
|
gMtlBasePath);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
}
|
}
|
||||||
REQUIRE(true == ret);
|
REQUIRE(true == ret);
|
||||||
REQUIRE(1 == materials.size());
|
REQUIRE(1 == materials.size());
|
||||||
@@ -551,11 +637,17 @@ TEST_CASE("tr_and_d", "[Issue43]") {
|
|||||||
std::vector<tinyobj::shape_t> shapes;
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
std::vector<tinyobj::material_t> materials;
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
|
||||||
|
std::string warn;
|
||||||
std::string err;
|
std::string err;
|
||||||
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/tr-and-d-issue-43.obj", gMtlBasePath);
|
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
|
||||||
|
"../models/tr-and-d-issue-43.obj", gMtlBasePath);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
if (!err.empty()) {
|
if (!err.empty()) {
|
||||||
std::cerr << err << std::endl;
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
}
|
}
|
||||||
REQUIRE(true == ret);
|
REQUIRE(true == ret);
|
||||||
REQUIRE(2 == materials.size());
|
REQUIRE(2 == materials.size());
|
||||||
@@ -564,6 +656,435 @@ TEST_CASE("tr_and_d", "[Issue43]") {
|
|||||||
REQUIRE(0.75 == Approx(materials[1].dissolve));
|
REQUIRE(0.75 == Approx(materials[1].dissolve));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("refl", "[refl]") {
|
||||||
|
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/refl.obj", gMtlBasePath);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!err.empty()) {
|
||||||
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
PrintInfo(attrib, shapes, materials);
|
||||||
|
|
||||||
|
REQUIRE(true == ret);
|
||||||
|
REQUIRE(5 == materials.size());
|
||||||
|
|
||||||
|
REQUIRE(materials[0].reflection_texname.compare("reflection.tga") == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("map_Bump", "[bump]") {
|
||||||
|
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/map-bump.obj", gMtlBasePath);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!err.empty()) {
|
||||||
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
PrintInfo(attrib, shapes, materials);
|
||||||
|
|
||||||
|
REQUIRE(true == ret);
|
||||||
|
REQUIRE(2 == materials.size());
|
||||||
|
|
||||||
|
REQUIRE(materials[0].bump_texname.compare("bump.jpg") == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("g_ignored", "[Issue138]") {
|
||||||
|
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-138.obj", gMtlBasePath);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!err.empty()) {
|
||||||
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
PrintInfo(attrib, shapes, materials);
|
||||||
|
|
||||||
|
REQUIRE(true == ret);
|
||||||
|
REQUIRE(2 == shapes.size());
|
||||||
|
REQUIRE(2 == materials.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("vertex-col-ext", "[Issue144]") {
|
||||||
|
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-vertexcol.obj", gMtlBasePath);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!err.empty()) {
|
||||||
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrintInfo(attrib, shapes, materials);
|
||||||
|
|
||||||
|
REQUIRE(true == ret);
|
||||||
|
REQUIRE((8 * 3) == attrib.colors.size());
|
||||||
|
|
||||||
|
REQUIRE(0 == Approx(attrib.colors[3 * 0 + 0]));
|
||||||
|
REQUIRE(0 == Approx(attrib.colors[3 * 0 + 1]));
|
||||||
|
REQUIRE(0 == Approx(attrib.colors[3 * 0 + 2]));
|
||||||
|
|
||||||
|
REQUIRE(0 == Approx(attrib.colors[3 * 1 + 0]));
|
||||||
|
REQUIRE(0 == Approx(attrib.colors[3 * 1 + 1]));
|
||||||
|
REQUIRE(1 == Approx(attrib.colors[3 * 1 + 2]));
|
||||||
|
|
||||||
|
REQUIRE(1 == Approx(attrib.colors[3 * 4 + 0]));
|
||||||
|
|
||||||
|
REQUIRE(1 == Approx(attrib.colors[3 * 7 + 0]));
|
||||||
|
REQUIRE(1 == Approx(attrib.colors[3 * 7 + 1]));
|
||||||
|
REQUIRE(1 == Approx(attrib.colors[3 * 7 + 2]));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("norm_texopts", "[norm]") {
|
||||||
|
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/norm-texopt.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(3.0 == Approx(materials[0].normal_texopt.bump_multiplier));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("zero-face-idx-value", "[Issue140]") {
|
||||||
|
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-140-zero-face-idx.obj", gMtlBasePath);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!err.empty()) {
|
||||||
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
|
}
|
||||||
|
REQUIRE(false == ret);
|
||||||
|
REQUIRE(!err.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("texture-name-whitespace", "[Issue145]") {
|
||||||
|
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/texture-filename-with-whitespace.obj",
|
||||||
|
gMtlBasePath);
|
||||||
|
|
||||||
|
if (!warn.empty()) {
|
||||||
|
std::cout << "WARN: " << warn << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!err.empty()) {
|
||||||
|
std::cerr << "ERR: " << err << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
REQUIRE(true == ret);
|
||||||
|
REQUIRE(err.empty());
|
||||||
|
REQUIRE(2 < materials.size());
|
||||||
|
|
||||||
|
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
|
#if 0
|
||||||
int
|
int
|
||||||
main(
|
main(
|
||||||
|
|||||||
1141
tiny_obj_loader.h
1141
tiny_obj_loader.h
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user