/********************************************************************************/
/* Small Town                                                                   */
/********************************************************************************/
/*팀명 : ~ing                                                                   */
/*팀원 : 20011641  이종문                                                       */
/*       20021424  김기철                                                       */
/*       20031549  이필중                                                       */
/********************************************************************************/
#include <math.h>
#include <stdio.h>
#include <windows.h>
#include <gl/glut.h>
#include "ReadBMP.h" 

#define	checkImageWidth		64
#define	checkImageHeight	64
#define NumberVertices		8
#define NumberFaces			5

typedef float vec3_t[3]; 
typedef float VertexArray[3];
typedef float NormalArray[3];
typedef float TextureCoordsArray[2];

float	size=3.;
float	theta=.0;
float	thetaDelta=.125;
float	eyeDelta=.125;
float	scale=1.0;
float	scaleDelta=1.125;
double	PI=atan(1.0)*4.0;

int		mouseX = 0;
int		mouseY = 0;
int		mouseState = 0;
int		mouseButton = 0; 
int		projection = 0;
int		depthtest = 1;
int		frontface = 0;
int		compositing = 0;
int		texture = 1;
int		magminfilter = 1;
int		repeating = 1;
int		wireframe = 0;
int		nslice = 5;
int		nside = 5;
int		texturesource = 0;
int		windowWidth;
int		windowHeight;


bool		idleFlag = 1;


GLubyte checkImage[checkImageHeight][checkImageWidth][4];

vec3_t rot = {0.,0.,0.};
vec3_t eye = {0.,0.,-5.};
vec3_t center = {0.,0.,0.};
vec3_t color[] = {
	{1.0f, 0.0f, 0.0f},		//red
	{0.0f, 1.0f, 0.0f},		//green
	{0.0f, 0.0f, 1.0f},		//blue
	{1.0f, 1.0f, 0.0f},		//yellow
	{1.0f, 0.0f, 1.0f},		//magenta
	{0.0f, 1.0f, 1.0f},		//cyan
	{1.0f, 1.0f, 1.0f},		//white
	{.25f, .25f, .25f},		//dark gray
	{.60f, .40f, .70f},		//barney purple
	{.98f, .625f, .12f},	//pumpkin orange
	{.98f, .04f, .70f},		//pastel pink
	{.75f, .75f, .75f},		//light gray
	{.60f, .40f, .12f}		//brown
};

MyImageStruct	myImage; 

void makeCheckImage(void)
{
   int i, j, c;
    
   for (i = 0; i < checkImageHeight; i++) {
      for (j = 0; j < checkImageWidth; j++) {
         c = ((((i&0x8)==0)^((j&0x8))==0))*255;
         checkImage[i][j][0] = (GLubyte) c;
         checkImage[i][j][1] = (GLubyte) c;
         checkImage[i][j][2] = (GLubyte) c;
         checkImage[i][j][3] = (GLubyte) i*(255/checkImageHeight);
      }
   }
}

void renderSphere( float cx, float cy, float cz, float r, int p )

{
	int i, j;
	const float PI     = 3.14159265358979f;
	const float TWOPI  = 6.28318530717958f;
	const float PIDIV2 = 1.57079632679489f;

	float theta1 = 0.0;
	float theta2 = 0.0;
	float theta3 = 0.0;

	float ex = 0.0f;
	float ey = 0.0f;
	float ez = 0.0f;

	float px = 0.0f;
	float py = 0.0f;
	float pz = 0.0f;

	// Disallow a negative number for radius.
	if( r < 0 )
		r = -r;

	// Disallow a negative number for precision.
	if( p < 0 )
		p = -p;

	// If the sphere is too small, just render a OpenGL point instead.
	if( p < 4 || r <= 0 ) 
	{
		glBegin( GL_POINTS );
		glVertex3f( cx, cy, cz );
		glEnd();
		return;
	}

    for(i = 0; i < p/2; ++i ) 
	{
        theta1 = i * TWOPI / p - PIDIV2;
        theta2 = (i + 1) * TWOPI / p - PIDIV2;

        glBegin(GL_TRIANGLE_STRIP); 
        {
            for(j = 0; j <= p; ++j )
            {
                theta3 = j * TWOPI / p;

                ex = cos(theta2) * cos(theta3);
                ey = sin(theta2);
                ez = cos(theta2) * sin(theta3);
                px = cx + r * ex;
                py = cy + r * ey;
                pz = cz + r * ez;

                glNormal3f( ex, ey, ez );
                glTexCoord2f( -(j/(float)p) , 2*(i+1)/(float)p );
                glVertex3f( px, py, pz );

                ex = cos(theta1) * cos(theta3);
                ey = sin(theta1);
                ez = cos(theta1) * sin(theta3);
                px = cx + r * ex;
                py = cy + r * ey;
                pz = cz + r * ez;

                glNormal3f( ex, ey, ez );
                glTexCoord2f( -(j/(float)p), 2*i/(float)p );
                glVertex3f( px, py, pz );
            }
        }
        glEnd();
	}
}



void myInit(void)
{    
	glClearColor(0.0, 0.0, 0.0, 0.0);
	makeCheckImage();
	glBindTexture(GL_TEXTURE_2D, 1);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 
		0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
		

	ReadBMP("lpj1.bmp", &myImage); 
	glBindTexture(GL_TEXTURE_2D, 1);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);
	
	ReadBMP("lpj2.bmp", &myImage); 
	glBindTexture(GL_TEXTURE_2D, 2);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);

	ReadBMP("lpj3.bmp", &myImage); 
	glBindTexture(GL_TEXTURE_2D, 3);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);

	

	ReadBMP("lpj1.bmp", &myImage); 
	glBindTexture(GL_TEXTURE_2D, 4);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);

	ReadBMP("lpj2.bmp", &myImage); //옆
	glBindTexture(GL_TEXTURE_2D, 5);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);

	ReadBMP("lpj3.bmp", &myImage); 
	glBindTexture(GL_TEXTURE_2D, 6);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);

	ReadBMP("lpj4.bmp", &myImage); //바닥
	glBindTexture(GL_TEXTURE_2D, 7);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);

	ReadBMP("seo6.bmp", &myImage); 
	glBindTexture(GL_TEXTURE_2D, 8);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);

	ReadBMP("lpj3.bmp", &myImage); //도로 
	glBindTexture(GL_TEXTURE_2D, 9);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);

	ReadBMP("seo3.bmp", &myImage); //지붕
	glBindTexture(GL_TEXTURE_2D, 10);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);

	ReadBMP("jh1.bmp", &myImage); //앞면
	glBindTexture(GL_TEXTURE_2D, 11);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);

	ReadBMP("jh3.bmp", &myImage); //지붕
	glBindTexture(GL_TEXTURE_2D, 12);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);

	ReadBMP("jh2.bmp", &myImage); //옆면
	glBindTexture(GL_TEXTURE_2D, 13);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);

	ReadBMP("seo1.bmp", &myImage); //앞면
	glBindTexture(GL_TEXTURE_2D, 14);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);

	ReadBMP("seo2.bmp", &myImage); //옆면
	glBindTexture(GL_TEXTURE_2D, 15);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);

	ReadBMP("sun.bmp", &myImage); //해
	glBindTexture(GL_TEXTURE_2D, 16);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);
	
	ReadBMP("moon.bmp", &myImage); //달
	glBindTexture(GL_TEXTURE_2D, 17);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, myImage.Width, myImage.Height, 
		0, GL_RGB, GL_UNSIGNED_BYTE, myImage.Pixels);

	
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_DEPTH_TEST);
	glPolygonMode(GL_BACK, GL_LINE);
	glPolygonMode(GL_FRONT, GL_FILL);
}

