1 /************************************************************************* 2 > File Name: frame.cpp 3 > Author: 4 > Mail: 5 > Created Time: 2015年11月21日 星期六 15时28分01秒 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<GL/glut.h> 10 using namespace std; 11 static GLfloat spin=0.0; 12 void init(void) 13 { 14 glClearColor(0.0,0.0,0.0,0.0); 15 glShadeModel(GL_SMOOTH); 16 } 17 18 void display(void) 19 { 20 glClear(GL_COLOR_BUFFER_BIT); 21 glPushMatrix(); 22 glColor3f(1.0,1.0,1.0); 23 24 glRotatef(spin,0.0f,0.0f,1.0f); 25 glBegin(GL_TRIANGLES); 26 glColor3f(1.0f,0.0f,0.0f); 27 glVertex3f(0.0f,0.0f,0.0f); 28 glColor3f(0.0f,1.0f,0.0f); 29 glVertex3f(30.0f,0.0f,0.0f); 30 glColor3f(0.0f,0.0f,1.0f); 31 glVertex3f(15.0f,50.0f,0.0f); 32 glEnd(); 33 34 glLoadIdentity(); 35 glRotatef(spin,1.0f,0.0f,0.0f); 36 glBegin(GL_QUADS); 37 glVertex3f(0.0f,0.0f,0.0f); 38 glVertex3f(-30.0f,0.0f,0.0f); 39 glVertex3f(-30.0f,30.0f,0.0f); 40 glVertex3f(0.0f,30.0f,0.0f); 41 glEnd(); 42 glPopMatrix(); 43 glutSwapBuffers(); 44 } 45 46 void spinDisplay(void) 47 { 48 spin=spin+2.0; 49 if(spin>360) 50 spin=spin-360.0; 51 glutPostRedisplay(); 52 } 53 54 void reshape(int w,int h) 55 { 56 glViewport(0,0,(GLsizei)w,(GLsizei)h); 57 glMatrixMode(GL_PROJECTION); 58 glLoadIdentity(); 59 glOrtho(-50.0,50.0,-50.0,50.0,-1.0,1.0); 60 glMatrixMode(GL_MODELVIEW); 61 glLoadIdentity(); 62 } 63 64 void mouse(int button,int state,int x,int y) 65 { 66 switch(button) 67 { 68 case GLUT_LEFT_BUTTON: 69 if(state == GLUT_DOWN) 70 glutIdleFunc(spinDisplay); 71 break; 72 case GLUT_MIDDLE_BUTTON: 73 if(state == GLUT_DOWN) 74 glutIdleFunc(NULL); 75 break; 76 default: 77 break; 78 } 79 } 80 81 int main(int argc,char ** argv) 82 { 83 glutInit(&argc,argv); 84 glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); 85 glutInitWindowSize(400,400); 86 glutInitWindowPosition(100,100); 87 glutCreateWindow("OpenGL test"); 88 init(); 89 90 glutDisplayFunc(display); 91 glutReshapeFunc(reshape); 92 glutMouseFunc(mouse); 93 94 glutMainLoop(); 95 return 0; 96 }
效果如图: