OpenGL + vc 6.0 콘 솔 '태양계 모델' 구현

8361 단어 OpenGL
OpenGL + vc 6.0 콘 솔 '태양계 모델' 구현
 
#define BITMAP_ID 0x4D42
#define PI 3.1415926

#include 
#include 
#include 
#include 
#include 

#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )

//----------       
typedef struct{
	int width;				//    
	int height;				//    
	unsigned int texID;		//     ID
	unsigned char * data;	//      
}texture;


static float year = 0, month =0,day = 0, angle=30;
static bool first=false;

texture * sun,* earth,* moon;			//    

//----------           
unsigned char * LoadBmpFile(char * filename,BITMAPINFOHEADER * bmpInfoHeader){
	
	FILE * file;
	BITMAPFILEHEADER bmpFileHeader;
	unsigned char * image;
	unsigned int imageIdx =0;
	unsigned char tempRGB;
	
	file = fopen(filename,"rb");		
	if(file == NULL)
		return 0;
	
	fread(& bmpFileHeader,sizeof(BITMAPFILEHEADER),1,file);			//    BMP    
	
	if (bmpFileHeader.bfType != BITMAP_ID)							//         BMP   
	{
		fclose(file);
		return 0;
	}
	
	fread(bmpInfoHeader,sizeof(BITMAPINFOHEADER),1,file);			//       
	fseek(file,bmpFileHeader.bfOffBits,SEEK_SET);					//                
	image = (unsigned char * )malloc(bmpInfoHeader->biSizeImage);	//          
	
	if (! image)
	{
		free(image);
		fclose(file);
		return 0;
	}
	
	
	fread(image,1,bmpInfoHeader->biSizeImage,file);					//       
	
	if (image == NULL)
	{
		fclose(file);
		return 0;	
	}
	
	//    R   B      RGB,          BGR
	for (imageIdx = 0;imageIdx < bmpInfoHeader->biSizeImage;imageIdx += 3)
	{
		tempRGB = image[imageIdx];
		image[imageIdx] = image[imageIdx + 2];
		image[imageIdx + 2] = tempRGB;
	}
	
	fclose(file);
	return image;
	
}

//----------       
texture * LoadTexFile(char * filename){
	
	BITMAPINFOHEADER texInfo;
	texture * thisTexture;
	
	thisTexture = (texture * )malloc(sizeof(texture));
	if(thisTexture == NULL)
		return 0;
	
	thisTexture->data = LoadBmpFile(filename,&texInfo);	//             
	if (thisTexture->data == NULL)
	{
		free(thisTexture);
		return 0;
	}

	thisTexture->width = texInfo.biWidth;				//         
	thisTexture->height = texInfo.biHeight;
	
	glGenTextures(1,&thisTexture->texID);				//        
	
	return thisTexture;
}

//----------             
BOOL LoadAllTextures(){

	sun = LoadTexFile("sun.bmp");				
	if(sun == NULL)
		return FALSE;
	
	glBindTexture(GL_TEXTURE_2D,sun->texID);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
	gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA,sun->width,sun->height,GL_RGB,GL_UNSIGNED_BYTE,sun->data);


	earth = LoadTexFile("earth.bmp");			
	if(earth == NULL)
		return FALSE;
	
	glBindTexture(GL_TEXTURE_2D,earth->texID);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
	gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA,earth->width,earth->height,GL_RGB,GL_UNSIGNED_BYTE,earth->data);


	moon = LoadTexFile("moon.bmp");			
	if(moon == NULL)
		return FALSE;
	
	glBindTexture(GL_TEXTURE_2D,moon->texID);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
	gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA,moon->width,moon->height,GL_RGB,GL_UNSIGNED_BYTE,moon->data);

	return TRUE;
}

void gltDrawSphere(GLfloat fRadius, GLint iSlices, GLint iStacks)
{
    GLfloat drho = (GLfloat)(3.141592653589) / (GLfloat) iStacks;
    GLfloat dtheta = 2.0f * (GLfloat)(3.141592653589) / (GLfloat) iSlices;
	GLfloat ds = 1.0f / (GLfloat) iSlices;
	GLfloat dt = 1.0f / (GLfloat) iStacks;
	GLfloat t = 1.0f;	
	GLfloat s = 0.0f;
    GLint i, j;     
	
	for (i = 0; i < iStacks; i++) 
	{
		GLfloat rho = (GLfloat)i * drho;
		GLfloat srho = (GLfloat)(sin(rho));
		GLfloat crho = (GLfloat)(cos(rho));
		GLfloat srhodrho = (GLfloat)(sin(rho + drho));
		GLfloat crhodrho = (GLfloat)(cos(rho + drho));
		
		
		glBegin(GL_TRIANGLE_STRIP);
        s = 0.0f;
		for ( j = 0; j <= iSlices; j++) 
		{
			GLfloat theta = (j == iSlices) ? 0.0f : j * dtheta;
			GLfloat stheta = (GLfloat)(-sin(theta));
			GLfloat ctheta = (GLfloat)(cos(theta));
			
			GLfloat x = stheta * srho;
			GLfloat y = ctheta * srho;
			GLfloat z = crho;
            
            glTexCoord2f(s, t);
            glNormal3f(x, y, z);
            glVertex3f(x * fRadius, y * fRadius, z * fRadius);
			
            x = stheta * srhodrho;
			y = ctheta * srhodrho;
			z = crhodrho;
			glTexCoord2f(s, t - dt);
            s += ds;
            glNormal3f(x, y, z);
            glVertex3f(x * fRadius, y * fRadius, z * fRadius);
		}
        glEnd();
		
        t -= dt;
	}
}



