from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import sys
import math
# Some api in the chain is translating the keystrokes to this octal string
# so instead of saying: ESCAPE = 27, we use the following.
ESCAPE = '\033'

XMIN = -10.0
XMAX = 10.0
YMIN = -10.0
YMAX = 10.0
M_PI = 3.14159265358979323846
kut = 0

# A general OpenGL initialization function.  Sets all of the initial parameters. 
def InitGL(Width, Height):				# We call this right after our OpenGL window is created.
    glClearColor(0.0, 0.0, 0.0, 0.0)	# This Will Clear The Background Color To Black
    glClearDepth(1.0)					# Enables Clearing Of The Depth Buffer
    glDepthFunc(GL_LESS)				# The Type Of Depth Test To Do
    glEnable(GL_DEPTH_TEST)				# Enables Depth Testing
    glShadeModel(GL_SMOOTH)				# Enables Smooth Color Shading
	
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()					# Reset The Projection Matrix
										# Calculate The Aspect Ratio Of The Window
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)

    glMatrixMode(GL_MODELVIEW)

# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
def skaliraj(Width, Height):
    xrange = XMAX - XMIN
    yrange = Height * xrange / Width

    glViewport(0, 0, Width, Height)		
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(XMIN, XMAX, -yrange / 2.0, yrange / 2.0, XMIN, XMAX)

def cone(a, b):
    t1 = 0
    t2 = 0
    v = a*3
    n1 = 0.0
    n2 = 0.0

    glBegin(GL_LINE_LOOP)
    glVertex3d(a, 0, 0)

    while t1<2*M_PI:
        glVertex3d(a * math.cos(t1), 0 , b * math.sin(t1))
        t1 += M_PI / 100.0
  
    glEnd()

    glBegin(GL_LINES)
    while t2<2*M_PI:
        glVertex3d(a * math.cos(t2), 0.0, b * math.sin(t2))
        glVertex3d(0.0, 0+v, 0)
        t2 += 2.0 * M_PI/16
    glEnd()

def pravac():
    t = YMIN
    glBegin(GL_LINE_STRIP)
    glVertex3d(XMAX/2 + 2, YMIN, 0)
    
    while t < 12:
        t += 1
        x = t/3 - 2
        y = 3*x + 6
        glVertex3d(x, y, 0)
    glEnd()


def osi():
    glColor3d(0.0, 0.0, 1.0)
    glBegin(GL_LINES)
    glVertex2d(XMIN, 0.0)
    glVertex2d(XMAX, 0.0)
    glVertex2d(0.0, YMIN)
    glVertex2d(0.0, YMAX)
    glEnd()


# The main drawing function. 
def iscrtaj():
    glClear(GL_COLOR_BUFFER_BIT )

    #osi
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    osi()

    #pravac
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    glColor3d(1.0, 0.0,0.0)
    pravac()

    #stozac
    glColor3d(0.0, 1.0, 0.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
 
    x1 = -2
    y1 = 0
    z1 = 0
    x2 = 0
    y2 = 6
    z2 = 0

    glTranslated(-2.0, 0.0, 0.0);
    glRotated(kut, x2-x1, y2-y1, z2-z1);
    glTranslated(2.0, 0.0, 0.0);
    cone(2.0, 2.0);
    
    glutSwapBuffers()

# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
def tipka(*args):
	# If escape is pressed, kill everything.
    if args[0] == ESCAPE:
	    glutDestroyWindow(window)
	    sys.exit()

def rotiraj():
    global kut
    kut += 0.2
    glutPostRedisplay()

def main():
	global window
	# For now we just pass glutInit one empty argument. I wasn't sure what should or could be passed in (tuple, list, ...)
	# Once I find out the right stuff based on reading the PyOpenGL source, I'll address this.
	glutInit(())
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA)
	
	glutInitWindowSize(640, 480)
	glutInitWindowPosition(0, 0)
	
	glutCreateWindow("Zadatak1")


	glutDisplayFunc(iscrtaj)
	glutReshapeFunc(skaliraj)
	glutKeyboardFunc(tipka)
	glutIdleFunc(rotiraj)
	glutMainLoop()

main()
    	
