# coding: utf-8
# practice3.py
#
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import sys
import math
import numpy as np

import wx

ESCAPE = '\033'

class QuadsPicture:
	def __init__(self):
		self.lastx = self.x = 0.
		self.lasty = self.y = 0.
		self.size = None
		glClearDepth(1.0)
		glEnable(GL_DEPTH_TEST)
		glClearColor(0.0, 0.5, 0.0, 0.0)
		glShadeModel(GL_SMOOTH)

		glMatrixMode(GL_PROJECTION)
		glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 15.0)
		glMatrixMode(GL_MODELVIEW)

		glTranslatef(0.0, 0.0, -10.0)
		glRotatef(self.lasty, 1.0, 0.0, 0.0)
		glRotatef(self.lastx, 0.0, 1.0, 0.0)
		#glEnable(GL_LIGHTING)
		glEnable(GL_LIGHT0)
		glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE)
		glEnable(GL_COLOR_MATERIAL)

		self.vpos = list()
		self.readpos()
		self.displaylist()

	def readpos(self):#ﾌｧｲﾙから頂点座標を読み込む
		pass
		#self.vpos.append([0.0,0.0,0.5])
		self.vpos.append([0.5,0.5,0.5])
		self.vpos.append([-0.5,0.5,0.5])
		self.vpos.append([-0.5,-0.5,0.5])
		self.vpos.append([0.5,-0.5,0.5])

		#self.vpos.append([0.0,0.0,-0.5])
		self.vpos.append([0.5,0.5,-0.5])
		self.vpos.append([0.5,-0.5,-0.5])
		self.vpos.append([-0.5,-0.5,-0.5])
		self.vpos.append([-0.5,0.5,-0.5])

		"""
		fp = open('coordpos.txt', 'r')
		num = int(fp.readline())
		poss = fp.read().splitlines()
		for i in xrange(num):
			self.vpos.append([float(x) for x in poss[i].split(',')])
		fp.close()
		"""
	def crossproduct(self, a, b, c):#面の法線ﾍﾞｸﾄﾙを計算
		cross = [0.0, 0.0, 0.0]
		cross[0] = (a[1]-b[1])*(c[2]-b[2]) - (a[2]-b[2])*(c[1]-b[1])
		cross[1] = (a[2]-b[2])*(c[0]-b[0]) - (a[0]-b[0])*(c[2]-b[2])
		cross[2] = (a[0]-b[0])*(c[1]-b[1]) - (a[1]-b[1])*(c[0]-b[0])
		cabs = math.sqrt(cross[0]**2+cross[1]**2+cross[2]**2)
		cc = [x/cabs for x in cross]
		return cc

	def mouseMotion(self, x, y):
		self.lastx = (self.x)
		self.lasty = (self.y)
		self.x = x
		self.y = y
		print x, y, self.x, self.y, self.lastx, self.lasty
		glutPostRedisplay()
	
	def mouse (self, button, state, x, y):
		print button, state, x, y
		print GLUT_DOWN, GLUT_LEFT_BUTTON
		if state == GLUT_DOWN:
			if button == GLUT_LEFT_BUTTON:
				pass


	def displaylist(self):
		ps = self.vpos
		self.index = glGenLists(1)
		glNewList(self.index, GL_COMPILE_AND_EXECUTE)

		glBegin(GL_QUADS)
		# 四角形の面1
		#glNormal3fv(self.crossproduct(ps[4],ps[1],ps[0]))
		glColor3f(1,0,0)
		glVertex3fv(ps[0])
		glVertex3fv(ps[1])
		glVertex3fv(ps[2])
		glVertex3fv(ps[3])
		# 四角形の面2
		#glNormal3fv(self.crossproduct(ps[5],ps[2],ps[1]))
		glColor3f(0,0,1)
		glVertex3fv(ps[4])
		glVertex3fv(ps[5])
		glVertex3fv(ps[6])
		glVertex3fv(ps[7])
		# 四角形の面3
		#......(続けて頂点の指定｡中略）......

		glEnd()

		glEndList()

	def display(self):
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
		glPushMatrix()
		w = glutGet(GLUT_WINDOW_WIDTH)
		h = glutGet(GLUT_WINDOW_HEIGHT)
		w = max(w,1.0)
		h = max(h,1.0)
		yscale = 180.0/w
		xscale = 180.0/h
		glRotatef(self.lastx, 0.0, 1.0, 0.0)
		glRotatef(self.lasty, 1.0, 0.0, 0.0)

		glCallList(self.index)

		glPopMatrix()
		glutSwapBuffers()

	def keyboard(self, *args):
		if args[0] in {ESCAPE,"q"}:
			sys.exit()

	def reshape(self, w, h):
		glViewport(0, 0, w, h)

#def GLDraw():
def main():
	glutInit(sys.argv)

	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH )
	glutInitWindowPosition( 100, 100 )
	glutInitWindowSize( 600, 600 )
	glutCreateWindow( sys.argv[0] )

	qp = QuadsPicture()
	glutDisplayFunc( qp.display )
	glutKeyboardFunc( qp.keyboard )
	glutReshapeFunc( qp.reshape )
	glutMotionFunc( qp.mouseMotion )
	glutMouseFunc( qp.mouse )

	print "Hit ESC key to quit."
	glutMainLoop()

class GLFrame(wx.Frame):
	def __init__(self, parent, id, title, pos=wx.DefaultPosition,
		     size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE,
		     name="frame"):
		style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE

		super(GLFrame, self).__init__(parent, id, title, pos, size, style, name)
		GLDraw()

def CreateMap (rpsom):
	main()
#	frame = wx.Frame(None, wx.ID_ANY, "New Frame")
#	sizer = wx.BoxSizer(wx.VERTICAL)
#	sizer.Add (main())
#	frame.Add(sizer)
#	frame.Show()

#def main():
#	app = wx.App()
#	frame = GLFrame(None, -1, "WXPython").Show()
#	app.MainLoop()
#main()
