#!/usr/bin/python2.4

# This is statement is required by the build system to query build info

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLE import *
from math import *
import sys
import random

mouseX=0
mouseY=0

SCALE = 1.0 #polygon scale factor
ESCALE = 1.0 # extrude scale factor

# the base 2D profile of the region you're extruding.
polygonProfile = ((-10.0, 0.0), (-8, 10.0), (0, 8), (0, 0.0))

# a list of tuples of your car, truck, bus etc extrusions in the form (extrude depth(R, G, B))
# every area you have data for will have a list like this
extrudes = [(-12, (0.0, 0.9, 0)), (-7, (0.2, 0.7, 0)), (-5, (.4, 0.5, 0)), (-4, (.7, 0.2, 0))]

mapPlane = (0, 0, -80)# where our map is in 3D space

def Extrude(extrudes, polygonProfile):
  # scale up/down the polygonProfile and write into new list.
  polygonNormal = []
  endPolygon = []
  extrudeDepth = 0
  extrudeStart = 0
  polygon = map(lambda x: (SCALE*x[0], SCALE*x[1]), polygonProfile[0:])

  # give polygon a normal
  for i in range(1, len(polygon)):
    ax = polygon[i][0] - polygon[i-1][0]
    ay = polygon[i][1] - polygon[i-1][1]
    alen = sqrt (ax*ax + ay*ay)
    polygonNormal.append((-ay / alen, ax / alen))
  polygonNormal.insert(0, polygonNormal[-1])

  for i in range(len(extrudes)):
    polygonColours = ((extrudes[i][1]),)*4
    extrudeDepth += extrudes[i][0]
    extrudeTarget = map(lambda x: (0, 0, ESCALE*x), (0.0, extrudeDepth-extrudes[i][0], extrudeDepth, 0.0))
    gleExtrusion(polygon, polygonNormal, None, extrudeTarget, polygonColours)

def MouseMotion (x, y):
  global mouseX, mouseY
  mouseX = x
  mouseY = y
  glutPostRedisplay ()

def Draw():
  global polygonProfile, extrudes  
  glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  # set up some matrices so that the object spins with the mouse
  gleSetJoinStyle(TUBE_NORM_FACET | TUBE_JN_ANGLE | TUBE_CONTOUR_CLOSED | TUBE_JN_CAP)
  glPushMatrix ()
  glTranslatef (mapPlane[0], mapPlane[1], mapPlane[2])
  glRotatef (mouseX, 0.0, 1.0, 0.0)
  glRotatef (mouseY, 1.0, 0.0, 0.0)
  Extrude(extrudes, polygonProfile)

  glPopMatrix ()
  glutSwapBuffers ()

if __name__ == '__main__':
  lightOnePosition = (40.0, 40, 100.0, 0.0)
  lightOneColor = (0.99, 0.99, 0.99, 1.0) 
  
  lightTwoPosition = (-40.0, 40, 100.0, 0.0)
  lightTwoColor = (0.99, 0.99, 0.99, 1.0) 
  
  # initialize glut 
  glutInit(sys.argv)
  glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
  glutCreateWindow("extrusion test")
  glutDisplayFunc(Draw)
  glutMotionFunc(MouseMotion)
  
  # initialize GL */
  glClearDepth (1.0)
  glEnable (GL_DEPTH_TEST)
  glClearColor (0.0, 0.0, 0.0, 0.0)
  glShadeModel (GL_SMOOTH)
  
  glMatrixMode (GL_PROJECTION)
  glFrustum (-9.0, 9.0, -9.0, 9.0, 50.0, 150.0)
  glMatrixMode(GL_MODELVIEW)
  
  # initialize lighting */
  glLightfv (GL_LIGHT0, GL_POSITION, lightOnePosition)
  glLightfv (GL_LIGHT0, GL_DIFFUSE, lightOneColor)
  glEnable (GL_LIGHT0)
  glLightfv (GL_LIGHT1, GL_POSITION, lightTwoPosition)
  glLightfv (GL_LIGHT1, GL_DIFFUSE, lightTwoColor)
  glEnable (GL_LIGHT1)
  glEnable (GL_LIGHTING)
  glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE)
  glEnable (GL_COLOR_MATERIAL)
  
  glutMainLoop ()
