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:
# 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
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)
glColor(1,1,1)
glBegin(GL_LINES)
glVertex(0,1,0); glVertex(5,1, 0) # X
glVertex(0,1,0); glVertex(0,5, 0) # Y
glVertex(0,1,0); glVertex(0,1, 5) # Z
glEnd()
glutSwapBuffers()
def glInit(self):
glClearColor(0.0, 0.0, 0.0, 0.0) # Black Background
glShadeModel(GL_SMOOTH) # Enables Smooth Color Shading
glClearDepth(1.0) # Depth Buffer Setup
glEnable(GL_DEPTH_TEST) # Enable Depth Buffer
glDepthFunc(GL_LESS) # The Type Of Depth Test To Do
[color="#1C2837"]
[font="Arial"][color="#1C2837"][color="#000000"][color="#1C2837"][color="#000000"]Did you guys even read his code? (I know you didn't!)[color="#1C2837"]
[color="#1C2837"][color="#000000"]greentiger: You're doing the same thing no matter what the value of 'view'. You probably wanted to do different things for different values of view... I guess moving the eye point.[/font]
[color="#1c2837"]
[font="Arial"][color="#1c2837"][color="#000000"][color="#1c2837"][color="#000000"]Did you guys even read his code? (I know you didn't!)
[color="#1c2837"][color="#000000"]greentiger: You're doing the same thing no matter what the value of 'view'. You probably wanted to do different things for different values of view... I guess moving the eye point.[/font]
To be fair I did read the code which is why I posted example code that I know works and is a rather nice example of gluLookAt to create a target camera. I quickly read his code i will admit but it absolutely does modify the values of axX and axZ which is what is passed to the camera... (sigh)
Edit: Also I have noticed that you may need to fix the indentation because gamedev ever so cleverly b@||@#%d it with their code formatter
Before I read this thread I actually hammered something out last night.
Once I get time later on today I will probably try these other approaches.
EDIT: I also haven't figured out how to put the input/key.event stuff in a separate file; i tried earlier but it complained about globals and the % operator not being usable for nonetype and int.
qint = 0
while True:
# for determining which quad the"view" is in, starts in "1"
quad = abs(qint % 4)
# 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
# move UP
if event.type == KEYDOWN and event.key == K_UP:
if quad == 0:
glTranslatef(-.5,0,-.5)
if quad == 1:
glTranslatef(-.5,0,.5)
if quad == 2:
glTranslatef(.5,0,.5)
if quad == 3:
glTranslatef(.5,0,-.5)
# move DOWN
if event.type == KEYDOWN and event.key == K_DOWN:
if quad == 0:
glTranslatef(.5,0,.5)
if quad == 1:
glTranslatef(.5,0,-.5)
if quad == 2:
glTranslatef(-.5,0,-.5)
if quad == 3:
glTranslatef(-.5,0,.5)
# move LEFT
if event.type == KEYDOWN and event.key == K_LEFT:
if quad == 0:
glTranslatef(-.5,0,.5)
if quad == 1:
glTranslatef(.5,0,.5)
if quad == 2:
glTranslatef(.5,0,-.5)
if quad == 3:
glTranslatef(-.5,0,-.5)
# move RIGHT
if event.type == KEYDOWN and event.key == K_RIGHT:
if quad == 0:
glTranslatef(.5,0,-.5)
if quad == 1:
glTranslatef(-.5,0,-.5)
if quad == 2:
glTranslatef(-.5,0,.5)
if quad == 3:
glTranslatef(.5,0,.5)
# move COUNTER CLOCKWISE
if event.type == KEYDOWN and (event.key == K_COMMA or event.key == K_LESS):
qint += 1
glRotatef(-90,0,1,0)
# move CLOCKWISE
if event.type == KEYDOWN and (event.key == K_PERIOD or event.key == K_GREATER):
qint -= 1
glRotatef(90,0,1,0)