// 范例P98.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <gl/glut.h>
static float year=0,day=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT); //清除所有像素
glColor3f(1.0,1.0,1.0); //设置画笔白色
/*在缓存中绘制4对顶点坐标的矩形*/
glPushMatrix();
glutWireSphere(1.0,20,16); //绘制太阳
glRotatef((GLfloat)year,0,1,0); //绕y轴旋转表示确定公转位置
glTranslatef(2,0,0); //平移到公转轨道
glRotatef((GLfloat)day,0,1,0); //绕y轴表示自转
glutWireSphere(0.2,10,8);
glPopMatrix();
glutSwapBuffers(); //立即显示出来
}
void init()
{
glClearColor(0.0,0.0,0.0,0.0); //用黑色清除背景
glShadeModel(GL_FLAT);
}
void reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60,(GLfloat)w/(GLfloat)h,1.0,20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,5, 0,0,0, 0,1,0);
glRotatef(45,1,0,0);
}
void spinDisplay(void)
{
day=day+1;
year=year+1.0/365;
if(day>360)
day-=360;
if(year>360)
year-=360;
glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
switch(button){
case GLUT_LEFT_BUTTON:
if(state==GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
case GLUT_MIDDLE_BUTTON:
if(state==GLUT_DOWN)
glutIdleFunc(0);
break;
default:
break;
}
}
int main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(250,250); //初始化窗口大小
glutInitWindowPosition(0,0); //初始化窗口位置
glutCreateWindow("hello"); //创建标题为“hello”的窗口
init(); //调用初始化函数
glutDisplayFunc(display); //注册回调函数
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop(); //进入主循环处理事件
return 0;
}
评论