資源簡介
完成一個簡單的日地月系統演示程序。要求必須考慮太陽的自轉、地球
和月亮的公轉和自轉;能夠演示地球上的白天黑夜和四季變化,以及月亮的圓缺效果。為了增強演示效果,請繪制出太陽、地球和月亮的經緯線、赤道和軸線。為了增強真實感,請在太陽、地球、月亮的表面使用合適的紋理。
紋理貼圖部分做的不太好。

代碼片段和文件信息
#include??
#include??
#include?
#include?
#include?
#ifndef?GL_BGR_EXT
#include?
#endif
float?sun?=0;
float?year?=?0;
float?month?=0;
float?day?=0;
GLuint?texSun;
GLuint?texPlanet;
GLuint?texMoon;
GLuint??Texture(const?char?*FileName)
{
FILE?*file?=fopen(FileName?“rb“);
fseek(filesizeof(BITMAPFILEHEADER)SEEK_SET);
BITMAPINFOHEADER?info;
fread(&info?sizeof(info)1file);
long?Width=info.biWidth;
long?Height=info.biHeight;
GLubyte*?pixels;
pixels?=?(GLubyte*)malloc(Width*Height*3);
fread(pixelsWidth*Height*31file);
fclose(file);
glTexImage2D(GL_TEXTURE_2D03WidthHeight0GL_BGR_EXTGL_UNSIGNED_BYTEpixels);
glTexParameteri(GL_TEXTURE_2DGL_TEXTURE_MAG_FILTERGL_LINEAR);
glTexParameteri(GL_TEXTURE_2DGL_TEXTURE_MIN_FILTERGL_LINEAR);
// glEnable(GL_TEXTURE_2D);
}
void?Keyboard(unsigned?char?keyint?xint?y)
{
if(key==27)
exit(0);
}
void?Timer(int?millis)
{
float?dday=6speed?=4;
float?dsun?=?speed*dday/27.5;????????//太陽自轉?
float?dmonth=speed*dday/29.5;????????//月亮自轉?
float?dyear?=speed*dday/365.2475;????//地球公轉?
sun?=?fmod(sun+dsun360);?????????//計算太陽自轉角度?
month?=fmod(month+dmonth360);???????//計算月亮自轉和公轉角度?
year?=fmod(year+dyear360);??????????//計算地球公轉角度?
day?=?fmod(day+dday360);????????????//計算地球自轉角度?
glutPostRedisplay();
glutTimerFunc(millisTimermillis);
}
void?Reshape(int?wint?h)?
{?
????glViewport(00wh);
????glMatrixMode(GL_PROJECTION);?
????glLoadIdentity();?
????gluPerspective(30(float)w/h11000);?
????glTranslatef(00-8);
glRotatef(30100);?
????glMatrixMode(GL_MODELVIEW);?
????glLoadIdentity();?
}
void?Light(float?r)
{
float?pos[]?[4]=
{
{r001}{-r001}{0r01}{0-r01}{00r1}{00-r1}{0.90.90.90.1}
};
for?(int?i=0;i<6;++i)
{
glLightfv(GL_LIGHT0+iGL_POSITIONpos[i]);
glLightfv(GL_LIGHT0+iGL_DIFFUSEpos[6]);
glEnable(GL_LIGHT0+i);
}?
}
void?Material(float?rfloat?gfloat?b)
{
float?aa[]={rgb1};
glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONTGL_DIFFUSEaa);
}
void?Color(float?rfloat?gfloat?b)
{
glDisable(GL_LIGHTING);
glColor3f(rgb);?
}
void?Wire(float?r)
{
Color(0.50.50.5);?
glutWireSphere(r*1.0052412);
}
void?Ball(float?r)
{
glTranslatef(-r00);
Color(0.90.10.9);
glutSolidSphere(r*0.1126);
}
void?Sun(float?r)
{
glEnable(GL_TEXTURE_2D);
glTexGeni(GL_S?GL_TEXTURE_GEN_MODE?GL_SPHERE_MAP);???//?設置s方向的紋理自動生成
glTexGeni(GL_T?GL_TEXTURE_GEN_MODE?GL_SPHERE_MAP);???//?設置t方向的紋理
glEnable(GL_TEXTURE_GEN_S);??????//?自動生成s方向紋理坐標
glEnable(GL_TEXTURE_GEN_T);??????//?自動生成t方向紋理坐標
glBindTexture(GL_TEXTURE_2DtexSun);??//選擇紋理texture[status]?
glPushMatrix();
glRotatef(90-100);?
Color(0.90.10.1);
glutSolidSphere(r2412);
Wire(r);
Ball(r);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
void?Planet(float?r)
{
glEnable(GL_TEXTURE_2D);
glTexGeni(
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????43254??2018-09-04?17:10??test8\moon.bmp
?????文件??????345654??2018-09-04?16:56??test8\planet.bmp
?????文件???????50918??2018-09-11?12:18??test8\planet.jpg
?????文件??????172854??2018-09-04?17:10??test8\sun.bmp
?????文件????????5117??2018-09-11?12:20??test8\test8.cpp
?????文件??????275761??2018-09-11?12:20??test8\test8.exe
?????目錄???????????0??2018-09-11?12:20??test8\
評論
共有 條評論