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

1.项目描述

  实现太阳系的模拟,包括天体的公转和自转。

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 <windows.h>

  2 #include <stdio.h>

  3 #include <math.h>

  4 #include <GL/gl.h>

  5 #include <GL/glu.h>

  6 #include <GL/glut.h>

  7 #include "gl/glaux.h"

  8 #define PI                3.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,

 13               month=0,

 14               Eday = 0,Merday=0,Vday=360,Marday=0,Jday=0,Sday=0,Uday=0,Nday=0,

 15               angle,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,

 17            monSpeed=12,

 18            MerYSpeed=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 {   /*自转控制*/

 24     Merday += MerDSpeed;

 25     if( Merday > 360.0 ) Merday -= 360.0;

 26     Vday-=VDSpeed;

 27     if( Vday <= 0 ) Vday += 360.0;    // 金星是逆向自转

 28     Eday += EDSpeed;

 29     if( Eday > 360.0 ) Eday -= 360.0;

 30     Marday+=MarDSpeed;

 31     if( Marday > 360.0 ) Marday -= 360.0;

 32     Jday+=JDSpeed;

 33     if( Jday > 360.0 ) Jday -= 360.0;

 34     Sday+=SDSpeed;

 35     if( Sday > 360.0 ) Sday -= 360.0;

 36     Uday+=UDSpeed;

 37     if( Uday > 360.0 ) Uday -= 360.0;

 38     Nday+=NDSpeed;

 39     if( Nday > 360.0 ) Nday -= 360.0;

 40     /*公转控制*/

 41     Meryear+=MerYSpeed;

 42     if( Meryear > 360.0 ) Meryear -= 360.0;

 43     Vyear += VYSpeed;

 44     if( Vyear > 360.0 ) Vyear -= 360.0;

 45     Eyear += EYSpeed;

 46     if( Eyear > 360.0 ) Eyear -= 360.0;

 47     Maryear += MarYSpeed;

 48     if( Maryear > 360.0 ) Maryear -= 360.0;

 49     Jyear += JYSpeed;

 50     if( Jyear > 360.0 ) Jyear -= 360.0;

 51     Syear += SYSpeed;

 52     if( Syear > 360.0 ) Syear -= 360.0;

 53     Uyear += UYSpeed;

 54     if( Uyear > 360.0 ) Uyear -= 360.0;

 55     Nyear += NYSpeed;

 56     if( Nyear > 360.0 ) Nyear -= 360.0;

 57 

 58     month += monSpeed;

 59     if( month > 360.0 ) month -= 360.0;

 60     glutPostRedisplay();

 61 }

 62 

 63 /*绘制球体*/

 64 void gltDrawSphere(GLfloat fRadius, GLint iSlices, GLint iStacks)

 65     {

 66     GLfloat drho = (GLfloat)(3.141592653589) / (GLfloat) iStacks;

 67     GLfloat dtheta = 2.0f * (GLfloat)(3.141592653589) / (GLfloat) iSlices;

 68     GLfloat ds = 1.0f / (GLfloat) iSlices;

 69     GLfloat dt = 1.0f / (GLfloat) iStacks;

 70     GLfloat t = 1.0f;    

 71     GLfloat s = 0.0f;

 72     GLint i, j;     

 73 

 74     for (i = 0; i < iStacks; i++) 

 75         {

 76         GLfloat rho = (GLfloat)i * drho;

 77         GLfloat srho = (GLfloat)(sin(rho));

 78         GLfloat crho = (GLfloat)(cos(rho));

 79         GLfloat srhodrho = (GLfloat)(sin(rho + drho));

 80         GLfloat crhodrho = (GLfloat)(cos(rho + drho));

 81 

 82         glBegin(GL_TRIANGLE_STRIP);

 83         s = 0.0f;

 84         for ( j = 0; j <= iSlices; j++) 

 85             {

 86             GLfloat theta = (j == iSlices) ? 0.0f : j * dtheta;

 87             GLfloat stheta = (GLfloat)(-sin(theta));

 88             GLfloat ctheta = (GLfloat)(cos(theta));

 89 

 90             GLfloat x = stheta * srho;

 91             GLfloat y = ctheta * srho;

 92             GLfloat z = crho;

 93             

 94             glTexCoord2f(s, t);

 95             glNormal3f(x, y, z);

 96             glVertex3f(x * fRadius, y * fRadius, z * fRadius);

 97 

 98             x = stheta * srhodrho;

 99             y = ctheta * srhodrho;

100             z = crhodrho;

101             glTexCoord2f(s, t - dt);

102             s += ds;

103             glNormal3f(x, y, z);

104             glVertex3f(x * fRadius, y * fRadius, z * fRadius);

105             }

106         glEnd();

107 

108         t -= dt;

109         }

110     }

111 

112 /*从文件获取BMP图片*/

113 AUX_RGBImageRec *LoadBMP(char *Filename)            // 载入位图图象

114 {

115     FILE *File=NULL;                                    // 文件句柄

116     if (!Filename)                                        // 确保文件名已提供

117     {

118         return NULL;                                    //如果没提供,返回 NULL

119 

120     }

121     File=fopen(Filename,"r");                            //尝试打开文件

122     if (File)                                            // 文件存在与否

123     {

124         fclose(File);                                    // 关闭句柄

125         return auxDIBImageLoad(Filename);                //载入位图并返回指针

126     }

127     return NULL;                                        // 如果载入失败,返回 NULL

128 }

129 int LoadGLTextures()                                    // 载入位图并转换成纹理

130 {

131     int Status=FALSE;                                    // Status 状态指示器

132     int i;

133     AUX_RGBImageRec *TextureImage[10];                    // 创建纹理的存储空间

134     char 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     };

147     TextureImage[0]= LoadBMP(bmp[0]);

148     TextureImage[1]=LoadBMP(bmp[1]);

149     TextureImage[2]=LoadBMP(bmp[2]);

150     TextureImage[3]=LoadBMP(bmp[3]);

151     TextureImage[4]=LoadBMP(bmp[4]);

152     TextureImage[5]=LoadBMP(bmp[5]);

153     TextureImage[6]=LoadBMP(bmp[6]);

154     TextureImage[7]=LoadBMP(bmp[7]);

155     TextureImage[8]=LoadBMP(bmp[8]);

156     TextureImage[9]=LoadBMP(bmp[9]);

157     for(i=0;i<10;i++)

158     {

159     memset(TextureImage[i],0,sizeof(void *)*1);               // 将指针设置为Null

160     /* 载入BMP图片,并查错*/

161     if (TextureImage[i]=LoadBMP(bmp[i]))

162     {

163         Status=TRUE;                                    // 将状态设置为TRUE

164         glGenTextures(1, &texture[i]);                    // 创建纹理

165         glBindTexture(GL_TEXTURE_2D, texture[i]);       // 根据不同Bitmap图片的数据产生不同的纹理

166         /*定义纹理*/

167         glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[i]->sizeX, TextureImage[i]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[i]->data);

168         /* 控制滤波*/

169         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);

170         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

171         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

172         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

173     }

174     if (TextureImage[i])                                    // 判断纹理是否存在

175     {

176         if (TextureImage[i]->data)                            // 判断纹理图像是否存在

177         {

178             free(TextureImage[i]->data);                    // 释放纹理图像占用的内存

179         }

180         free(TextureImage[i]);                                // 释放图像结构

181     }

182     }

183     

184     return Status;                                        // 返回状态

185 }

186 

187 void display(void)

188 {   /*材质参数*/

189     GLfloat sun_mat_ambient[4]={1.0,1.0,1.0,0.0};

190     GLfloat sun_mat_diffuse[4]={1.0,1.0,0.5,1.0};

191     GLfloat sun_mat_specular[4]={1.0,1.0,1.0,1.0};

192     GLfloat sun_mat_shininess[]={10.0};

193     GLfloat sun_mat_emission[4]={0.1,0.1,0.1,1.0};

194     

195     GLfloat mat_ambient[4]={0.2,0.2,0.2,1.0};

196     GLfloat mat_diffuse[4]={1.0,1.0,1.0,1.0};

197     GLfloat mat_specular[4]={0.5,0.5,0.5,1.0};

198     GLfloat mat_shininess[]={5.0};

199     

200     GLfloat light_position[] = { 0.0, 0.0, 0.0, 1.0 };          // 光源位置设置

201 

202     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);         // 清空屏幕和深度缓存

203     glLoadIdentity();                                           // 重置当前的模型观察矩阵

204 

205     gluLookAt(0,viewPoint[0],viewPoint[1],0,0,0,viewPoint[2],1,0);       // 观察点设置

206 

207      /* draw sun */

208     glPushMatrix();

209     glLightfv(GL_LIGHT0, GL_POSITION, light_position);            // 创建光源

210     glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,sun_mat_ambient);   // 材质设定

211     glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,sun_mat_diffuse);

212     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,sun_mat_specular);

213     glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,sun_mat_shininess);

214     glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,sun_mat_emission);

215     glBindTexture(GL_TEXTURE_2D, texture[0]);                     // 绑定纹理

