在3D空间中移动图像

bbo*_*boe 2 python opengl pyglet

我正在尝试在python中创建徽标可视化,并且我想在3D空间中设置多个图像的动画,使得图像总是"面向"屏幕的中心,并且图像围绕某个固定路径移动.我之前用python使用Vizard完成了这个,但是,我想在一个"免费"和跨平台庄园中这样做.

什么是最快(读取最短的代码量)使用pyglet获取成像映射四边形的句柄,我可以操纵所述四边形的位置和方向?

bbo*_*boe 6

以下是我能想出的最简单的代码,允许我将图像定位在位置(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空间中移动图像 "的博客文章中找到.