diff --git a/punyverse/assets/textures.txt b/punyverse/assets/textures.txt index bd541a4..92765c9 100644 --- a/punyverse/assets/textures.txt +++ b/punyverse/assets/textures.txt @@ -3,6 +3,7 @@ mercury.jpg earth.jpg +cloudmap.jpg moon.jpg mars.jpg jupiter.jpg diff --git a/punyverse/assets/textures/cloudmap.jpg b/punyverse/assets/textures/cloudmap.jpg new file mode 100644 index 0000000..d0a5844 Binary files /dev/null and b/punyverse/assets/textures/cloudmap.jpg differ diff --git a/punyverse/assets/textures/cloudmap.png b/punyverse/assets/textures/cloudmap.png deleted file mode 100644 index 71b6064..0000000 Binary files a/punyverse/assets/textures/cloudmap.png and /dev/null differ diff --git a/punyverse/game.py b/punyverse/game.py index 0e7dbbc..5c3f5f9 100644 --- a/punyverse/game.py +++ b/punyverse/game.py @@ -337,14 +337,14 @@ class Applet(pyglet.window.Window): if self.cloud and hasattr(entity, 'cloudmap') and entity.cloudmap: glPushMatrix() - glEnable(GL_ALPHA_TEST) + glEnable(GL_BLEND) glTranslatef(*entity.location) pitch, yaw, roll = entity.rotation glRotatef(pitch, 1, 0, 0) glRotatef(yaw, 0, 1, 0) glRotatef(roll, 0, 0, 1) glCallList(entity.cloudmap) - glDisable(GL_ALPHA_TEST) + glDisable(GL_BLEND) glPopMatrix() if self.orbit and hasattr(entity, 'get_orbit') and hasattr(entity, 'parent'): diff --git a/punyverse/texture.py b/punyverse/texture.py index 57bd805..b114370 100644 --- a/punyverse/texture.py +++ b/punyverse/texture.py @@ -3,6 +3,7 @@ from pyglet.gl import * from ctypes import c_int, byref, c_ulong import os.path import struct +import itertools try: from _glgeom import bgr_to_rgb @@ -41,7 +42,7 @@ try: except ImportError: from StringIO import StringIO -__all__ = ['load_texture'] +__all__ = ['load_texture', 'load_clouds', 'load_image'] id = 0 cache = {} @@ -147,15 +148,7 @@ def check_size(width, height): raise ValueError('Texture not power of two') -def load_texture(file): - if os.path.isabs(file): - path = file - file = os.path.basename(path) - else: - path = os.path.join(os.path.dirname(__file__), "assets", "textures", file) - - if path in cache: - return cache[path] +def load_image(file, path): print "Loading image %s..." % file, try: @@ -163,7 +156,7 @@ def load_texture(file): except IOError: print 'exists not' raise ValueError('Texture exists not') - type, width, height = image_info(file.read(8192)) + type, width, height = image_info(file.read(65536)) file.seek(0, 0) if type: check_size(width, height) @@ -192,9 +185,6 @@ def load_texture(file): mode = GL_RGBA if 'A' in raw.format else GL_RGB # Flip from BGR to RGB - # I hate you too, Pyglet... - # REGULAR EXPRESSIONS ARE NOT MEANT TO PARSE BINARY DATA!!! - #texture = raw.get_data('RGBA', width * 4) if safe else raw.data[::-1] if 'BGR' in raw.format else raw.data if raw.format in ('BGR', 'BGRA'): if bgra: mode = {GL_RGBA: GL_BGRA, GL_RGB: GL_BGR}[mode] @@ -205,6 +195,20 @@ def load_texture(file): texture = raw.data else: texture = raw.get_data('RGBA', width * 4) + return path, width, height, len(raw.format), mode, texture + + +def load_texture(file): + if os.path.isabs(file): + path = file + file = os.path.basename(path) + else: + path = os.path.join(os.path.dirname(__file__), 'assets', 'textures', file) + + if path in cache: + return cache[path] + + path, width, height, depth, mode, texture = load_image(file, path) buffer = c_ulong() glGenTextures(1, byref(buffer)) @@ -215,7 +219,41 @@ def load_texture(file): filter = GL_NEAREST if badcard else GL_LINEAR glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter) - gluBuild2DMipmaps(GL_TEXTURE_2D, len(raw.format), width, height, mode, GL_UNSIGNED_BYTE, texture) + gluBuild2DMipmaps(GL_TEXTURE_2D, depth, width, height, mode, GL_UNSIGNED_BYTE, texture) + + cache[path] = id + + return id + + +def load_clouds(file): + if os.path.isabs(file): + path = file + file = os.path.basename(path) + else: + path = os.path.join(os.path.dirname(__file__), 'assets', 'textures', file) + + if path in cache: + return cache[path] + + path, width, height, depth, mode, texture = load_image(file, path) + + buffer = c_ulong() + glGenTextures(1, byref(buffer)) + id = buffer.value + + pixels = bytearray(len(texture) * 4) + white = chr(255) + pixels[:] = itertools.chain.from_iterable(itertools.izip(itertools.repeat(white), itertools.repeat(white), + itertools.repeat(white), + itertools.islice(texture, 0, None, depth))) + + glBindTexture(GL_TEXTURE_2D, id) + + filter = GL_NEAREST if badcard else GL_LINEAR + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter) + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, str(pixels)) cache[path] = id diff --git a/punyverse/world.json b/punyverse/world.json index 9178e08..1a2c88f 100644 --- a/punyverse/world.json +++ b/punyverse/world.json @@ -59,7 +59,7 @@ "rotation": 86400, "division": 70, "atmosphere": { - "cloud_texture": "cloudmap.png", + "cloud_texture": ["cloudmap.jpg", "cloudmap_small.jpg"], "diffuse_texture": "atmosphere_earth.png", "diffuse_size": 30 }, diff --git a/punyverse/world.py b/punyverse/world.py index c3848ca..deb4e57 100644 --- a/punyverse/world.py +++ b/punyverse/world.py @@ -24,7 +24,7 @@ from math import pi, sqrt G = 6.67384e-11 # Gravitation Constant -def get_best_texture(info, optional=False): +def get_best_texture(info, optional=False, loader=load_texture): cheap = False skip = False texture = None @@ -37,14 +37,14 @@ def get_best_texture(info, optional=False): break continue try: - texture = load_texture(item) + texture = loader(item) except ValueError: pass else: break else: try: - texture = load_texture(info) + texture = loader(info) except ValueError: if optional: skip = True @@ -139,7 +139,7 @@ def load_world(file): cloud_texture = atmosphere_data.get('cloud_texture', None) corona_texture = atmosphere_data.get('corona_texture', None) if cloud_texture is not None: - cheap, _, cloud_texture = get_best_texture(cloud_texture) + cheap, _, cloud_texture = get_best_texture(cloud_texture, loader=load_clouds) if not cheap: cloudmap_id = compile(sphere, radius + 2, division, division, cloud_texture, lighting=False)