diff --git a/examples/viewer/premake4.lua b/examples/viewer/premake4.lua index 66da6f9..3b6ca1f 100644 --- a/examples/viewer/premake4.lua +++ b/examples/viewer/premake4.lua @@ -14,6 +14,7 @@ solution "objview" configuration { "linux" } linkoptions { "`pkg-config --libs glfw3`" } links { "GL", "GLU", "m", "GLEW", "X11", "Xrandr", "Xinerama", "Xi", "Xxf86vm", "Xcursor", "dl" } + linkoptions { "-pthread" } configuration { "windows" } -- Path to GLFW3 diff --git a/examples/viewer/viewer.cc b/examples/viewer/viewer.cc index 4f1c211..2b1bdf4 100644 --- a/examples/viewer/viewer.cc +++ b/examples/viewer/viewer.cc @@ -26,6 +26,89 @@ #include "trackball.h" +#ifdef _WIN32 +#ifdef __cplusplus +extern "C" { +#endif +#include +#include +#ifdef __cplusplus +} +#endif +#pragma comment(lib, "winmm.lib") +#else +#if defined(__unix__) || defined(__APPLE__) +#include +#else +#include +#endif +#endif + +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 +}; + typedef struct { GLuint vb; // vertex buffer int numTriangles; @@ -83,6 +166,10 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], std::vector& dr tinyobj::attrib_t attrib; std::vector shapes; std::vector materials; + + timerutil tm; + + tm.start(); std::string err; bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename, NULL); @@ -90,11 +177,15 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], std::vector& dr std::cerr << err << std::endl; } + tm.end(); + if (!ret) { std::cerr << "Failed to load " << filename << std::endl; return false; } + printf("Parsing time: %d [ms]\n", tm.msec()); + printf("# of vertices = %d\n", (int)(attrib.vertices.size()) / 3); printf("# of normals = %d\n", (int)(attrib.normals.size()) / 3); printf("# of texcoords = %d\n", (int)(attrib.texcoords.size()) / 2);