New clouds that actually looks realistic. Closes #99.

This commit is contained in:
Quantum 2013-11-09 14:27:35 -05:00
parent 59b6f2c7c0
commit c0f4807d88
7 changed files with 61 additions and 22 deletions

View file

@ -3,6 +3,7 @@
mercury.jpg mercury.jpg
earth.jpg earth.jpg
cloudmap.jpg
moon.jpg moon.jpg
mars.jpg mars.jpg
jupiter.jpg jupiter.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 351 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 MiB

View file

@ -337,14 +337,14 @@ class Applet(pyglet.window.Window):
if self.cloud and hasattr(entity, 'cloudmap') and entity.cloudmap: if self.cloud and hasattr(entity, 'cloudmap') and entity.cloudmap:
glPushMatrix() glPushMatrix()
glEnable(GL_ALPHA_TEST) glEnable(GL_BLEND)
glTranslatef(*entity.location) glTranslatef(*entity.location)
pitch, yaw, roll = entity.rotation pitch, yaw, roll = entity.rotation
glRotatef(pitch, 1, 0, 0) glRotatef(pitch, 1, 0, 0)
glRotatef(yaw, 0, 1, 0) glRotatef(yaw, 0, 1, 0)
glRotatef(roll, 0, 0, 1) glRotatef(roll, 0, 0, 1)
glCallList(entity.cloudmap) glCallList(entity.cloudmap)
glDisable(GL_ALPHA_TEST) glDisable(GL_BLEND)
glPopMatrix() glPopMatrix()
if self.orbit and hasattr(entity, 'get_orbit') and hasattr(entity, 'parent'): if self.orbit and hasattr(entity, 'get_orbit') and hasattr(entity, 'parent'):

View file

@ -3,6 +3,7 @@ from pyglet.gl import *
from ctypes import c_int, byref, c_ulong from ctypes import c_int, byref, c_ulong
import os.path import os.path
import struct import struct
import itertools
try: try:
from _glgeom import bgr_to_rgb from _glgeom import bgr_to_rgb
@ -41,7 +42,7 @@ try:
except ImportError: except ImportError:
from StringIO import StringIO from StringIO import StringIO
__all__ = ['load_texture'] __all__ = ['load_texture', 'load_clouds', 'load_image']
id = 0 id = 0
cache = {} cache = {}
@ -147,15 +148,7 @@ def check_size(width, height):
raise ValueError('Texture not power of two') raise ValueError('Texture not power of two')
def load_texture(file): def load_image(file, path):
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]
print "Loading image %s..." % file, print "Loading image %s..." % file,
try: try:
@ -163,7 +156,7 @@ def load_texture(file):
except IOError: except IOError:
print 'exists not' print 'exists not'
raise ValueError('Texture 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) file.seek(0, 0)
if type: if type:
check_size(width, height) check_size(width, height)
@ -192,9 +185,6 @@ def load_texture(file):
mode = GL_RGBA if 'A' in raw.format else GL_RGB mode = GL_RGBA if 'A' in raw.format else GL_RGB
# Flip from BGR to 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 raw.format in ('BGR', 'BGRA'):
if bgra: if bgra:
mode = {GL_RGBA: GL_BGRA, GL_RGB: GL_BGR}[mode] mode = {GL_RGBA: GL_BGRA, GL_RGB: GL_BGR}[mode]
@ -205,6 +195,20 @@ def load_texture(file):
texture = raw.data texture = raw.data
else: else:
texture = raw.get_data('RGBA', width * 4) 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() buffer = c_ulong()
glGenTextures(1, byref(buffer)) glGenTextures(1, byref(buffer))
@ -215,7 +219,41 @@ def load_texture(file):
filter = GL_NEAREST if badcard else GL_LINEAR filter = GL_NEAREST if badcard else GL_LINEAR
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_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 cache[path] = id

View file

@ -59,7 +59,7 @@
"rotation": 86400, "rotation": 86400,
"division": 70, "division": 70,
"atmosphere": { "atmosphere": {
"cloud_texture": "cloudmap.png", "cloud_texture": ["cloudmap.jpg", "cloudmap_small.jpg"],
"diffuse_texture": "atmosphere_earth.png", "diffuse_texture": "atmosphere_earth.png",
"diffuse_size": 30 "diffuse_size": 30
}, },

View file

@ -24,7 +24,7 @@ from math import pi, sqrt
G = 6.67384e-11 # Gravitation Constant 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 cheap = False
skip = False skip = False
texture = None texture = None
@ -37,14 +37,14 @@ def get_best_texture(info, optional=False):
break break
continue continue
try: try:
texture = load_texture(item) texture = loader(item)
except ValueError: except ValueError:
pass pass
else: else:
break break
else: else:
try: try:
texture = load_texture(info) texture = loader(info)
except ValueError: except ValueError:
if optional: if optional:
skip = True skip = True
@ -139,7 +139,7 @@ def load_world(file):
cloud_texture = atmosphere_data.get('cloud_texture', None) cloud_texture = atmosphere_data.get('cloud_texture', None)
corona_texture = atmosphere_data.get('corona_texture', None) corona_texture = atmosphere_data.get('corona_texture', None)
if cloud_texture is not 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: if not cheap:
cloudmap_id = compile(sphere, radius + 2, division, division, cloud_texture, cloudmap_id = compile(sphere, radius + 2, division, division, cloud_texture,
lighting=False) lighting=False)