Original Post
I'm teaching myself python and openGL (fun). I can draw a basic scene and am having problems with working out how to move the camera.
The pan left/right bit works, but the rotation is all screwy. Does anyone have a better way of doing this? Any help would be appreciated.
Can someone help me out? Here's the code I think applies:
And my camera function
[attachment=5210:source.zip]
The pan left/right bit works, but the rotation is all screwy. Does anyone have a better way of doing this? Any help would be appreciated.
Can someone help me out? Here's the code I think applies:
# from main.py
# === MAIN PROGRAM LOOP ===
# initialize camera and viewport
gfxInit()
# for rotating the camera (default count)
keyint = 4
# for moving the camera (default values)
axX = 0
axY = 0
axZ = 0
while True:
# get the event
key = pygame.key.get_pressed()
for event in pygame.event.get():
# if the user clicked the X button, end program
if event.type == pygame.QUIT:
return
# if the user presses and holds F4 and left or right ALT keys, end program
if event.type == KEYDOWN and event.key == K_F4 and (key[K_LALT] or key[K_RALT]):
return
# if RIGHT ARROW pressed then rotate view right
if event.type == KEYDOWN and (event.key == K_COMMA or event.key == K_LESS):
keyint += 1
# if LEFT ARROW pressed then rotate view left
if event.type == KEYDOWN and (event.key == K_PERIOD or event.key == K_GREATER):
keyint -= 1
# if RIGHT ARROW pressed then move view right relative to view
if event.type == KEYDOWN and event.key == K_RIGHT:
view = abs(keyint % 4)
if view == 0:
axX += 5
axZ -= 5
if view == 1:
axX -= 5
axZ -= 5
if view == 2:
axX -= 5
axZ += 5
if view == 3:
axX += 5
axZ += 5
# if LEFT ARROW pressed then move view left relative of view
if event.type == KEYDOWN and event.key == K_LEFT:
view = abs(keyint % 4)
if view == 0:
axX -= 5
axZ += 5
if view == 1:
axX += 5
axZ += 5
if view == 2:
axX += 5
axZ -= 5
if view == 3:
axX -= 5
axZ -= 5
# if UP ARROW pressed then move view "up" relative to view
if event.type == KEYDOWN and event.key == K_UP:
view = abs(keyint % 4)
if view == 0:
axX -= 5
axZ -= 5
if view == 1:
axX -= 5
axZ += 5
if view == 2:
axX += 5
axZ += 5
if view == 3:
axX += 5
axZ -= 5
# if DOWN KEY pressed then move view "down" relative to view
if event.type == KEYDOWN and event.key == K_DOWN:
view = abs(keyint % 4)
if view == 0:
axX += 5
axZ += 5
if view == 1:
axX += 5
axZ -= 5
if view == 2:
axX -= 5
axZ -= 5
if view == 3:
axX -= 5
axZ += 5
# if HOME key pressed reset view
if event.type == KEYDOWN and event.key == K_HOME:
keyint = 0
axX = 0
axY = 0
axZ = 0
gfxCamera(keyint, axX, axY, axZ)
gfxShowCoords()
drawMap(mapWidth, mapArray)
pygame.display.flip() # update the screen with what we've drawn
# === MAIN LOOP END ===
And my camera function
# from gfx.py
def gfxCamera(keyint, axX, axY, axZ):
""" Modifies the view by X, Y, Z axes and rotates.
"""
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) #clear the color and depth buffers
glLoadIdentity() #clear current (in this case, modelview) matrix to the identity matrix
view = abs(keyint % 4)
if view == 0:
# default view: distance from look-at, coords of look-at, vertical angle
gluLookAt((axX+15),(axY+15),(axZ+15), axX,axY,axZ, 0,1,0)
if view == 1:
gluLookAt((axX+15),(axY+15),(axZ-15), axX,axY,axZ, 0,1,0)
if view == 2:
gluLookAt((axX-15),(axY+15),(axZ-15), axX,axY,axZ, 0,1,0)
if view == 3:
gluLookAt((axX-15),(axY+15),(axZ-15), axX,axY,axZ, 0,1,0)
[attachment=5210:source.zip]