void myinit(void) 
{
	glClearColor (0.0, 0.0, 0.0, 0.0);

	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);	//      

	GLfloat light0_ambient[]  = {1, 1, 1, 1};	//   
	GLfloat light0_diffuse[]  = {1, 1, 1, 1};	//   
	GLfloat light0_position[] = {0, 0, 0, 1};	//    

	glLightfv(GL_LIGHT0,GL_AMBIENT,light0_ambient);
	glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_diffuse);
	glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
	
	LoadAllTextures();			//    
}

void myidle()
{
	day+=angle;
	glutPostRedisplay();

}

void mymouse(int button,int state,int x,int y)
{	
	if(state==GLUT_DOWN && button==GLUT_LEFT_BUTTON){
		if(first)
		{
			glutIdleFunc(myidle);
			first=!first;
		}
		else
		{
			glutIdleFunc(0);
			first=!first;
		}
	}	
}

void mykeyboard(unsigned char key, int x, int y) 
{
	if (key == 27)         
		exit(0); 
} 

void mydisplay(void)
{
	year=day/365;
	month=day/30;

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	GLfloat mat_ambient1[]  = {1,0,0,1};
	GLfloat mat_emission[]  = {1,1,1,0};

	GLfloat mat_ambient2[]  = {0.4,0.4,0.8,1};
	GLfloat  no_emission[]  = {0,0,0,1};

	glPushMatrix();	
		glBindTexture(GL_TEXTURE_2D,sun->texID);
		glRotatef (month, 0.0, 1.0, 0.0);				//    
		glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient1);
		glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
		glRotatef (90, -1, 0, 0);
		gltDrawSphere(1.0, 40, 40);			//    
	glPopMatrix();

	glPushMatrix();
		glRotatef (year, 0.0, 1.0, 0.0);				//           (    )
		glPushMatrix();
			glBindTexture(GL_TEXTURE_2D,earth->texID);
			glTranslatef (3.0, 0.0, 0.0);
			glRotatef (month, 0.0, 1.0, 0.0);			//     
			glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient2);
			glMaterialfv(GL_FRONT, GL_EMISSION, no_emission);
			glRotatef (90, -1, 0, 0);
			gltDrawSphere(0.4, 40, 40);		//    	
		glPopMatrix();

		glPushMatrix();
			glBindTexture(GL_TEXTURE_2D,moon->texID);
			glTranslatef (3.0, 0.0, 0.0);
			glRotatef (60, -1, 1, 0);
			glRotatef (month, 0, 1.0, 0);				//      
			glTranslatef (0.6, 0.0, 0.0);
			glRotatef (90, -1, 0, 0);
			gltDrawSphere(0.07, 20, 20);	//    
		glPopMatrix();
	glPopMatrix();

	glFlush();
	glutSwapBuffers();   
}

void myreshape (int w, int h)
{
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);				//      
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
		
	gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1, 20);		//    
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
		
	gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);	//        	
}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize (1000, 600); 
	glutInitWindowPosition (100, 100);
	glutCreateWindow (argv[0]);
	myinit ();

	glutDisplayFunc(mydisplay); 
	glutReshapeFunc(myreshape);
	glutIdleFunc(myidle);
	glutMouseFunc(mymouse);
	glutKeyboardFunc(mykeyboard);

	glutMainLoop();
	return 0;
}


 
 
설명: 상기 코드 는 vc 콘 솔 에서 컴 파일 되 어 실 행 됩 니 다.
이 링크 는 이 프로그램의 소스 코드 파일 (이 프로그램 에 필요 한 bmp 그림 3 개 및 exe 파일 포함)
http://download.csdn.net/detail/shen_gan/4279261
 
다음은 실행 효과 그림:

좋은 웹페이지 즐겨찾기