#include<GL/glut.h>
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glut32.lib")
#include<stdio.h>
#include <windows.h>
static int year = 0, day = 0, month = 0, solid = 0;
void spinDisplay(void)
{
year += 10;
day +=5;
month += 2;
if(year > 360)
year -= 360;
if(day > 360)
day -= 360;
if(month > 360)
month -= 360;
if(solid > 360)
solid -= 360;
glutPostRedisplay();
Sleep(1000);
}
void mouse(int button, int state, int x, int y)
{
switch(button)
{
case GLUT_LEFT_BUTTON:
if(state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
default:
break;
}
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
void display(void)
{
// 画红色的太阳
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glPushMatrix();
glutWireSphere(1.0, 15, 16);
glRotatef((GLfloat) year, 0.0, 1.0, 0.0); //地球绕太阳公转
glRotatef(40.0, 1.0, 0.0, 0.0);
//画蓝色的地球
glColor3f(0.0, 0.0, 1.0);
glTranslatef(2.0, 0.0, 0.0);
glRotatef((GLfloat) day, 0.0, 1.0, 0.0); //地球绕自己的轴自转
glutWireSphere(0.2, 10, 8);
//画黄色的月球
glColor3f(1.0, 1.0, 0.0);
glPushMatrix();
glRotatef((GLfloat) month, 0.0, 1.0, 0.0); //月亮绕地球公转
glTranslatef(0.5, 0.0, 0.0);
glRotatef((GLfloat) solid, 0.0, 1.0, 0.0); //月亮绕自己的轴自转
glutWireSphere(0.1, 5, 4);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h); //左下角坐标为(0,0),宽度为w,高度为h
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w / (GLfloat) h, 1.0, 20.0); //平面上视野的角度60,纵横比,近侧和远侧的距离
glMatrixMode(GL_MODELVIEW); //视图和模型变换
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0); //前三个坐标为眼睛的位置,中间是看的方向,后部是向上的方向
}
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 'd':
day = (day + 10) % 360;
glutPostRedisplay();
break;
case 'D':
day = (day - 10) % 360;
glutPostRedisplay();
break;
case 'y':
year = (year + 5) % 360;
glutPostRedisplay();
break;
case 'Y':
year = (year - 5) % 360;
glutPostRedisplay();
break;
case 'm':
month = (month + 2) % 360;
glutPostRedisplay();
break;
case 'M':
month = (month - 2) % 360;
glutPostRedisplay();
break;
case 's':
solid = (solid + 2) % 360;
glutPostRedisplay();
break;
case 'S':
solid = (solid - 2) % 360;
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(100, 100);
glutCreateWindow("hello");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
- 1
- 2
- 3
前往页