#include <stdio.h>
#include<GL\glut.h>
static float xrot = 0.0;
static float yrot = 0.0;
static float zrot = 0.0;
void cube() //立方体参数(每一面的顶点以及顶点的颜色)
{
glBegin(GL_QUADS);
glColor3f(1.0,1.0,0.0);
glVertex3f( 1.0, 1.0,-1.0);
glColor3f(0.0,1.0,0.0);
glVertex3f(-1.0, 1.0,-1.0);
glColor3f(0.0,1.0,1.0);
glVertex3f(-1.0, 1.0, 1.0);
glColor3f(1.0,1.0,1.0);
glVertex3f( 1.0, 1.0, 1.0);
glColor3f(1.0,0.0,1.0);
glVertex3f( 1.0,-1.0, 1.0);
glColor3f(0.0,0.0,1.0);
glVertex3f(-1.0,-1.0, 1.0);
glColor3f(0.0,0.0,0.0);
glVertex3f(-1.0,-1.0,-1.0);
glColor3f(1.0,0.0,0.0);
glVertex3f( 1.0,-1.0,-1.0);
glColor3f(1.0,1.0,1.0);
glVertex3f( 1.0, 1.0, 1.0);
glColor3f(0.0,1.0,1.0);
glVertex3f(-1.0, 1.0, 1.0);
glColor3f(0.0,0.0,1.0);
glVertex3f(-1.0,-1.0, 1.0);
glColor3f(1.0,0.0,1.0);
glVertex3f( 1.0,-1.0, 1.0);
glColor3f(1.0,0.0,0.0);
glVertex3f( 1.0,-1.0,-1.0);
glColor3f(0.0,0.0,0.0);
glVertex3f(-1.0,-1.0,-1.0);
glColor3f(0.0,1.0,0.0);
glVertex3f(-1.0, 1.0,-1.0);
glColor3f(1.0,1.0,0.0);
glVertex3f( 1.0, 1.0,-1.0);
glColor3f(0.0,1.0,1.0);
glVertex3f(-1.0, 1.0, 1.0);
glColor3f(0.0,1.0,0.0);
glVertex3f(-1.0, 1.0,-1.0);
glColor3f(0.0,0.0,0.0);
glVertex3f(-1.0,-1.0,-1.0);
glColor3f(0.0,0.0,1.0);
glVertex3f(-1.0,-1.0, 1.0);
glColor3f(1.0,1.0,0.0);
glVertex3f( 1.0, 1.0,-1.0);
glColor3f(1.0,1.0,1.0);
glVertex3f( 1.0, 1.0, 1.0);
glColor3f(1.0,0.0,1.0);
glVertex3f( 1.0,-1.0, 1.0);
glColor3f(1.0,0.0,0.0);
glVertex3f( 1.0,-1.0,-1.0);
glEnd();
}
void display() //展示
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //用当前缓冲区清除值来清除指定缓冲区
glLoadIdentity(); //重置为单位矩阵
glTranslatef(0, 0, -5); //平移变换
glRotatef(xrot, 1, 0, 0); //旋转变换
glRotatef(yrot, 0, 1, 0);
glRotatef(zrot, 0, 0, 1);
//glPolygonMode(GL_FRONT, GL_LINE);
cube();
xrot = xrot + 1;
yrot = yrot + 1;
zrot = zrot + 1;
glutSwapBuffers(); //交换缓冲区指针
}
void reshape(int w, int h) //改变形态
{
if(h==0) h = 1;
glViewport(0, 0, (GLsizei) w, (GLsizei) h); //打开窗口
glMatrixMode(GL_PROJECTION); //设置当前矩阵模式
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0); //指定视景体
glMatrixMode(GL_MODELVIEW);
}
void init(int width, int height ) //起始
{
if(height == 0)
height = 1;
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glDepthFunc(GL_LESS); //指定深度缓冲比较值
glEnable(GL_DEPTH_TEST); //启用深度测试
glShadeModel(GL_SMOOTH); //着色模式:渐变色
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)width/(GLfloat)height, 1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int w, int h) //键盘操作
{
if(key == 'f') // 进入全屏
glutFullScreen();
if(key == 'F') // 退出全屏
{
glutReshapeWindow(640, 480); // 设置窗口大小(不能用初始化的函数)
glutPositionWindow(400, 100); // 设置窗口位置(不能用初始化的函数)
}
/*if(key == 27) // ESC退出程序
exit(0);*/
}
int main(int argc, char** argv) //主函数
{
glutInit(&argc, argv); //初始化GLUT库
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(400, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("旋转的渐变色立方体");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
init(640, 480);
glutMainLoop();
return 0;
}