OpenGL 顏色立方體
阿新 • • 發佈:2018-11-12
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!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.0f; if(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();}
效果圖: