Created
October 8, 2013 03:58
-
-
Save davepape/6879264 to your computer and use it in GitHub Desktop.
using a texture map with an alpha channel to get a cut-out sprite
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 TexAlphaSquare: | |
def __init__(self, width, height, xpos, ypos, texturefile, color): | |
self.xpos = xpos | |
self.ypos = ypos | |
self.heading = 0 | |
self.color = color | |
img = pyglet.image.load(texturefile) | |
self.texture = img.get_texture() | |
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) | |
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() | |
glBindTexture(GL_TEXTURE_2D, 0) | |
glDisable(GL_TEXTURE_2D) | |
glDisable(GL_BLEND) | |
objects = [ TexAlphaSquare(640, 480, 0, 0, 'hst9904a.jpg', [1,1,1,1]), | |
TexAlphaSquare(192, 192, 0, 0, 'rocket.tif', [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() | |
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 | |
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