/*
    This file is part of Society Chess.
    Society Chess is a network chess game.
    Copyright (C) 2011  Sterling Pickens

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "opengl.h"

#define RESTART 0
#define PAUSED 1
#define EXIT 2
#define SHOW_FPS 3
//bool ignoreRepeats = FALSE;
struct timeval starttime, endtime;
int hundred_frames = 0;
double te0 = 0;
char fps[16];
char turn_chars_a[24]; //white team's turn!
char turn_chars_b[24]; //black team's turn!
char check_chars[32];
char checkmate_chars[32];
float board_x;
float board_y;
float win_x;
float win_y;
float win_aspect;
int mouse_x, mouse_y;
float fs_x, fs_y, fs_z;
int Show_FPS;

//[abc] [123] .x1 .y1 = left bottom  x2 y2 = right top
struct vertex{
	float x1;
	float y1;
	float x2;
	float y2;
}bounds[8][8];

const char alphabet[] = "ABCDEFGH";
const char numbers[]  = "12345678";

static void OpenGLInit(void);
static void Animate(void );
static void ResizeWindow(int w, int h);

//static int glutCreateMenu(void (*func)(int value));
//static void glutAttachMenu(int button);

static void processMenuEvents(int option);
static void keyboard (unsigned char key, int x, int y);
static void processMouse(int button, int state, int x, int y);
static void Adjust_Bounds(void);

//static void initLightAndMaterial(void);


static void processMouse(int button, int state, int x, int y){
	int xc, yc;
	static struct movement move;
	static int valid;
	//static int is_in_check;

	if(checkmate || op_checkmate)
		return;

	/*
	if(checkmate){ 
		(void)strcpy(checkmate_chars, "checkmate: You have LOST!");
		return;
	}
	if(op_checkmate){
		(void)strcpy(checkmate_chars, "checkmate: You have WON!");
		return;
	}
	*/

	if(button == GLUT_LEFT_BUTTON){
		mouse_x = x;
		mouse_y = y;
		for(xc=0; xc<8; xc++){
			for(yc=0; yc<8; yc++){
				if(x>bounds[xc][yc].x1 && x<bounds[xc][yc].x2){
					if(y<bounds[xc][yc].y1 && y>bounds[xc][yc].y2){
						if(state == GLUT_DOWN){
							(void)memset((void *)&move, 0, sizeof(struct movement) );
							//button pressed   the movement source
							move.source_x = xc;
							move.source_y = yc;
							//check if valid/moveable/your-turn
							//set move.valid_option 0/1
							//is_in_check = check;
							valid = Valid_Source((struct movement *)&move);
							printf("source valid: %d\n", valid);
						}
						if(state == GLUT_UP && valid){
							//button released  the movement destination
							move.dest_x = xc;
							move.dest_y = yc;
							move.caller = 0;
							valid = Valid_Destination(my_pieces, oponent_pieces, (struct movement *)&move);
							//set move.valid_option 0/1
							//make move if TRUE
							printf("dest valid: %d\n", valid);

							if(valid){
								//if(is_in_check){
								//	is_in_check = Simulate_Is_Check(my_pieces, oponent_pieces, (struct movement *)&move);
								//	if(is_in_check){
								//		printf("Invalid: move does not remove your king from check!\n");
								//		return;
								//	}
								//}
								printf("moving to %c %c\n", alphabet[xc], numbers[yc]);
								Make_Move(my_pieces, oponent_pieces, (struct movement *)&move);
								if(!local_game)
									my_turn = 0;
								sleep(1);
								Send_OK = 1;
							}
						}
						//printf("you clicked position: %c %c\n", alphabet[xc], numbers[yc]);
						//changed = 1;
						return;
					}
				}
			}
		}
		//printf("you did not click in a board position!\n");
		//changed = 1;
		return;
	}
}

static void Adjust_Bounds(void){
	int xc, yc;
	float lx, ty, rx, by;
	float eighth_x, eighth_y;
	(void)strcpy(turn_chars_a, "white team's turn!");
	(void)strcpy(turn_chars_b, "black team's turn!");

	lx = win_x-board_x-(win_x-board_x)/2;
	ty = win_y-board_y-(win_y-board_y)/2;
	by = ty + board_y;
	rx = lx + board_x;

	eighth_x = board_x/8;
	eighth_y = board_y/8;

	for(xc=0; xc<8; xc++){
		for(yc=0; yc<8; yc++){
			//.x1 .y1 = left bottom  x2 y2 = right top
			bounds[xc][yc].x1 = lx + xc*eighth_x;
			bounds[xc][yc].y1 = by - yc*eighth_y;
			bounds[xc][yc].x2 = lx + eighth_x + xc*eighth_x;
			bounds[xc][yc].y2 = by - eighth_y - yc*eighth_y;
		}
	}
	fs_x = eighth_x/4/104.76;
	fs_y = eighth_x/4/119.05;
	fs_z = 0;
}

static void createGLUTMenus(){
	int menu;
	menu = glutCreateMenu(processMenuEvents);
	glutAddMenuEntry("Restart",RESTART);
	glutAddMenuEntry("Pause",PAUSED);
	glutAddMenuEntry("Show FPS",SHOW_FPS);
	glutAddMenuEntry("Exit",EXIT);
	glutAttachMenu(GLUT_RIGHT_BUTTON);
}

static void processMenuEvents(int option){
	switch (option){
		case RESTART :
			break;
		case PAUSED :
			break;
		case SHOW_FPS :
			Show_FPS = ~Show_FPS;
			//if(Show_FPS == 0)
			//	Show_FPS = 1;
			//else
			//	Show_FPS = 0;
			break;
		case EXIT :
			exit(0);
			break;
	}
	//changed = 1;
}

static void special (int key, int x, int y){
	switch(key){
		case GLUT_KEY_UP:
			board_y++;
			//x/y = aspect
			board_x = win_aspect*board_y;
			break;
		case GLUT_KEY_DOWN:
			board_y--;
			board_x = win_aspect*board_y;
			break;
		case GLUT_KEY_RIGHT:
			break;
		case GLUT_KEY_LEFT:
			break;
	}
	Adjust_Bounds();
	//changed = 1;
}

static void keyboard(unsigned char key, int x, int y){
	if(key==27) //esc key exits
		exit(0);
}

void *Create_OpenGL(void *data){
	win_x = 800;
	win_y = 600;
	board_x = .65*win_x;
	board_y = board_x*win_y/win_x;
	//checkmate = 0;
	//op_checkmate = 0;
	Show_FPS = 0;
	Adjust_Bounds();
	//while(!my_turn || my_team_color == 3){
	//	sleep(1);
	//}

	if(!IS_Server){
		while(!my_turn)
			sleep(1);
		Setup_Pieces();
	}
	//while(!my_turn)
	//sleep(1);
	//if(!IS_Server){
	//	Setup_Pieces();
	//}else{
	//	while(my_turn == 0)
	//		sleep(1);
	//}


	glutInit(&My_argc,My_argv);
//	if(my_team_color){
//		printf("YOU ARE THE %s team!\n", "black");
//	}else{
//		printf("YOU ARE THE %s team!\n", "white");
//	}
	//	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB );
	glutInitWindowPosition( 0, 0 );
	glutInitWindowSize( win_x, win_y );
	glutCreateWindow( "societychess test version" );
	OpenGLInit();
	//changed = 1;
	//	initLightAndMaterial();
	glutReshapeFunc( ResizeWindow );
	glutDisplayFunc( Animate);
	glutIgnoreKeyRepeat(0);
	glutSpecialFunc(special);
	glutKeyboardFunc (keyboard);
	createGLUTMenus();
	glutMouseFunc(processMouse);
	glutMainLoop(  );
	pthread_exit(NULL);
}

static void Animate(void){

	if(Show_FPS){
		if(hundred_frames == 0)
			gettimeofday(&starttime, NULL);
		hundred_frames++;
	}
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	//	glPushMatrix();
	//glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
	//glEnable ( GL_COLOR_MATERIAL ) ;
	//glEnable(GL_LIGHT_0);

	//int Font_w = glutStrokeWidth(GLUT_STROKE_MONO_ROMAN, 'A');
	//GLfloat Font_h = glutStrokeHeight(GLUT_STROKE_MONO_ROMAN);
	//int Font_h = 119;
	board_x = win_aspect*board_y;
    int xc, yc;
    float lx, ty, rx, by;
    float eighth_x, eighth_y;

        lx = win_x-board_x-(win_x-board_x)/2;
        ty = win_y-board_y-(win_y-board_y)/2;
        by = ty + board_y;
        rx = lx + board_x;

        eighth_x = board_x/8;
        eighth_y = board_y/8;

	glLoadIdentity();
	//160-82-45
	glColor3f((float)160/255, (float)82/255, (float)45/255);
	//glColor3f( 0.871, 0.722, 0.529 );
	//glColor3f( 0.545, 0.271, 0.075 );
	glRectf(bounds[0][0].x1-eighth_x, bounds[0][0].y1+eighth_y, bounds[7][7].x2+eighth_x, bounds[7][7].y2-eighth_y*2);

	for(xc=0; xc<8; xc++){
		glLoadIdentity();
		glPushMatrix();

		glColor3f(1.0, 0.0, 0.0);
		glTranslatef(bounds[xc][0].x1 + eighth_x/2, bounds[0][0].y1 + eighth_y/2, 0);
		//glRotated(
    glRotatef(180.0, 1, 0, 0);
    glScalef(fs_x, fs_y, fs_z);

		glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, alphabet[xc]);
		glPopMatrix();
		for(yc=0; yc<8; yc++){
                      //.x1 .y1 = left bottom  x2 y2 = right top
                        //bounds[xc][yc].x1 = lx + xc*eighth_x;
                        //bounds[xc][yc].y1 = by - yc*eighth_y;
                        //bounds[xc][yc].x2 = lx + eighth_x + xc*eighth_x;
                        //bounds[xc][yc].y2 = by - eighth_y - yc*eighth_y;
			if(xc==0){
				glLoadIdentity();
				glPushMatrix();

				glColor3f(0.0, 0.0, 1.0);
				glTranslatef(bounds[0][0].x1 - eighth_x/2, bounds[0][yc].y1 - eighth_y/2, 0);
    glRotatef(180.0, 1, 0, 0);
    glScalef(fs_x, fs_y, fs_z);

				glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, numbers[yc]);
				glPopMatrix();
			}
			glLoadIdentity();
			if( ((yc+xc)%2) == 0){
				glColor3f( 0.45, 0.45, 0.45 );
			}else{
				glColor3f( 0.6, 0.6, 0.6 );
			}
			glBegin(GL_QUADS);
			glVertex3f(bounds[xc][yc].x1, bounds[xc][yc].y2, 0.0f); //top left
			glVertex3f(bounds[xc][yc].x2, bounds[xc][yc].y2, 0.0f); //top right
			glVertex3f(bounds[xc][yc].x2, bounds[xc][yc].y1, 0.0f); //bottom right
			glVertex3f(bounds[xc][yc].x1, bounds[xc][yc].y1, 0.0f); //bottom left
			glEnd();
		}
	}

	//draw the pieces
	for(xc=0; xc<16; xc++){
		if(my_pieces[xc].alive){
			glLoadIdentity();
			glPushMatrix();

			if(my_team_color)
				glColor3f(0.0, 0.0, 0.0);
			else
				glColor3f(1.0, 1.0, 1.0);

			glTranslatef(bounds[0][0].x1 + eighth_x*my_pieces[xc].position.x + (eighth_x/8)*3,
			              bounds[0][0].y1 - eighth_y*my_pieces[xc].position.y - (eighth_y/8)*3,
						  0);
    glRotatef(180.0, 1, 0, 0);
    glScalef(fs_x, fs_y, fs_z);


			switch(my_pieces[xc].type){
				case 0:
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'P');
					break;
				case 1:
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'R');
					break;
				case 2:
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'K');
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'n');
					break;
				case 3:
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'B');
					break;
				case 4:
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'Q');
					break;
				case 5:
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'K');
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'i');
					break;
				default:
					break;
			}
			glPopMatrix();
		}
		if(oponent_pieces[xc].alive){
			glLoadIdentity();
			glPushMatrix();

			if(my_team_color)
				glColor3f(1.0, 1.0, 1.0);
			else
				glColor3f(0.0, 0.0, 0.0);
			glTranslatef(bounds[0][0].x1 + eighth_x*oponent_pieces[xc].position.x + (eighth_x/8)*3,
		        	      bounds[0][0].y1 - eighth_y*oponent_pieces[xc].position.y - (eighth_y/8)*3,
						  0 );
    glRotatef(180.0, 1, 0, 0);
    glScalef(fs_x, fs_y, fs_z);

			switch(oponent_pieces[xc].type){
				case 0:
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'P');
					break;
				case 1:
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'R');
					break;
				case 2:
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'K');
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'n');
					break;
				case 3:
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'B');
					break;
				case 4:
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'Q');
					break;
				case 5:
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'K');
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, 'i');
					break;
				default:
					break;
			}
			glPopMatrix();
		}

	}

	if(Show_FPS){
		glLoadIdentity();
		glPushMatrix();
		glColor3f(0.0, 1.0, 0.0);
		//glTranslate2f(bounds[7][7].x2-eighth_x, bounds[7][7].y2-eighth_y);
		glTranslatef(bounds[7][7].x2-eighth_x*2.5, bounds[7][7].y2-eighth_y*1.65, 0);
		//glRotatef(180.0, (bounds[7][7].x2-eighth_x) + Font_w*(strlen(fps)/2) , (bounds[7][7].y2-eighth_y) + Font_h/2, 0.0);
		glRotatef(180.0, 1, 0, 0);
		glScalef(fs_x, fs_y, fs_z);
		for(xc=0; xc<strlen(fps); xc++){
			glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, fps[xc]);
		}
		glPopMatrix();
	}

	glLoadIdentity();
	glPushMatrix();
	glTranslatef(bounds[7][7].x2-eighth_x*3.75, bounds[7][7].y2-eighth_y*1.25, 0);
	glRotatef(180.0, 1, 0, 0);
	glScalef(fs_x, fs_y, fs_z);

	if(my_team_color){
		glColor3f(0.0, 0.0, 0.0);
		(void)strcpy(checkmate_chars, "You are Black team");
	}else if(my_team_color == 0){
		glColor3f(1.0, 1.0, 1.0);
		(void)strcpy(checkmate_chars, "You are White team");
	}else{
		(void)strcpy(checkmate_chars, "No team yet!");
	}
	for(xc=0; xc<strlen(checkmate_chars); xc++){
		glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, checkmate_chars[xc]);
	}
	glPopMatrix();


	glLoadIdentity();
	glPushMatrix();
	//glColor3f(0.0, 1.0, 0.0);
	//my_team_color = 3;
	//if(my_team_color == 3)
	//	glTranslatef(bounds[0][0].x1-eighth_x, bounds[0][7].y2-eighth_y*0.65, 0);
	//else
		glTranslatef(bounds[0][0].x1-eighth_x, bounds[0][7].y2-eighth_y, 0);
	glRotatef(180.0, 1, 0, 0);
	glScalef(fs_x*1.1, fs_y*1.1, fs_z);

	if(!checkmate && !op_checkmate){
		if(my_team_color == 3){
			(void)strcpy(checkmate_chars, "NET: LISTENING!");
			glColor3f(0.0, 1.0, 0.0);
			for(xc=0; xc<strlen(checkmate_chars); xc++){
				glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, checkmate_chars[xc]);
			}
			//glPopMatrix();

			//glLoadIdentity();
			//glPushMatrix();
			//glRotatef(180.0, 1, 0, 0);
			//glScalef(fs_x*1.1, fs_y*1.1, fs_z);
			//glColor3f(0.0, 1.0, 0.0);
			//glTranslatef(bounds[0][0].x1, eighth_y/8, 0);
			//(void)strcpy(checkmate_chars, "CONNECTION!");
			//for(xc=0; xc<strlen(checkmate_chars); xc++){
			//	glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, checkmate_chars[xc]);
			//}
			//glPopMatrix();
		}else if(my_turn){
			if(my_team_color){ //i'm black team
				glColor3f(0.0, 0.0, 0.0);
				for(xc=0; xc<strlen(turn_chars_a); xc++){
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, turn_chars_b[xc]);
				} //i'm white team
			}else{
				glColor3f(1.0, 1.0, 1.0);
				for(xc=0; xc<strlen(turn_chars_a); xc++){
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, turn_chars_a[xc]); 
				}
			}
		}else{
			if(my_team_color){
				glColor3f(1.0, 1.0, 1.0);
				for(xc=0; xc<strlen(turn_chars_a); xc++){
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, turn_chars_a[xc]);
				}
			}else{
				glColor3f(0.0, 0.0, 0.0);
				for(xc=0; xc<strlen(turn_chars_b); xc++){
					glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, turn_chars_b[xc]);
				}
			}
		}
	}
	if(checkmate){
		(void)strcpy(checkmate_chars, "checkmate: You have LOST!");
		glColor3f(1.0, 0.0, 0.0);
		for(xc=0; xc<strlen(checkmate_chars); xc++){
			glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, checkmate_chars[xc]);
		}
	}else if(op_checkmate){
		(void)strcpy(checkmate_chars, "checkmate: You have WON!");
		glColor3f(1.0, 0.0, 0.0);
		for(xc=0; xc<strlen(checkmate_chars); xc++){
			glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, checkmate_chars[xc]);
		}
	}
	glPopMatrix();

	if(check || op_check){
		if(check){
			(void)strcpy(check_chars, "your king is in check!");
		}
		if(op_check){
			(void)strcpy(check_chars, "enemy king is in check!");
		}
		if(check && op_check){
			(void)strcpy(check_chars, "both kings are in check!");
		}
		glLoadIdentity();
		glPushMatrix();
		glTranslatef(bounds[0][0].x1, bounds[0][7].y2-eighth_y*0.5, 0);
		glRotatef(180.0, 1, 0, 0);
		glScalef(fs_x*1.05, fs_y*1.05, fs_z);
		glColor3f(1.0, 1.0, 0.0);
		for(xc=0; xc<strlen(check_chars); xc++){
			glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, check_chars[xc]);
		}
		/*
		if(check){ //my king is checked
			for(xc=0; xc<strlen(check_chars); xc++){
				glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, op_checkmate_chars[xc]);
			}
		}else if(check == 2){ //op king is checked
			for(xc=0; xc<strlen(check_chars); xc++){
				glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, op_checkmate_chars[xc]);
			}
		}else if(check == 3){ //both kings in check

		}
		*/
		glPopMatrix();
	}


	glFlush();
	glutSwapBuffers();
	glutPostRedisplay();
	if(Show_FPS){
		gettimeofday(&endtime, NULL);
		if((unsigned long)endtime.tv_sec > (unsigned long)starttime.tv_sec){
			te0=((double)(endtime.tv_sec*1000000-starttime.tv_sec*1000000+endtime.tv_usec-starttime.tv_usec))/1000000;
			(void)memset(fps, '\0', 16); 
			(void)sprintf(fps, "FPS: %.02lf", hundred_frames/te0);
			//printf("FPS: %lu\n", (unsigned long)endtime.tv_sec );
			hundred_frames = 0;
		}
	}
}

static void OpenGLInit(void)
{
    glShadeModel( GL_SMOOTH );
    glClearColor( 0.0, 0.0, 0.0, 0.0 );
    glDisable( GL_DEPTH_TEST );
	//glEnable ( GL_LIGHTING ) ;

	//    glDepthFunc(GL_ALWAYS);
	//    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	//    glEnable( GL_DEPTH_TEST );
	//    glClearDepth( 1.0 );
    glClear(GL_COLOR_BUFFER_BIT);
}

static void ResizeWindow(int w, int h)
{
	//win_x = w;
	//win_y = h;
    float aspectRatio;
        h = (h == 0) ? 1 : h;
        w = (w == 0) ? 1 : w;
        glViewport( 0, 0, w, h );       // View port uses whole window
        aspectRatio = (float)w/(float)h;
        win_x = w;
        win_y = h;
	//board_x = aspect*win_y;
	//board_y = 
	//board_x = 
	//board_y = 
	//board_y = board_x*win_y/win_x;
	//printf("x: %f y: %f\n", win_x, win_y);
	win_aspect = aspectRatio;
	Adjust_Bounds();

    // Set up the projection view matrix (not very well!)
    glMatrixMode( GL_PROJECTION );

    glLoadIdentity();
	//    gluPerspective( 70.0, aspectRatio, 1.0, 40.0 );

    // Select the Modelview matrix
	glOrtho (0, w, h, 0, 0, 1);
    glMatrixMode( GL_MODELVIEW );
	//changed = 1;
}