void myDisplay (void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	if(!projection)
		gluLookAt(eye[0], eye[1], eye[2], center[0], center[1], center[2], 0, 1, 0);
	glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
	glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
	glRotatef(rot[2], 0.0f, 0.0f, 1.0f);
	glScalef(scale/2.0, scale/2.0, scale/2.0);

	//-----------------------------------------------------------------------가운데 집
	glBindTexture(GL_TEXTURE_2D, 1);
	glColor3fv(color[1]);
	glFrontFace(GL_CCW);
	glBegin(GL_POLYGON);
	glTexCoord2f(0.0, 0.0); glVertex3f(0., 0., 1.);

	glTexCoord2f(1.0, 0.0); glVertex3f(1., 0., 1.);
	
	glTexCoord2f(1.0, 0.67); glVertex3f(1., 0.67, 1.);
	glTexCoord2f(0.5, 1.0); glVertex3f(0.5, 1., 1.);
	
	glTexCoord2f(0.0, 0.67); glVertex3f(0., 0.67, 1.);
	
	glEnd();

	glBindTexture(GL_TEXTURE_2D, 2);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0., 0.); glVertex3f(1., 0., 1.);
	glTexCoord2f(1., 0.); glVertex3f(1., 0., -1.);
	glTexCoord2f(1., 1.); glVertex3f(1., 0.67, -1.);
	glTexCoord2f(0., 1.); glVertex3f(1., 0.67, 1.);
	glEnd();

	glBindTexture(GL_TEXTURE_2D, 3);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0., 0.); glVertex3f(1., 0.67, 1.);
	glTexCoord2f(1., 0.); glVertex3f(1., 0.67, -1.);
	glTexCoord2f(1., 1.); glVertex3f(0.5, 1., -1.);
	glTexCoord2f(0., 1.); glVertex3f(0.5, 1., 1.);
	glEnd();
	

	glFrontFace(GL_CW);

	glBindTexture(GL_TEXTURE_2D, 4);
	glColor3fv(color[1]);	
	glBegin(GL_POLYGON);
	glTexCoord2f(0.0, 0.0); glVertex3f(0., 0., -1.);

	glTexCoord2f(1.0, 0.0); glVertex3f(1., 0., -1.);
	
	glTexCoord2f(1.0, 0.67); glVertex3f(1., 0.67, -1.);

	glTexCoord2f(0.5, 1.0); glVertex3f(0.5, 1., -1.);
	
	glTexCoord2f(0.0, 0.67); glVertex3f(0., 0.67, -1.);
	
	glEnd();


	glBindTexture(GL_TEXTURE_2D, 5);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0., 0.); glVertex3f(0., 0., 1.);
	glTexCoord2f(1., 0.); glVertex3f(0., 0., -1.);
	glTexCoord2f(1., 1.); glVertex3f(0., 0.67, -1.);
	glTexCoord2f(0., 1.); glVertex3f(0., 0.67, 1.);
	glEnd();

	
	glBindTexture(GL_TEXTURE_2D, 6);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0., 0.); glVertex3f(0., 0.67, 1.);
	glTexCoord2f(1., 0.); glVertex3f(0., 0.67, -1.);
	glTexCoord2f(1., 1.); glVertex3f(0.5, 1., -1.);
	glTexCoord2f(0., 1.); glVertex3f(0.5, 1., 1.);
	glEnd();

	//---------------------------------------------------------------------------왼쪽집
	glBindTexture(GL_TEXTURE_2D, 11);
	glColor3fv(color[1]);
	glFrontFace(GL_CCW);
	glBegin(GL_POLYGON);
	glTexCoord2f(0.0, 0.0); glVertex3f(-1.5, 0., 0.5);

	glTexCoord2f(1.0, 0.0); glVertex3f(-0.5, 0., 0.5);
	
	glTexCoord2f(1.0, 0.67); glVertex3f(-0.5, 0.67, 0.5);
	glTexCoord2f(0.5, 1.0); glVertex3f(-1, 1., 0.5);
	
	glTexCoord2f(0.0, 0.67); glVertex3f(-1.5, 0.67, 0.5);
	
	glEnd();

	glBindTexture(GL_TEXTURE_2D, 13);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0., 0.); glVertex3f(-0.5, 0., 0.5);
	glTexCoord2f(1., 0.); glVertex3f(-0.5, 0., 0);
	glTexCoord2f(1., 1.); glVertex3f(-0.5, 0.67, 0);
	glTexCoord2f(0., 1.); glVertex3f(-0.5, 0.67, 0.5);
	glEnd();

	glBindTexture(GL_TEXTURE_2D, 12);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0., 0.); glVertex3f(-0.5, 0.67, 0.5);
	glTexCoord2f(1., 0.); glVertex3f(-0.5, 0.67, 0);
	glTexCoord2f(1., 1.); glVertex3f(-1, 1., 0);
	glTexCoord2f(0., 1.); glVertex3f(-1, 1., 0.5);
	glEnd();
	

	glFrontFace(GL_CW);

	glBindTexture(GL_TEXTURE_2D, 11);
	glColor3fv(color[1]);	
	glBegin(GL_POLYGON);
	glTexCoord2f(0.0, 0.0); glVertex3f(-1.5, 0., 0);

	glTexCoord2f(1.0, 0.0); glVertex3f(-0.5, 0., 0);
	
	glTexCoord2f(1.0, 0.67); glVertex3f(-0.5, 0.67, 0);

	glTexCoord2f(0.5, 1.0); glVertex3f(-1, 1., 0);
	
	glTexCoord2f(0.0, 0.67); glVertex3f(-1.5, 0.67, 0);
	
	glEnd();


	glBindTexture(GL_TEXTURE_2D, 13);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0., 0.); glVertex3f(-1.5, 0., 0.5);
	glTexCoord2f(1., 0.); glVertex3f(-1.5, 0., 0);
	glTexCoord2f(1., 1.); glVertex3f(-1.5, 0.67,0);
	glTexCoord2f(0., 1.); glVertex3f(-1.5, 0.67, 0.5);
	glEnd();

	
	glBindTexture(GL_TEXTURE_2D, 12);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0., 0.); glVertex3f(-1.5, 0.67, 0.5);
	glTexCoord2f(1., 0.); glVertex3f(-1.5, 0.67, 0);
	glTexCoord2f(1., 1.); glVertex3f(-1, 1., 0);
	glTexCoord2f(0., 1.); glVertex3f(-1, 1., 0.5);
	glEnd();

	//--------------------------------------------------------------------------오른쪽집
	glBindTexture(GL_TEXTURE_2D, 14);
	glColor3fv(color[1]);
	glFrontFace(GL_CCW);
	glBegin(GL_POLYGON);
	glTexCoord2f(0.0, 0.0); glVertex3f(1.2, 0., 0.2);

	glTexCoord2f(1.0, 0.0); glVertex3f(2.2, 0., 0.2);
	
	glTexCoord2f(1.0, 0.67); glVertex3f(2.2, 0.67, 0.2);
	glTexCoord2f(0.5, 1.0); glVertex3f(1.7, 1., 0.2);
	
	glTexCoord2f(0.0, 0.67); glVertex3f(1.2, 0.67, 0.2);
	
	glEnd();

	glBindTexture(GL_TEXTURE_2D, 15);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0., 0.); glVertex3f(2.2, 0., 0.2);
	glTexCoord2f(1., 0.); glVertex3f(2.2, 0., -0.5);
	glTexCoord2f(1., 1.); glVertex3f(2.2, 0.67, -0.5);
	glTexCoord2f(0., 1.); glVertex3f(2.2, 0.67, 0.2);
	glEnd();

	glBindTexture(GL_TEXTURE_2D, 10);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0., 0.); glVertex3f(2.2, 0.67, 0.2);
	glTexCoord2f(1., 0.); glVertex3f(2.2, 0.67, -0.5);
	glTexCoord2f(1., 1.); glVertex3f(1.7, 1., -0.5);
	glTexCoord2f(0., 1.); glVertex3f(1.7, 1., 0.2);
	glEnd();
	

	glFrontFace(GL_CW);

	glBindTexture(GL_TEXTURE_2D, 14);
	glColor3fv(color[1]);	
	glBegin(GL_POLYGON);
	glTexCoord2f(0.0, 0.0); glVertex3f(1.2, 0., -0.5);

	glTexCoord2f(1.0, 0.0); glVertex3f(2.2, 0., -0.5);
	
	glTexCoord2f(1.0, 0.67); glVertex3f(2.2, 0.67, -0.5);

	glTexCoord2f(0.5, 1.0); glVertex3f(1.7, 1., -0.5);
	
	glTexCoord2f(0.0, 0.67); glVertex3f(1.2, 0.67, -0.5);
	
	glEnd();


	glBindTexture(GL_TEXTURE_2D, 15);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0., 0.); glVertex3f(1.2, 0., 0.2);
	glTexCoord2f(1., 0.); glVertex3f(1.2, 0., -0.5);
	glTexCoord2f(1., 1.); glVertex3f(1.2, 0.67, -0.5);
	glTexCoord2f(0., 1.); glVertex3f(1.2, 0.67, 0.2);
	glEnd();

	
	glBindTexture(GL_TEXTURE_2D, 10);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0., 0.); glVertex3f(1.2, 0.67, 0.2);
	glTexCoord2f(1., 0.); glVertex3f(1.2, 0.67, -0.5);
	glTexCoord2f(1., 1.); glVertex3f(1.7, 1., -0.5);
	glTexCoord2f(0., 1.); glVertex3f(1.7, 1., 0.2);
	glEnd();


	//-------------------------------------------------------------------바닥
	glFrontFace(GL_CCW);
	glBindTexture(GL_TEXTURE_2D, 7);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0., 0.); glVertex3f(3., 0., 1.5);
	glTexCoord2f(20, 0.); glVertex3f(3., 0., -3.);
	glTexCoord2f(20, 20); glVertex3f(-2., 0., -3.);
	glTexCoord2f(0., 20); glVertex3f(-2., 0., 1.5);
	glEnd();

	//-------------------------------------------------------------------도로 앞 바닥
	glFrontFace(GL_CW);
	glBindTexture(GL_TEXTURE_2D, 7);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0., 0.); glVertex3f(3., 0., 2);
	glTexCoord2f(5, 0.); glVertex3f(3., 0., 3.);
	glTexCoord2f(5, 20); glVertex3f(-2., 0., 3.);
	glTexCoord2f(0., 20); glVertex3f(-2., 0., 2);
	glEnd();
	
	//-------------------------------------------------------------------도로
	glFrontFace(GL_CCW);
	glBindTexture(GL_TEXTURE_2D, 9);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0., 0.); glVertex3f(3., 0., 2);
	glTexCoord2f(1, 0.); glVertex3f(3., 0., 1.5);
	glTexCoord2f(1, 3); glVertex3f(-2., 0., 1.5);
	glTexCoord2f(0., 3); glVertex3f(-2., 0., 2);
	glEnd();

	//-------------------------------------------------------------------배경
	glFrontFace(GL_CCW);
	glBindTexture(GL_TEXTURE_2D, 8);
	glColor3fv(color[3]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0, 0); glVertex3f(-2., 0., -3.);
	glTexCoord2f(1, 0.); glVertex3f(3., 0., -3.);
	glTexCoord2f(1., 1); glVertex3f(3., 3., -3.);
	glTexCoord2f(0., 1); glVertex3f(-2., 3., -3.);
	glEnd();
	
	
	//-------------------------------------------------------------------태양

	glPushMatrix();
		glRotatef(theta, 0., 0., 1.);
		glEnable(GL_TEXTURE_2D);  
		glBindTexture(GL_TEXTURE_2D, 16); 
		renderSphere(3.0, 0.0, 0.0, 0.25, 20 ); 
	
	glPopMatrix();

	//-------------------------------------------------------------------달	
	glPushMatrix();
		glRotatef(theta, 0., 0., 1.);
		glEnable(GL_TEXTURE_2D);  
		glBindTexture(GL_TEXTURE_2D, 17); 
		renderSphere(-3.0, 0.0, 0.0, 0.25, 20 ); 
	
	glPopMatrix();
	
	
	glFlush();
	glutSwapBuffers();
}

