#!/usr/bin/python
############
# OpenGL_4.py
# This program creates a cube.
############ 

import os
from OpenGL.GL import *
from OpenGL.GLU import *
import pygame
from pygame.locals import *

rquad = 0.0
xrot = yrot = zrot = 0.0
textures = [0,0]

# Function that sets up a window given width+height
def windowsize((width, height)):
    glViewport(0, 0, width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45, 1.0*width/height, 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

# Function for initializing OpenGL
def initialize():
    glShadeModel(GL_SMOOTH)
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClearDepth(1.0)
    glEnable(GL_DEPTH_TEST)
    glDepthFunc(GL_LEQUAL)
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

# the drawgraphics function does all the work when called
# Have to change GL_QUADS 4 points to 24 to make the cube 
def drawgraphics():
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    glTranslatef(0.0, 0.0, -5.0)
    
    global rquad
    glRotatef(rquad, 1.0, 0.0, 0.0)

    glColor3f(0.1, 0.9, 0.5)	
    glBegin(GL_QUADS) 
    glColor3f(0.1, 0.9, 0.5)
    
    # Front Face 
    glVertex3f( 1.0, 1.0,-1.0)
    glVertex3f(-1.0, 1.0,-1.0)		
    glVertex3f(-1.0, 1.0, 1.0)		
    glVertex3f( 1.0, 1.0, 1.0)		

    # Back Face	
    glVertex3f( 1.0,-1.0, 1.0)
    glVertex3f(-1.0,-1.0, 1.0)		
    glVertex3f(-1.0,-1.0,-1.0)		
    glVertex3f( 1.0,-1.0,-1.0)		

    # Top Face		
    glVertex3f( 1.0, 1.0, 1.0)
    glVertex3f(-1.0, 1.0, 1.0)		
    glVertex3f(-1.0,-1.0, 1.0)		
    glVertex3f( 1.0,-1.0, 1.0)		

    # Bottom Face  	
    glVertex3f( 1.0,-1.0,-1.0)
    glVertex3f(-1.0,-1.0,-1.0)
    glVertex3f(-1.0, 1.0,-1.0)		
    glVertex3f( 1.0, 1.0,-1.0)		

    # Right face	
    glVertex3f(-1.0, 1.0, 1.0)
    glVertex3f(-1.0, 1.0,-1.0)		
    glVertex3f(-1.0,-1.0,-1.0)		
    glVertex3f(-1.0,-1.0, 1.0)		

    # Left Face	
    glVertex3f( 1.0, 1.0,-1.0)
    glVertex3f( 1.0, 1.0, 1.0)
    glVertex3f( 1.0,-1.0, 1.0)		
    glVertex3f( 1.0,-1.0,-1.0)		
    glEnd()	

    
    rquad+= 0.1

def main():

    # Initialize OpenGL and Pygame
    video_flags = OPENGL|DOUBLEBUF
    pygame.init()
    pygame.display.set_mode((640,480), video_flags)

    # Call our window and initialize function
    windowsize((640,480))
    initialize()

    frames = 0
    ticks = pygame.time.get_ticks()
    while 1:
        event = pygame.event.poll()
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            break
        # And draw
        drawgraphics()
        pygame.display.flip()
        frames = frames+1

    


if __name__ == '__main__': main()