/*
static void initLightAndMaterial(void){
  static float ambient[] =
  {0.1, 0.1, 0.1, 1.0};
  static float diffuse[] =
  {1.0, 1.0, 1.0, 1.0};
  static float position[] =
  {90.0, 90.0, 150.0, 0.0};

  static float front_mat_shininess[] =
  {60.0};
  static float front_mat_specular[] =
  {0.2, 0.2, 0.2, 1.0};
  static float front_mat_diffuse[] =
  {0.5, 0.5, 0.28, 1.0};
  static float back_mat_shininess[] =
  {60.0};
  static float back_mat_specular[] =
  {0.5, 0.5, 0.2, 1.0};
  static float back_mat_diffuse[] =
  {1.0, 0.9, 0.2, 1.0};
  static float lmodel_ambient[] =
  {1.0, 1.0, 1.0, 1.0};
	static float lmodel_twoside[] =
	{GL_TRUE};
	static float lmodel_oneside[] =
	{GL_FALSE};


  //setMatrix();
//  glEnable(GL_DEPTH_TEST);
//  glDepthFunc(GL_LEQUAL);

//  glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
//  glLightfv(GL_LIGHT0, GL_POSITION, position);

//  glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
//  glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
//  glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
//  glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);
//  glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
  glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);

//  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
//  glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);

  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
//  glShadeModel(GL_SMOOTH);
}
*/