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,0,0),
	(0,1,0),
	(0,0,1),
	(0,0,0),
	(1,1,1),
	(0,1,1),
	(1,0,0),
	(0,1,0),
	(0,0,1),
	(0,0,0),
	(1,1,1),
	(0,1,1),
]

def Draw():
	glBegin(GL_QUADS)


	for surface in surfaces:
                x = 0
                for vertex in surface:
                    x = 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()
	display = (800,600)
	pygame.display.set_mode(display,DOUBLEBUF|OPENGL)
	
	gluPerspective(45, (display[0]/display[1]),0.1,50.0)
	glTranslatef(1,1,-40)
	object_passed = False
	#glRotatef(0,0,0,0)

	while not object_passed:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				pygame.quit()
				quit()
			if event.type == KEYDOWN:
				if event.key == pygame.K_LEFT:
					glTranslatef(-0.5,0,0)
				if event.key == pygame.K_RIGHT:
					glTranslatef(0.5,0,0)
				if event.key == pygame.K_UP:
					glTranslatef(0,0.5,0)
				if event.key == pygame.K_DOWN:
					glTranslatef(0,-0.5,0)
				if event.key == pygame.K_1:
					glTranslatef(0,0,1)
				if event.key == pygame.K_2:
					glTranslatef(0,0,-1)

		
		#glRotatef(1,3,1,1)
		x = glGetDoublev(GL_MODELVIEW_MATRIX)
		print(str(x))
		camera_x = x[3][0]
		camera_y = x[3][1]
		camera_z = x[3][2]
		if camera_z < -1:
			object_passed = True

		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
		glTranslatef(0,0,0.5)
		Draw()
		pygame.display.flip()
		pygame.time.wait(10)

for x in range(10):
	main()
pygame.quit()
quit()







