Support loading .obj from gzip compression.
This commit is contained in:
@@ -1,3 +1,8 @@
|
|||||||
|
newoption {
|
||||||
|
trigger = "with-zlib",
|
||||||
|
description = "Build with zlib."
|
||||||
|
}
|
||||||
|
|
||||||
solution "objview"
|
solution "objview"
|
||||||
-- location ( "build" )
|
-- location ( "build" )
|
||||||
configurations { "Release", "Debug" }
|
configurations { "Release", "Debug" }
|
||||||
@@ -5,44 +10,51 @@ solution "objview"
|
|||||||
|
|
||||||
project "objview"
|
project "objview"
|
||||||
|
|
||||||
kind "ConsoleApp"
|
kind "ConsoleApp"
|
||||||
language "C++"
|
language "C++"
|
||||||
files { "viewer.cc", "trackball.cc" }
|
files { "viewer.cc", "trackball.cc" }
|
||||||
includedirs { "./" }
|
includedirs { "./" }
|
||||||
includedirs { "../../" }
|
includedirs { "../../" }
|
||||||
|
|
||||||
buildoptions { "-std=c++11" }
|
buildoptions { "-std=c++11" }
|
||||||
--buildoptions { "-fsanitize=address" }
|
|
||||||
--linkoptions { "-fsanitize=address" }
|
|
||||||
|
|
||||||
configuration { "linux" }
|
if _OPTIONS['with-zlib'] then
|
||||||
linkoptions { "`pkg-config --libs glfw3`" }
|
defines { 'ENABLE_ZLIB' }
|
||||||
links { "GL", "GLU", "m", "GLEW", "X11", "Xrandr", "Xinerama", "Xi", "Xxf86vm", "Xcursor", "dl" }
|
links { 'z' }
|
||||||
linkoptions { "-pthread" }
|
end
|
||||||
|
|
||||||
configuration { "windows" }
|
-- Uncomment if you want address sanitizer(gcc/clang only)
|
||||||
-- Path to GLFW3
|
--buildoptions { "-fsanitize=address" }
|
||||||
includedirs { '../../../../local/glfw-3.1.2.bin.WIN64/include' }
|
--linkoptions { "-fsanitize=address" }
|
||||||
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' }
|
|
||||||
|
|
||||||
links { "glfw3", "glew32", "gdi32", "winmm", "user32", "glu32","opengl32", "kernel32" }
|
configuration { "linux" }
|
||||||
defines { "_CRT_SECURE_NO_WARNINGS" }
|
linkoptions { "`pkg-config --libs glfw3`" }
|
||||||
|
links { "GL", "GLU", "m", "GLEW", "X11", "Xrandr", "Xinerama", "Xi", "Xxf86vm", "Xcursor", "dl" }
|
||||||
|
linkoptions { "-pthread" }
|
||||||
|
|
||||||
configuration { "macosx" }
|
configuration { "windows" }
|
||||||
includedirs { "/usr/local/include" }
|
-- Path to GLFW3
|
||||||
buildoptions { "-Wno-deprecated-declarations" }
|
includedirs { '../../../../local/glfw-3.1.2.bin.WIN64/include' }
|
||||||
libdirs { "/usr/local/lib" }
|
libdirs { '../../../../local/glfw-3.1.2.bin.WIN64/lib-vc2013' }
|
||||||
links { "glfw3", "GLEW" }
|
-- Path to GLEW
|
||||||
linkoptions { "-framework OpenGL", "-framework Cocoa", "-framework IOKit", "-framework CoreVideo" }
|
includedirs { '../../../../local/glew-1.13.0/include' }
|
||||||
|
libdirs { '../../../../local/glew-1.13.0/lib/Release/x64' }
|
||||||
|
|
||||||
configuration "Debug"
|
links { "glfw3", "glew32", "gdi32", "winmm", "user32", "glu32","opengl32", "kernel32" }
|
||||||
defines { "DEBUG" }
|
defines { "_CRT_SECURE_NO_WARNINGS" }
|
||||||
flags { "Symbols"}
|
|
||||||
|
|
||||||
configuration "Release"
|
configuration { "macosx" }
|
||||||
defines { "NDEBUG" }
|
includedirs { "/usr/local/include" }
|
||||||
flags { "Optimize"}
|
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"}
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,13 @@
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cstring>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
#if defined(ENABLE_ZLIB)
|
||||||
|
#include <zlib.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
|
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
@@ -135,6 +140,78 @@ const char *mmap_file(size_t *len, const char* filename)
|
|||||||
#endif
|
#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)
|
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;
|
std::vector<vertex_index> faces;
|
||||||
|
|
||||||
size_t data_len = 0;
|
size_t data_len = 0;
|
||||||
const char* data = nullptr;
|
const char* data = get_file_data(&data_len, filename);
|
||||||
data = mmap_file(&data_len, filename);
|
|
||||||
if (data == nullptr) {
|
if (data == nullptr) {
|
||||||
exit(-1);
|
exit(-1);
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user