Last active
December 24, 2015 14:19
-
-
Save davepape/6811847 to your computer and use it in GitHub Desktop.
Create a texture-mapped disc
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 | |
from math import * | |
from pyglet.gl import * | |
window = pyglet.window.Window() | |
keyboard = pyglet.window.key.KeyStateHandler() | |
window.push_handlers(keyboard) | |
def makeCircle(numPoints): | |
verts = [0,0] | |
texcoords = [0.5,0.5] | |
for i in range(numPoints+1): | |
angle = radians(float(i)/numPoints * 360.0) | |
x = 100*cos(angle) | |
y = 100*sin(angle) | |
verts += [x,y] | |
s = (cos(angle) + 1) / 2.0 | |
t = (sin(angle) + 1) / 2.0 | |
texcoords += [s,t] | |
return pyglet.graphics.vertex_list(numPoints+2, ('v2f', verts), ('t2f', texcoords)) | |
class TexturedCircle: | |
def __init__(self, width, height, xpos, ypos, texturefile): | |
self.xpos = xpos | |
self.ypos = ypos | |
self.angle = 0 | |
self.size = 1 | |
self.texture = pyglet.image.load(texturefile).get_texture() | |
x = width/2.0 | |
y = height/2.0 | |
self.vlist = makeCircle(16) | |
def draw(self): | |
glPushMatrix() | |
glTranslatef(self.xpos, self.ypos, 0) | |
glRotatef(self.angle, 0, 0, 1) | |
glScalef(self.size, self.size, self.size) | |
glColor3f(1,1,1) | |
glEnable(GL_TEXTURE_2D) | |
glBindTexture(GL_TEXTURE_2D, self.texture.id) | |
self.vlist.draw(GL_TRIANGLE_FAN) | |
glDisable(GL_TEXTURE_2D) | |
glPopMatrix() | |
@window.event | |
def on_draw(): | |
glClearColor(0, 0.3, 0.5, 0) | |
glClear(GL_COLOR_BUFFER_BIT) | |
square1.draw() | |
def update(dummy): | |
global square1 | |
if keyboard[pyglet.window.key.A]: | |
square1.xpos -= 5 | |
if keyboard[pyglet.window.key.D]: | |
square1.xpos += 5 | |
if keyboard[pyglet.window.key.W]: | |
square1.ypos += 5 | |
if keyboard[pyglet.window.key.S]: | |
square1.ypos -= 5 | |
if keyboard[pyglet.window.key.UP]: | |
square1.size *= 1.1 | |
if keyboard[pyglet.window.key.DOWN]: | |
square1.size /= 1.1 | |
if keyboard[pyglet.window.key.LEFT]: | |
square1.angle += 5 | |
if keyboard[pyglet.window.key.RIGHT]: | |
square1.angle -= 5 | |
square1 = TexturedCircle(120, 120, 300, 200, "texture.jpg") | |
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