旋转的正方体

简介: #include GLfloat rquad;      //立方体旋转角度void init(void){ glClearColor(0.0f,0.0f,0.0f,0.0f); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST);   //激活深度测试...

#include <GL/glut.h>
GLfloat rquad;      //立方体旋转角度
void init(void)
{
 glClearColor(0.0f,0.0f,0.0f,0.0f);
 glShadeModel(GL_SMOOTH);
 glEnable(GL_DEPTH_TEST);   //激活深度测试,以隐藏被遮挡面
}

void display(void)
{
 //清除颜色缓存和深度缓存
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glLoadIdentity();
 glTranslatef(0.0f,0.0f,-7.0f);
 glRotatef(rquad,1.0f,1.0f,1.0f);
 glBegin(GL_QUADS);
 glColor3f(0.0f,1.0f,0.0f);
 glVertex3f(1.0f,1.0f,-1.0f);
 glVertex3f(-1.0f,1.0f,-1.0f);
 glVertex3f(-1.0f,1.0f,1.0f);
 glVertex3f(1.0f,1.0f,-1.0f);

 glColor3f(1.0f,0.5f,0.0f);
 glVertex3f(1.0f,-1.0f,1.0f);
 glVertex3f(-1.0f,-1.0f,1.0f);
 glVertex3f(-1.0f,-1.0f,-1.0f);
 glVertex3f(1.0f,-1.0f,-1.0f);

 glColor3f(1.0f,0.0f,0.0f);
 glVertex3f(1.0f,1.0f,1.0f);
 glVertex3f(-1.0f,1.0f,1.0f);
 glVertex3f(-1.0f,-1.0f,1.0f);
 glVertex3f(1.0f,-1.0f,1.0f);

 glColor3f(1.0f,1.0f,0.0f);
 glVertex3f(1.0f,-1.0f,-1.0f);
 glVertex3f(-1.0f,-1.0f,-1.0f);
 glVertex3f(-1.0f,1.0f,-1.0f);
 glVertex3f(1.0f,1.0f,-1.0f);

 glColor3f(0.0f,0.0f,1.0f);
 glVertex3f(-1.0f,1.0f,1.0f);
 glVertex3f(-1.0f,1.0f,-1.0f);
 glVertex3f(-1.0f,-1.0f,-1.0f);
 glVertex3f(-1.0f,-1.0f,1.0f);

 glColor3f(1.0f,0.0f,1.0f);
 glVertex3f(1.0f,1.0f,-1.0f);
 glVertex3f(1.0f,1.0f,1.0f);
 glVertex3f(1.0f,-1.0f,1.0f);
 glVertex3f(-1.0f,-1.0f,-1.0f);

  glEnd();
  rquad-=0.2f;            //加一个角度
  glutSwapBuffers();      //交换双缓存
}

void reshape(int width,int height)
{
 glViewport(0,0,width,height);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
}

void keyboard(unsigned char key,int x,int y)
{
 switch (key)
 {
   case 27:
    //exit(0);
   break;
      default:
    break;
 }
}

int main(int argc,char** argv)
{
 glutInit(&argc,argv);
 //使用双缓存模式和深度缓存
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
 glutInitWindowSize(600,350);
 glutInitWindowPosition(200,200);
 glutCreateWindow("旋转动画");
 init();
 glutDisplayFunc(display);
 glutReshapeFunc(reshape);
 glutKeyboardFunc(keyboard);
 glutIdleFunc(display);   //设置空闲时用的函数
 glutMainLoop();
 return 0;
}

相关文章
|
8月前
|
计算机视觉
旋转
【5月更文挑战第15天】旋转。
61 1
|
5月前
|
前端开发 JavaScript
会旋转的圣诞树
这篇文章介绍了如何使用HTML、CSS和JavaScript实现一个动态旋转的圣诞树效果,包括详细的实现步骤和编码实现,以及最终效果的展示。
会旋转的圣诞树
圆角三角形,二次方贝塞尔曲线
圆角三角形,二次方贝塞尔曲线
|
6月前
|
前端开发 JavaScript
canvas系列教程01——直线、三角形、多边形、矩形、调色板
canvas系列教程01——直线、三角形、多边形、矩形、调色板
163 0
|
7月前
|
前端开发
canvas图形操作(缩放、旋转、位移)
canvas图形操作(缩放、旋转、位移)
|
图形学
Unity 之 获取物体的旋转角正确数值
不管父物体如何设置,都能获取到物体本身旋转角度的正确数值
1078 0
四元数与三维旋转
四元数与三维旋转
170 0
四元数与三维旋转
立体照片旋转
旋转照片+开关灯
76 0
立体照片旋转
c3旋转立方体
要求:六面立方体,立体旋转 
96 0
c3旋转立方体