一:开发环境
作者:嘟粥yyds
时间:2022年12月13日
开发工具:Visual Studio 2017
开发环境:OpenGL环境(#include <GL/glut.h>)
二:实现功能
2.1 实现了简易版地月系统
#include <windows.h>
#include <GL/glut.h>
#define Imagewidth 24
GLdouble speed = 0.1, day, month, year, solarcycle;
//月亮、地球、太阳的半径
GLfloat mr = 0.15, er = 0.3, sr = 0.4;
//月亮、地球的公转半径
GLfloat mrr = 0.6, err = 2;
//条纹纹理
GLubyte Image[3 * Imagewidth];
//创建条纹纹理
void makeStripeImage(void){
for (int j = 0; j < Imagewidth; j++) {
Image[3 * j] = 94;
Image[3 * j + 1] = (GLubyte)((j <= 3) ? 455 : 0);
Image[3 * j + 2] = 452;
}
}
/* 参数设置,用于自动生成纹理坐标 */
GLfloat sgenparams[] = { 1.0, 1.0, 0.0, 0.0 };
void init(){
glClearColor(0, 0, 0.1, 1);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage1D(GL_TEXTURE_1D, 0, 4, Imagewidth, 0, GL_RGB, GL_UNSIGNED_BYTE, Image);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(GL_S, GL_OBJECT_PLANE, sgenparams);
glEnable(GL_POINT_SMOOTH | GL_LINE_SMOOTH | GL_POLYGON_SMOOTH);
}
void keyboard(GLubyte key, GLint x, GLint y){
if (key == 27) exit(0);
}
void timer(GLint millis){
day += speed * 360 / 0.997;
month += speed * 360 / 29.5;
year += speed * 360 / 365.2475;
solarcycle += speed * 360 / 27.5;
glutPostRedisplay();
glutTimerFunc(millis, timer, millis);
}
void reshape(GLint w, GLint h){
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30, (GLfloat)w / h, 1, 1000);
glTranslatef(0, 0, -8);
glRotatef(30, 1, 0, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void light(GLfloat r){
GLfloat pos[][4] ={
{r,0,0,1},{-r,0,0,1},{0,r,0,1},
{0,-r,0,1},{0,0,r,1},{0,0,-r,1}
};
GLfloat params[] = { 0.9,0.9,0.9,1 };
for (int i = 0; i < 6; i++){
glLightfv(GL_LIGHT0 + i, GL_POSITION, pos[i]);
glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, params);
glEnable(GL_LIGHT0 + i);
}
}
void material(GLfloat r, GLfloat g, GLfloat b){
GLfloat params[] = { r,g,b,1 };
glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONT, GL_DIFFUSE, params);
}
void color(GLfloat r, GLfloat g, GLfloat b){
glDisable(GL_LIGHTING);
glColor3f(r, g, b);
}
// 经纬线
void wire(GLfloat r){
color(0.5, 0.5, 0.5);
glutWireSphere(1.005*r, 24, 12);
}
// 自转轴
void axle(GLfloat r){
color(0, 0, 0);
glBegin(GL_LINE);
glVertex3f(0, 0, -r * 1.2);
glVertex3f(0, 0, r*1.2);
glEnd();
}
//地球材质
void material_earth(void){
glPushAttrib(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_CURRENT_BIT);
glEnable(GL_COLOR_MATERIAL);
GLfloat mat_specular[] = { 1.0, 0.0, 0.0, 1.0 }; //材质镜面反射颜色参数
GLfloat mat_shininess[] = { 50.0 }; // 镜面反射指数参数
GLfloat mat_diffuse[] = { 0.0,0.0,1.0,1.0 }; //材质散射颜色
GLfloat white_light[] = { 1.0, 0.0,0.0, 1.0 };
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); //材质镜面反射颜色
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); //镜面反射指数
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); //材质的散射颜色
glMaterialfv(GL_FRONT, GL_AMBIENT, white_light); //材质的环境颜色
glPopAttrib();
}
//月球材质
void material_moon(void){
glPushAttrib(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_CURRENT_BIT);
glEnable(GL_COLOR_MATERIAL);
GLfloat mat_specular[] = { 1.0, 0.0, 0.0, 1.0 }; //材质镜面反射颜色参数
GLfloat mat_shininess[] = { 50.0 }; // 镜面反射指数参数
GLfloat mat_diffuse[] = { 0.0,0.0,1.0,1.0 }; //材质散射颜色
GLfloat white_light[] = { 1.0, 0.0,0.0, 1.0 };
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); //材质镜面反射颜色
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); //镜面反射指数
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); //材质的散射颜色
glMaterialfv(GL_FRONT, GL_AMBIENT, white_light); //材质的环境颜色
glPopAttrib();
}
void sun(GLfloat r){
glPushMatrix();
glRotatef(90, -1, 0, 0);
color(0.9, 0.1, 0.1);
GLfloat sun_emission[] = { 0.1f, 0.0f, 0.0f, 1.0f };//太阳颜色
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, sun_emission);
glutSolidSphere(r, 60, 60);
wire(r);
glPopMatrix();
}
void earth(GLfloat r){
glPushMatrix();
material_earth();
glRotatef(90, -1, 0, 0);
material(0.1, 0.1, 0.9);
GLfloat earth_emission[] = { 0.2f, 0.4f, 0.9f, 1.0f };//地球颜色
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, earth_emission);
glutSolidSphere(r, 50, 50);
wire(r);
glPopMatrix();
}
void moon(GLfloat r){
glPushMatrix();
material_moon();
glRotatef(90, -1, 0, 0);
material(0.75, 0.75, 0.1);
GLfloat moon_emission[] = { 0.502f, 0.5411f, 0.529f, 1.0f };//月亮颜色
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, moon_emission);
glutSolidSphere(r, 50, 50);
wire(r);
glPopMatrix();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// 太阳光照、太阳自转
light(sr);
glPushMatrix();
glRotatef(solarcycle, 0, 1, 0);
sun(sr);
glPopMatrix();
// 地球公转
glRotatef(year, 0, 1, 0);
glTranslatef(err, 0, 0);
glRotatef(-year, 0, 1, 0);
glRotatef(-23.5, 0, 0, 1);
// 地球自转
glPushMatrix();
glRotatef(day, 0, 1, 0);
earth(er);
glPopMatrix();
// 月球公转
glRotatef(month, 0, 1, 0);
glTranslatef(mrr, 0, 0);
moon(mr);
glutSwapBuffers();
}
int main(){
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("嘟粥yyds");
glutFullScreen();
glutKeyboardFunc(keyboard);
init();
glutTimerFunc(25, timer, 25);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}
2.2 运行结果
地月系统