旋转立方体(opengl)

简介: #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;
}

相关文章
|
4月前
|
前端开发 JavaScript
canvas系列教程01——直线、三角形、多边形、矩形、调色板
canvas系列教程01——直线、三角形、多边形、矩形、调色板
95 0
|
计算机视觉
OpenCV-绘制旋转矩形
OpenCV-绘制旋转矩形
168 0
c3旋转立方体
要求:六面立方体,立体旋转 
90 0
c3旋转立方体
|
缓存 Swift iOS开发
OpenGL ES 案例02:GLKit绘制立方体+旋转
OpenGL ES 案例02:GLKit绘制立方体+旋转
195 0
OpenGL ES 案例02:GLKit绘制立方体+旋转
|
Swift 容器
OpenGL ES 案例03:CoreAnimation绘制立方体+旋转
OpenGL ES 案例03:CoreAnimation绘制立方体+旋转
208 0
OpenGL ES 案例03:CoreAnimation绘制立方体+旋转
|
机器学习/深度学习
【OpenGL】十六、OpenGL 绘制四边形 ( 绘制 GL_QUADS 四边形 )
【OpenGL】十六、OpenGL 绘制四边形 ( 绘制 GL_QUADS 四边形 )
464 0
【OpenGL】十六、OpenGL 绘制四边形 ( 绘制 GL_QUADS 四边形 )
【OpenGL】十三、OpenGL 绘制三角形 ( 绘制单个三角形 | 三角形绘制顺序 | 绘制多个三角形 )(一)
【OpenGL】十三、OpenGL 绘制三角形 ( 绘制单个三角形 | 三角形绘制顺序 | 绘制多个三角形 )(一)
230 0
【OpenGL】十三、OpenGL 绘制三角形 ( 绘制单个三角形 | 三角形绘制顺序 | 绘制多个三角形 )(一)
【OpenGL】十三、OpenGL 绘制三角形 ( 绘制单个三角形 | 三角形绘制顺序 | 绘制多个三角形 )(二)
【OpenGL】十三、OpenGL 绘制三角形 ( 绘制单个三角形 | 三角形绘制顺序 | 绘制多个三角形 )(二)
133 0
【OpenGL】十三、OpenGL 绘制三角形 ( 绘制单个三角形 | 三角形绘制顺序 | 绘制多个三角形 )(二)
|
Windows
【OpenGL】十八、OpenGL 绘制多边形 ( 绘制 GL_POLYGON 模式多边形 )
【OpenGL】十八、OpenGL 绘制多边形 ( 绘制 GL_POLYGON 模式多边形 )
580 0
【OpenGL】十八、OpenGL 绘制多边形 ( 绘制 GL_POLYGON 模式多边形 )