#!/usr/bin/python
############
# OpenGL_1.py
# This program creates a square
############ 

import os
from OpenGL.GL import *
from OpenGL.GLU import *
import pygame
from pygame.locals import *


# These variables - rquad, xrot, and textures, aren't used till the later examples
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
def drawgraphics():
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    glTranslatef(0.0, 0.0, -5.0)
    # GL_QUADS for a square object
    glBegin(GL_QUADS)
    # Set the 4 square points
    glVertex3f(-1.0, 1.0, 0)
    glVertex3f(1.0, 1.0, 0)
    glVertex3f(1.0, -1.0, 0)
    glVertex3f(-1.0, -1.0, 0)
    glEnd()

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 functions
    windowsize((640,480))
    initialize()
    #set frames to 0 before loop starts
    frames = 0
    # Have pygame keep track of time
    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
        # Draw the square
        drawgraphics()
        pygame.display.flip()
        frames = frames+1
    
if __name__ == '__main__': main()

