Would like help with GluLookAt and python code.

Started by
6 comments, last by greentiger 12 years, 7 months ago
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

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]
Advertisement
Does anyone know of any good python/Opengl tutorials?
Hi there - I recently ran into a similar problem myself, and found this tutorial very helpful:

http://beta.wikiversity.org/wiki/Computer_graphics_--_2008-2009_--_info.uvt.ro/Laboratory_7

The tutorial is in Java (using the JOGL openGL wrapper library), but the concepts will still apply.

Hope it helps!
what exactly are you attempting to achieve with the gluLookAt?

here is a test 3rdPersonCamera that uses gluLookAt

#!/usr/bin/env python

import sys
import math
try:
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *

except:
print '''ERROR: PyOpenGL not installed properly.'''
sys.exit()

class thirdPersonApp:
def __init__(self):
#player coordinates
self.playerX = 27.0
self.playerY = 0.5
self.playerZ = 27.0

#camera coordinates
self.camX = 44.0
self.camY = 25.0
self.camZ = 44.0



self.playerRot = 225.0
self.cameraRot = 20.0

self.camDist = 50.0
#self.camHeight = 25.0

self.moveVel = 1.0
self.rotVel = 6.0

self.setupGL()

def setupGL(self):
glutInit()
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowSize (400, 400);
glutInitWindowPosition(100, 100)
glutCreateWindow("Third Person Test Application")

self.glInit()

glutIdleFunc(self.onIdle)
glutDisplayFunc(self.onDraw)
glutReshapeFunc(self.onResize)
glutKeyboardFunc(self.onKeyPress)
glutMainLoop()

def onIdle(self):
self.onDraw()

def onDraw(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)# Clear The Screen And The Depth Buffer
glLoadIdentity()# Reset The View

self.camX = self.playerX+self.camDist*math.sin(self.cameraRot*math.pi/180.0)
#self.CamY = self.playerY-self.camDist
self.CamZ = self.playerZ+self.camDist*math.cos(self.cameraRot*math.pi/180.0)

gluLookAt(self.camX,self.camY,self.camZ,self.playerX,self.playerY,self.playerZ,0,1,0)

glColor(0.5,0.5,0.5)
glBegin(GL_LINES)

i = 0
while i < 10:
glVertex(-50,0,-50+i*10); glVertex(50,0,-50+i*10)
glVertex(-50+i*10,0,-50); glVertex(-50+i*10,0,50)
i = i+1
glEnd()

#z-fight
self.playerY = 0.5

glTranslatef(self.playerX,self.playerY,self.playerZ)
glRotatef(self.playerRot,0,1,0)

glBegin(GL_TRIANGLES)
glColor(1,1,1); glVertex( 0,0, 7)
glColor(1,0,0); glVertex(-5,0,-5)
glColor(1,0,0); glVertex(+5,0,-5)
glEnd()

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

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # Realy Nice perspective calculations

def onResize(self,w,h):
glViewport(0,0,w,h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, float(w)/float(h), 0.5, 1024.0)
glMatrixMode(GL_MODELVIEW)

def onKeyPress(self,*args):
if args[0] == chr(27):#ESC Key
self.eXit()
elif args[0] == "w":
self.playerX = self.playerX+self.moveVel*math.sin(self.playerRot*math.pi/180.0)
self.playerZ = self.playerZ+self.moveVel*math.cos(self.playerRot*math.pi/180.0)
elif args[0] == "s":
self.playerX = self.playerX-self.moveVel*math.sin(self.playerRot*math.pi/180.0)
self.playerZ = self.playerZ-self.moveVel*math.cos(self.playerRot*math.pi/180.0)
elif args[0] == "a":
self.playerRot = self.playerRot+self.rotVel
elif args[0] == "d":
self.playerRot = self.playerRot-self.rotVel
elif args[0] == "q":
self.camY = self.camY - 1.0
elif args[0] == "e":
self.camY = self.camY + 1.0

if self.camY <= 10.0:
self.camY = 10.0
if self.camY >= 180.0:
self.camY = 180.0
glutPostRedisplay()

def eXit(self):
#destroy glForm
sys.exit()

if __name__ == "__main__":
thirdPersonApp()

it's a bit messy because it is so old and based off of a delphi application, but it should help you to better understand gluLookAt :cool:
[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]
[size="1"]

[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

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)


Quite right, I apologise. rolleyes.gif


[size="1"]
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)



gfxClear()
gfxShowCoords()
gfxDrawMap(mapWidth, mapArray)



pygame.display.flip() # update the screen with what we've drawn
# === MAIN LOOP END ===

This topic is closed to new replies.

Advertisement