Created
October 8, 2013 04:00
-
-
Save davepape/6879277 to your computer and use it in GitHub Desktop.
applying a "filter" - a square with different blending functions is drawn on top of the rendered scene. The first form fades everything in/out; the second multiplies everything by a color; the third creates a negative image
This file contains hidden or 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 BlendFilter: | |
| def __init__(self, srcFunc, destFunc, color): | |
| self.srcFunc = srcFunc | |
| self.destFunc = destFunc | |
| self.color = color | |
| self.vlist = pyglet.graphics.vertex_list(4, ('v2f', [-1,-1, 1,-1, -1,1, 1,1])) | |
| def draw(self): | |
| glMatrixMode(GL_PROJECTION) | |
| glPushMatrix() | |
| glLoadIdentity() | |
| glMatrixMode(GL_MODELVIEW) | |
| glPushMatrix() | |
| glLoadIdentity() | |
| glEnable(GL_BLEND) | |
| glBlendFunc(self.srcFunc, self.destFunc) | |
| glColor4f(self.color[0],self.color[1],self.color[2],self.color[3]) | |
| self.vlist.draw(GL_TRIANGLE_STRIP) | |
| glPopMatrix() | |
| glMatrixMode(GL_PROJECTION) | |
| glPopMatrix() | |
| glMatrixMode(GL_MODELVIEW) | |
| class TexAlphaSquare: | |
| def __init__(self, width, height, xpos, ypos, heading, texturefile, color): | |
| self.xpos = xpos | |
| self.ypos = ypos | |
| self.heading = heading | |
| self.color = color | |
| if texturefile: | |
| img = pyglet.image.load(texturefile) | |
| self.texture = img.get_texture() | |
| else: | |
| self.texture = None | |
| verts = [-width/2.0, -height/2.0, width/2.0, -height/2.0, -width/2.0, height/2.0, width/2.0, height/2.0] | |
| texcoords = [0,0, 1,0, 0,1, 1,1] | |
| self.vlist = pyglet.graphics.vertex_list(4, ('v2f', verts), ('t2f', texcoords)) | |
| 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(self.color[0], self.color[1], self.color[2], self.color[3]) | |
| glPushMatrix() | |
| glTranslatef(self.xpos, self.ypos, 0) | |
| glRotatef(self.heading, 0, 0, 1) | |
| self.vlist.draw(GL_TRIANGLE_STRIP) | |
| glPopMatrix() | |
| if self.texture: | |
| glBindTexture(GL_TEXTURE_2D, 0) | |
| glDisable(GL_TEXTURE_2D) | |
| glDisable(GL_BLEND) | |
| objects = [ TexAlphaSquare(640, 480, 0, 0, 0, 'hst9904a.jpg', [1,1,1,1]), | |
| TexAlphaSquare(192, 192, 192, 0, 0, 'rocket.tif', [1,1,1,1]) ] | |
| thefilter = BlendFilter(GL_ZERO, GL_SRC_ALPHA, [1,1,1,0.5]) | |
| #thefilter = BlendFilter(GL_ZERO, GL_SRC_COLOR, [1,0,0.5,1]) | |
| #thefilter = BlendFilter(GL_ONE_MINUS_DST_COLOR, GL_ZERO, [1,1,1,1]) | |
| @window.event | |
| def on_draw(): | |
| glClearColor(0, 0, 0, 0) | |
| glClear(GL_COLOR_BUFFER_BIT) | |
| glLoadIdentity() | |
| glTranslatef(320, 240, 0) | |
| for o in objects: | |
| o.draw() | |
| thefilter.draw() | |
| def update(dt): | |
| global objects | |
| angle = (time.time() % 10) * 36 | |
| objects[1].xpos = 192 * math.sin(angle * math.pi/180.0) | |
| objects[1].ypos = 192 * 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment