import time, math from pyglet.gl import * window = pyglet.window.Window() class BlendFilter: def __init__(self, srcFunc=GL_ZERO, destFunc=GL_SRC_ALPHA, color=[1,1,1,1]): self.srcFunc = srcFunc self.destFunc = destFunc self.color = color def draw(self): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() glMatrixMode(GL_MODELVIEW) glEnable(GL_BLEND) glBlendFunc(self.srcFunc, self.destFunc) glColor4f(self.color[0],self.color[1],self.color[2],self.color[3]) glBegin(GL_QUADS) glVertex2f(-1, -1) glVertex2f(-1, 1) glVertex2f(1, 1) glVertex2f(1, -1) glEnd() glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) class TexAlphaSquare: def __init__(self, width=1.0, height=1.0, xpos=0.0, ypos=0.0, heading=0, texturefile=None): self.width = width self.height = height self.xpos = xpos self.ypos = ypos self.heading = heading if texturefile: self.texture = pyglet.image.load(texturefile).get_texture() else: self.texture = None def draw(self): glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) if self.texture: glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, self.texture.id) glColor4f(1,1,1,1) glPushMatrix() glTranslatef(self.xpos, self.ypos, 0) glRotatef(self.heading, 0, 0, 1) glBegin(GL_QUADS) glTexCoord2f(0, 0) glVertex2f(-self.width/2.0, -self.height/2.0) glTexCoord2f(1, 0) glVertex2f(self.width/2.0, -self.height/2.0) glTexCoord2f(1, 1) glVertex2f(self.width/2.0, self.height/2.0) glTexCoord2f(0, 1) glVertex2f(-self.width/2.0, self.height/2.0) glEnd() glPopMatrix() if self.texture: glBindTexture(GL_TEXTURE_2D, 0) glDisable(GL_TEXTURE_2D) glDisable(GL_BLEND) objects = [ TexAlphaSquare(10, 10, texturefile='hst9904a.jpg'), TexAlphaSquare(3, 3, xpos=3, ypos=0, texturefile='rocket.tif') ] thefilter = BlendFilter(GL_ZERO, GL_SRC_ALPHA, color=[1,1,1,0.5]) #thefilter = BlendFilter(GL_ZERO, GL_SRC_COLOR, color=[1,0,0.5,1]) #thefilter = BlendFilter(GL_ONE_MINUS_DST_COLOR, GL_ZERO, color=[1,1,1,1]) @window.event def on_draw(): glClearColor(0, 0.3, 0.5, 0) glClear(GL_COLOR_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(-5, 5, -5, 5) glMatrixMode(GL_MODELVIEW) for o in objects: o.draw() thefilter.draw() def update(dt): global objects angle = (time.time() % 10) * 36 objects[1].xpos = 3 * math.sin(angle * math.pi/180.0) objects[1].ypos = 3 * math.cos(angle * math.pi/180.0) objects[1].heading = 270 - angle thefilter.color[3] = (1.0+math.sin(time.time()))/2.0 pyglet.clock.schedule_interval(update,1/60.0) pyglet.app.run()