@@ -241,7 +241,7 @@ TinyObjLoader now use `real_t` for floating point data type.
241241Default is ` float(32bit) ` .
242242You can enable ` double(64bit) ` precision by using ` TINYOBJLOADER_USE_DOUBLE ` define.
243243
244- #### Example code
244+ #### Example code (Deprecated API)
245245
246246``` c++
247247#define TINYOBJLOADER_IMPLEMENTATION // define this in only * one* .cc
@@ -302,6 +302,69 @@ for (size_t s = 0; s < shapes.size(); s++) {
302302
303303```
304304
305+ #### Example code (New Object Oriented API)
306+
307+ ``` c++
308+ #define TINYOBJLOADER_IMPLEMENTATION // define this in only * one* .cc
309+ #include "tiny_obj_loader.h"
310+
311+
312+ std::string inputfile = " cornell_box.obj" ;
313+ tinyobj::ObjReaderConfig reader_config;
314+ reader_config.mtl_search_path = " ./" ; // Path to material files
315+
316+ tinyobj::ObjReader reader;
317+
318+ if (!reader.ParseFromFile(inputfile, reader_config)) {
319+ if (!reader.Error().empty()) {
320+ std::cerr << "TinyObjReader: " << reader.Error();
321+ }
322+ exit (1);
323+ }
324+
325+ if (!reader.Warning().empty()) {
326+ std::cout << "TinyObjReader: " << reader.Warning();
327+ }
328+
329+ auto & attrib = reader.GetAttrib();
330+ auto & shapes = reader.GetShapes();
331+ auto & materials = reader.GetMaterials();
332+
333+ // Loop over shapes
334+ for (size_t s = 0 ; s < shapes.size(); s++) {
335+ // Loop over faces(polygon)
336+ size_t index_offset = 0;
337+ for (size_t f = 0; f < shapes[ s] .mesh.num_face_vertices.size(); f++) {
338+ int fv = shapes[ s] .mesh.num_face_vertices[ f] ;
339+
340+ // Loop over vertices in the face.
341+ for (size_t v = 0; v < fv; v++) {
342+ // access to vertex
343+ tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
344+ tinyobj::real_t vx = attrib.vertices[3*idx.vertex_index+0];
345+ tinyobj::real_t vy = attrib.vertices[3*idx.vertex_index+1];
346+ tinyobj::real_t vz = attrib.vertices[3*idx.vertex_index+2];
347+ tinyobj::real_t nx = attrib.normals[3*idx.normal_index+0];
348+ tinyobj::real_t ny = attrib.normals[3*idx.normal_index+1];
349+ tinyobj::real_t nz = attrib.normals[3*idx.normal_index+2];
350+ tinyobj::real_t tx = attrib.texcoords[2*idx.texcoord_index+0];
351+ tinyobj::real_t ty = attrib.texcoords[2*idx.texcoord_index+1];
352+ // Optional: vertex colors
353+ // tinyobj::real_t red = attrib.colors[3*idx.vertex_index+0];
354+ // tinyobj::real_t green = attrib.colors[3*idx.vertex_index+1];
355+ // tinyobj::real_t blue = attrib.colors[3*idx.vertex_index+2];
356+ }
357+ index_offset += fv;
358+
359+ // per-face material
360+ shapes[s].mesh.material_ids[f];
361+ }
362+ }
363+
364+ ```
365+
366+
367+
305368## Optimized loader
306369
307370Optimized multi-threaded .obj loader is available at ` experimental/ ` directory.
0 commit comments