本文介绍了如何使用 gluLookAt() 修改 OpenGL 的透视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to change the angles at which I view the models I create. I looked around and found out about the gluLookAt() function, which I think does what I wan't to do. However, I have absolutely no idea how to use gluLookAt(). I popped it into my code below, but I can't seem to get it to function properly. Anyway, my code (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()?

All I want this code to do is modify the angle at which I view some rotating pyramids and cubes. Can anyone explain how to modify this code to use gluLookAt()?

PS:是否有一个很好的页面来解释 OpenGL(矩阵、3d 数学、glLoadIdentity() 的任何功能等)?我可以写代码,但我不知道我在做什么.

PS: Is there a nice page which explains OpenGL (Matrices, 3d math, whatever glLoadIdentity() does, etc)? I can write the code, but I have no idea what I'm doing.

推荐答案

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

You are totally missing the point of gluLookAt. You have to call it once on the current matrix, then don't clean it and modify it so the other object are drawn accordingly.

当你这样做时:

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

gluLookAt 的功能与 glRotateglTranslate 的功能一样.

gluLookAt functions just as glRotate and glTranslate functions.

这篇关于如何使用 gluLookAt() 修改 OpenGL 的透视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 17:42