在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:tinyobjloader/tinyobjloader开源软件地址:https://github.com/tinyobjloader/tinyobjloader开源编程语言:C++ 91.1%开源软件介绍:tinyobjloaderTiny but powerful single file wavefront obj loader written in C++03. No dependency except for C++ STL. It can parse over 10M polygons with moderate memory and time.
If you are looking for C89 version, please see https://github.com/syoyo/tinyobjloader-c . Version noticeWe recommend to use We have released new version v1.0.0 on 20 Aug, 2016.
Old version is available as What's new
Requirements
Old versionPrevious old version is available in Exampletinyobjloader can successfully load 6M triangles Rungholt scene. http://casual-effects.com/data/index.html
Use caseTinyObjLoader is successfully used in ... New version(v1.0.x)
Old version(v0.9.x)
Features
Primitives
TODO
LicenseTinyObjLoader is licensed under MIT license. Third party licenses.
UsageInstallationOne option is to simply copy the header file into your project and to make sure that Building tinyobjloader - Using vcpkg(not recommended though)Alghouth it is not a recommended way, you can download and install tinyobjloader using the vcpkg dependency manager:
The tinyobjloader port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository. Data format
Each
Note that when float data typeTinyObjLoader now use Robust triangulationWhen you enable Built-in trinagulation code may not work well in some polygon shape. You can define Example code (Deprecated API)#define TINYOBJLOADER_IMPLEMENTATION // define this in only *one* .cc
// Optional. define TINYOBJLOADER_USE_MAPBOX_EARCUT gives robust trinagulation. Requires C++11
//#define TINYOBJLOADER_USE_MAPBOX_EARCUT
#include "tiny_obj_loader.h"
std::string inputfile = "cornell_box.obj";
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string warn;
std::string err;
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, inputfile.c_str());
if (!warn.empty()) {
std::cout << warn << std::endl;
}
if (!err.empty()) {
std::cerr << err << std::endl;
}
if (!ret) {
exit(1);
}
// Loop over shapes
for (size_t s = 0; s < shapes.size(); s++) {
// Loop over faces(polygon)
size_t index_offset = 0;
for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) {
size_t fv = size_t(shapes[s].mesh.num_face_vertices[f]);
// Loop over vertices in the face.
for (size_t v = 0; v < fv; v++) {
// access to vertex
tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
tinyobj::real_t vx = attrib.vertices[3*size_t(idx.vertex_index)+0];
tinyobj::real_t vy = attrib.vertices[3*size_t(idx.vertex_index)+1];
tinyobj::real_t vz = attrib.vertices[3*size_t(idx.vertex_index)+2];
// Check if `normal_index` is zero or positive. negative = no normal data
if (idx.normal_index >= 0) {
tinyobj::real_t nx = attrib.normals[3*size_t(idx.normal_index)+0];
tinyobj::real_t ny = attrib.normals[3*size_t(idx.normal_index)+1];
tinyobj::real_t nz = attrib.normals[3*size_t(idx.normal_index)+2];
}
// Check if `texcoord_index` is zero or positive. negative = no texcoord data
if (idx.texcoord_index >= 0) {
tinyobj::real_t tx = attrib.texcoords[2*size_t(idx.texcoord_index)+0];
tinyobj::real_t ty = attrib.texcoords[2*size_t(idx.texcoord_index)+1];
}
// Optional: vertex colors
// tinyobj::real_t red = attrib.colors[3*size_t(idx.vertex_index)+0];
// tinyobj::real_t green = attrib.colors[3*size_t(idx.vertex_index)+1];
// tinyobj::real_t blue = attrib.colors[3*size_t(idx.vertex_index)+2];
}
index_offset += fv;
// per-face material
shapes[s].mesh.material_ids[f];
}
}
Example code (New Object Oriented API)#define TINYOBJLOADER_IMPLEMENTATION // define this in only *one* .cc
// Optional. define TINYOBJLOADER_USE_MAPBOX_EARCUT gives robust trinagulation. Requires C++11
//#define TINYOBJLOADER_USE_MAPBOX_EARCUT
#include "tiny_obj_loader.h"
std::string inputfile = "cornell_box.obj";
tinyobj::ObjReaderConfig reader_config;
reader_config.mtl_search_path = "./"; // Path to material files
tinyobj::ObjReader reader;
if (!reader.ParseFromFile(inputfile, reader_config)) {
if (!reader.Error().empty()) {
std::cerr << "TinyObjReader: " << reader.Error();
}
exit(1);
}
if (!reader.Warning().empty()) {
std::cout << "TinyObjReader: " << reader.Warning();
}
auto& attrib = reader.GetAttrib();
auto& shapes = reader.GetShapes();
auto& materials = reader.GetMaterials();
// Loop over shapes
for (size_t s = 0; s < shapes.size(); s++) {
// Loop over faces(polygon)
size_t index_offset = 0;
for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) {
size_t fv = size_t(shapes[s].mesh.num_face_vertices[f]);
// Loop over vertices in the face.
for (size_t v = 0; v < fv; v++) {
// access to vertex
tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
tinyobj::real_t vx = attrib.vertices[3*size_t(idx.vertex_index)+0];
tinyobj::real_t vy = attrib.vertices[3*size_t(idx.vertex_index)+1];
tinyobj::real_t vz = attrib.vertices[3*size_t(idx.vertex_index)+2];
// Check if `normal_index` is zero or positive. negative = no normal data
if (idx.normal_index >= 0) {
tinyobj::real_t nx = attrib.normals[3*size_t(idx.normal_index)+0];
tinyobj::real_t ny = attrib.normals[3*size_t(idx.normal_index)+1];
tinyobj::real_t nz = attrib.normals[3*size_t(idx.normal_index)+2];
}
// Check if `texcoord_index` is zero or positive. negative = no texcoord data
if (idx.texcoord_index >= 0) {
tinyobj::real_t tx = attrib.texcoords[2*size_t(idx.texcoord_index)+0];
tinyobj::real_t ty = attrib.texcoords[2*size_t(idx.texcoord_index)+1];
}
// Optional: vertex colors
// tinyobj::real_t red = attrib.colors[3*size_t(idx.vertex_index)+0];
// tinyobj::real_t green = attrib.colors[3*size_t(idx.vertex_index)+1];
// tinyobj::real_t blue = attrib.colors[3*size_t(idx.vertex_index)+2];
}
index_offset += fv;
// per-face material
shapes[s].mesh.material_ids[f];
}
}
Optimized loaderOptimized multi-threaded .obj loader is available at Here is some benchmark result. Time are measured on MacBook 12(Early 2016, Core m5 1.2GHz).
Python bindingCI + PyPI uploadcibuildwheels + twine upload for each git tagging event is handled in Azure Pipeline. How to bump version(For developer)
TestsUnit tests are provided in |
2022-08-15
2022-08-17
2022-09-23
2023-10-27
2022-08-18
请发表评论