From 0ae75da2a70aa7df84c2040618594257bd4caa87 Mon Sep 17 00:00:00 2001 From: Quantum Date: Sat, 2 Nov 2013 15:00:47 -0400 Subject: [PATCH] Added a sort of corona effect. --- punyverse/entity.py | 1 + punyverse/game.py | 9 +++++++-- punyverse/glgeom.py | 39 ++++++++++++++++++++++++++++++++++++++- punyverse/world.json | 8 +++++++- punyverse/world.py | 31 ++++++++++++++++++++++--------- 5 files changed, 75 insertions(+), 13 deletions(-) diff --git a/punyverse/entity.py b/punyverse/entity.py index b79c63e..50ef295 100644 --- a/punyverse/entity.py +++ b/punyverse/entity.py @@ -45,6 +45,7 @@ class Body(Entity): self.rotation_angle = kwargs.pop('rotation_angle', 5) self.atmosphere = kwargs.pop('atmosphere', 0) self.cloudmap = kwargs.pop('cloudmap', 0) + self.corona = kwargs.pop('corona', 0) self.last_tick = 0 self.mass = kwargs.pop('mass', None) self.world = kwargs.pop('world') diff --git a/punyverse/game.py b/punyverse/game.py index 01a5ea5..2bc8929 100644 --- a/punyverse/game.py +++ b/punyverse/game.py @@ -314,7 +314,9 @@ class Applet(pyglet.window.Window): glPopAttrib() glPopMatrix() - if self.atmosphere and hasattr(entity, 'atmosphere') and entity.atmosphere: + has_corona = hasattr(entity, 'corona') and entity.corona + has_atmosphere = hasattr(entity, 'atmosphere') and entity.atmosphere + if self.atmosphere and (has_corona or has_atmosphere): glPushMatrix() x0, y0, z0 = entity.location dx, dy, dz = x - x0, y - y0, z - z0 @@ -326,7 +328,10 @@ class Applet(pyglet.window.Window): glTranslatef(x0, y0, z0) glRotatef(pitch, 1, 0, 0) glRotatef(yaw, 0, 1, 0) - glCallList(entity.atmosphere) + if has_corona: + glCallList(entity.corona) + if has_atmosphere: + glCallList(entity.atmosphere) glPopMatrix() if self.cloud and hasattr(entity, "cloudmap") and entity.cloudmap: diff --git a/punyverse/glgeom.py b/punyverse/glgeom.py index 6796366..c9e52a1 100644 --- a/punyverse/glgeom.py +++ b/punyverse/glgeom.py @@ -4,7 +4,8 @@ from random import random, uniform TWOPI = pi * 2 -__all__ = ['compile', 'ortho', 'frustrum', 'crosshair', 'circle', 'disk', 'sphere', 'colourball', 'torus', 'belt'] +__all__ = ['compile', 'ortho', 'frustrum', 'crosshair', 'circle', 'disk', 'sphere', 'colourball', 'torus', 'belt', + 'flare'] def compile(pointer, *args, **kwargs): @@ -77,6 +78,42 @@ def disk(rinner, router, segs, tex): glDisable(GL_TEXTURE_2D) +def flare(rinner, router, res, prob, tex): + glEnable(GL_TEXTURE_2D) + glDisable(GL_LIGHTING) + glBindTexture(GL_TEXTURE_2D, tex) + last_x = 1 + last_y = 0 + last_theta = 0 + factor = TWOPI / res + rdelta = (router - rinner) * 5 + glBegin(GL_QUADS) + for i in xrange(res + 1): + theta = last_theta + factor + x = cos(theta) + y = sin(theta) + if random() > prob: + distance = router + rdelta * random() + avg_theta = (last_theta + theta) / 2 + x0, y0 = rinner * last_x, rinner * last_y + x1, y1 = rinner * x, rinner * y + x2, y2 = distance * cos(avg_theta), distance * sin(avg_theta) + glTexCoord2f(0, 0) + glVertex2f(x0, y0) + glTexCoord2f(0, 1) + glVertex2f(x1, y1) + glTexCoord2f(1, 0) + glVertex2f(x2, y2) + glTexCoord2f(1, 1) + glVertex2f(x2, y2) + last_theta = theta + last_x = x + last_y = y + glEnd() + glEnable(GL_LIGHTING) + glDisable(GL_TEXTURE_2D) + + def sphere(r, lats, longs, tex, lighting=True, fv4=GLfloat * 4): """ Sphere function from the OpenGL red book. diff --git a/punyverse/world.json b/punyverse/world.json index e11b1ed..649ebf7 100644 --- a/punyverse/world.json +++ b/punyverse/world.json @@ -20,7 +20,13 @@ "pitch": -90, "yaw": 7.25, "mass": 1.9891e+30, - "rotation": 2164320 + "rotation": 2164320, + "atmosphere": { + "corona_texture": "sun_corona.png", + "corona_size": 300, + "corona_division": 100, + "corona_prob": 0.5 + } }, "mercury": { "texture": ["mercury.jpg", "mercury_small.jpg", [0.44, 0.43, 0.43]], diff --git a/punyverse/world.py b/punyverse/world.py index 6545574..ad8ba06 100644 --- a/punyverse/world.py +++ b/punyverse/world.py @@ -131,22 +131,35 @@ def load_world(file): atmosphere_id = 0 cloudmap_id = 0 + corona_id = 0 if 'atmosphere' in info: atmosphere_data = info['atmosphere'] - size = e(atmosphere_data.get('diffuse_size', None)) + atm_size = e(atmosphere_data.get('diffuse_size', None)) atm_texture = atmosphere_data.get('diffuse_texture', None) cloud_texture = atmosphere_data.get('cloud_texture', None) - cheap, _, cloud_texture = get_best_texture(cloud_texture) - if not cheap: - cloudmap_id = compile(sphere, radius + 2, division, division, cloud_texture, - lighting=False) - cheap, _, atm_texture = get_best_texture(atm_texture) - if not cheap: - atmosphere_id = compile(disk, radius, radius + size, 30, atm_texture) + corona_texture = atmosphere_data.get('corona_texture', None) + if cloud_texture is not None: + cheap, _, cloud_texture = get_best_texture(cloud_texture) + if not cheap: + cloudmap_id = compile(sphere, radius + 2, division, division, cloud_texture, + lighting=False) + if corona_texture is not None: + cheap, _, corona = get_best_texture(corona_texture) + if not cheap: + corona_size = atmosphere_data.get('corona_size', radius / 2) + corona_division = atmosphere_data.get('corona_division', 100) + corona_ratio = atmosphere_data.get('corona_ratio', 0.5) + corona_id = compile(flare, radius, radius + corona_size, corona_division, + corona_ratio, corona) + elif atm_texture is not None: + cheap, _, atm_texture = get_best_texture(atm_texture) + if not cheap: + atmosphere_id = compile(disk, radius, radius + atm_size, 30, atm_texture) theta = 360 / (rotation + .0) if rotation else 0 object = type(object_id, (x, y, z), (pitch, yaw, roll), rotation_angle=theta, - atmosphere=atmosphere_id, cloudmap=cloudmap_id, background=background, **params) + atmosphere=atmosphere_id, cloudmap=cloudmap_id, background=background, + corona=corona_id, **params) world.tracker.append(object) if 'ring' in info: