以下是我能想出的最简单的代码,允许我将图像定位在位置(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()
Run Code Online (Sandbox Code Playgroud)
我发现最困难的部分是必须更换on_resize函数才能按照我的预期工作,因为默认的正交投影不起作用.
我发现Jess Hill 对纹理映射的NeHe教程的pyglet转换最有帮助.
完整的徽标可视化代码可以在我刚刚写的标题为" 使用Pyglet在3D空间中移动图像 "的博客文章中找到.