2011-10-07 54 views
2

我正在嘗試在python中創建徽標可視化,並且我想在3D空間中設置多個圖像的動畫,以便圖像總是「面對」屏幕的中心,並且圖像移動一些固定的路徑。我之前用python使用Vizard完成了這個,但是,我想在「免費」和跨平臺的莊園中做到這一點。在3D空間中移動圖像

使用pyglet獲取映像映射四邊形的句柄,我可以操縱四邊形的位置和方向,最快(讀取最短的代碼量)是什麼?

回答

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() 

我發現最困難的部分是,on_resize函數必須替換才能按照我的預期工作,因爲默認的正交投影不起作用。

我發現的NeHe tutorial on texture mapping是最有幫助的。

完整的徽標可視化代碼可以在我剛寫的標題爲「Moving Images in 3D Space with Pyglet」的博客條目中找到。