我正在尝试学习 PyOpenGL,但我不确定如何使用 gluLookAt 来移动相机。我有一个图像显示,但我想我可能会丢失一些允许我使用 gluLookAt 的东西?传入的文字墙,我不确定问题可能出在哪里。我已经删除了着色器和纹理代码,因为我认为它不相关,但如果是,我可以发布它。
import sys
import ctypes
import numpy
from OpenGL import GL, GLU
from OpenGL.GL import shaders
from OpenGL.arrays import vbo
import pygame
from numpy import array
class OpenGLSprite():
def __init__(self,_sprite='',_vertexShader = None, _fragmentShader = None):
if not isinstance(_sprite, pygame.Surface):
self.sprite = pygame.image.load(_sprite)
else: self.sprite = _sprite
vertexData = numpy.array([
# X Y Z U, V
-1.0, -1.0, 0, 0.0, 0.0,
-1.0, 1.0, 0, 0.0, 1.0,
1.0, 1.0, 0, 1.0, 1.0,
1.0, 1.0, 0, 1.0, 1.0,
1.0, -1.0, 0, 1.0, 0.0,
-1.0, -1.0, 0, 0.0, 0.0,
], dtype=numpy.float32)
self.loadTexture()
self.buildShaders(_vertexShader, _fragmentShader)
self.VAO = GL.glGenVertexArrays(1)
GL.glBindVertexArray(self.VAO)
self.VBO = GL.glGenBuffers(1)
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, self.VBO)
GL.glBufferData(GL.GL_ARRAY_BUFFER, vertexData.nbytes, vertexData,
GL.GL_STATIC_DRAW)
positionAttrib = GL.glGetAttribLocation(self.shaderProgram, 'position')
coordsAttrib = GL.glGetAttribLocation(self.shaderProgram, 'texCoords')
GL.glEnableVertexAttribArray(0)
GL.glEnableVertexAttribArray(1)
GL.glVertexAttribPointer(positionAttrib, 3, GL.GL_FLOAT, GL.GL_FALSE, 20,
None)
# the last parameter is a pointer
GL.glVertexAttribPointer(coordsAttrib, 2, GL.GL_FLOAT, GL.GL_TRUE, 20,
ctypes.c_void_p(12))
# load texture and assign texture unit for shaders
self.texUnitUniform = GL.glGetUniformLocation(self.shaderProgram, 'texUnit')
# Finished
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0)
GL.glBindVertexArray(0)
def render(self):
GL.glClearColor(0, 0, 0, 1)
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
# active shader program
GL.glUseProgram(self.shaderProgram)
try:
# Activate texture
GL.glActiveTexture(GL.GL_TEXTURE0)
GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture)
GL.glUniform1i(self.texUnitUniform, 0)
# Activate array
GL.glBindVertexArray(self.VAO)
# draw triangle
GL.glDrawArrays(GL.GL_TRIANGLES, 0, 6)
finally:
GL.glBindVertexArray(0)
GL.glUseProgram(0)
def main():
pygame.init()
screen = pygame.display.set_mode((640,480),pygame.OPENGL|pygame.DOUBLEBUF)
GL.glClearColor(0.5, 0.5, 0.5, 1.0)
camX = 0.0
camZ = 3.0
sprite = OpenGLSprite('logo-bluebg-square.png')
status = 0
while status == 0:
for event in pygame.event.get():
if event.type == pygame.QUIT:
status = 1
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
status = 1
if event.key == pygame.K_LEFT:
camX -= 0.1
if event.key == pygame.K_LEFT:
camX += 0.1
GLU.gluLookAt(camX,0.0,camZ,
0.0,0.0,0.0,
0.0,1.0,0.0)
sprite.render()
pygame.display.flip()
return 0
if __name__ == '__main__':
main()
无论我修改什么参数,主游戏循环中的 gluLookAt 似乎都没有做任何事情。用按键更改 camX 不会影响任何东西,它只是一直直视表面。据我了解,gluLookAt 的前三个输入应该是“相机”的位置,对吗?因此,如果它正在寻找原点并且相机的 X 位置在移动,它应该在旋转和移动表面,对吗?在设置对象以允许它与 gluLookAt 一起工作时,我需要做些什么特别的事情吗?在调用它以将转换应用于某些东西之后我是否必须做一些事情?它需要在对象的绘制代码中,还是在游戏循环中?