from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import sys
import time
#import imageLoader

'''
Sources:
http://www.opengl.org/sdk/docs/man/xhtml/glColorMaterial.xml
'''

# A global variable storing our window ID
# (not very pythonic by standard practice for OpenGL)
window = 0

ESCAPE = '\033' # The esc character
def keyPressed(*args):
    if args[0] == ESCAPE: 	# If escape is pressed, kill everything.
        glutDestroyWindow(window)
        sys.exit()

def SolarSystem():
    global angle
    angle += .5
    
    #sets up radiant light from the sun at the origin
    glColorMaterial(GL_FRONT_AND_BACK, GL_EMISSION)
    
    #the sun
    Planet(12, 1, .9, .4, PlanetType.SUN)
    #glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE)

    #Mercury
    glPushMatrix()
    glRotate(15*angle, 0, 1, 0)
    #move so orbit has r=4
    glTranslate(12+4,0,0)
    #radius 0.6, color (.52, .52, .52)
    Planet(.6, .52, .52, .52, PlanetType.MERCURY)
    glPopMatrix()

    #Venus
    glPushMatrix()
    glRotate(2.5*angle, 0, 1, 0)
    #move so orbit has r=7
    glTranslate(12+7,0,0)
    #planet has radius 1, color (.7, .7, 0)
    Planet(1, .72, .45, .12, PlanetType.VENUS)
    glPopMatrix()

    #Earth
    glPushMatrix()
    glRotate(angle, 0, 1, 0)
    #move so orbit has r=10
    glTranslate(12+10,0,0)
    Planet(1.2, .37, .4, 0.6, PlanetType.EARTH)
    glPopMatrix()

    #Mars
    glPushMatrix()
    glRotate(1.03*angle, 0, 1, 0)
    #move so orbit has r=15
    glTranslate(12+15,0,0)
    Planet(.8, .8, .55, .32, PlanetType.MARS)
    glPopMatrix()

    #Jupiter
    glPushMatrix()
    glRotate(0.4*angle, 0, 1, 0)
    #move so orbit has r=52
    glTranslate(12+52,0,0)
    Planet(10, .52, .42, .35, PlanetType.JUPITER)
    glPopMatrix()

    #Saturn
    glPushMatrix()
    glRotate(0.45*angle, 0, 1, 0)
    #move so orbit has r=95
    glTranslate(12+95,0,0)
    Planet(9, .92, .77, .51, PlanetType.SATURN)
    glPopMatrix()

    #Uranus
    glPushMatrix()
    glRotate(-0.71*angle, 0, 1, 0)
    #move so orbit has r=196
    glTranslate(12+196,0,0)
    Planet(5, .73, .88, .88, PlanetType.URANUS)
    glPopMatrix()

    #Neptune
    glPushMatrix()
    glRotate(0.71*angle, 0, 1, 0)
    #move so orbit has r=300
    glTranslate(12+300,0,0)
    Planet(4, .45, .65, 1, PlanetType.NEPTUNE)
    glPopMatrix()

class PlanetType(object):
    SUN = 0
    MERCURY = 1
    VENUS = 2
    EARTH = 3
    MARS = 4
    JUPITER = 5
    SATURN = 6
    URANUS = 7
    NEPTUNE = 8

def Planet(r, c1, c2, c3, planet_type):
    glColor3f(c1, c2, c3)
    glutSolidSphere(r, 20, 20)
    #put here to give everything a sun-colored glow
    #glColorMaterial(GL_FRONT_AND_BACK, GL_EMISSION)
    if planet_type == 1:
        #Moon that starts above the planet
        Moon(0,0,1)
        #(technically, Mars actually has 2 moons...)
    if planet_type == 2:    
        glPushMatrix()
        #moon that starts above the planet
        Moon(0,0,1)
        glPopMatrix()
        #moon with a satellite that starts right of the planet
        Moon(1,0,0, True)

def Moon(x, y, z, satellite = False):
    glRotate(angle*8, x,y,z)
    #Translate (in a different direction than the axis of rotation)
    ## to move out of the planet's center
    glTranslate(y*2, z*2, x*2) 
    glColor3f(.5, .5, .5)
    glutSolidSphere(.2, 10, 10)
    if satellite:
        Satellite()

def Satellite():
    glRotate(angle*12, 1,0,0)
    #Translate out of the moon's center
    glTranslate(0, .5, 0)
    glColor3f(0,0,0)
    glutSolidSphere(.1, 5, 5)

def mydisplay() :
    global angle
    global framecount
    global start
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    SolarSystem()
    glutSwapBuffers()

def drawSolarSystem():
    global window
    global angle
    global framecount
    global start
    start = time.time()
    framecount = 0
    angle = 0
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)

    glutInitWindowSize(400,400)
    window = glutCreateWindow("Large view")
    glutKeyboardFunc(keyPressed)
    glClearColor(0,0,0,1)
    glShadeModel(GL_SMOOTH)

    glutDisplayFunc(mydisplay)
    glEnable(GL_DEPTH_TEST)
    glDepthFunc(GL_LEQUAL)
    glutIdleFunc(mydisplay)

    
    #Lighting
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)
    glEnable(GL_COLOR_MATERIAL)
    glLightfv(GL_LIGHT0, GL_DIFFUSE, (.25,.25,.25, 1))
    glLightfv(GL_LIGHT0, GL_POSITION, (0,0,0,1))
    #glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, (-2,-2,-4))
    

    '''
    #Texture
    global texture
    texture= glGenTextures(1); # number of textures to generate                                                   glBindTexture(GL_TEXTURE_2D, texture
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)

    img = imageLoader.loadImage("gravel_texture_gif.gif")
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.width, img.height, 0,      
         GL_RGB, GL_UNSIGNED_BYTE, img.data)
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
    glEnable(GL_TEXTURE_2D)
    '''

    #glEnable(GL_CULL_FACE)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45, 1, 0.1, 400)
    gluLookAt(0, 40, 80, 0, 0, 0, 0, 1, 0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    
    glutMainLoop();


drawSolarSystem()











