I'm using python3
I have just started learning openGL and I need some help turning the camera of your view with a mouse (Like a FPS game). Right now my program looks like i'm moving but i'm just moving the object so if you could tell me how to move the camera(forward, backwards, etc) too that would be nice. Thanks in advance
My Code:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
vertices = (
(1, -1, -1),
(1, 1, -1),
(-1, 1, -1),
(-1, -1, -1),
(1, -1, 1),
(1, 1, 1, ),
(-1, -1, 1),
(-1, 1, 1),
)
edges = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7),
)
surfaces = (
(0,1,2,3),
(3,2,7,6),
(6,7,5,4),
(4,5,1,0),
(1,5,7,2),
(4,0,3,6),
)
colors = (
(1,1,1),
(0,0,0),
(0,1,1),
(0,0,0),
(0,1,1),
(1,0,1),
(0,0,0),
(1,1,1),
(0,0,0),
(0,1,1),
)
def Cube():
glBegin(GL_QUADS)
for surface in surfaces:
x = 0
for vertex in surface:
x += 1
glColor3fv(colors[x])
glVertex3fv(vertices[vertex])
glEnd()
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(vertices[vertex])
glEnd()
def main():
pygame.init()
x = 0
y = 0
z = 0
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL|RESIZABLE)
pygame.event.set_grab(True)
pygame.mouse.set_visible( False )
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0, 0, -5)
glRotatef(0, 0, 0, 0)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
if event.key == pygame.K_a:
x = 0.1
elif event.key == pygame.K_d:
x = -0.1
elif event.key == pygame.K_w:
z = 0.1
elif event.key == pygame.K_s:
z = -0.1
elif event.type == pygame.KEYUP:
if event.key == pygame.K_a and x > 0:
x = 0
elif event.key == pygame.K_d and x < 0:
x = 0
if event.key == pygame.K_w and z > 0:
z = 0
if event.key == pygame.K_s and z < 0:
z = 0
glTranslatef(x,y,z)
#glRotatef(1, 2, 3, 4)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Cube()
pygame.display.flip()
pygame.time.wait(10)
main()
See Question&Answers more detail:
os