从glTranslatef切换到gluLookAt后,opengl不会呈现所需的输出

时间:2012-03-27 13:11:00

标签: python opengl pyglet

我无法让gluLookAt工作。我有以下代码,使用glTranslatef

按预期工作
import pyglet 
from pyglet.gl import *

window = pyglet.window.Window() 

def draw_square():
    glBegin(GL_QUADS)
    glVertex3f(0, 0, 0.0)
    glVertex3f(100, 0, 0.0)
    glVertex3f(100, 100, 0.0)
    glVertex3f(0, 100, 0.0)
    glEnd()

def on_draw(): 
    window.clear() 
    draw_square()

from pyglet.window import key
@window.event
def on_key_press(symbol, modifiers):
    SHIFT_X_BY = 5.5
    if symbol == key.LEFT:
        glTranslatef(-SHIFT_X_BY, 0.0, 0.0)
    elif symbol == key.RIGHT:
        glTranslatef(SHIFT_X_BY, 0.0, 0.0)
    elif symbol == key.UP:
        glTranslatef(0.0, SHIFT_X_BY, 0.0)
    elif symbol == key.DOWN:
        glTranslatef(0.0, -SHIFT_X_BY, 0.0)

window.on_draw = on_draw
pyglet.app.run()

现在,我没有使用glTranslatef,而是认为我可以移动相机。我修改了代码以改为调用gluLookAt

import pyglet 
from pyglet.gl import *

window = pyglet.window.Window(resizable=True)

def draw_square():
    glBegin(GL_QUADS)
    glVertex3f(0, 0, 0.0)
    glVertex3f(100, 0, 0.0)
    glVertex3f(100, 100, 0.0)
    glVertex3f(0, 100, 0.0)
    glEnd()

def on_draw(): 
    window.clear() 
    draw_square()


pos = {'x': 0, 'y': 0, 'z': 0}

from pyglet.window import key
@window.event
def on_key_press(symbol, modifiers):
    global pos
    SHIFT_X_BY = 5.5
    if symbol == key.LEFT:
        pos['x'] = pos['x'] + SHIFT_X_BY
    elif symbol == key.RIGHT:
        pos['x'] = pos['x'] - SHIFT_X_BY
    elif symbol == key.UP:
        pos['y'] = pos['y'] + SHIFT_X_BY
    elif symbol == key.DOWN:
        pos['y'] = pos['y'] - SHIFT_X_BY

    gluLookAt(pos['x'], pos['y'], pos['z'], 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);

window.on_draw = on_draw
pyglet.app.run()

但是,如果我现在按下其中一个箭头键,方块就会消失。打电话给gluLookAt我做错了什么?

编辑以下on_draw()功能也不起作用(我只是黑屏):

def on_draw(): 
    glLoadIdentity()
    gluLookAt(pos['x'], pos['y'], pos['z'], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
    draw_square()

2 个答案:

答案 0 :(得分:2)

gluLookAt有三种类型的向量:“眼睛”,“中心”和“向上”。

眼睛是你看的地方,你看的中心,向上的矢量用于告诉凸轮它是如何“打开”的。通常你设置为0,1,0:

gluLookAt(pos ['x'],pos ['y'],pos ['z'],0.0,0.0,0.0, 0.0,1.0,0.0 );

http://pyopengl.sourceforge.net/documentation/manual/gluLookAt.3G.html

答案 1 :(得分:0)

您是否有任何初始化代码可以设置投影矩阵? modelview是你的活动矩阵吗?大卫有一个正确的想法,首先用glLoadIdentity清除矩阵(gluLookAt将自己与堆栈上的当前矩阵相乘),但是如果你没有切换到模型视图矩阵那么擦除投影矩阵会引起问题。