#include <windows.h>
#include <gl\gl.h>			
#include <gl\glu.h>			
#include <gl\glaux.h>
#include <gl\glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "glm.h"
#include "ReadBMP.h"


#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "glaux.lib")


#define maping 17

typedef float vec3_t[3]; 

vec3_t rot = {0,0,0};
vec3_t eye = {0,0,0};
vec3_t center = {0,0,0};
float	theta=.0;
float	thetaDelta=.125;
float	eyeDelta=.525;
double	rei=0;
double  center_l=0.65;
bool    idleFlag =1;
bool	ttt=false;
int		t=0;
int		projection = 0;
int rotX = 0,rotY = 0; 
int newx = 0, newy = 0; 
int oldx = 0, oldy = 0; 
int bClick = 0;
int r=0 , xRot = 0.0f;

///////////////////
GLUquadricObj *c; //물체포인터


bool		idleFlag1 = 1;
GLint		N=6;
GLint		mouseX = 0;
GLint		mouseY = 0;
GLint		mouseState = 0;
GLint		mouseButton = 0; 
GLfloat 	size=30.0, Mecury_theta=0.0, Venus_theta=0.0, Earth_theta=0, Moon_theta=0, Mars_theta=0, Jupiter_theta=0,c_theta=0,
Saturn_theta=0,Uranus_theta=0, Neptune_theta=0, Pluto_theta=0 , Black_Hall = 0.0, ufo = 0.0;

GLfloat		Self_Sun=0, Self_Mercury=0, Self_Venus=0, Self_Earth=0, Self_Mars=0, Self_Jupiter=0, Self_Saturn=0, Self_Uranus=0, Self_Neptune=0, Self_Pluto=0,self_c=0;
GLfloat	xTheta=0., yTheta=0., zTheta=0., thetaDelta1=.125;
GLfloat	scale=1.0, scaleDelta=1.01;
MyImageStruct	myImage; 
AUX_RGBImageRec *texture[maping];
GLfloat xPos = 0.0f,zPos = 0.0f;
HWND		hWnd=NULL;

int lighting=1;
static numplean=10;

GLuint tex[maping];	//텍스쳐변수
AUX_RGBImageRec *texRec[maping];

GLUquadricObj *obj;

AUX_RGBImageRec *LoadBMPFile(char *filename) //BMP확장자 파일을 로딩하게한다.
{
	FILE *hFile = NULL;
	if(!filename) return NULL;
	hFile = fopen(filename, "r");
	if(hFile) 
	{
		fclose(hFile);
		return auxDIBImageLoad(filename);
	}   
	return NULL;
}

void loadtexture()	//텍스처 로딩
{
	AUX_RGBImageRec *texRec[maping];
	memset(texRec, 0, sizeof(void *)*maping);
	
	texRec[0]=LoadBMPFile("image\\Sun.bmp");
	texRec[1]=LoadBMPFile("image\\Mercury.bmp");
	texRec[2]=LoadBMPFile("image\\Venus.bmp");
	texRec[3]=LoadBMPFile("image\\earth.bmp");
	texRec[4]=LoadBMPFile("image\\Moon.bmp");
	texRec[5]=LoadBMPFile("image\\Mars.bmp");
	texRec[6]=LoadBMPFile("image\\Jupiter.bmp");
	texRec[7]=LoadBMPFile("image\\saturn.bmp");
	texRec[8]=LoadBMPFile("image\\Uranus.bmp");
	texRec[9]=LoadBMPFile("image\\Neptune.bmp");
	texRec[10]=LoadBMPFile("image\\Pluto.bmp");
	texRec[11]=LoadBMPFile("image\\c.bmp");
	texRec[12]=LoadBMPFile("image\\bbb.bmp");
	texRec[13]=LoadBMPFile("image\\personface.bmp");
	texRec[14]=LoadBMPFile("image\\personbody.bmp");
	texRec[15]=LoadBMPFile("image\\personjean.bmp");
	texRec[16]=LoadBMPFile("image\\b3.bmp");
	{
		glGenTextures(maping, &tex[0]);
		for(int i=0; i<maping; i++) 
		{            
			glBindTexture(GL_TEXTURE_2D, tex[i]);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);    
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);    
			glTexImage2D(GL_TEXTURE_2D, 0, 3, texRec[i]->sizeX, texRec[i]->sizeY, 0,GL_RGB, GL_UNSIGNED_BYTE,texRec[i]->data);
		}
	}
	// 쓸모없어진 데이터들 Delete
	for(int i=0; i<maping; i++) 
	{
		if(texRec[i]) 
		{
			if(texRec[i]->data) free(texRec[i]->data);
			free(texRec[i]);
		} 
	} 
	// Texture사용을 가능하게 렌더상태 설정
	glEnable(GL_TEXTURE_2D);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	
	ReadBMP(".\\Image\\space2.bmp", &myImage); 
	glBindTexture(GL_TEXTURE_2D, maping+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);
	glEnable(GL_CULL_FACE);		
	glEnable(GL_COLOR_MATERIAL);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	
	glEnable(GL_NORMALIZE);
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_DEPTH_TEST);
	//	glPolygonMode(GL_BACK, GL_LINE);
	glPolygonMode(GL_FRONT, GL_FILL);
	
	
}

