如何使用gluLookAt()修改OpenGL的透视图?

时间:2013-02-21 16:08:33

标签: python opengl

我想改变我查看我创建的模型的角度。我环顾四周,发现了gluLookAt()函数,我认为这是我不想做的事情。但是,我完全不知道如何使用gluLookAt()。我将它弹出到我下面的代码中,但我似乎无法让它正常运行。无论如何,我的代码(python):

#!/usr/bin/env python

from OpenGL.GL import *
from OpenGL.GLU import *

import pygame

def pyramid():

    #draw a pyramid here

def cube():

    #draw a cube here

class mainClass():
    def mainMethod(self):   
        from sys import exit

        print ("Initalizing...")

        resolution = (400, 300)

        pygame.init()
        screen = pygame.display.set_mode(resolution, pygame.OPENGL|pygame.DOUBLEBUF)

        self.xPer, self.yPer, self.zPer = 0, 0, 0
        print ("Doing GL stuff...")
        glViewport(self.xPer, self.yPer, resolution[0], resolution[1])

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45.0, float(resolution[0]) / resolution[1], 0.1, 1000.0)#Distance, 

        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        print ("More GL stuff...")
        glEnable(GL_DEPTH_TEST)

        #glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

        glShadeModel(GL_SMOOTH)
        glClearColor(0.0, 0.0, 0.0, 0.0)

        print ("Minor details...")
        clock = pygame.time.Clock()

        rot_tri = 0
        rot_quad = 0

        print ("Mainloop...")
        while True:

            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    pygame.quit()
                    exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        pygame.quit()
                        exit()
                    elif event.key == pygame.K_a:
                        self.xPer += 50

                    elif event.key == pygame.K_d:
                        self.xPer -= 50
                        #glViewport(self.xPer, self.yPer, resolution[0], resolution[1])
                    elif event.key == pygame.K_w:
                        self.yPer -= 50
                        #glViewport(self.xPer, self.yPer, resolution[0], resolution[1])
                    elif event.key == pygame.K_s:
                        self.yPer += 50
                        #glViewport(self.xPer, self.yPer, resolution[0], resolution[1])
                    elif event.key == pygame.K_f:
                        self.zPer += 50
                    elif event.key == pygame.K_v:
                        self.zPer -= 50


            glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity()
            gluLookAt(10, 10, 10, self.xPer, self.yPer, self.zPer, 0, 1, -1);

            glLoadIdentity()
            glTranslatef(-1.5, 0.0, -6.0)
            glRotatef(rot_tri, 0.0, 1.0, 0.0)
            pyramid()

            glLoadIdentity()
            glTranslate(1.5, 0.0, -6.0)
            glRotatef(rot_quad, 1.0, 0.0, 0.0)
            cube()

            rot_tri += 2.0
            rot_quad -= 1.5

            pygame.display.set_caption("hello_opengl.py FPS: %i" % clock.get_fps())

            pygame.display.flip()
            clock.tick()

t = mainClass()
t.mainMethod()

我想要这个代码所做的就是修改我查看一些旋转金字塔和立方体的角度。任何人都可以解释如何修改此代码以使用gluLookAt()?

PS:有没有一个很好的页面解释OpenGL(矩阵,3d数学,无论glLoadIdentity()做什么等)?我可以写代码,但我不知道我在做什么。

1 个答案:

答案 0 :(得分:0)

你完全忽略了gluLookAt的观点。你必须在当前矩阵上调用一次,然后清理它并修改它,以便相应地绘制另一个对象。

当你这样做时:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(10, 10, 10, ...); // <-- this line has no effect
glLoadIdentity();

gluLookAt功能就像glRotateglTranslate一样。