Minúsculo, mas poderoso, o carregador OBJ de frente de onda de arquivo único escrito em C ++ 03. Sem dependência, exceto C ++ STL. Ele pode analisar mais de 10m polígonos com memória e tempo moderados.
tinyobjloader é bom para incorporar .Obj Loader ao seu renderizador (Iluminação Global) ;-)
Se você estiver procurando por uma versão C99, consulte https://github.com/syoyo/tinyobjloader-c.
Recomendamos usar a filial master ( main ). Seu candidato a liberação v2.0. A maioria dos recursos agora é quase robusta e estável (a tarefa restante para a versão v2.0 está polindo a API C ++ e Python e corrige o código de triangulação interno).
Lançamos a nova versão v1.0.0 em 20 de agosto de 2016. A versão antiga está disponível como v0.9.x Branch https://github.com/syoyo/tinyobjloader/tree/v0.9.x
python . Ver também https://pypi.org/project/tinyobjloader/) A versão antiga anterior está disponível no ramo v0.9.x

O TinyObjloader pode carregar com sucesso a cena dos triângulos de 6m Rungholt. http://casual-effects.com/data/index.html

Tinyobjloader é usado com sucesso em ...
TINYOBJLOADER_USE_DOUBLE graças ao NOMA
- motor 3D com gráficos modernospython Pasta.f ) l ) p ) O TinyObjloader está licenciado sob licença do MIT.
Uma opção é simplesmente copiar o arquivo de cabeçalho para o seu projeto e garantir que TINYOBJLOADER_IMPLEMENTATION seja definido exatamente uma vez.
Embora não seja uma maneira recomendada, você pode baixar e instalar o TinyObjloader usando o gerenciador de dependência VCPKG:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install tinyobjloader
A porta Tinyobjloader no VCPKG é mantida atualizada pelos membros da equipe da Microsoft e pelos colaboradores da comunidade. Se a versão estiver desatualizada, crie uma solicitação de problema ou puxe no repositório VCPKG.
attrib_t contém uma matriz única e linear de dados de vértices (posição, normal e texcoord).
attrib_t::vertices => 3 floats per vertex
v[0] v[1] v[2] v[3] v[n-1]
+-----------+-----------+-----------+-----------+ +-----------+
| x | y | z | x | y | z | x | y | z | x | y | z | .... | x | y | z |
+-----------+-----------+-----------+-----------+ +-----------+
attrib_t::normals => 3 floats per vertex
n[0] n[1] n[2] n[3] n[n-1]
+-----------+-----------+-----------+-----------+ +-----------+
| x | y | z | x | y | z | x | y | z | x | y | z | .... | x | y | z |
+-----------+-----------+-----------+-----------+ +-----------+
attrib_t::texcoords => 2 floats per vertex
t[0] t[1] t[2] t[3] t[n-1]
+-----------+-----------+-----------+-----------+ +-----------+
| 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 |
+-----------+-----------+-----------+-----------+ +-----------+
Cada shape_t::mesh_t não contém dados de vértice, mas contém índice de matriz para attrib_t . Consulte loader_example.cc para obter mais detalhes.
mesh_t::indices => array of vertex indices.
+----+----+----+----+----+----+----+----+----+----+ +--------+
| i0 | i1 | i2 | i3 | i4 | i5 | i6 | i7 | i8 | i9 | ... | i(n-1) |
+----+----+----+----+----+----+----+----+----+----+ +--------+
Each index has an array index to attrib_t::vertices, attrib_t::normals and attrib_t::texcoords.
mesh_t::num_face_vertices => array of the number of vertices per face(e.g. 3 = triangle, 4 = quad , 5 or more = N-gons).
+---+---+---+ +---+
| 3 | 4 | 3 | ...... | 3 |
+---+---+---+ +---+
| | | |
| | | +-----------------------------------------+
| | | |
| | +------------------------------+ |
| | | |
| +------------------+ | |
| | | |
|/ |/ |/ |/
mesh_t::indices
| face[0] | face[1] | face[2] | | face[n-1] |
+----+----+----+----+----+----+----+----+----+----+ +--------+--------+--------+
| i0 | i1 | i2 | i3 | i4 | i5 | i6 | i7 | i8 | i9 | ... | i(n-3) | i(n-2) | i(n-1) |
+----+----+----+----+----+----+----+----+----+----+ +--------+--------+--------+
Observe que quando o sinalizador triangulate é verdadeiro no argumento tinyobj::LoadObj() , num_face_vertices são todos preenchidos com 3 (triângulo).
TinyObjloader agora use real_t para o tipo de dados de ponto flutuante. O padrão é float(32bit) . Você pode ativar a precisão double(64bit) usando o TINYOBJLOADER_USE_DOUBLE define.
Quando você habilita triangulation (o padrão está ativado), os polígonos triangular do TinyObjloader (rostos com 4 ou mais vértices).
O código de triangulação embutido pode não funcionar bem em alguma forma de polígono.
Você pode definir TINYOBJLOADER_USE_MAPBOX_EARCUT para triangulação robusta usando mapbox/earcut.hpp . Isso requer compilador C ++ 11. E você precisa copiar mapbox/earcut.hpp para o seu projeto. Se você possui seu próprio arquivo mapbox/earcut.hpp em seu projeto, poderá definir TINYOBJLOADER_DONOT_INCLUDE_MAPBOX_EARCUT para que mapbox/earcut.hpp não esteja incluído dentro de tiny_obj_loader.h .
# define TINYOBJLOADER_IMPLEMENTATION // define this in only *one* .cc
// Optional. define TINYOBJLOADER_USE_MAPBOX_EARCUT gives robust triangulation. 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];
}
}
# define TINYOBJLOADER_IMPLEMENTATION // define this in only *one* .cc
// Optional. define TINYOBJLOADER_USE_MAPBOX_EARCUT gives robust triangulation. 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];
}
}
O Otimizado Loader .OBJ multi-thread está disponível no Diretório experimental/ . Se você deseja que o desempenho absoluto carregue dados .OBJ, este carregador otimizado se encaixará em seu objetivo. Observe que o carregador otimizado usa o encadeamento C ++ 11 e faz menos verificações de erros, mas pode funcionar com a maioria dos dados .OBJ.
Aqui está algum resultado de referência. O tempo é medido no MacBook 12 (início de 2016, Core M5 1,2 GHz).
$ python -m pip install tinyobjloader
Consulte Python/Sample.py, por exemplo, uso da ligação do Python do TinyObjloader.
CIBUILDWHEELS + Upload de barbante para cada evento de marcação Git é tratado em ações do GitHub e Cirrus CI (BRACT Builds).
black a python ( python/sample.py )release . Confirme o IC Build está ok.v (por exemplo, v2.1.0 )git push --tags Os testes de unidade são fornecidos no diretório tests . Consulte tests/README.md para obter detalhes.