// void myAxis(void) //격자선 그린다
// {
// 	int	i;
// 	glColor3f(.60, .40, .70); 
// 	glBegin(GL_LINES);
// 	for(i=1; 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();
// }
// 
// 

/*
void myAxis(void)
{
int	i;
glColor3f(1., 1., 1.); 
glutWireCube(size*2.0);
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(0., 0., 0.); 
glBegin(GL_LINES);
glVertex2f(0, -size);
glVertex2f(0, size);
glVertex2f(-size, 0);
glVertex2f(size, 0);
glEnd();
}
*/
void TexturesInit()
{
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
				
	
	glNewList(1, GL_COMPILE);
	texture[0] = auxDIBImageLoad(".\\Image\\Sun.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[0]->sizeX, texture[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[0]->data);
	glEndList();
	
	glNewList(2, GL_COMPILE);
	texture[1] = auxDIBImageLoad(".\\Image\\Mercury.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[1]->sizeX, texture[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[1]->data);
	glEndList();
	
	glNewList(3, GL_COMPILE);
	texture[2] = auxDIBImageLoad(".\\Image\\Venus.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[2]->sizeX, texture[2]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[2]->data);
	glEndList();
	
	glNewList(4, GL_COMPILE);
	texture[3] = auxDIBImageLoad(".\\Image\\earth.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[3]->sizeX, texture[3]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[3]->data);
	glEndList();
	
	glNewList(5, GL_COMPILE);
	texture[4] = auxDIBImageLoad(".\\Image\\Moon.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[4]->sizeX, texture[4]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[4]->data);
	glEndList();
	
	glNewList(6, GL_COMPILE);
	texture[5] = auxDIBImageLoad(".\\Image\\Mars.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[5]->sizeX, texture[5]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[5]->data);
	glEndList();
	
	glNewList(7, GL_COMPILE);
	texture[6] = auxDIBImageLoad(".\\Image\\Jupiter.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[6]->sizeX, texture[6]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[6]->data);
	glEndList();
	
	glNewList(8, GL_COMPILE);
	texture[7] = auxDIBImageLoad(".\\Image\\Saturn.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[7]->sizeX, texture[7]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[7]->data);
	glEndList();
	
	glNewList(9, GL_COMPILE);
	texture[8] = auxDIBImageLoad(".\\Image\\Uranus.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[8]->sizeX, texture[8]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[8]->data);
	glEndList();
	
	glNewList(10, GL_COMPILE);
	texture[9] = auxDIBImageLoad(".\\Image\\Neptune.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[9]->sizeX, texture[9]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[9]->data);
	glEndList();
	
	glNewList(11, GL_COMPILE);
	texture[10] = auxDIBImageLoad(".\\Image\\pluto.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[10]->sizeX, texture[10]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[10]->data);
	glEndList();
	
	
	glNewList(12, GL_COMPILE);
	texture[11] = auxDIBImageLoad(".\\Image\\c.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[11]->sizeX, texture[11]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[11]->data);
	glEndList();
	
	glNewList(13, GL_COMPILE);
	texture[12] = auxDIBImageLoad(".\\Image\\bbb.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[12]->sizeX, texture[12]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[12]->data);
	glEndList();

	glNewList(14, GL_COMPILE);
	texture[13] = auxDIBImageLoad(".\\Image\\personface.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[13]->sizeX, texture[13]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[13]->data);
	glEndList();

	glNewList(15, GL_COMPILE);
	texture[14] = auxDIBImageLoad(".\\Image\\personbody.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[14]->sizeX, texture[14]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[14]->data);
	glEndList();

	glNewList(16, GL_COMPILE);
	texture[15] = auxDIBImageLoad(".\\Image\\personjean.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[15]->sizeX, texture[15]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[15]->data);
	glEndList();

		glNewList(17, GL_COMPILE);
	texture[16] = auxDIBImageLoad(".\\Image\\b3.bmp");
	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture[16]->sizeX, texture[16]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture[16]->data);
	glEndList();
}
///////////////////////시점변환///////////
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);
}

bool InitGL(GLvoid)										
{	
	obj = gluNewQuadric();	
	loadtexture();
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);		
	
	glEnable(GL_NORMALIZE);
	
	glShadeModel(GL_SMOOTH);							
	glClearColor(0.0, 0.0, 0.0, 0.0);				
	glClearDepth(1.0f);									
	glEnable(GL_DEPTH_TEST);						
	glEnable(GL_COLOR_MATERIAL);	
	
	glDepthFunc(GL_LEQUAL);								
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	
	
    gluQuadricNormals(obj, GLU_SMOOTH);
    gluQuadricOrientation(obj, GLU_OUTSIDE);
    gluQuadricTexture(obj, GL_FALSE);
	
	glEnable(GL_LIGHTING);	
	
    glEnable(GL_LIGHT0);
	glEnable(GL_LIGHT1);	
    glEnable(GL_COLOR_MATERIAL);
	TexturesInit();
	glEnable(GL_TEXTURE_2D);
	
	return true;	
}
//////////////////////////사람////////////////////
void myInit()
{
	c=gluNewQuadric(); // 메모리를 할당시킨다. 
	gluQuadricDrawStyle(c, GLU_FILL); //도형의 스타일을 정함
}
////////////////////////////////////////
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 Single_Earth(int numplean)
{
	glLoadIdentity();  
				
	glPushMatrix();
	glLoadIdentity();
	gluLookAt(0.0,0.0,-5,0.0,0.0,0.0,0.0,1.0,0.0);
	
	
	glPushMatrix();
	glRotatef(Self_Earth , 0.0f, 1.0f, 0.0f);
	glBindTexture(GL_TEXTURE_2D,tex[numplean]);
	renderSphere(0.0, 0.0, 0.0, 6.0, 30);	//지구
	glPopMatrix();
	
	glRotatef (Moon_theta, 0.0, 0.0, 1.0);
	glTranslatef(8.5, 0.0, 0.0);
	glBindTexture(GL_TEXTURE_2D,tex[4]);
	renderSphere(0.0, 0.0, 0.0, 1.0, 30);	//달
	glPopMatrix();
	
	
	
	glBegin(GL_LINES);
	glVertex3d(0.0, -55.0, 0.0);
	glVertex3d(0.0, 55.0, 0.0);
	glEnd();
	
	glPopMatrix();
	
}

void Single_Saturn(int numplane)
{
	glLoadIdentity();  
				
	glPushMatrix();
	glLoadIdentity();
	gluLookAt(0.0,0.0,-5,0.0,0.0,0.0,0.0,1.0,0.0);
	
	glPushMatrix();
	glRotatef(Self_Earth , 0.0f, 1.0f, 0.0f);
	glBindTexture(GL_TEXTURE_2D,tex[7]);
	renderSphere(0.0, 0.0, 0.0, 6.0, 30);	//토성
	glPopMatrix();
				glRotatef (Saturn_theta, 0.0, 0.0, 1.0);
				glTranslatef (-0.0, 0.0, 0.0);
				glRotatef(45.0,1.0,0.0,0.0);
				gluDisk(obj,7.3,9.0,40,40);	//토성고리
				glPopMatrix();
				
				
				glBegin(GL_LINES);
				glVertex3d(0.0, -55.0, 0.0);
				glVertex3d(0.0, 55.0, 0.0);
				glEnd();
				glPopMatrix();
				
}

void single_display(int numplane)
{	
	glLoadIdentity();  
				
	glPushMatrix();
		glLoadIdentity();
		gluLookAt(0.0,0.0,-5,0.0,0.0,0.0,0.0,1.0,0.0);
		
		glPushMatrix();
			glRotatef(Self_Earth , 0.0f, 1.0f, 0.0f);
			glEnable(GL_TEXTURE_2D);                   // 매핑을 사용하기 위해 사용
			glBindTexture(GL_TEXTURE_2D, tex[numplane]); // tex[0]이 태양
			renderSphere(0.0, 0.0, 0.0, 6., 30 );      // x, y, z, 반지름, 정밀도
			glDisable(GL_TEXTURE_2D);
		glPopMatrix();
				
			glBegin(GL_LINES);
			glVertex3d(0.0, -55.0, 0.0);
			glVertex3d(0.0, 55.0, 0.0);
			glEnd();
		glPopMatrix();
				
}


void myDisplay(void) 
{
	glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);	
	glEnable(GL_TEXTURE_2D);
	
	glLoadIdentity();
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	//static int temp = scale;
	glRotatef(xTheta, 1.0, 0.0, 0.0);
	glRotatef(yTheta, 0.0, 1.0, 0.0);
	glRotatef(zTheta, 0.0, 0.0, 1.0);
	//glScalef(scale, temp, temp);
	
	glTranslatef(xPos,0.0f,0.0f);
	
	if(numplean==10)
	{
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glColor3f(0.75, 0.75, 0.75); 
		
		
		glPushMatrix(); //1번
			if(!projection)
				gluLookAt(eye[0], eye[1], eye[2], center[0], center[1], center[2], 0, 1, 0);
			
			
			glEnable(GL_TEXTURE_2D);
		
			///////////////////////////////////////////////UFO라인
			 glPushMatrix();
			 	glBindTexture(GL_TEXTURE_2D,tex[16]);
				 glRotatef(ufo,10.0,-90.0,-120.0);	//자전
				
				 glPushMatrix();
					glScalef(0.2,0.2,0.2);
					glTranslatef(80,90,0);
					 glRotatef(120, 1., -1.0, 0.); 
					
				// 위
					 glPushMatrix();
					 glTranslatef(0., 3, 0.);
					 glRotatef(90, 1., 0., 0.);
					 gluCylinder(c, 10, 15,  3, 50, 10);
					// glColor3f(.98f, .04f, .70f);
					 glPopMatrix();

				// 아래
					 glPushMatrix();
					 glTranslatef(0., 0, 0.);
					 glRotatef(90, 1., 0., 0.);
					 gluCylinder(c, 15, 10,   3, 50, 10);
					 //glColor3f(.98f, .04f, .70f);
					 glPopMatrix();
 
					 glFlush();
				 glPopMatrix();
				 //glutSwapBuffers();
		glPopMatrix();
			
	//////////////////////ufo////////////////		
			
			/////////////////////////////외계인//////////////
			
		glPushMatrix();		//2

			//glClear(GL_COLOR_BUFFER_BIT);
			//glLoadIdentity();
			//glRotatef(xTheta, 1.0, 0.0, 0.0);
			//glRotatef(yTheta, 0.0, 1.0, 0.0);
			//glRotatef(zTheta, 0.0, 0.0, 1.0);
			glColor3f(.98f, .04f, .70f);
			
			glScalef(.03, .03, .03);
			
			//머리
			glPushMatrix();
			glBindTexture(GL_TEXTURE_2D,tex[13]);

				glTranslatef(0, 152, 0.);
				glColor3f(.98f, .04f, .70f);
				glutSolidSphere(10 ,10,10);
			glPopMatrix();
			
			glPushMatrix();//2-1
				glTranslatef(0.,130.,0.);
				//몸통
				glPushMatrix();
							glBindTexture(GL_TEXTURE_2D,tex[14]);

					glTranslatef(0.,1, 0.);
					glScalef(1, 1.5, 0.8);	
					glColor3f(.98f, .04f, .70f);
					glutSolidCube(12); 
				glPopMatrix();
				
				glPushMatrix();
					glBindTexture(GL_TEXTURE_2D,tex[15]);	
					//오른쪽 다리
					glPushMatrix();
						glTranslatef(3, -9.5, 0.);
						glutSolidSphere(2 , 10, 10);
						glTranslatef(0., -1.5, 0.);
						glRotatef(90, 1., 0., 0.);
						gluCylinder(c, 2, 2, 16, 12, 10);
						glColor3f(.98f, .04f, .70f);
					glPopMatrix();
				
				//왼쪽 다리
					glPushMatrix();
						glTranslatef(-3, -9.5, 0.);
						glutSolidSphere(2 , 10, 10);
						glTranslatef(0., -1.5, 0.);
						glRotatef(90, 1., 0., 0.);
						gluCylinder(c, 2, 2, 16, 12, 10);
						glColor3f(.98f, .04f, .70f);
					glPopMatrix();
				glPopMatrix();

				glPushMatrix();
					glBindTexture(GL_TEXTURE_2D,tex[13]);	

					//오른팔
					glPushMatrix();
						glRotatef(30, 0., 0., 1.);
						glTranslatef(11, 3, 0.);
						glutSolidSphere(2 , 10, 10);
						glRotatef(90, 1., 0., 0.);
						gluCylinder(c, 2, 2, 15, 12, 10);
						glColor3f(.98f, .04f, .70f);
					glPopMatrix();
					
					//왼팔
					glPushMatrix();
						glRotatef(-30, 0., 0., 1.);
						glTranslatef(-11, 3, 0.);
						glutSolidSphere(2 , 10, 10);
						glRotatef(90, 1., 0., 0.);
						gluCylinder(c, 2, 2, 15, 12, 10);
						glColor3f(.98f, .04f, .70f);
					glPopMatrix();
				glPopMatrix();
			glPopMatrix();//2-1
			glFlush();
		glPopMatrix();//2
			
			
			///////////////////////////////////////////////////////////////
			
			glPushMatrix();
				glBindTexture(GL_TEXTURE_2D,tex[12]);
				glRotatef(Black_Hall,-25.0,-24.0,0.0);	//자전
				renderSphere(-25.0,-24.0,0.0, 3, 20);	//블랙홀
			glPopMatrix();
			
			glPushMatrix();
				glBindTexture(GL_TEXTURE_2D,tex[0]);
				glRotatef(Self_Sun,1.0,1.0,1.0);	//자전
				renderSphere(0.0, 0.0, 0.0, 3, 30);	//태양 x,y,z,반지름 , 정밀도
			glPopMatrix();
			
			glPushMatrix();
				glRotatef (Mecury_theta, 0.0,0.0, 1.0);	//공전
				glTranslatef(4.5, 0.0, 0.0);
				glPushMatrix();
					glRotatef(Self_Mercury,1.0,1.0,1.0);	//자전
					glBindTexture(GL_TEXTURE_2D,tex[1]);
					renderSphere(0.0, 0.0, 0.0, 0.62, 30);	//수성
				glPopMatrix();
			glPopMatrix();
			
			glPushMatrix();
				glRotatef (Venus_theta, 0.0, 0.0, 1.0);	//공전
				glTranslatef(-6.4, 0.0, 0.0);
				glPushMatrix();
					glRotatef(Self_Venus,1.0,1.0,1.0);	//자전
					glBindTexture(GL_TEXTURE_2D,tex[2]);
					renderSphere(0.0, 0.0, 0.0, 0.9, 30);	//금성
				glPopMatrix();//6
			glPopMatrix();//5
			
			glPushMatrix();
				glRotatef (Earth_theta, 0.0, 0.0, 1.0);	//공전
				glTranslatef(9.6, 0.0, 0.0);
				glPushMatrix();
					glRotatef(Self_Earth,1.0,1.0,1.0);	//지구자전처리
					glBindTexture(GL_TEXTURE_2D,tex[3]);
					renderSphere(0.0, 0.0, 0.0, 0.8, 30);	//지구
				glPopMatrix();
				
				glRotatef (Moon_theta, 0.0, 0.0, 1.0);
				glTranslatef(1.5, 0.0, 0.0);
				glBindTexture(GL_TEXTURE_2D,tex[4]);
				renderSphere(0.0, 0.0, 0.0, 0.27, 30);	//달
			glPopMatrix();
				
			glPushMatrix();
				glRotatef (Mars_theta, 0.0, 0.0, 1.0);	//공전
				glTranslatef(-12.3, 0.0, 0.0);
				glPushMatrix();
					glRotatef(Self_Mars,1.0,1.0,1.0);	//자전
					glBindTexture(GL_TEXTURE_2D,tex[5]);
					renderSphere(0.0, 0.0, 0.0, 0.64, 30);	//화성
				glPopMatrix();
			glPopMatrix();
			
			glPushMatrix();
				glRotatef (Jupiter_theta, 0.0, 0.0, 1.0);	//공전
				glTranslatef(15.2, 0.0, 0.0);
				glPushMatrix();
					glRotatef(Self_Jupiter,1.0,1.0,1.0);	//자전
					glBindTexture(GL_TEXTURE_2D,tex[6]);
					renderSphere(0.0, 0.0, 0.0, 1.5, 30);	//목성
				glPopMatrix();
			glPopMatrix();
			
			glPushMatrix();
				glRotatef (Saturn_theta, 0.0, 0.0, 1.0);	//공전
				glTranslatef(-19.6, 0.0, 0.0);
				glPushMatrix();
					glRotatef(Self_Saturn,1.0,1.0,1.0);	//자전
					glBindTexture(GL_TEXTURE_2D,tex[7]);
					renderSphere(0.0, 0.0, 0.0, 1.3, 30);	//토성
				glPopMatrix();
				glRotatef (Saturn_theta, 0.0, 0.0, 1.0);
				glTranslatef (-0.0, 0.0, 0.0);
				glRotatef(45.0,1.0,0.0,0.0);
				gluDisk(obj,1.3,2.0,40,40);	//토성고리
			glPopMatrix();
			
			glPushMatrix();
				glRotatef (Uranus_theta, 0.0, 0.0, 1.0);	//공전
				glTranslatef(22.2, 0.0, 0.0);
				glPushMatrix();
					glRotatef(Self_Uranus,1.0,1.0,1.0);	//자전
					glBindTexture(GL_TEXTURE_2D,tex[8]);
					renderSphere(0.0, 0.0, 0.0, 1.1, 30);	//천왕성
				glPopMatrix();
			glPopMatrix();
			
			glPushMatrix();
				glRotatef (Neptune_theta, 0.0, 0.0, 1.0);	//공전
				glTranslatef(-25.4, 0.0, 0.0);
				glPushMatrix();
					glRotatef(Self_Neptune,1.0,1.0,1.0);	//자전
					glBindTexture(GL_TEXTURE_2D,tex[9]);
					renderSphere(0.0, 0.0, 0.0, 1, 30);	//해왕성
				glPopMatrix();
			glPopMatrix();
			
			glPushMatrix();
				glRotatef(Pluto_theta, 0.0, 0.0, 1.0);	//공전
				glTranslatef(28.6, 0.0, 0.0);
				glPushMatrix();
					glRotatef(Self_Pluto,1.0,1.0,1.0);	//자전
					glBindTexture(GL_TEXTURE_2D,tex[10]);
					renderSphere(0.0, 0.0, 0.0, 0.9, 30);	//명왕성
				glPopMatrix();
			glPopMatrix();
	
			/////////////////혜성//////////////
			// 
			// 	double c, theta, x, y, z;
			// 	//glClear(GL_COLOR_BUFFER_BIT);
			// 	glPushMatrix(); 
			// 	
			// 	//glRotatef(-rotX, 1,0,0);
			// //	glRotatef(-rotY, 0,1,0);
			// //	glRotatef(0,0,0,1);
			// //	glRotatef(0,0,0,-10);
			// 
			// 	glRotatef(self_c,-10.0,-10.0,0.0);
			// 	glTranslatef(20,25, 0);
			// 	//glTranslatef(-27,-22,0);
			// 	
			// 	c = M_PI/180.0; 
			// 	glBegin(GL_TRIANGLE_FAN);
			// 		//glBindTexture(GL_TEXTURE_2D,tex[11]);
			// 	x = 0.0;
			// 	y = 1.0;
			// 	z = 1.0;
			// 	glVertex3d(x+3, y+2, z);
			// 
			// 	for (theta = 0.0; theta <= 360.0; theta += 10.0)
			// 	{
			// 		x = cos(c*theta);
			// 		y = sin(c*theta);
			// 		z = -1; glVertex3d(x, y, z);
			// 	} 
			// 
			// 	glEnd();
			// // 	glBegin(GL_TRIANGLE_FAN); 
			// //	glVertex3d(0,0,-1); 
			// //  	for (theta = 0.0; theta <= 360.0; theta += 10.0) 
			// //  	{
			// //  		x = cos(c*theta);
			// //  		y = sin(c*theta); 
			// //  	z = -1; glVertex3d(x, y, z); 
			// //	} 
	
			glFlush(); 
			//////////////////////////////////배경맵핑
			//왼쪽
			glBindTexture(GL_TEXTURE_2D, maping+1);
			glBegin(GL_QUADS);
			glTexCoord2f(0.0, 0.0); glVertex3f(-size, -size, size);
			glTexCoord2f(1.0, 0.0); glVertex3f(-size, -size, -size);
			glTexCoord2f(1.0, 1.0); glVertex3f(-size, size, -size);
			glTexCoord2f(0.0, 1.0); glVertex3f(-size, size, size);
			//뒷면
			glTexCoord2f(0.0, 0.0); glVertex3f(-size, -size, -size);
			glTexCoord2f(1.0, 0.0); glVertex3f(size, -size, -size);
			glTexCoord2f(1.0, 1.0); glVertex3f(size, size, -size);
			glTexCoord2f(0.0, 1.0); glVertex3f(-size, size, -size);
			//윗면.
			glTexCoord2f(0.0, 0.0); glVertex3f(-size, size, -size);
			glTexCoord2f(1.0, 0.0); glVertex3f(size, size, -size);
			glTexCoord2f(1.0, 1.0); glVertex3f(size, size, size);
			glTexCoord2f(0.0, 1.0); glVertex3f(-size, size, size);
			//밑면
			glTexCoord2f(0.0, 0.0); glVertex3f(-size, -size,-size);
			glTexCoord2f(1.0, 0.0); glVertex3f(-size, -size, size);
			glTexCoord2f(1.0, 1.0); glVertex3f(size, -size, size);
			glTexCoord2f(0.0, 1.0); glVertex3f(size, -size, -size);
			//오른쪽.
			glTexCoord2f(0.0, 0.0); glVertex3f(size, -size, size);
			glTexCoord2f(1.0, 0.0); glVertex3f(size, size, size);
			glTexCoord2f(1.0, 1.0); glVertex3f(size, size, -size);
			glTexCoord2f(0.0, 1.0); glVertex3f(size, -size, -size);
			//앞면.
			glTexCoord2f(0.0, 0.0); glVertex3f(-size, -size, size);
			glTexCoord2f(1.0, 0.0); glVertex3f(-size, size, size);
			glTexCoord2f(1.0, 1.0); glVertex3f(size, size, size);
			glTexCoord2f(0.0, 1.0); glVertex3f(size, -size, size);
			glEnd();
			
			glPushMatrix();
				glTranslatef(-0.01,0.02,center_l);
				glScalef(scale, scale, scale);
			glPopMatrix();
	
			glFlush();
		glPopMatrix(); //1번끝
		
		//	glutSwapBuffers();
	} //if문끝
	
	else if (numplean==0)
	{
		single_display(0);
		
	}
	else if (numplean==1)
	{
		single_display(1);
	}
	else if (numplean==2)
	{
		single_display(2);
	}
	else if (numplean==3)
	{
		Single_Earth(3);
	}
	else if (numplean==4)
	{
		single_display(4);
	}
	else if (numplean==5)
	{
		single_display(6);
	}
	else if (numplean==6)
	{
		Single_Saturn(7);
	}
	else if (numplean==7)
	{
		single_display(8);
	}
	else if (numplean==8)
	{
		single_display(9);
	}
	else if (numplean==9)
	{
		single_display(10);
	}
	//myDraw();
	glFlush();
	glutSwapBuffers();
}
//////////사람/////////////////////
void my_Reshape(int width, int height)
{
	glClearColor (.75, .75, .75, 0.0);
	glViewport(0,0,width,height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-size, size, -size, size, -size, size);
	glMatrixMode(GL_MODELVIEW);
}

///////////////////////////////////

void init()
{
	int u=3; 
	glClearColor (0.0, 0.0, 0.0, 1.0); 
	glColor3f(1.0, 1.0, 1.0); 
	glMatrixMode (GL_PROJECTION); 
	glLoadIdentity ();
	glOrtho(-u, u, -u, u, u, -u);
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
} 
void jmMotion(int x, int y) 
{
	if(bClick) 
	{ 
		newx = x; 
		newy = y; 
		rotX = rotX + newy - oldy;
		rotY = rotY + newx - oldx;
		oldx = newx; oldy = newy; 
	} 
}




void myReshape(int width, int height)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glClearColor (0., 0., 0., 0.0);
	glViewport(0,0,width,height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	//glOrtho(-size, size, -size, size, -size, size); 
	if(projection) {
		glOrtho(-size, size, -size, size, -size, size); 
		eye[2] = 0.;
	}
    else {
		gluPerspective(60., (float)width/height, .01, 1000.);
		eye[2] = 50.;
		myLookAt(0);
	}
    glEnable(GL_DEPTH_TEST);
	//glOrtho(-size, size, -size, size, -size, size);
	glMatrixMode(GL_MODELVIEW);
}

void myIdle(void)//행성들의 공전 속도
{
	Mecury_theta+=0.9;	
	Venus_theta+=0.7;	
	Earth_theta+=0.5;	
	Moon_theta+=0.75;	
	Mars_theta+=0.625;	
	Jupiter_theta+=0.65;
	Saturn_theta+=0.6;
	Uranus_theta+=0.55;
	Neptune_theta+=0.45;
	Pluto_theta+=0.4;
	Black_Hall += 0.04;
	c_theta+=0.5;
//	if(ufo < 15 && ufo > 0)
		ufo += 0.7;
//	else
	//	ufo -= 0.5;
	
	self_c+=0.04;
	Self_Sun+=0.04;	//각행성별 자전주기
	Self_Mercury+=0.0172;
	Self_Venus+=0.004;	
	Self_Earth+=1.0;	
	Self_Mars+=1.1;
	Self_Jupiter+=2.4;
	Self_Saturn+=2.24;
	Self_Uranus+=1.38;
	Self_Neptune+=1.48;
	Self_Pluto+=0.15;
		
	if(ttt)
	{
		
		if(t < 90)			
		{
			if(rei>0.02)
			{
				if(center_l>3)
				{
					center_l=0.65;
					if(center_l>3)
					{
						center_l=0.65;
						
					}
					else
						center_l+=0.05;
					
				}
				else
					center_l+=0.05;
			}
			else
				rei+=0.002;
		}
		
		else {
			t--;
		}
	}
	else 
	{
		t++;
		if(t > 135)
			ttt = true;
	}
	
	
	
	if(scale >= 4)
	{
	}
	else
		scale*=scaleDelta;
	
	//////////////사람//////
	//	 theta+=0.3;
	////////////////////
	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 if(btn==GLUT_RIGHT_BUTTON && 
		state == GLUT_DOWN) {
		mouseState=state;
		mouseButton=btn;
		mouseX=x;
		mouseY=y;
	}
	else if(btn==GLUT_RIGHT_BUTTON && 
		state == GLUT_UP) {
		mouseState=-1;
	}
	else return;
	
	if(idleFlag1 && state==GLUT_DOWN) 
		glutIdleFunc(NULL);
	else if(idleFlag1 && state==GLUT_UP) 
		glutIdleFunc(myIdle);
	glutPostRedisplay();
}

void myMotion(int x, int y)
{
	if(mouseButton == GLUT_LEFT_BUTTON && mouseState == GLUT_DOWN) {
		yTheta -= (mouseX - x)/1.;
		xTheta -= (mouseY - y)/1.;
	}
	//	else if(mouseButton == GLUT_RIGHT_BUTTON && mouseState == GLUT_DOWN) {
	//		if(mouseY!=y) scale = scale * 
	//			pow(scaleDelta, (mouseY - y)/1.);
	//	}
	else return;
	mouseX = x;
	mouseY = y;
	glutPostRedisplay();
}

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 theKey, int x, int y)
{
	switch (theKey) {
	case ' ' : idleFlag1=!idleFlag1;
		
		if(idleFlag1) glutIdleFunc(myIdle);
		else glutIdleFunc(NULL);
		break;
	case 'l':	//라이트닝
		lighting=!lighting;
		if (lighting)glEnable(GL_LIGHTING);
		else glDisable(GL_LIGHTING);
		break;
	case 'z':	//Zoom IN
		scale*=scaleDelta;	
		break;
	case 'x':	//Zoom out
		scale/=scaleDelta;
		break;
	case 27: exit(-1); // esc key
	}
	glutPostRedisplay();
}

void Screen_menu(int value)
{
	numplean=value;
	
	switch(numplean)
	{
	case 1:
		numplean=0;
		MessageBox(hWnd, "반지름(Km) : 695,500\n질량(10*24승 Kg) : 1,989,000\n밀도(g/cm3) : 1.41\n중력(지구=1) : 28\n자전주기 : 25.4일\n공전주기 : -", "태양", MB_OK);	
		break;
	case 2:
		numplean=1;
		glutPostRedisplay();
		MessageBox(hWnd, "반지름(Km) : 2439.7\n질량(10*24승 Kg) : 0.330\n밀도(g/cm3) : 5.43\n중력(지구=1) : 0.38\n자전주기 : 58.6일\n공전주기 : 88.0일", "수성", MB_OK);
		break;
	case 3:
		numplean=2;
		MessageBox(hWnd, "반지름(Km) : 6051.8\n질량(10*24승 Kg) : 4.87\n밀도(g/cm3) : 5.24\n중력(지구=1) : 0.91\n자전주기 : 243일\n공전주기 : 224.7일", "금성", MB_OK);		
		break;
	case 4:
		numplean=3;
		MessageBox(hWnd, "반지름(Km) : 6378.1\n질량(10*24승 Kg) : 5.97\n밀도(g/cm3) : 5.52\n중력(지구=1) : 1.00\n자전주기 : 23.93시간\n공전주기 : 365.24일", "지구", MB_OK);		
		break;
	case 5:
		numplean=4;
		MessageBox(hWnd, "반지름(Km) : 3397\n질량(10*24승 Kg) : 0.642\n밀도(g/cm3) : 3.93\n중력(지구=1) : 0.38\n자전주기 : 24.62시간\n공전주기 : 1.88년", "화성", MB_OK);		
		break;
	case 6:
		numplean=5;
		MessageBox(hWnd, "반지름(Km) : 71492\n질량(10*24승 Kg) : 1899\n밀도(g/cm3) : 1.33\n중력(지구=1) : 2.14\n자전주기 : 9.93시간\n공전주기 : 11.86년", "목성", MB_OK);		
		break;
	case 7:
		numplean=6;
		MessageBox(hWnd, "반지름(Km) : 60268\n질량(10*24승 Kg) : 568\n밀도(g/cm3) : 0.69\n중력(지구=1) : 0.74\n자전주기 : 10.7시간\n공전주기 : 29.4년", "토성", MB_OK);		
		break;
	case 8:
		numplean=7;
		MessageBox(hWnd, "반지름(Km) : 25559\n질량(10*24승 Kg) : 86.8\n밀도(g/cm3) : 1.27\n중력(지구=1) : 0.86\n자전주기 : 17.2시간\n공전주기 : 84.0년", "천왕성", MB_OK);		
		break;
	case 9:
		numplean=8;
		MessageBox(hWnd, "반지름(Km) : 24764\n질량(10*24승 Kg) : 102\n밀도(g/cm3) : 1.64\n중력(지구=1) : 1.10\n자전주기 : 16.1시간\n공전주기 : 164.8년", "해왕성", MB_OK);		
		break;
	case 10:
		numplean=9;
		MessageBox(hWnd, "반지름(Km) : 1151\n질량(10*24승 Kg) : 0.013\n밀도(g/cm3) : 2.0\n중력(지구=1) : 0.08\n자전주기 : 6.4일\n공전주기 : 247.9년", "명왕성", MB_OK);		
		break;
	case 11:
		numplean=10;
	}
	glutPostRedisplay();
}

void main(int argc, char** argv)
{
	myInit();
	glutInit(&argc,argv);
	//InitGL();
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(900,800);
	glutInitWindowPosition(0,0);
	glutCreateWindow("Solar System");
	glutReshapeFunc(myReshape);
	glutDisplayFunc(myDisplay); 
	glutMouseFunc(myMouse);
	glutMotionFunc(myMotion);
    glutSpecialFunc(mySKeyboard);
	glutKeyboardFunc(myKeyboard);
	GLint MyMainmenu=glutCreateMenu(Screen_menu);
	glutAddMenuEntry("Sun",1);
	glutAddMenuEntry("Mecury",2);
	glutAddMenuEntry("Venus",3);
	glutAddMenuEntry("Earth",4);
	glutAddMenuEntry("Mars",5);
	glutAddMenuEntry("Jupiter",6);
	glutAddMenuEntry("Saturn",7);
	glutAddMenuEntry("Uranus",8);
	glutAddMenuEntry("Neptune",9);
	glutAddMenuEntry("Pluto",10);
	glutAddMenuEntry("Space Planet",11);
	glutAttachMenu(GLUT_RIGHT_BUTTON);
	
	InitGL();
	
	glutMainLoop();
}
