Support loading .obj from gzip compression.

This commit is contained in:
Syoyo Fujita
2016-05-16 01:24:52 +09:00
parent 00251e9a5b
commit 8205067928
2 changed files with 123 additions and 35 deletions

View File

@@ -1,3 +1,8 @@
newoption {
trigger = "with-zlib",
description = "Build with zlib."
}
solution "objview"
-- location ( "build" )
configurations { "Release", "Debug" }
@@ -5,44 +10,51 @@ solution "objview"
project "objview"
kind "ConsoleApp"
language "C++"
files { "viewer.cc", "trackball.cc" }
includedirs { "./" }
includedirs { "../../" }
kind "ConsoleApp"
language "C++"
files { "viewer.cc", "trackball.cc" }
includedirs { "./" }
includedirs { "../../" }
buildoptions { "-std=c++11" }
--buildoptions { "-fsanitize=address" }
--linkoptions { "-fsanitize=address" }
buildoptions { "-std=c++11" }
configuration { "linux" }
linkoptions { "`pkg-config --libs glfw3`" }
links { "GL", "GLU", "m", "GLEW", "X11", "Xrandr", "Xinerama", "Xi", "Xxf86vm", "Xcursor", "dl" }
linkoptions { "-pthread" }
if _OPTIONS['with-zlib'] then
defines { 'ENABLE_ZLIB' }
links { 'z' }
end
configuration { "windows" }
-- Path to GLFW3
includedirs { '../../../../local/glfw-3.1.2.bin.WIN64/include' }
libdirs { '../../../../local/glfw-3.1.2.bin.WIN64/lib-vc2013' }
-- Path to GLEW
includedirs { '../../../../local/glew-1.13.0/include' }
libdirs { '../../../../local/glew-1.13.0/lib/Release/x64' }
-- Uncomment if you want address sanitizer(gcc/clang only)
--buildoptions { "-fsanitize=address" }
--linkoptions { "-fsanitize=address" }
links { "glfw3", "glew32", "gdi32", "winmm", "user32", "glu32","opengl32", "kernel32" }
defines { "_CRT_SECURE_NO_WARNINGS" }
configuration { "linux" }
linkoptions { "`pkg-config --libs glfw3`" }
links { "GL", "GLU", "m", "GLEW", "X11", "Xrandr", "Xinerama", "Xi", "Xxf86vm", "Xcursor", "dl" }
linkoptions { "-pthread" }
configuration { "macosx" }
includedirs { "/usr/local/include" }
buildoptions { "-Wno-deprecated-declarations" }
libdirs { "/usr/local/lib" }
links { "glfw3", "GLEW" }
linkoptions { "-framework OpenGL", "-framework Cocoa", "-framework IOKit", "-framework CoreVideo" }
configuration { "windows" }
-- Path to GLFW3
includedirs { '../../../../local/glfw-3.1.2.bin.WIN64/include' }
libdirs { '../../../../local/glfw-3.1.2.bin.WIN64/lib-vc2013' }
-- Path to GLEW
includedirs { '../../../../local/glew-1.13.0/include' }
libdirs { '../../../../local/glew-1.13.0/lib/Release/x64' }
configuration "Debug"
defines { "DEBUG" }
flags { "Symbols"}
links { "glfw3", "glew32", "gdi32", "winmm", "user32", "glu32","opengl32", "kernel32" }
defines { "_CRT_SECURE_NO_WARNINGS" }
configuration "Release"
defines { "NDEBUG" }
flags { "Optimize"}
configuration { "macosx" }
includedirs { "/usr/local/include" }
buildoptions { "-Wno-deprecated-declarations" }
libdirs { "/usr/local/lib" }
links { "glfw3", "GLEW" }
linkoptions { "-framework OpenGL", "-framework Cocoa", "-framework IOKit", "-framework CoreVideo" }
configuration "Debug"
defines { "DEBUG" }
flags { "Symbols"}
configuration "Release"
defines { "NDEBUG" }
flags { "Optimize"}

View File

@@ -9,8 +9,13 @@
#include <limits>
#include <cmath>
#include <cassert>
#include <cstring>
#include <algorithm>
#if defined(ENABLE_ZLIB)
#include <zlib.h>
#endif
#include <GL/glew.h>
#ifdef __APPLE__
@@ -135,6 +140,78 @@ const char *mmap_file(size_t *len, const char* filename)
#endif
}
bool gz_load(std::vector<char>* buf, const char* filename)
{
gzFile file;
file = gzopen (filename, "r");
if (! file) {
fprintf (stderr, "gzopen of '%s' failed: %s.\n", filename,
strerror (errno));
exit (EXIT_FAILURE);
return false;
}
while (1) {
int err;
int bytes_read;
unsigned char buffer[1024];
bytes_read = gzread (file, buffer, 1024);
buf->insert(buf->end(), buffer, buffer + 1024);
//printf ("%s", buffer);
if (bytes_read < 1024) {
if (gzeof (file)) {
break;
}
else {
const char * error_string;
error_string = gzerror (file, & err);
if (err) {
fprintf (stderr, "Error: %s.\n", error_string);
exit (EXIT_FAILURE);
return false;
}
}
}
}
gzclose (file);
return true;
}
const char* get_file_data(size_t *len, const char* filename)
{
char *ext = strrchr(filename, '.');
size_t data_len = 0;
const char* data = nullptr;
#if defined(ENABLE_ZLIB)
if (strcmp(ext, ".gz") == 0) {
// gzipped data.
printf("compressed\n");
std::vector<char> buf;
bool ret = gz_load(&buf, filename);
if (ret) {
char *p = static_cast<char*>(malloc(buf.size() + 1)); // @fixme { implement deleter }
memcpy(p, &buf.at(0), buf.size());
p[buf.size()] = '\0';
data = p;
data_len = buf.size();
}
} else {
#else
{
#endif
data = mmap_file(&data_len, filename);
}
(*len) = data_len;
return data;
}
bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename)
{
@@ -144,8 +221,7 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename)
std::vector<vertex_index> faces;
size_t data_len = 0;
const char* data = nullptr;
data = mmap_file(&data_len, filename);
const char* data = get_file_data(&data_len, filename);
if (data == nullptr) {
exit(-1);
return false;