Reputation: 4402
I'm trying to create a logo visualization in python and I would like to animate a number of images in 3D space such that the images are always "facing" the center of the screen and the images move about some fixed path. I've done this before with python using Vizard, however, I'd like to do this in a "free", and cross platform manor.
What's the quickest (read shortest amount of code) using pyglet to get a handle to an imaged mapped quad that I can manipulate the position and orientation of said quad?
Upvotes: 2
Views: 1791
Reputation: 4402
The following is the simplest code I could come up with that allowed me to position the image at position (0, 0, -10):
#!/usr/bin/env python
import pyglet
from pyglet.gl import *
window = pyglet.window.Window()
glEnable(GL_DEPTH_TEST)
image = pyglet.image.load('imgs/appfolio.png')
texture = image.get_texture()
glEnable(texture.target)
glBindTexture(texture.target, texture.id)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height,
0, GL_RGBA, GL_UNSIGNED_BYTE,
image.get_image_data().get_data('RGBA', image.width * 4))
rect_w = float(image.width) / image.height
rect_h = 1
@window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(0, 0, -10)
glBindTexture(texture.target, texture.id)
glBegin(GL_QUADS)
glTexCoord2f(0.0, 0.0); glVertex3f(-rect_w, -rect_h, 0.0)
glTexCoord2f(1.0, 0.0); glVertex3f( rect_w, -rect_h, 0.0)
glTexCoord2f(1.0, 1.0); glVertex3f( rect_w, rect_h, 0.0)
glTexCoord2f(0.0, 1.0); glVertex3f(-rect_w, rect_h, 0.0)
glEnd()
def on_resize(width, height):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(65.0, width/float(height), 0.1, 1000.0)
glMatrixMode(GL_MODELVIEW)
window.on_resize = on_resize # we need to replace so can't use @window.event
pyglet.app.run()
The most difficult part I found was that the on_resize function had to be replaced in order to work as I expected it to, as the default orthographic projection doesn't work.
I found Jess Hill's pyglet conversion of the NeHe tutorial on texture mapping to be most helpful.
The complete logo visualization code can be found at a blog entry I just wrote titled, "Moving Images in 3D Space with Pyglet".
Upvotes: 6