Added compression support to wavefront loader.

Removed massive generated C file.
This commit is contained in:
Quantum 2013-11-23 12:12:48 -05:00
parent 878db3658a
commit e9403fe746
2 changed files with 62 additions and 12995 deletions

File diff suppressed because it is too large Load diff

View file

@ -7,6 +7,20 @@ from punyverse.texture import load_texture
include "_cyopengl.pxi" include "_cyopengl.pxi"
from uuid import uuid4 from uuid import uuid4
import os import os
import gzip
import bz2
import zipfile
def zip_open(file):
zip = zipfile.ZipFile(file)
return zip.open(zip.namelist()[0])
openers = {
'gz': gzip.open,
'bz2': bz2.BZ2File,
'zip': zip_open,
}
cdef enum: cdef enum:
FACE_TRIANGLES FACE_TRIANGLES
@ -178,8 +192,8 @@ cdef class WavefrontObject(object):
group.faces.append(Face(type, vindices, nindices, tindices, face_vertices, face_normals, face_textures)) group.faces.append(Face(type, vindices, nindices, tindices, face_vertices, face_normals, face_textures))
cdef void material(self, list words): cdef bint material(self, list words) except False:
self.perform_io(os.path.join(self.root, words[1])) return self.perform_io(os.path.join(self.root, words[1]))
cdef void use_material(self, list words): cdef void use_material(self, list words):
mat = words[1] mat = words[1]
@ -199,24 +213,16 @@ cdef class WavefrontObject(object):
self.groups.append(group) self.groups.append(group)
self.current_group = group self.current_group = group
cdef inline void perform_io(self, unicode file): cdef inline bint perform_io(self, unicode file) except False:
mbcsfile = file.encode('mbcs') cdef const char *type
cdef char* fname = mbcsfile
cdef FILE* cfile
cfile = fopen(fname, 'rb')
if cfile == NULL:
raise IOError(2, "No such file or directory: '%s'" % file)
cdef size_t bufsize = 2048
cdef char *buf = <char*>malloc(bufsize)
cdef ssize_t read
cdef char *type
cdef list words cdef list words
cdef int hash, length cdef int hash, length
while fgets(buf, bufsize, cfile): ext = os.path.splitext(file)[1].lstrip('.')
if buf[0] in (0, 10, 13, 35): reader = openers.get(ext, open)(file)
with reader:
for buf in reader:
if not buf or buf.startswith(('\r', '\n', '#')):
continue # Empty or comment continue # Empty or comment
words = buf.split() words = buf.split()
type = words[0] type = words[0]
@ -254,8 +260,7 @@ cdef class WavefrontObject(object):
self.new_material(words) self.new_material(words)
elif strcmp(type, b'map_Kd') == 0: elif strcmp(type, b'map_Kd') == 0:
self.material_texture(words) self.material_texture(words)
free(buf) return True
fclose(cfile)
model_base = None model_base = None