Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d5c722125a | ||
|
|
16ed0ac129 | ||
|
|
b69d2a2c55 | ||
|
|
e210379335 | ||
|
|
659976b8f3 | ||
|
|
96ba498d70 | ||
|
|
7ecb0b2f37 | ||
|
|
a14bbdb065 | ||
|
|
bfedfbb1fb | ||
|
|
41f46c7fd7 | ||
|
|
a62dd278e2 |
12
README.md
12
README.md
@@ -17,6 +17,12 @@ Tiny but powerful single file wavefront obj loader written in C++. No dependency
|
||||
|
||||
`tinyobjloader` is good for embedding .obj loader to your (global illumination) renderer ;-)
|
||||
|
||||
Notice!
|
||||
-------
|
||||
|
||||
`master` branch will be replaced with `develop` branch in the near future: https://github.com/syoyo/tinyobjloader/tree/develop
|
||||
`develop` branch has more better support and clean API interface for loading .obj and also it has optimized multi-threaded parser(probably 10x faster than `master`). If you are new to use `TinyObjLoader`, I highly recommend to use `develop` branch.
|
||||
|
||||
|
||||
What's new
|
||||
----------
|
||||
@@ -61,6 +67,12 @@ TinyObjLoader is successfully used in ...
|
||||
* pbrt-v3 https://github.com/mmp/pbrt-v3
|
||||
* cocos2d-x https://github.com/cocos2d/cocos2d-x/
|
||||
* Android Vulkan demo https://github.com/SaschaWillems/Vulkan
|
||||
* voxelizer https://github.com/karimnaaji/voxelizer
|
||||
* Probulator https://github.com/kayru/Probulator
|
||||
* OptiX Prime baking https://github.com/nvpro-samples/optix_prime_baking
|
||||
* FireRays SDK https://github.com/GPUOpen-LibrariesAndSDKs/FireRays_SDK
|
||||
* parg, tiny C library of various graphics utilities and GL demos https://github.com/prideout/parg
|
||||
* Opengl unit of ChronoEngine https://github.com/projectchrono/chrono-opengl
|
||||
* Your project here!
|
||||
|
||||
Features
|
||||
|
||||
89
test.cc
89
test.cc
@@ -8,6 +8,90 @@
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#include <mmsystem.h>
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#pragma comment(lib, "winmm.lib")
|
||||
#else
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <ctime>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// not thread-safe
|
||||
class timerutil {
|
||||
public:
|
||||
#ifdef _WIN32
|
||||
typedef DWORD time_t;
|
||||
|
||||
timerutil() { ::timeBeginPeriod(1); }
|
||||
~timerutil() { ::timeEndPeriod(1); }
|
||||
|
||||
void start() { t_[0] = ::timeGetTime(); }
|
||||
void end() { t_[1] = ::timeGetTime(); }
|
||||
|
||||
time_t sec() { return (time_t)((t_[1] - t_[0]) / 1000); }
|
||||
time_t msec() { return (time_t)((t_[1] - t_[0])); }
|
||||
time_t usec() { return (time_t)((t_[1] - t_[0]) * 1000); }
|
||||
time_t current() { return ::timeGetTime(); }
|
||||
|
||||
#else
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
typedef unsigned long int time_t;
|
||||
|
||||
void start() { gettimeofday(tv + 0, &tz); }
|
||||
void end() { gettimeofday(tv + 1, &tz); }
|
||||
|
||||
time_t sec() { return (time_t)(tv[1].tv_sec - tv[0].tv_sec); }
|
||||
time_t msec() {
|
||||
return this->sec() * 1000 +
|
||||
(time_t)((tv[1].tv_usec - tv[0].tv_usec) / 1000);
|
||||
}
|
||||
time_t usec() {
|
||||
return this->sec() * 1000000 + (time_t)(tv[1].tv_usec - tv[0].tv_usec);
|
||||
}
|
||||
time_t current() {
|
||||
struct timeval t;
|
||||
gettimeofday(&t, NULL);
|
||||
return (time_t)(t.tv_sec * 1000 + t.tv_usec);
|
||||
}
|
||||
|
||||
#else // C timer
|
||||
// using namespace std;
|
||||
typedef clock_t time_t;
|
||||
|
||||
void start() { t_[0] = clock(); }
|
||||
void end() { t_[1] = clock(); }
|
||||
|
||||
time_t sec() { return (time_t)((t_[1] - t_[0]) / CLOCKS_PER_SEC); }
|
||||
time_t msec() { return (time_t)((t_[1] - t_[0]) * 1000 / CLOCKS_PER_SEC); }
|
||||
time_t usec() { return (time_t)((t_[1] - t_[0]) * 1000000 / CLOCKS_PER_SEC); }
|
||||
time_t current() { return (time_t)clock(); }
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
private:
|
||||
#ifdef _WIN32
|
||||
DWORD t_[2];
|
||||
#else
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
struct timeval tv[2];
|
||||
struct timezone tz;
|
||||
#else
|
||||
time_t t_[2];
|
||||
#endif
|
||||
#endif
|
||||
};
|
||||
|
||||
static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials, bool triangulate = true)
|
||||
{
|
||||
std::cout << "# of shapes : " << shapes.size() << std::endl;
|
||||
@@ -131,8 +215,11 @@ TestLoadObj(
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
|
||||
timerutil t;
|
||||
t.start();
|
||||
std::string err;
|
||||
bool ret = tinyobj::LoadObj(shapes, materials, err, filename, basepath, flags);
|
||||
t.end();
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
@@ -143,6 +230,8 @@ TestLoadObj(
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("Parse time: %lu [msecs]\n", t.msec());
|
||||
|
||||
bool triangulate( ( flags & tinyobj::triangulation ) == tinyobj::triangulation );
|
||||
PrintInfo(shapes, materials, triangulate );
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// Copyright 2012-2015, Syoyo Fujita.
|
||||
// Copyright 2012-2016, Syoyo Fujita.
|
||||
//
|
||||
// Licensed under 2-clause BSD license.
|
||||
//
|
||||
@@ -267,6 +267,40 @@ struct obj_shape {
|
||||
std::vector<float> vt;
|
||||
};
|
||||
|
||||
//See http://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf
|
||||
std::istream& safeGetline(std::istream& is, std::string& t)
|
||||
{
|
||||
t.clear();
|
||||
|
||||
// The characters in the stream are read one-by-one using a std::streambuf.
|
||||
// That is faster than reading them one-by-one using the std::istream.
|
||||
// Code that uses streambuf this way must be guarded by a sentry object.
|
||||
// The sentry object performs various tasks,
|
||||
// such as thread synchronization and updating the stream state.
|
||||
|
||||
std::istream::sentry se(is, true);
|
||||
std::streambuf* sb = is.rdbuf();
|
||||
|
||||
for(;;) {
|
||||
int c = sb->sbumpc();
|
||||
switch (c) {
|
||||
case '\n':
|
||||
return is;
|
||||
case '\r':
|
||||
if(sb->sgetc() == '\n')
|
||||
sb->sbumpc();
|
||||
return is;
|
||||
case EOF:
|
||||
// Also handle the case when the last line has no line ending
|
||||
if(t.empty())
|
||||
is.setstate(std::ios::eofbit);
|
||||
return is;
|
||||
default:
|
||||
t += (char)c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define IS_SPACE( x ) ( ( (x) == ' ') || ( (x) == '\t') )
|
||||
#define IS_DIGIT( x ) ( (unsigned int)( (x) - '0' ) < (unsigned int)10 )
|
||||
#define IS_NEW_LINE( x ) ( ( (x) == '\r') || ( (x) == '\n') || ( (x) == '\0') )
|
||||
@@ -636,38 +670,34 @@ static bool exportFaceGroupToShape(
|
||||
shape.mesh.num_vertices.push_back(static_cast<unsigned char>(npolys));
|
||||
shape.mesh.material_ids.push_back(material_id); // per face
|
||||
}
|
||||
}
|
||||
|
||||
if( normals_calculation && shape.mesh.normals.empty() )
|
||||
{
|
||||
const unsigned int nIndexs = shape.mesh.indices.size();
|
||||
shape.mesh.normals.resize(shape.mesh.positions.size());
|
||||
if( nIndexs % 3 == 0 )
|
||||
{
|
||||
for ( register unsigned int iIndices = 0; iIndices < nIndexs; iIndices+=3 )
|
||||
{
|
||||
float3 v1, v2, v3;
|
||||
memcpy(&v1, &shape.mesh.positions[shape.mesh.indices[iIndices] * 3], sizeof(float3));
|
||||
memcpy(&v2, &shape.mesh.positions[shape.mesh.indices[iIndices + 1] * 3], sizeof(float3));
|
||||
memcpy(&v3, &shape.mesh.positions[shape.mesh.indices[iIndices + 2] * 3], sizeof(float3));
|
||||
if (normals_calculation && shape.mesh.normals.empty()) {
|
||||
const size_t nIndexs = shape.mesh.indices.size();
|
||||
if (nIndexs % 3 == 0) {
|
||||
shape.mesh.normals.resize(shape.mesh.positions.size());
|
||||
for (register size_t iIndices = 0; iIndices < nIndexs; iIndices += 3) {
|
||||
float3 v1, v2, v3;
|
||||
memcpy(&v1, &shape.mesh.positions[shape.mesh.indices[iIndices] * 3], sizeof(float3));
|
||||
memcpy(&v2, &shape.mesh.positions[shape.mesh.indices[iIndices + 1] * 3], sizeof(float3));
|
||||
memcpy(&v3, &shape.mesh.positions[shape.mesh.indices[iIndices + 2] * 3], sizeof(float3));
|
||||
|
||||
float3 v12( v1,v2 );
|
||||
float3 v13( v1,v3 );
|
||||
float3 v12(v1, v2);
|
||||
float3 v13(v1, v3);
|
||||
|
||||
float3 normal = v12.crossproduct(v13);
|
||||
normal.normalize();
|
||||
float3 normal = v12.crossproduct(v13);
|
||||
normal.normalize();
|
||||
|
||||
memcpy(&shape.mesh.normals[shape.mesh.indices[iIndices] * 3], &normal, sizeof(float3));
|
||||
memcpy(&shape.mesh.normals[shape.mesh.indices[iIndices + 1] * 3], &normal, sizeof(float3));
|
||||
memcpy(&shape.mesh.normals[shape.mesh.indices[iIndices + 2] * 3], &normal, sizeof(float3));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "WARN: The shape " << name << " does not have a topology of triangles, therfore the normals calculation could not be performed. Select the tinyobj::triangulation flag for this object." << std::endl;
|
||||
err += ss.str();
|
||||
}
|
||||
}
|
||||
memcpy(&shape.mesh.normals[shape.mesh.indices[iIndices] * 3], &normal, sizeof(float3));
|
||||
memcpy(&shape.mesh.normals[shape.mesh.indices[iIndices + 1] * 3], &normal, sizeof(float3));
|
||||
memcpy(&shape.mesh.normals[shape.mesh.indices[iIndices + 2] * 3], &normal, sizeof(float3));
|
||||
}
|
||||
} else {
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "WARN: The shape " << name << " does not have a topology of triangles, therfore the normals calculation could not be performed. Select the tinyobj::triangulation flag for this object." << std::endl;
|
||||
err += ss.str();
|
||||
}
|
||||
}
|
||||
|
||||
shape.name = name;
|
||||
@@ -686,12 +716,9 @@ void LoadMtl(std::map<std::string, int> &material_map,
|
||||
material_t material;
|
||||
InitMaterial(material);
|
||||
|
||||
size_t maxchars = 8192; // Alloc enough size.
|
||||
std::vector<char> buf(maxchars); // Alloc enough size.
|
||||
while (inStream.peek() != -1) {
|
||||
inStream.getline(&buf[0], static_cast<std::streamsize>(maxchars));
|
||||
|
||||
std::string linebuf(&buf[0]);
|
||||
std::string linebuf;
|
||||
safeGetline(inStream, linebuf);
|
||||
|
||||
// Trim newline '\r\n' or '\n'
|
||||
if (linebuf.size() > 0) {
|
||||
@@ -976,12 +1003,9 @@ bool LoadObj(std::vector<shape_t> &shapes, // [output]
|
||||
|
||||
shape_t shape;
|
||||
|
||||
int maxchars = 8192; // Alloc enough size.
|
||||
std::vector<char> buf(static_cast<size_t>(maxchars)); // Alloc enough size.
|
||||
while (inStream.peek() != -1) {
|
||||
inStream.getline(&buf[0], maxchars);
|
||||
|
||||
std::string linebuf(&buf[0]);
|
||||
std::string linebuf;
|
||||
safeGetline(inStream, linebuf);
|
||||
|
||||
// Trim newline '\r\n' or '\n'
|
||||
if (linebuf.size() > 0) {
|
||||
|
||||
Reference in New Issue
Block a user