OpenGL 颜色立方体

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

代码如下:

#include <stdio.h>#define GLUT_DISABLE_ATEXIT_HACK#include <gl/glut.h>#pragma comment(lib, "glut32.lib")GLfloat AngleX;GLfloat AngleY;void display(void){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPushMatrix(); {  glRotatef(AngleX,1.0f,0.0f,0.0f);  glRotatef(AngleY,0.0f,1.0f,0.0f);  glBegin(GL_POLYGON); //前表面  glColor3ub((GLubyte)255,(GLubyte)255,(GLubyte)255);//颜色设置为白色  glVertex3f(50.0f,50.0f,50.0f);  glColor3ub((GLubyte)255,(GLubyte)255,(GLubyte)0);//颜色设置为黄色  glVertex3f(50.0f,-50.0f,50.0f);  glColor3ub((GLubyte)255,(GLubyte)0,(GLubyte)0);//颜色设置为红色  glVertex3f(-50.0f,-50.0f,50.0f);  glColor3ub((GLubyte)255,(GLubyte)0,(GLubyte)255);//颜色设置为白色  glVertex3f(-50.0f,50.0f,50.0f);  glEnd();  glBegin(GL_POLYGON); //后表面  glColor3f(0.0f,1.0f,1.0f);//颜色设置为青色  glVertex3f(50.0f,50.0f,-50.0f);  glColor3f(0.0f,1.0f,0.0f);//颜色设置为绿色  glVertex3f(50.0f,-50.0f,-50.0f);  glColor3f(0.0f,0.0f,0.0f);//颜色设置为黑色  glVertex3f(-50.0f,-50.0f,-50.0f);  glColor3f(0.0f,0.0f,1.0f);//颜色设置为蓝色  glVertex3f(-50.0f,50.0f,-50.0f);  glEnd();  glBegin(GL_POLYGON); //上表面  glColor3d(0.0,1.0,1.0);//颜色设置为青色  glVertex3f(50.0f,50.0f,-50.0f);  glColor3d(1.0,1.0,1.0);//颜色设置为白色  glVertex3f(50.0f,50.0f,50.0f);  glColor3d(1.0,0.0,1.0);//颜色设置为品红色  glVertex3f(-50.0f,50.0f,50.0f);  glColor3d(0.0,0.0,1.0);//颜色设置为蓝色  glVertex3f(-50.0f,50.0f,-50.0f);  glEnd();  glBegin(GL_POLYGON); //下表面  glColor3ub(0u,255u,0u);//颜色设置为绿色  glVertex3f(50.0f,-50.0f,-50.0f);  glColor3ub(255u,255u,0u);//颜色设置为黄色  glVertex3f(50.0f,-50.0f,50.0f);  glColor3ub(255u,0u,0u);//颜色设置为红色  glVertex3f(-50.0f,-50.0f,50.0f);  glColor3ub(0u,0u,0u);//颜色设置为黑色  glVertex3f(-50.0f,-50.0f,-50.0f);  glEnd();  glBegin(GL_POLYGON); //左表面  glColor3ub((GLubyte)255,(GLubyte)255,(GLubyte)255);//颜色设置为白色  glVertex3f(50.0f,50.0f,50.0f);  glColor3ub((GLubyte)0,(GLubyte)255,(GLubyte)255);//颜色设置为青色  glVertex3f(50.0f,50.0f,-50.0f);  glColor3ub((GLubyte)0,(GLubyte)255,(GLubyte)0);//颜色设置为绿色  glVertex3f(50.0f,-50.0f,-50.0f);  glColor3ub((GLubyte)255,(GLubyte)255,(GLubyte)0);//颜色设置为黄色  glVertex3f(50.0f,-50.0f,50.0f);  glEnd();  glBegin(GL_POLYGON); //右表面  glColor3f(1.0f,0.0f,1.0f);//颜色设置为品红色  glVertex3f(-50.0f,50.0f,50.0f);  glColor3f(0.0f,0.0f,1.0f);//颜色设置为蓝色  glVertex3f(-50.0f,50.0f,-50.0f);  glColor3f(0.0f,0.0f,0.0f);//颜色设置为黑色  glVertex3f(-50.0f,-50.0f,-50.0f);  glColor3f(1.0f,0.0f,0.0f);//颜色设置为红色  glVertex3f(-50.0f,-50.0f,50.0f);  glEnd(); } glPopMatrix(); glutSwapBuffers();}void reshape(int w, int h){ GLfloat aspect = (GLfloat)w / (GLfloat)h; GLfloat nRange = 100.0f; glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); //将当前矩阵指定为投影模式 glLoadIdentity(); //设置三维投影区 if (w<=h) {  glOrtho(-nRange, nRange, -nRange * aspect, nRange * aspect, -nRange, nRange); }  else {  glOrtho(-nRange, nRange, -nRange / aspect, nRange / aspect, -nRange, nRange); }}void key_board(GLint key,GLint x,GLint y)if(key == GLUT_KEY_UP) {  AngleX -= 5.0f; } if(key == GLUT_KEY_DOWN) {  AngleX += 5.0f; } if(key == GLUT_KEY_LEFT) {  AngleY -= 5.0f; } if(key == GLUT_KEY_RIGHT) {  AngleY += 5.0f; } if(AngleX > 355.0f) {  AngleX = 0.0f; } if(AngleX < 0.0f) {  AngleX = 355.0f; } if(AngleY > 355.0f)  AngleY = 0.0fif(AngleY < 0.0f) {  AngleY = 355.0f; } glutPostRedisplay();}void init(){ AngleX = 45.0f; AngleY = 315.0f; glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_DITHER); glShadeModel(GL_SMOOTH);}void main(int argc,char* argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(480,480); glutCreateWindow("OpenGL颜色立方体"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutSpecialFunc(key_board); //设置处理特殊击键的回调函数 init(); glutMainLoop();}

效果图:

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值