void myLookAt(int key)
{
	if(key == GLUT_KEY_UP) {
		eye[2] = eye[2]-cos(theta)*eyeDelta;
		eye[0] = eye[0]+sin(theta)*eyeDelta;
	}
	else if(key == GLUT_KEY_DOWN) {
		eye[2] = eye[2]+cos(theta)*eyeDelta;
		eye[0] = eye[0]-sin(theta)*eyeDelta;
	}
	center[2] = eye[2]-cos(theta);
	center[0] = eye[0]+sin(theta);
}

void myResize (int width, int height)
{
    if (!height) return;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
	if(projection) {
		glOrtho(-size, size, -size, size, -size, size); 
		eye[2] = 0.;
	}
    else {
		gluPerspective(60., (float)width/height, .1, 10.);
		eye[2] = 5.;
		myLookAt(0);
	}
	glMatrixMode(GL_MODELVIEW);
	windowWidth=width;
	windowHeight=height;
}

void mySKeyboard (int key, int x, int y)
{
    switch (key) {
		case GLUT_KEY_UP : break;
		case GLUT_KEY_DOWN : break;
		case GLUT_KEY_LEFT : theta-=thetaDelta; break;
		case GLUT_KEY_RIGHT : theta+=thetaDelta; break;
		default : return;
    }
	myLookAt(key);
	glutPostRedisplay();
}

void myKeyboard (unsigned char key, int x, int y)
{
	switch (key)
	{
		case 'p':
			projection = !projection;
			myResize(windowWidth, windowHeight);
		break;
		case 'd':
			depthtest = !depthtest;
			if(depthtest) glEnable(GL_DEPTH_TEST);
			else glDisable(GL_DEPTH_TEST);
		break;
		case 'f':
			frontface = !frontface;
			if(frontface) glFrontFace(GL_CW);
			else glFrontFace(GL_CCW);
		break;
		case 'r':
			repeating = !repeating;
			if(repeating) {
				glBindTexture(GL_TEXTURE_2D, 1);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
				glBindTexture(GL_TEXTURE_2D, 2);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
				glBindTexture(GL_TEXTURE_2D, 3);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
				glBindTexture(GL_TEXTURE_2D, 4);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
				glBindTexture(GL_TEXTURE_2D, 5);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
				glBindTexture(GL_TEXTURE_2D, 6);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
				glBindTexture(GL_TEXTURE_2D, 7);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
			}
			else {
				glBindTexture(GL_TEXTURE_2D, 1);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
				glBindTexture(GL_TEXTURE_2D, 2);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
				glBindTexture(GL_TEXTURE_2D, 3);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
				glBindTexture(GL_TEXTURE_2D, 4);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
				glBindTexture(GL_TEXTURE_2D, 5);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
				glBindTexture(GL_TEXTURE_2D, 6);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
				glBindTexture(GL_TEXTURE_2D, 7);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
			}
		break;
		case 'c':
			compositing = !compositing;
			if(compositing) {
				glBindTexture(GL_TEXTURE_2D, 1);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
				glBindTexture(GL_TEXTURE_2D, 2);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
				glBindTexture(GL_TEXTURE_2D, 3);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
				glBindTexture(GL_TEXTURE_2D, 4);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
				glBindTexture(GL_TEXTURE_2D, 5);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
				glBindTexture(GL_TEXTURE_2D, 6);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
				glBindTexture(GL_TEXTURE_2D, 7);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
			}
			else {
				glBindTexture(GL_TEXTURE_2D, 1);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
				glBindTexture(GL_TEXTURE_2D, 2);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
				glBindTexture(GL_TEXTURE_2D, 3);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
				glBindTexture(GL_TEXTURE_2D, 4);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
				glBindTexture(GL_TEXTURE_2D, 5);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
				glBindTexture(GL_TEXTURE_2D, 6);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
				glBindTexture(GL_TEXTURE_2D, 7);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
			}
		break;
		case 'm':
			magminfilter = !magminfilter;
			if(magminfilter) {
				glBindTexture(GL_TEXTURE_2D, 1);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
				glBindTexture(GL_TEXTURE_2D, 2);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
				glBindTexture(GL_TEXTURE_2D, 3);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
				glBindTexture(GL_TEXTURE_2D, 4);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
				glBindTexture(GL_TEXTURE_2D, 5);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
				glBindTexture(GL_TEXTURE_2D, 6);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
				glBindTexture(GL_TEXTURE_2D, 7);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
				

			}
			else {
				glBindTexture(GL_TEXTURE_2D, 1);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
				glBindTexture(GL_TEXTURE_2D, 2);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
				glBindTexture(GL_TEXTURE_2D, 3);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
				glBindTexture(GL_TEXTURE_2D, 4);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
				glBindTexture(GL_TEXTURE_2D, 5);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
				glBindTexture(GL_TEXTURE_2D, 6);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
				glBindTexture(GL_TEXTURE_2D, 7);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
			}
		break;
		case 't':
			texture = !texture;
			if(texture) glEnable(GL_TEXTURE_2D);
			else glDisable(GL_TEXTURE_2D);
		break;
		case 'z':
			scale*=scaleDelta;
		break;
		case 'x':
			scale/=scaleDelta;
		break;
	}
	glutPostRedisplay();
}

void myIdle(void)
{
	theta+=0.2;

	glutPostRedisplay();
}

void myMouse(int btn, int state, int x, int y)
{
	if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
		glutIdleFunc(NULL);
		mouseState=state;
		mouseButton=btn;
		mouseX=x;
		mouseY=y;
	}
	else if(btn==GLUT_LEFT_BUTTON && state == GLUT_UP)
		mouseState=-1;
	else return;

	if(idleFlag && state==GLUT_DOWN) 
		glutIdleFunc(NULL);
	else if(idleFlag && state==GLUT_UP) 
		glutIdleFunc(myIdle);
	glutPostRedisplay();
}

void myMotion(int x, int y)
{
	if(mouseButton == GLUT_LEFT_BUTTON && mouseState == GLUT_DOWN) {
		rot[1] -= (mouseX - x)/5.;
		rot[0] -= (mouseY - y)/5.;
		glutPostRedisplay();
		mouseX=x;
		mouseY=y;
	}
}

void main (void)
{
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize(1000,1000);
	glutCreateWindow("Small Town");
	glutDisplayFunc(myDisplay);
	glutReshapeFunc(myResize);
	glutKeyboardFunc(myKeyboard);
    glutSpecialFunc(mySKeyboard);
	glutMouseFunc(myMouse);
	glutMotionFunc(myMotion);
	myInit();
	glutMainLoop();
}