216         glRotatef((GLfloat) Eyear, 0.0, 1.0, 0.0);

217         glRotatef(90,1,0.0,0.0);

218         gltDrawSphere(1.0,200,200);             //绘制球体

219     glPopMatrix();

220 

221 /* draw Mercury */

222     glPushMatrix();

223     glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);   // 材质设定

224     glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);

225     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);

226     glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);

227     glBindTexture(GL_TEXTURE_2D, texture[1]);                 // 绑定纹理

228         glRotatef((GLfloat) Meryear, 0.0, 1.0, 0.0);   //公转

229         glTranslatef (0.0, 0.0, 1.8);           //与太阳的距离

230         glRotatef((GLfloat) Merday, 0.0, 1.0, 0.0);    //自转

231         glRotatef(90,1.0,0.0,0.0);

232         gltDrawSphere(0.2,200,200);        

233    glPopMatrix();

234 

235 /*画水星轨道*/

236    glDisable(GL_TEXTURE_2D);

237    glBegin(GL_LINE_LOOP);

238    glColor3f(1.0,1.0,1.0);

239     for(angle=0;angle<=360;angle++)

240             glVertex3f(1.8*sin(DEG2RAD(angle)),0,1.8*cos(DEG2RAD(angle)));        

241    glEnd();

242    glEnable(GL_TEXTURE_2D);

243 

244 /* draw Venus */

245     glPushMatrix();

246     glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);   // 材质设定

247     glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);

248     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);

249     glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);

250     glBindTexture(GL_TEXTURE_2D, texture[2]);                 // 绑定纹理

251         glRotatef((GLfloat) Vyear, 0.0, 1.0, 0.0);

252         glTranslatef (0.0, 0.0, 2.5);           //与太阳的距离

253         glRotatef((GLfloat) Vday, 0.0, 1.0, 0.0);

254         glRotatef(90,1.0,0.0,0.0);

255         gltDrawSphere(0.22,200,200);        

256     glPopMatrix();

257 

258 /*画金星轨道*/

259     glDisable(GL_TEXTURE_2D);

260     glBegin(GL_LINE_LOOP);

261     glColor3f(1.0,1.0,1.0);

262     for(angle=0;angle<=360;angle++)

263             glVertex3f(2.5*sin(DEG2RAD(angle)),0,2.5*cos(DEG2RAD(angle)));        

264     glEnd();

265     glEnable(GL_TEXTURE_2D);

266 

267 /* draw Earth */

268     glPushMatrix();

269     glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);   // 材质设定

270     glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);

271     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);

272     glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);

273     glBindTexture(GL_TEXTURE_2D, texture[3]);                 // 绑定纹理

274         glRotatef((GLfloat) Eyear, 0.0, 1.0, 0.0);

275         glTranslatef (0.0, 0.0, 3.2);           //与太阳的距离

276         glRotatef((GLfloat) Eday, 0.0, 1.0, 0.0);

277         glRotatef(90,1.0,0.0,0.0);

278         gltDrawSphere(0.2,200,200);        

279     glPopMatrix();

280 

281 /*画地球轨道*/

282     glDisable(GL_TEXTURE_2D);

283     glBegin(GL_LINE_LOOP);

284     glColor3f(1.0,1.0,1.0);

285     for(angle=0;angle<=360;angle++)

286             glVertex3f(3.2*sin(DEG2RAD(angle)),0,3.2*cos(DEG2RAD(angle)));        

287     glEnd();

288     glEnable(GL_TEXTURE_2D);

289 

290 /* draw moon */

291     glPushMatrix();

292     glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);   // 材质设定

293     glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);

294     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);

295     glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);

296     glBindTexture(GL_TEXTURE_2D, texture[4]);                 // 绑定纹理

297         glRotatef((GLfloat) Eyear, 0.0, 1.0, 0.0);

298         glTranslatef (0.0, 0.0, 3.2);           //与太阳的距离

299         glRotatef((GLfloat) month, 0.0, 1.0, 0.0);

300         glTranslatef (0.0, 0.0, 0.3);          //与地球的距离

301         glRotatef((GLfloat) Eday, 0.0, 1.0, 0.0);

302         glRotatef(90,1.0,0.0,0.0);

303         glColor3f (1.0, 1.0, 1.0);

304         gltDrawSphere(0.06,100,100);           

305    glPopMatrix();

306 

307 /* draw Mars */

308        glPushMatrix();

309     glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);   // 材质设定

310     glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);

311     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);

312     glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);

313     glBindTexture(GL_TEXTURE_2D, texture[5]);                 // 绑定纹理

