#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];
bool	eflag = false;
bool	wflag = false;
bool	qflag = false;
bool	aflag = false;
bool	dflag = false;
bool	sflag = false;
bool	pflag = false;
bool	pflag2 = false;
float	size=3.;
float	theta=.0;
float	theta2=.0;
float	rtheta=.0;
float	ytheta=.0;
float	thetaDelta=.125;
float	eyeDelta=.125;
float	scale=0.01;
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;

GLubyte checkImage[checkImageHeight][checkImageWidth][4];
GLint		N=6;
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 myInit(void)
{    

	GLfloat ambientLight[]={0.3f,0.3f,0.3f,1.0f};
	GLfloat diffuseLight[]={0.7f,0.7f,0.7f,1.0f};
	GLfloat specular[]={1.0f,1.0f,1.0f,1.0f};
	GLfloat specref[]={1.0f,1.0f,1.0f,1.0f};
	GLfloat position[]={400.0f,300.0f,700.0f,1.0f};

glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
	glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
	glLightfv(GL_LIGHT0, GL_POSITION, position);
	glEnable(GL_LIGHT0);

	glClearColor(0.0, 0.0, 0.0, 0.0);
	
	ReadBMP("base.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("morae1.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("greed.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("screen.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("person2.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("ball.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("G1.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("G2.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("G3.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("G4.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("glove2.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);
	

	glEnable(GL_TEXTURE_2D);
	glEnable(GL_DEPTH_TEST);
	glPolygonMode(GL_BACK, GL_LINE);
	glPolygonMode(GL_FRONT, GL_FILL);
}
void myAxis(void)
{
	int	i;
	glColor3f(.98, .04, .70); 
	glBegin(GL_LINES);
	for(i=0; i<=N; i++) {
		glVertex2f(-size+2.0*i*size/N, -size);
		glVertex2f(-size+2.0*i*size/N, size);
		glVertex2f(-size, -size+2.0*i*size/N);
		glVertex2f(size, -size+2.0*i*size/N);
	}
	glEnd();
	glColor3f(.25, .25, .25); 
	glBegin(GL_LINES);
		glVertex2f(0, -size);
		glVertex2f(0, size);
		glVertex2f(-size, 0);
		glVertex2f(size, 0);
	glEnd();
}
void DrawTexturedMesh(VertexArray *vertices, NormalArray *normalVectors, 
	GLint *faces, GLint nFaces, GLint verticesPerFace,
	TextureCoordsArray *textureCoords, GLint *textureIndexes) { 
	int  j, k, n=0, f; 
	if(wireframe) f = GL_LINE_LOOP;
	else f = GL_POLYGON;
	for (j=0; j<nFaces; j++) { 
		glBegin(f); 
		glNormal3fv(normalVectors[j]); 
		for (k=0; k<verticesPerFace; k++) { 
			glTexCoord2fv(textureCoords[textureIndexes[n]]); 
			glVertex3fv(vertices[faces[n++]]); 
		} 
		glEnd(); 
	} 
}
void DrawSphere(float radius, int numberSlices, int numberSides)
{
	GLint                numberVertices;
	GLint                numberFaces;
	VertexArray         *vertices;
	NormalArray         *normals;
	GLint               *faces; 
	TextureCoordsArray  *textureCoordinates; 
	GLint               *textureIndexes;
	int                  j, k, n, m;
	float                x, y, z;
	float                u, v;
	float                uInc, vInc;

	// allocate the needed memory for the data
	numberVertices = (numberSides+1) * (numberSlices+1);
	numberFaces = (numberSides) * (numberSlices);
	vertices = (VertexArray *) malloc( sizeof(VertexArray) * numberVertices);
	normals = (NormalArray *) malloc( sizeof(NormalArray) * numberFaces);
	faces = (GLint *) malloc( sizeof(GLint) * numberFaces * 4);
	textureCoordinates = (TextureCoordsArray *)
	malloc( sizeof(TextureCoordsArray) * numberVertices);
	textureIndexes = (GLint *) malloc( sizeof(GLint) * numberFaces * 4);

	if (vertices != 0 && normals != 0 && faces != 0 && 
		textureCoordinates != 0 && textureIndexes != 0) {
		vInc = 2.*PI/numberSides;
		uInc = PI/numberSlices;
		n = 0;
		for (k=0, u = -0.5*PI; k<=numberSlices; k++, u += uInc) {
			for (j=0, v = 0.0; j<=numberSides; j++, v += vInc) {
				// Generate the vertices and texture coordinates around this slice
				vertices[n][0] = radius*cos(u)*cos(v);
				vertices[n][1] = radius*cos(u)*sin(v);
				vertices[n][2] = radius*sin(u);

				textureCoordinates[n][0] = (float) j / (float) numberSides;
				textureCoordinates[n][1] = (float) k / (float) numberSlices;

				if (n < numberFaces) {
					// Calculate the normal vector for the face using the middle
					//   point on the face
					x = radius*cos(u+uInc*0.5)*cos(v+0.5*vInc);
					y = radius*cos(u+uInc*0.5)*sin(v+0.5*vInc);
					z = radius*sin(u+uInc*0.5);
					normals[n][0] = x / radius;
					normals[n][1] = y / radius;
					normals[n][2] = z / radius;
				}
				n++;
			}
		}

		// Create the faces
		n = 0;
		m = 0;
		for (k=0; k<numberSlices; k++) {
			for (j=0; j<numberSides; j++) {
				// Generate the face and texture indexes for this face
				faces[n] = textureIndexes[n] = m;                n++;
				faces[n] = textureIndexes[n] = m+1;              n++;
				faces[n] = textureIndexes[n] = m+1+numberSides+1;  n++;
				faces[n] = textureIndexes[n] = m+numberSides+1;  n++;
				m++;
			}
			m++;
		}
		DrawTexturedMesh(vertices, normals, faces, numberFaces, 4,
			textureCoordinates, textureIndexes); 
	}

	// Reclaim the allocated memory
	if (vertices != NULL)           free( vertices );
	if (normals != NULL)            free( normals  );
	if (faces != NULL)              free( faces    );
	if (textureCoordinates != NULL) free( textureCoordinates );
	if (textureIndexes != NULL)     free( textureIndexes );
}

void swingEvent()
{
	if(pflag)
	{
		if(theta>1.5 && theta>2)
		{
			pflag=false;
			theta=0;
			theta2=0;
		}
		//glTranslatef(0,theta2,0);
		glRotatef(-360*sin(theta),1,0,0);
	}
}

void pitcher()
{
		
	glRotatef(90,0,1,0);
	glPushMatrix();
		glColor3f(0,0,0);
		glScalef(1,1.5,0.5);		
		glutWireCube(10.0);//몸통
	glPopMatrix();

	glPushMatrix();//오른어깨
		glTranslatef(6,5.8,0);
		glutWireSphere(2,10,10);
	glPopMatrix();

	glPushMatrix();//오른팔	
	swingEvent();
		glTranslatef(6,15,0);
		glutWireSphere(1.8,10,10);
		glScalef(0.35,2,0.35);
		glutWireCube(10.0);
	glPopMatrix();
	//glPushMatrix();
	//	glPushMatrix();//오른팔꿈치
	//		glTranslatef(10,-1,0);
	//		glutWireSphere(1.3,10,10);
	//	glPopMatrix();
	//	glPushMatrix();//오른팔목
	//		glTranslatef(10,-4,0);
	//		glScalef(0.5,1.2,0.5);
	//		glutWireCube(5.0);
	//	glPopMatrix();
	//	glPushMatrix();//오른손
	//		glTranslatef(10,-7,0);
	//		glutWireSphere(1.5,10,10);		
	//	glPopMatrix();
	//glPopMatrix();

	glPushMatrix();//왼어깨
		glTranslatef(-6,5.8,0);
		glutWireSphere(2,10,10);
	glPopMatrix();
	glPushMatrix();//왼팔
		glTranslatef(-8.4,1.7,0);
		glRotatef(-30,0,0,1);
		glScalef(0.5,1.2,0.5);
		glutWireCube(5.0);
	glPopMatrix();
	glPushMatrix();	//머리	
		glTranslatef(0,9,0);
		glutWireCube(3.0);
		glTranslatef(0,4,0);
		glutWireSphere(4,10,10);
	glPopMatrix();
	glPushMatrix();//왼팔꿈치
		glTranslatef(-10,-1,0);
		glutWireSphere(1.3,10,10);
	glPopMatrix();

	glPushMatrix();//왼팔목
		glTranslatef(-10,-4,0);		
		glScalef(0.5,1.2,0.5);		
		glutWireCube(5.0);
	glPopMatrix();
	glPushMatrix();//왼손
		glTranslatef(-10,-7,0);
		glutWireSphere(1.5,10,10);		
	glPopMatrix();
	glPushMatrix();//오른허벅지
		glTranslatef(3.2,-12.5,0);
		glScalef(0.7,2,0.7);
		glutWireCube(5.0);
	glPopMatrix();
	
	
	glPushMatrix();//오른무릎
		glTranslatef(3.2,-17.5,0);
		glutWireSphere(1.8,10,10);
	glPopMatrix();
	
	glPushMatrix();//오른다리		
		glTranslatef(3.2,-22.5,0);		
		glScalef(0.7,2,0.7);
		glutWireCube(5.0);
	glPopMatrix();

	glPushMatrix();//왼다리
		glTranslatef(-3.2,-7.5,0);
			
		glTranslatef(0,-10,0);
		glutWireSphere(1.8,10,10);
		glScalef(0.35,2,0.35);
		glutWireCube(10.0);
	glPopMatrix();	

}
void myIdle(void)
{	
	if(eflag)
	{
		rtheta+=1;
		
	}
	if(qflag)//커브
	{
		rtheta+=3;
		ytheta+=0.8;
		
	}
	if(wflag)//돌직구
	{
		rtheta+=10;
		
	}
	if(aflag)//직구
	{
		rtheta+=3;
		ytheta+=0.1;
		
	}
	if(dflag)	//싱커
	{
		if(ytheta<135)
			rtheta+=1;
		else
			rtheta-=2;

		ytheta+=3;
		
	}
	if(sflag)//포크
	{
		if(ytheta<120)
			rtheta+=1;
		else
			rtheta-=10;

		ytheta+=3;
		
	}
	if(pflag)
	{
		theta+=0.1;
	}
	if(pflag2)
	{
		theta2+=0.1;
	}

	glutPostRedisplay();
}
void pitchingevent()
{
	if(eflag)
	{
		if(rtheta>190)
		{
			eflag=false;
			rtheta=0;
			
		}
		glRotatef(rtheta,-1,1,0);
		
	}
	if(qflag)//커브
	{
		if(rtheta>170)
		{
			qflag=false;
			rtheta=0;
			ytheta=0;
			
		}
		glTranslatef(0,0,ytheta);
		glRotatef(rtheta,1,0.5,0);
		
	}
	if(wflag)	//돌직구
	{
		if(rtheta>180)
		{
			wflag=false;
			rtheta=0;
			
		}
		glTranslatef(0,-rtheta/4,2*rtheta);
		
	}
	if(aflag)//직구
	{
		if(rtheta>170)
		{
			aflag=false;
			rtheta=0;	
		}
		
		glTranslatef(0,-rtheta/4,2*rtheta);
		
	}
	if(dflag)	//싱커
	{
		if(ytheta>170)
		{
			dflag=false;
			rtheta=0;
			ytheta=0;
		}
		glTranslatef(rtheta,-ytheta/4,2*ytheta);
		
	}
	if(sflag)//포크
	{
		if(ytheta>170)
		{
			sflag=false;
			rtheta=0;
			ytheta=0;      
		}	
		glTranslatef(0,rtheta/2,2*ytheta);
	}
}
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, scale, scale);
	
	
	glBindTexture(GL_TEXTURE_2D, 3);//바닥
	glColor3fv(color[1]);
	glBegin(GL_POLYGON);
	glTexCoord2f(0.0, 0.0); glVertex3f(0,-30,250);
	glTexCoord2f(1.0, 0.0); glVertex3f(800,-30,-400);
	glTexCoord2f(1.0, 1.0); glVertex3f(400,-30,-600);
	glTexCoord2f(0.0, 1.0); glVertex3f(0,-30,-700);
	glTexCoord2f(1.0, 0.0); glVertex3f(-400,-30,-600);
	glTexCoord2f(1.0, 1.0); glVertex3f(-800,-30,-400);
	glEnd();



//////////////////////////투수
	glPushMatrix();
		glTranslatef(0,18,-150);
		glRotatef(90,0,1,0);
		pitcher();
		glColor3fv(color[7]);
	glPopMatrix();
	glColor3fv(color[7]);
//////////////////////////////////////////
	glPushMatrix();
	glColor3fv(color[0]);
	glTranslatef(0,0,200);
		glPushMatrix();	
		glColor3fv(color[0]);
		glBindTexture(GL_TEXTURE_2D, 11);
		glBegin(GL_QUADS);//포수
		glTexCoord2f(1.0, 0);glVertex3f(20,-25,0);
		glTexCoord2f(1.0, 1.0);glVertex3f(20,0,0);
		glTexCoord2f(0, 1.0);glVertex3f(-20,0,0);
		glTexCoord2f(0, 0);glVertex3f(-20,-25,0);
		glEnd();
		glPopMatrix();
	glPopMatrix();

		//공
	glPushMatrix();
		pitchingevent();
		glTranslatef(0,70,-135);
		glTranslatef(-5,-25,-10);
		glRotatef(10*rtheta,1,1,1);
		glBindTexture(GL_TEXTURE_2D, 6);
		DrawSphere(4., 10, 10);
	glPopMatrix();

	//전광판(1)
	glColor3fv(color[12]);
	glBindTexture(GL_TEXTURE_2D, 7);
	glBegin(GL_QUADS);
	glTexCoord2f(1.0, 0);glVertex3f(800,-30,-400);
	glTexCoord2f(1.0,1.0);glVertex3f(800,100,-400);
	glTexCoord2f(0, 1.0);glVertex3f(400,100,-600);
	glTexCoord2f(0, 0);glVertex3f(400,-30,-600);
	glEnd();
	//전광판(2)
	glColor3fv(color[0]);
	glBindTexture(GL_TEXTURE_2D, 8);
	glBegin(GL_QUADS);
	glTexCoord2f(1.0, 0);glVertex3f(400,-30,-600);
	glTexCoord2f(1.0, 1.0);glVertex3f(400,100,-600);
	glTexCoord2f(0, 1.0);glVertex3f(0,100,-700);
	glTexCoord2f(0, 0);glVertex3f(0,-30,-700);
	glEnd();
	//전광판(3)
	glColor3fv(color[8]);
	glBindTexture(GL_TEXTURE_2D, 9);
	glBegin(GL_QUADS);
	glTexCoord2f(1.0, 0);glVertex3f(0,-30,-700);
	glTexCoord2f(1.0, 1.0);glVertex3f(0,100,-700);
	glTexCoord2f(0, 1.0);glVertex3f(-400,100,-600);
	glTexCoord2f(0, 0);glVertex3f(-400,-30,-600);
	glEnd();
    //전광판(4)
	glColor3fv(color[12]);
	glBindTexture(GL_TEXTURE_2D, 10);
	glBegin(GL_QUADS);
	glTexCoord2f(1.0, 0);glVertex3f(-400,-30,-600);
	glTexCoord2f(1.0, 1.0);glVertex3f(-400,100,-600);
	glTexCoord2f(0, 1.0);glVertex3f(-800,100,-400);
	glTexCoord2f(0, 0);glVertex3f(-800,-30,-400);
	glEnd();

	//관객(1)
	glColor3fv(color[0]);
	glBindTexture(GL_TEXTURE_2D, 5);
	glBegin(GL_QUADS);
	glTexCoord2f(1.0, 0.0);glVertex3f(800,100,-400);
	glTexCoord2f(1.0, 1.0);glVertex3f(800,300,-500);
	glTexCoord2f(0.0, 1.0);glVertex3f(400,300,-700);
	glTexCoord2f(0.0, 0.0);glVertex3f(400,100,-600);
	glEnd();
	//관객(2)
	glColor3fv(color[0]);
	glBindTexture(GL_TEXTURE_2D, 5);
	glBegin(GL_QUADS);
	glTexCoord2f(1.0, 0.0);glVertex3f(-400,100,-600);
	glTexCoord2f(1.0, 1.0);glVertex3f(-400,300,-700);
	glTexCoord2f(0.0, 1.0);glVertex3f(-800,300,-500);
	glTexCoord2f(0.0, 0.0);glVertex3f(-800,100,-400);
	glEnd();
	//스코어판(1)
	glColor3fv(color[2]);
	glBindTexture(GL_TEXTURE_2D, 4);
	glBegin(GL_QUADS);
	glTexCoord2f(1.0, 0.0);glVertex3f(400,100,-600);
	glTexCoord2f(1.0, 1.0);glVertex3f(400,400,-700);
	glTexCoord2f(0.0, 1.0);glVertex3f(-400,400,-700);
	glTexCoord2f(0.0, 0.0);glVertex3f(-400,100,-600);
	glEnd();
	

	//라인(0~1루)
	glColor3fv(color[6]);
	glBegin(GL_QUADS);
	glVertex3f(0,-28,200);
	glVertex3f(450,-28,-150);
	glVertex3f(450,-28,-150);
	glVertex3f(0,-28,200);
	glEnd();
	//라인(1~2루)
	glColor3fv(color[6]);
	glBegin(GL_QUADS);
	glVertex3f(450,-28,-150);
	glVertex3f(0,-28,-400);
	glVertex3f(0,-28,-400);
	glVertex3f(450,-28,-150);
	glEnd();
	//(2~3루)
	glColor3fv(color[6]);
	glBegin(GL_QUADS);
	glVertex3f(-450,-28,-150);
	glVertex3f(0,-28,-400);
	glVertex3f(0,-28,-400);
	glVertex3f(-450,-28,-150);
	glEnd();
	//라인(0~3루)
	glBegin(GL_QUADS);
	glColor3fv(color[6]);
	glVertex3f(0,-28,200);
	glVertex3f(-450,-28,-150);
	glVertex3f(-450,-28,-150);
	glVertex3f(0,-28,200);
	glEnd();
	//홈(베이스)
	glBindTexture(GL_TEXTURE_2D, 1);
	glColor3fv(color[6]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0, 1.0);glVertex3f(0,-28,220);
	glTexCoord2f(1.0, 1.0);glVertex3f(20,-28,200);
	glTexCoord2f(1.0, 0.0);glVertex3f(0,-28,180);
	glTexCoord2f(0.0, 0.0);glVertex3f(-20,-28,200);
	glEnd();
	//1루(베이스)
	glBindTexture(GL_TEXTURE_2D, 1);
	glColor3fv(color[6]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0, 1.0);glVertex3f(450,-28,-130);
	glTexCoord2f(1.0, 1.0);glVertex3f(470,-28,-150);
	glTexCoord2f(1.0, 0.0);glVertex3f(450,-28,-180);
	glTexCoord2f(0.0, 0.0);glVertex3f(430,-28,-150);
	glEnd();
	//2루(베이스)
	glBindTexture(GL_TEXTURE_2D, 1);
	glColor3fv(color[6]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0, 1.0);glVertex3f(0,-28,-380);
	glTexCoord2f(1.0, 1.0);glVertex3f(20,-28,-400);
	glTexCoord2f(1.0, 0.0);glVertex3f(0,-28,-420);
	glTexCoord2f(0.0, 0.0);glVertex3f(-20,-28,-380);
	glEnd();
	//3루(베이스)
	glBindTexture(GL_TEXTURE_2D, 1);
	glColor3fv(color[6]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0, 1.0);glVertex3f(-450,-28,-130);
	glTexCoord2f(1.0, 1.0);glVertex3f(-430,-28,-150);
	glTexCoord2f(1.0, 0.0);glVertex3f(-450,-28,-180);
	glTexCoord2f(0.0, 0.0);glVertex3f(-470,-28,-150);
	glEnd();
	//3루(흙)
	glBindTexture(GL_TEXTURE_2D, 2);
	glColor3fv(color[3]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0, 1.0); glVertex3f(-450,-29,-50);
	glTexCoord2f(1.0, 1.0); glVertex3f(-350,-29,-150);
	glTexCoord2f(1.0, 0.0); glVertex3f(-450,-29,-250);
	glTexCoord2f(0.0, 0.0); glVertex3f(-550,-29,-150);
	glEnd();
	//2루(흙)
	glBindTexture(GL_TEXTURE_2D, 2);
	glColor3fv(color[3]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0, 1.0);glVertex3f(0,-29,-300);
	glTexCoord2f(1.0, 1.0);glVertex3f(100,-29,-400);
	glTexCoord2f(1.0, 0.0);glVertex3f(0,-29,-500);
	glTexCoord2f(0.0, 0.0);glVertex3f(-100,-29,-400);
	glEnd();
	//1루(흙)
	glBindTexture(GL_TEXTURE_2D, 2);
	glColor3fv(color[3]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0, 0.0);glVertex3f(450,-29,-250);
	glTexCoord2f(1.0, 0.0);glVertex3f(350,-29,-150);
	glTexCoord2f(1.0, 1.0);glVertex3f(450,-29,-50);
	glTexCoord2f(0.0, 1.0);glVertex3f(550,-29,-150);
	glEnd();
	//홈(흙)
	glBindTexture(GL_TEXTURE_2D, 2);
	glColor3fv(color[3]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0, 0.0);glVertex3f(0,-29,300);
	glTexCoord2f(1.0, 0.0);glVertex3f(100,-29,200);
	glTexCoord2f(1.0, 1.0);glVertex3f(0,-29,100);
	glTexCoord2f(0.0, 1.0);glVertex3f(-100,-29,200);
	glEnd();
	

	//마운드
	glPushMatrix();
		glColor3fv(color[8]);
		glTranslatef(0,-80,-150);
		glTranslatef(-5,-25,-10);
		glBindTexture(GL_TEXTURE_2D, 2);
		DrawSphere(100, 10, 10);
	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)
{
	glClearColor(.75, .75, .75, 0.0);
    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 '5':wflag=true;//돌직구
		pflag=true;
		pflag2=true;
		break;
	case '2':qflag=true;//커브
		pflag=true;
		pflag2=true;
		break;
	case '1':aflag=true;//직구
		pflag=true;
		pflag2=true;
		break;
	case '3':dflag=true;//싱커
		pflag=true;
		pflag2=true;
		break;
	case '4':sflag=true;	//포크
		pflag=true;
		pflag2=true;
		break;
		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);
			}
			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);
			}
		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);
			}
			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);
			}
		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); 
			}
			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); 
			}
		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 myMouse(int btn, int state, int x, int y)
{
	if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
		
		mouseState=state;
		mouseButton=btn;
		mouseX=x;
		mouseY=y;
	}
	else if(btn==GLUT_LEFT_BUTTON && state == GLUT_UP)
		mouseState=-1;
	else return;
	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(800,500);
	glutCreateWindow("PICTHINGMAN");
	glutDisplayFunc(myDisplay);
	glutReshapeFunc(myResize);
	glutKeyboardFunc(myKeyboard);
    glutSpecialFunc(mySKeyboard);
	glutIdleFunc(myIdle);
	glutMouseFunc(myMouse);
	glutMotionFunc(myMotion);
	myInit();
	glutMainLoop();
}
