import os,sys
#HACK: CHANGE
sys.path.append("C:\\dev\\Python Projects\\Current\\OpenGL Library\\3 OpenGL Library")
from glLibImport import *

Screen = [512,512]

Window = glLibWindow(Screen,fullscreen=False,caption="",multisample=16,position=GLLIB_CENTER,vsync=False)
View3D = glLibView3D((0,0,Screen[0],Screen[1]),90,0.1,5000.0)

Light1 = glLibLight(1)
Light1.enable()
Light1.set_ambient([1,1,1])
Light1.set_diffuse([1,1,1])
Light1.set_specular([1,1,1])
Light1.set_pos([4,4,4])
Light1.set_type(GLLIB_POINT_LIGHT)

glEnable(GL_LIGHTING)

Shader = glLibShader()
Shader.render_equation("""
//vec3 ambient_light = light_ambient(light1).rgb;
vec3 diffuse_light = light_diffuse(light1).rgb*light_att(light1);
vec3 specular_light = light_specular_ph(light1).rgb*light_att(light1);
//color.rgb += ambient_color.rgb*ambient_light;
color.rgb += diffuse_color.rgb*diffuse_light;
color.rgb += specular_color.rgb*specular_light;""")
print Shader.compile()

def quit():
    pygame.quit(); sys.exit()
def GetInput():
    global camera_rot, camera_radius
    key = pygame.key.get_pressed()
    mpress = pygame.mouse.get_pressed()
    mrel = pygame.mouse.get_rel()
    for event in pygame.event.get():
        if   event.type == QUIT: quit()
        elif event.type == KEYDOWN:
            if   event.key == K_ESCAPE: quit()
def draw_sphere(position):
    glPushMatrix()
    glTranslatef(position[0],position[1],position[2])
    quadric = gluNewQuadric()
    gluQuadricDrawStyle(quadric,GLU_FILL)
    gluQuadricNormals(quadric,GLU_SMOOTH)
    gluSphere(quadric,1.0,50,50)
    glPopMatrix()
def Draw():
    glClearColor(64.0/255.0,64.0/255.0,64.0/255.0,1.0)
    Window.clear()
    View3D.set_view()
    gluLookAt(0,5,2,
              0,0,0,
              0,0,1)
    Light1.set()
    
    glLibUseShader(Shader)
    glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,[0.05,0.05,1.0,1.0]);
    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,[0.2,0.3,1.0,1.0]);
    glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,3.0);
    #Plane
    glBegin(GL_QUADS)
    glNormal3f(0,0,1)
    glVertex3f(-10000,-10000,0)
    glVertex3f( 10000,-10000,0)
    glVertex3f( 10000, 10000,0)
    glVertex3f(-10000, 10000,0)
    glEnd()
    glLibUseShader(None)

    glLibUseShader(Shader)
    glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,[1.0,0.05,0.05,1.0])
    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,[1.0,0.9,0.9,1.0])
    glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,10.0)
    #Sphere 1
    draw_sphere([0,0,1])
    glLibUseShader(None)

    glLibUseShader(Shader)
    glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,[0.05,1.0,0.05,1.0])
    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,[0.9,1.0,0.9,1.0])
    glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,30.0)
    #Sphere 2
    draw_sphere([2,2,0.2])
    glLibUseShader(None)

    glLibUseShader(Shader)
    glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,[1.0,1.0,0.05,1.0])
    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,[1.0,1.0,0.9,1.0])
    glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,100.0)
    #Triangle
    glBegin(GL_TRIANGLES)
    v1 = [-1, -1, .02]
    v2 = [2,-2,.02]
    v3 = [-1.5,-1.5,1.5]
    glNormal3fv( vec_negate(normalize(cross(vec_subt(v2,v1),vec_subt(v3,v1)))) )
    glVertex3fv(v1)
    glVertex3fv(v2)
    glVertex3fv(v3)
    glEnd()
    glLibUseShader(None)
    
    Window.flip()
def main():
    Clock = pygame.time.Clock()
    while True:
        GetInput()
        Draw()
        Clock.tick(60)
if __name__ == '__main__': glLibTestErrors(main)
