[GL]行星运行2——功能实现

1.项目描述
【[GL]行星运行2——功能实现】实现太阳系的模拟,包括天体的公转和自转。
2. 实现思路

3. 功能实现
打开VS2010,新建C++ Win32控制台空项目;新建Plant.cpp文件;添加文件夹Data,将位图文件拷贝到文件夹中并添加到Data中。
工程属性->配置(设为‘所有配置’)->配置属性->C/C++->预处理器定义(设为GLUT_BUILDING_LIB)
链接器->输入->附加依赖项中添加glu32.lib; glut32.lib; glut.lib; GLaux.lib; opengl32.lib
添加Plant.cpp代码:

1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include "gl/glaux.h" 8 #define PI3.141592654 9 #define DEG2RAD(angle)angle*(PI/180) 10 11 static GLdouble viewPoint[]= {1.6,8.5,-0.3}; 12 static double Eyear = 0,Vyear=0,Meryear=0,Maryear=0, Jyear=0,Syear=0,Uyear=0,Nyear=0, 13month=0, 14Eday = 0,Merday=0,Vday=360,Marday=0,Jday=0,Sday=0,Uday=0,Nday=0, 15angle,r; 16 static double MerDSpeed=0.72,VDSpeed=0.15,EDSpeed=36.0,MarDSpeed=35.0,JDSpeed=90.0,SDSpeed=81.0,UDSpeed=49.6,NDSpeed=53.6, 17monSpeed=12, 18MerYSpeed=4,VYSpeed=1.6,EYSpeed=1,MarYSpeed=0.5,JYSpeed=0.09,SYSpeed=0.03,UYSpeed=0.01,NYSpeed=0.006; 19 GLuint texture[10]; 20 21 22 void AutoRun(void) 23 {/*自转控制*/ 24Merday += MerDSpeed; 25if( Merday > 360.0 ) Merday -= 360.0; 26Vday-=VDSpeed; 27if( Vday <= 0 ) Vday += 360.0; // 金星是逆向自转 28Eday += EDSpeed; 29if( Eday > 360.0 ) Eday -= 360.0; 30Marday+=MarDSpeed; 31if( Marday > 360.0 ) Marday -= 360.0; 32Jday+=JDSpeed; 33if( Jday > 360.0 ) Jday -= 360.0; 34Sday+=SDSpeed; 35if( Sday > 360.0 ) Sday -= 360.0; 36Uday+=UDSpeed; 37if( Uday > 360.0 ) Uday -= 360.0; 38Nday+=NDSpeed; 39if( Nday > 360.0 ) Nday -= 360.0; 40/*公转控制*/ 41Meryear+=MerYSpeed; 42if( Meryear > 360.0 ) Meryear -= 360.0; 43Vyear += VYSpeed; 44if( Vyear > 360.0 ) Vyear -= 360.0; 45Eyear += EYSpeed; 46if( Eyear > 360.0 ) Eyear -= 360.0; 47Maryear += MarYSpeed; 48if( Maryear > 360.0 ) Maryear -= 360.0; 49Jyear += JYSpeed; 50if( Jyear > 360.0 ) Jyear -= 360.0; 51Syear += SYSpeed; 52if( Syear > 360.0 ) Syear -= 360.0; 53Uyear += UYSpeed; 54if( Uyear > 360.0 ) Uyear -= 360.0; 55Nyear += NYSpeed; 56if( Nyear > 360.0 ) Nyear -= 360.0; 57 58month += monSpeed; 59if( month > 360.0 ) month -= 360.0; 60glutPostRedisplay(); 61 } 62 63 /*绘制球体*/ 64 void gltDrawSphere(GLfloat fRadius, GLint iSlices, GLint iStacks) 65{ 66GLfloat drho = (GLfloat)(3.141592653589) / (GLfloat) iStacks; 67GLfloat dtheta = 2.0f * (GLfloat)(3.141592653589) / (GLfloat) iSlices; 68GLfloat ds = 1.0f / (GLfloat) iSlices; 69GLfloat dt = 1.0f / (GLfloat) iStacks; 70GLfloat t = 1.0f; 71GLfloat s = 0.0f; 72GLint i, j; 73 74for (i = 0; i < iStacks; i++) 75{ 76GLfloat rho = (GLfloat)i * drho; 77GLfloat srho = (GLfloat)(sin(rho)); 78GLfloat crho = (GLfloat)(cos(rho)); 79GLfloat srhodrho = (GLfloat)(sin(rho + drho)); 80GLfloat crhodrho = (GLfloat)(cos(rho + drho)); 81 82glBegin(GL_TRIANGLE_STRIP); 83s = 0.0f; 84for ( j = 0; j <= iSlices; j++) 85{ 86GLfloat theta = (j == iSlices) ? 0.0f : j * dtheta; 87GLfloat stheta = (GLfloat)(-sin(theta)); 88GLfloat ctheta = (GLfloat)(cos(theta)); 89 90GLfloat x = stheta * srho; 91GLfloat y = ctheta * srho; 92GLfloat z = crho; 93 94glTexCoord2f(s, t); 95glNormal3f(x, y, z); 96glVertex3f(x * fRadius, y * fRadius, z * fRadius); 97 98x = stheta * srhodrho; 99y = ctheta * srhodrho; 100z = crhodrho; 101glTexCoord2f(s, t - dt); 102s += ds; 103glNormal3f(x, y, z); 104glVertex3f(x * fRadius, y * fRadius, z * fRadius); 105} 106glEnd(); 107 108t -= dt; 109} 110} 111 112 /*从文件获取BMP图片*/ 113 AUX_RGBImageRec *LoadBMP(char *Filename)// 载入位图图象 114 { 115FILE *File=NULL; // 文件句柄 116if (!Filename)// 确保文件名已提供 117{ 118return NULL; //如果没提供,返回 NULL 119 120} 121File=fopen(Filename,"r"); //尝试打开文件 122if (File)// 文件存在与否 123{ 124fclose(File); // 关闭句柄 125return auxDIBImageLoad(Filename); //载入位图并返回指针 126} 127return NULL; // 如果载入失败,返回 NULL 128 } 129 int LoadGLTextures()// 载入位图并转换成纹理 130 { 131int Status=FALSE; // Status 状态指示器 132int i; 133AUX_RGBImageRec *TextureImage[10]; // 创建纹理的存储空间 134char bmp[10][30]={ 135"Data/Sun.bmp", 136"Data/Mercury.bmp", 137"Data/Venus.bmp", 138"Data/earth.bmp", 139"Data/moon.bmp", 140"Data/Mars.bmp", 141"Data/Jutiper.bmp", 142"Data/Saturn.bmp", 143"Data/Uranus.bmp", 144"Data/Neptune.bmp", 145 146}; 147TextureImage[0]= LoadBMP(bmp[0]); 148TextureImage[1]=LoadBMP(bmp[1]); 149TextureImage[2]=LoadBMP(bmp[2]); 150TextureImage[3]=LoadBMP(bmp[3]); 151TextureImage[4]=LoadBMP(bmp[4]); 152TextureImage[5]=LoadBMP(bmp[5]); 153TextureImage[6]=LoadBMP(bmp[6]); 154TextureImage[7]=LoadBMP(bmp[7]); 155TextureImage[8]=LoadBMP(bmp[8]); 156TextureImage[9]=LoadBMP(bmp[9]); 157for(i=0; i<10; i++) 158{ 159memset(TextureImage[i],0,sizeof(void *)*1); // 将指针设置为Null 160/* 载入BMP图片,并查错*/ 161if (TextureImage[i]=LoadBMP(bmp[i])) 162{ 163Status=TRUE; // 将状态设置为TRUE 164glGenTextures(1, &texture[i]); // 创建纹理 165glBindTexture(GL_TEXTURE_2D, texture[i]); // 根据不同Bitmap图片的数据产生不同的纹理 166/*定义纹理*/ 167glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[i]->sizeX, TextureImage[i]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[i]->data); 168/* 控制滤波*/ 169glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 170glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 171glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 172glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 173} 174if (TextureImage[i])// 判断纹理是否存在 175{ 176if (TextureImage[i]->data)// 判断纹理图像是否存在 177{ 178free(TextureImage[i]->data); // 释放纹理图像占用的内存 179} 180free(TextureImage[i]); // 释放图像结构 181} 182} 183 184return Status; // 返回状态 185 } 186 187 void display(void) 188 {/*材质参数*/ 189GLfloat sun_mat_ambient[4]={1.0,1.0,1.0,0.0}; 190GLfloat sun_mat_diffuse[4]={1.0,1.0,0.5,1.0}; 191GLfloat sun_mat_specular[4]={1.0,1.0,1.0,1.0}; 192GLfloat sun_mat_shininess[]={10.0}; 193GLfloat sun_mat_emission[4]={0.1,0.1,0.1,1.0}; 194 195GLfloat mat_ambient[4]={0.2,0.2,0.2,1.0}; 196GLfloat mat_diffuse[4]={1.0,1.0,1.0,1.0}; 197GLfloat mat_specular[4]={0.5,0.5,0.5,1.0}; 198GLfloat mat_shininess[]={5.0}; 199 200GLfloat light_position[] = { 0.0, 0.0, 0.0, 1.0 }; // 光源位置设置 201 202glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清空屏幕和深度缓存 203glLoadIdentity(); // 重置当前的模型观察矩阵 204 205gluLookAt(0,viewPoint[0],viewPoint[1],0,0,0,viewPoint[2],1,0); // 观察点设置 206 207/* draw sun */ 208glPushMatrix(); 209glLightfv(GL_LIGHT0, GL_POSITION, light_position); // 创建光源 210glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,sun_mat_ambient); // 材质设定 211glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,sun_mat_diffuse); 212glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,sun_mat_specular); 213glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,sun_mat_shininess); 214glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,sun_mat_emission); 215glBindTexture(GL_TEXTURE_2D, texture[0]); // 绑定纹理 216glRotatef((GLfloat) Eyear, 0.0, 1.0, 0.0); 217glRotatef(90,1,0.0,0.0); 218gltDrawSphere(1.0,200,200); //绘制球体 219glPopMatrix(); 220 221 /* draw Mercury */ 222glPushMatrix(); 223glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient); // 材质设定 224glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse); 225glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular); 226glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess); 227glBindTexture(GL_TEXTURE_2D, texture[1]); // 绑定纹理 228glRotatef((GLfloat) Meryear, 0.0, 1.0, 0.0); //公转 229glTranslatef (0.0, 0.0, 1.8); //与太阳的距离 230glRotatef((GLfloat) Merday, 0.0, 1.0, 0.0); //自转 231glRotatef(90,1.0,0.0,0.0); 232gltDrawSphere(0.2,200,200); 233glPopMatrix(); 234 235 /*画水星轨道*/ 236glDisable(GL_TEXTURE_2D); 237glBegin(GL_LINE_LOOP); 238glColor3f(1.0,1.0,1.0); 239for(angle=0; angle<=360; angle++) 240glVertex3f(1.8*sin(DEG2RAD(angle)),0,1.8*cos(DEG2RAD(angle))); 241glEnd(); 242glEnable(GL_TEXTURE_2D); 243 244 /* draw Venus */ 245glPushMatrix(); 246glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient); // 材质设定 247glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse); 248glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular); 249glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess); 250glBindTexture(GL_TEXTURE_2D, texture[2]); // 绑定纹理 251glRotatef((GLfloat) Vyear, 0.0, 1.0, 0.0); 252glTranslatef (0.0, 0.0, 2.5); //与太阳的距离 253glRotatef((GLfloat) Vday, 0.0, 1.0, 0.0); 254glRotatef(90,1.0,0.0,0.0); 255gltDrawSphere(0.22,200,200); 256glPopMatrix(); 257 258 /*画金星轨道*/ 259glDisable(GL_TEXTURE_2D); 260glBegin(GL_LINE_LOOP); 261glColor3f(1.0,1.0,1.0); 262for(angle=0; angle<=360; angle++) 263glVertex3f(2.5*sin(DEG2RAD(angle)),0,2.5*cos(DEG2RAD(angle))); 264glEnd(); 265glEnable(GL_TEXTURE_2D); 266 267 /* draw Earth */ 268glPushMatrix(); 269glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient); // 材质设定 270glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse); 271glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular); 272glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess); 273glBindTexture(GL_TEXTURE_2D, texture[3]); // 绑定纹理 274glRotatef((GLfloat) Eyear, 0.0, 1.0, 0.0); 275glTranslatef (0.0, 0.0, 3.2); //与太阳的距离 276glRotatef((GLfloat) Eday, 0.0, 1.0, 0.0); 277glRotatef(90,1.0,0.0,0.0); 278gltDrawSphere(0.2,200,200); 279glPopMatrix(); 280 281 /*画地球轨道*/ 282glDisable(GL_TEXTURE_2D); 283glBegin(GL_LINE_LOOP); 284glColor3f(1.0,1.0,1.0); 285for(angle=0; angle<=360; angle++) 286glVertex3f(3.2*sin(DEG2RAD(angle)),0,3.2*cos(DEG2RAD(angle))); 287glEnd(); 288glEnable(GL_TEXTURE_2D); 289 290 /* draw moon */ 291glPushMatrix(); 292glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient); // 材质设定 293glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse); 294glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular); 295glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess); 296glBindTexture(GL_TEXTURE_2D, texture[4]); // 绑定纹理 297glRotatef((GLfloat) Eyear, 0.0, 1.0, 0.0); 298glTranslatef (0.0, 0.0, 3.2); //与太阳的距离 299glRotatef((GLfloat) month, 0.0, 1.0, 0.0); 300glTranslatef (0.0, 0.0, 0.3); //与地球的距离 301glRotatef((GLfloat) Eday, 0.0, 1.0, 0.0); 302glRotatef(90,1.0,0.0,0.0); 303glColor3f (1.0, 1.0, 1.0); 304gltDrawSphere(0.06,100,100); 305glPopMatrix(); 306 307 /* draw Mars */ 308glPushMatrix(); 309glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient); // 材质设定 310glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse); 311glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular); 312glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess); 313glBindTexture(GL_TEXTURE_2D, texture[5]); // 绑定纹理 314glRotatef((GLfloat)Maryear, 0.0, 1.0, 0.0); 315glTranslatef (0.0, 0.0, 3.9); //与太阳的距离 316glRotatef((GLfloat) Marday, 0.0, 1.0, 0.0); 317glRotatef(90,1.0,0.0,0.0); 318gltDrawSphere(0.15,200,200); 319glPopMatrix(); 320 321 /*画火星轨道*/ 322glDisable(GL_TEXTURE_2D); 323glBegin(GL_LINE_LOOP); 324glColor3f(1.0,1.0,1.0); 325for(angle=0; angle<=360; angle++) 326glVertex3f(3.9*sin(DEG2RAD(angle)),0,3.9*cos(DEG2RAD(angle))); 327glEnd(); 328glEnable(GL_TEXTURE_2D); 329 330 /* draw Jupiter*/ 331glPushMatrix(); 332glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient); // 材质设定 333glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse); 334glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular); 335glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess); 336glBindTexture(GL_TEXTURE_2D, texture[6]); // 绑定纹理 337glRotatef((GLfloat)Jyear, 0.0, 1.0, 0.0); 338glTranslatef (0.0, 0.0, 4.5); //与太阳的距离 339glRotatef((GLfloat) Jday, 0.0, 1.0, 0.0); 340glRotatef(90,1.0,0.0,0.0); 341gltDrawSphere(0.3,200,200); 342glPopMatrix(); 343 344 /*画木星轨道*/ 345glDisable(GL_TEXTURE_2D); 346glBegin(GL_LINE_LOOP); 347glColor3f(1.0,1.0,1.0); 348for(angle=0; angle<=360; angle++) 349glVertex3f(4.5*sin(DEG2RAD(angle)),0,4.5*cos(DEG2RAD(angle))); 350glEnd(); 351glEnable(GL_TEXTURE_2D); 352 353 /* draw Saturn*/ 354glPushMatrix(); 355glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient); // 材质设定 356glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse); 357glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular); 358glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess); 359glBindTexture(GL_TEXTURE_2D, texture[7]); // 绑定纹理 360glRotatef((GLfloat)Syear, 0.0, 1.0, 0.0); 361glTranslatef (0.0, 0.0, 5.5); //与太阳的距离 362glRotatef((GLfloat) Sday, 0.0, 1.0, 0.0); 363glRotatef(90,1.0,0.0,0.0); 364gltDrawSphere(0.26,200,200); 365glPopMatrix(); 366 367 /*画土星的光环*/ 368glPushMatrix(); 369glRotatef((GLfloat)Syear, 0.0, 1.0, 0.0); 370glTranslatef (0.0, 0.0, 5.5); 371glBegin(GL_LINE_LOOP); 372for(r=0.32; r<=0.4; r+=0.006) 373for(angle=0; angle<=360; angle++) 374glVertex3f(r*sin(DEG2RAD(angle)),0,r*cos(DEG2RAD(angle))); 375glEnd(); 376glPopMatrix(); 377 378 /*画土星轨道*/ 379glDisable(GL_TEXTURE_2D); 380glBegin(GL_LINE_LOOP); 381glColor3f(1.0,1.0,1.0); 382for(angle=0; angle<=360; angle++) 383glVertex3f(5.5*sin(DEG2RAD(angle)),0,5.5*cos(DEG2RAD(angle))); 384glEnd(); 385glEnable(GL_TEXTURE_2D); 386 387 /* draw Uranus*/ 388glPushMatrix(); 389glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient); // 材质设定 390glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse); 391glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular); 392glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess); 393glBindTexture(GL_TEXTURE_2D, texture[8]); // 绑定纹理 394glRotatef((GLfloat)Uyear, 0.0, 1.0, 0.0); 395glTranslatef (0.0, 0.0, 6.3); //与太阳的距离 396glRotatef((GLfloat) Uday, 0.0, 1.0, 0.0); 397glRotatef(90,1.0,0.0,0.0); 398gltDrawSphere(0.24,200,200); 399glPopMatrix(); 400 401 /*画天王星轨道*/ 402glDisable(GL_TEXTURE_2D); 403glBegin(GL_LINE_LOOP); 404glColor3f(1.0,1.0,1.0); 405for(angle=0; angle<=360; angle++) 406glVertex3f(6.3*sin(DEG2RAD(angle)),0,6.3*cos(DEG2RAD(angle))); 407glEnd(); 408glEnable(GL_TEXTURE_2D); 409 410 /* draw Neptune*/ 411glPushMatrix(); 412glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient); // 材质设定 413glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse); 414glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular); 415glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess); 416glBindTexture(GL_TEXTURE_2D, texture[9]); // 绑定纹理 417glRotatef((GLfloat)Nyear, 0.0, 1.0, 0.0); 418glTranslatef (0.0, 0.0, 7); //与太阳的距离 419glRotatef((GLfloat) Nday, 0.0, 1.0, 0.0); 420glRotatef(90,1.0,0.0,0.0); 421gltDrawSphere(0.23,200,200); 422glPopMatrix(); 423 424 /*画海王星轨道*/ 425glDisable(GL_TEXTURE_2D); 426glBegin(GL_LINE_LOOP); 427glColor3f(1.0,1.0,1.0); 428for(angle=0; angle<=360; angle++) 429glVertex3f(7*sin(DEG2RAD(angle)),0,7*cos(DEG2RAD(angle))); 430glEnd(); 431glEnable(GL_TEXTURE_2D); 432 433glutSwapBuffers(); 434glFlush(); 435 } 436 437 void myinit(void) 438 { 439 440GLfloat light_ambient[]={1.0, 1.0, 1.0, 1.0}; //环境光 441GLfloat light_diffuse[]={1.0, 1.0, 1.0, 1.0}; //漫反射光 442GLfloat light_specular[]={1.0, 1.0, 1.0, 1.0}; //镜面反射光 443 444//glClearColor(0.5f, 0.5f, 0.5f, 1.0f); //设置背景颜色 445glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); 446glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); 447glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); 448 449if (LoadGLTextures())// 调用纹理载入子例程 450glEnable(GL_LIGHTING); // 开启光照效果 451glEnable(GL_LIGHT0); 452glEnable(GL_TEXTURE_2D); //启动纹理映射 453glShadeModel(GL_SMOOTH); // 启用阴影平滑 454 455glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // 说明映射方式 456glDepthFunc(GL_LESS); 457glClearDepth(1.0f); // 深度缓存创建 458glEnable(GL_DEPTH_TEST); // 开启深度测试 459glDepthFunc(GL_LEQUAL); 460 461 } 462 463 464 void myReshape(GLsizei w, GLsizei h) 465 { 466glViewport(0, 0, w, h); 467glMatrixMode(GL_PROJECTION); 468glLoadIdentity(); 469gluPerspective(800.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); 470glMatrixMode(GL_MODELVIEW); 471glLoadIdentity(); 472glTranslatef (0.0, 0.0, -5.0); 473 } 474 475 void View_SpeedControl(unsigned char key ,int x,int y) 476 { 477switch(key) 478{ 479 480case'z': 481viewPoint[0]+= 0.1; 482break; 483 484case'x': 485viewPoint[0]-= 0.1; 486break; 487 488case'c': 489viewPoint[1]+=0.1; 490break; 491 492case'v': 493viewPoint[1]-=0.1; 494break; 495 496case'b': 497viewPoint[2]+=0.1; 498break; 499 500case'n': 501viewPoint[2]-=0.1; 502break; 503 504case'=': 505{MerDSpeed*=1.5,VDSpeed*=1.5,EDSpeed*=1.5,MarDSpeed*=1.5,JDSpeed*=1.5,SDSpeed*=1.5,UDSpeed*=1.5,NDSpeed*=1.5, 506monSpeed*=2, 507MerYSpeed*=2,VYSpeed*=2,EYSpeed*=2,MarYSpeed*=2,JYSpeed*=2,SYSpeed*=2,UYSpeed*=2,NYSpeed*=2; }; 508break; 509case'-': 510{MerDSpeed/=1.5,VDSpeed/=1.5,EDSpeed/=1.5,MarDSpeed/=1.5,JDSpeed/=1.5,SDSpeed/=1.5,UDSpeed/=1.5,NDSpeed/=1.5, 511monSpeed/=1.5, 512MerYSpeed/=2,VYSpeed/=2,EYSpeed/=2,MarYSpeed/=2,JYSpeed/=2,SYSpeed/=2,UYSpeed/=2,NYSpeed/=2; }; 513break; 514 515default: 516break; 517 518} 519display(); 520 } 521 int main(int argc, char** argv) 522 { 523glutInit(&argc,argv); 524glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB | GLUT_DEPTH); 525glutInitWindowSize(800,600); 526glutCreateWindow("solar system"); 527myinit(); 528glutDisplayFunc(display); 529glutReshapeFunc(myReshape); 530glutKeyboardFunc(View_SpeedControl); 531glutIdleFunc(AutoRun); 532glutMainLoop(); 533 534 }

运行结果:
[GL]行星运行2——功能实现
文章图片

转载于:https://www.cnblogs.com/yhlx125/archive/2013/03/26/2981880.html

    推荐阅读