1

我想更改查看我创建的模型的角度。我环顾四周,发现了 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() 所做的任何事情等)?我可以编写代码,但我不知道我在做什么。

4

1 回答 1

0

你完全错过了gluLookAt. 您必须在当前矩阵上调用一次,然后不要清理它并修改它,以便相应地绘制另一个对象。

当你这样做时:

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

gluLookAt函数glRotateglTranslate函数一样。

于 2013-02-21T23:41:19.397 回答