314         glRotatef((GLfloat)  Maryear, 0.0, 1.0, 0.0);

315         glTranslatef (0.0, 0.0, 3.9);           //与太阳的距离

316         glRotatef((GLfloat) Marday, 0.0, 1.0, 0.0);

317         glRotatef(90,1.0,0.0,0.0);

318         gltDrawSphere(0.15,200,200);        

319     glPopMatrix();

320 

321 /*画火星轨道*/

322     glDisable(GL_TEXTURE_2D);

323     glBegin(GL_LINE_LOOP);

324     glColor3f(1.0,1.0,1.0);

325     for(angle=0;angle<=360;angle++)

326             glVertex3f(3.9*sin(DEG2RAD(angle)),0,3.9*cos(DEG2RAD(angle)));        

327     glEnd();

328     glEnable(GL_TEXTURE_2D);

329 

330 /* draw Jupiter*/

331     glPushMatrix();

332     glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);   // 材质设定

333     glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);

334     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);

335     glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);

336     glBindTexture(GL_TEXTURE_2D, texture[6]);                 // 绑定纹理

337         glRotatef((GLfloat)  Jyear, 0.0, 1.0, 0.0);

338         glTranslatef (0.0, 0.0, 4.5);           //与太阳的距离

339         glRotatef((GLfloat) Jday, 0.0, 1.0, 0.0);

340         glRotatef(90,1.0,0.0,0.0);

341         gltDrawSphere(0.3,200,200);        

342     glPopMatrix();

343 

344 /*画木星轨道*/

345     glDisable(GL_TEXTURE_2D);

346     glBegin(GL_LINE_LOOP);

347     glColor3f(1.0,1.0,1.0);

348     for(angle=0;angle<=360;angle++)

349             glVertex3f(4.5*sin(DEG2RAD(angle)),0,4.5*cos(DEG2RAD(angle)));        

350     glEnd();

351     glEnable(GL_TEXTURE_2D);

352 

353 /* draw Saturn*/

354     glPushMatrix();

355     glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);   // 材质设定

356     glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);

357     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);

358     glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);

359     glBindTexture(GL_TEXTURE_2D, texture[7]);                 // 绑定纹理

360         glRotatef((GLfloat)  Syear, 0.0, 1.0, 0.0);

361         glTranslatef (0.0, 0.0, 5.5);           //与太阳的距离

362         glRotatef((GLfloat) Sday, 0.0, 1.0, 0.0);

363         glRotatef(90,1.0,0.0,0.0);

364         gltDrawSphere(0.26,200,200);        

365     glPopMatrix();

366 

367 /*画土星的光环*/

368     glPushMatrix();

369     glRotatef((GLfloat)  Syear, 0.0, 1.0, 0.0);

370     glTranslatef (0.0, 0.0, 5.5);

371     glBegin(GL_LINE_LOOP);

372     for(r=0.32;r<=0.4;r+=0.006)

373     for(angle=0;angle<=360;angle++)

374             glVertex3f(r*sin(DEG2RAD(angle)),0,r*cos(DEG2RAD(angle)));        

375     glEnd();

376     glPopMatrix();

377 

378 /*画土星轨道*/

379     glDisable(GL_TEXTURE_2D);

380     glBegin(GL_LINE_LOOP);

381     glColor3f(1.0,1.0,1.0);

382     for(angle=0;angle<=360;angle++)

383             glVertex3f(5.5*sin(DEG2RAD(angle)),0,5.5*cos(DEG2RAD(angle)));        

384     glEnd();

385     glEnable(GL_TEXTURE_2D);

386 

387 /* draw Uranus*/

388     glPushMatrix();

389     glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);   // 材质设定

390     glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);

391     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);

392     glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);

393     glBindTexture(GL_TEXTURE_2D, texture[8]);                 // 绑定纹理

394         glRotatef((GLfloat)  Uyear, 0.0, 1.0, 0.0);

395         glTranslatef (0.0, 0.0, 6.3);             //与太阳的距离

396         glRotatef((GLfloat) Uday, 0.0, 1.0, 0.0);

397         glRotatef(90,1.0,0.0,0.0);

398         gltDrawSphere(0.24,200,200);        

399     glPopMatrix();

400 

401 /*画天王星轨道*/

402     glDisable(GL_TEXTURE_2D);

403     glBegin(GL_LINE_LOOP);

404     glColor3f(1.0,1.0,1.0);

405     for(angle=0;angle<=360;angle++)

406             glVertex3f(6.3*sin(DEG2RAD(angle)),0,6.3*cos(DEG2RAD(angle)));        

