Created
October 8, 2013 03:57
-
-
Save davepape/6879253 to your computer and use it in GitHub Desktop.
a soft-edged disc with alpha blending - the outer edge is fully transparent, while the center of the disc is opaque
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time, math | |
from pyglet.gl import * | |
window = pyglet.window.Window() | |
class Square: | |
def __init__(self, color): | |
self.color = color | |
self.vlist = pyglet.graphics.vertex_list(4, ('v2f', [-128,-128, 128,-128, -128,128, 128,128])) | |
def draw(self): | |
glColor3f(self.color[0],self.color[1],self.color[2]) | |
self.vlist.draw(GL_TRIANGLE_STRIP) | |
def update(self,dt): | |
pass | |
class AlphaWheel: | |
def __init__(self, moveradius, color): | |
self.moveradius = moveradius | |
self.xpos = 0 | |
self.ypos = 0 | |
verts = [0,0] | |
colors = [color[0], color[1], color[2], 1] | |
for angle in range(0,361, 10): | |
verts += [math.sin(angle*math.pi/180.0)*128, math.cos(angle*math.pi/180.0)*128] | |
colors += [color[0], color[1], color[2], 0] | |
self.vlist = pyglet.graphics.vertex_list(len(verts)/2, ('v2f', verts), ('c4f', colors)) | |
def draw(self): | |
# glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) | |
glEnable(GL_BLEND) | |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) | |
glPushMatrix() | |
glTranslatef(self.xpos, self.ypos, 0) | |
self.vlist.draw(GL_TRIANGLE_FAN) | |
glPopMatrix() | |
glDisable(GL_BLEND) | |
def update(self,dt): | |
self.xpos = self.moveradius * math.sin(time.time()) | |
self.ypos = self.moveradius * math.cos(time.time()) | |
objects = [ Square([1,1,1]), AlphaWheel(128, [1,0,0]) ] | |
@window.event | |
def on_draw(): | |
glClearColor(0, 0.3, 0.5, 0) | |
glClear(GL_COLOR_BUFFER_BIT) | |
glLoadIdentity() | |
glTranslatef(320, 240, 0) | |
for o in objects: | |
o.draw() | |
def update(dt): | |
for o in objects: | |
o.update(dt) | |
pyglet.clock.schedule_interval(update,1/60.0) | |
pyglet.app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment