#!/usr/bin/python
############
# OpenGL_5.py
# This program creates a cube colored with a loaded texture.
############ 

import os
from OpenGL.GL import *
from OpenGL.GLU import *
import pygame
from pygame.locals import *

rquad = 0.0
# Now we use all of these 
# x,y, and z rots are the rotations on each axis
xrot = yrot = zrot = 0.0
# textures for loading the .bmp image
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():
    glEnable(GL_TEXTURE_2D)
    loadtextures()
    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)


# New function to find, load, and use the texture
def loadtextures():
    # Need to find and load the texture
    point_to_file = os.path.join('dtcfe.bmp')
    texture_surface = pygame.image.load(point_to_file)
    texture_buffer = pygame.image.tostring(texture_surface, "RGBX", 1)

    glBindTexture(GL_TEXTURE_2D, textures[0])
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, texture_surface.get_width(), texture_surface.get_height(), 0,
                  GL_RGBA, GL_UNSIGNED_BYTE, texture_buffer );
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)



# changes here to use texture instead of drawing
# drawgraphics still does most of the work
def drawgraphics():
    global xrot, yrot, zrot
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    glTranslatef(0.0, 0.0, -5.0)
    
    global rquad
    glRotatef(xrot,1.0,0.0,0.0)
    glRotatef(yrot,0.0,1.0,0.0)
    glRotatef(zrot,0.0,0.0,1.0)


    	
    glBegin(GL_QUADS) 

    # Front Face 
    glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)	
    glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)	
    glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0,  1.0)	
    glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0,  1.0)	
	
    # Back Face
    glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0)	
    glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)	
    glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)	
    glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0)	
	
    # Top Face
    glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)	
    glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,  1.0,  1.0)	
    glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,  1.0,  1.0)	
    glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)	
	
    # Bottom Face       
    glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0)	
    glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, -1.0, -1.0)	
    glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)	
    glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)	
	
    # Right face
    glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0)	
    glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0)	
    glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0,  1.0)	
    glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0)	
	
    # Left Face
    glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0)	
    glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0,  1.0)	
    glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0,  1.0)	
    glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0, -1.0)	
	
    glEnd();	
    
    # Use XYZ to rotate - speed it up a bit	
    xrot = xrot + 0.9
    yrot = yrot + 0.9
    zrot = zrot + 0.9

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()