407     glEnd();

408     glEnable(GL_TEXTURE_2D);

409 

410 /* draw Neptune*/

411     glPushMatrix();

412     glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);   // 材质设定

413     glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);

414     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);

415     glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);

416     glBindTexture(GL_TEXTURE_2D, texture[9]);                 // 绑定纹理

417         glRotatef((GLfloat)  Nyear, 0.0, 1.0, 0.0);

418         glTranslatef (0.0, 0.0, 7);           //与太阳的距离

419         glRotatef((GLfloat) Nday, 0.0, 1.0, 0.0);

420         glRotatef(90,1.0,0.0,0.0);

421         gltDrawSphere(0.23,200,200);        

422     glPopMatrix();

423 

424 /*画海王星轨道*/

425     glDisable(GL_TEXTURE_2D);

426     glBegin(GL_LINE_LOOP);

427     glColor3f(1.0,1.0,1.0);

428     for(angle=0;angle<=360;angle++)

429             glVertex3f(7*sin(DEG2RAD(angle)),0,7*cos(DEG2RAD(angle)));        

430     glEnd();

431     glEnable(GL_TEXTURE_2D);

432 

433     glutSwapBuffers();    

434     glFlush();

435 }

436 

437 void myinit(void) 

438 {

439     

440     GLfloat light_ambient[]={1.0, 1.0, 1.0, 1.0};     //环境光

441     GLfloat light_diffuse[]={1.0, 1.0, 1.0, 1.0};     //漫反射光

442     GLfloat light_specular[]={1.0, 1.0, 1.0, 1.0};    //镜面反射光

443 

444     //glClearColor(0.5f, 0.5f, 0.5f, 1.0f);              //设置背景颜色

445     glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);

446     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);

447     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

448 

449     if (LoadGLTextures())                            // 调用纹理载入子例程 

450     glEnable(GL_LIGHTING);                          // 开启光照效果

451     glEnable(GL_LIGHT0);

452     glEnable(GL_TEXTURE_2D);                        //启动纹理映射 

453     glShadeModel(GL_SMOOTH);                        // 启用阴影平滑

454     

455     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);   // 说明映射方式

456     glDepthFunc(GL_LESS);

457     glClearDepth(1.0f);                                // 深度缓存创建

458     glEnable(GL_DEPTH_TEST);                        // 开启深度测试

459     glDepthFunc(GL_LEQUAL);        

460 

461 }

462 

463 

464 void myReshape(GLsizei w, GLsizei h)

465 {

466     glViewport(0, 0, w, h);

467     glMatrixMode(GL_PROJECTION);

468     glLoadIdentity();

469     gluPerspective(800.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);

470     glMatrixMode(GL_MODELVIEW);

471     glLoadIdentity();

472     glTranslatef (0.0, 0.0, -5.0);

473 }

474 

475 void View_SpeedControl(unsigned char key ,int x,int y)

476 {

477     switch(key)

478     {

479 

480     case'z':

481         viewPoint[0]+= 0.1;

482         break;

483 

484     case'x':

485         viewPoint[0]-= 0.1;

486         break;

487 

488     case'c':

489         viewPoint[1]+=0.1;

490         break;

491 

492     case'v':

493         viewPoint[1]-=0.1;

494         break;

495 

496     case'b':

497         viewPoint[2]+=0.1;

498         break;

499 

500     case'n':

501         viewPoint[2]-=0.1;

502         break;

503 

504     case'=':

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,

506            monSpeed*=2,

507            MerYSpeed*=2,VYSpeed*=2,EYSpeed*=2,MarYSpeed*=2,JYSpeed*=2,SYSpeed*=2,UYSpeed*=2,NYSpeed*=2;};

508         break;

509     case'-':

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,

511            monSpeed/=1.5,

512            MerYSpeed/=2,VYSpeed/=2,EYSpeed/=2,MarYSpeed/=2,JYSpeed/=2,SYSpeed/=2,UYSpeed/=2,NYSpeed/=2;};

513         break;

514 

515     default:

516         break;

517 

518     }

519     display();

520 }

521 int main(int argc, char** argv)

522 {

523     glutInit(&argc,argv);

524     glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB | GLUT_DEPTH);

525     glutInitWindowSize(800,600);

526     glutCreateWindow("solar system");

527     myinit();

528     glutDisplayFunc(display);

529     glutReshapeFunc(myReshape);

530     glutKeyboardFunc(View_SpeedControl);

531     glutIdleFunc(AutoRun);

532     glutMainLoop();

533 

534 }

运行结果:

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

你可能感兴趣的:(实现)