1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#include "stdafx.h" #include <iostream> #define GLUT_DISABLE_ATEXIT_HACK #include "glut.h" using namespace std; static float rtri=0; static float rquad=0.1; void MainDisplay(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//清楚屏幕及其缓存 glLoadIdentity();//重置当前模型观察矩阵 glTranslatef(-1.5,0,-6); glRotatef(rtri,0,1,0); glBegin(GL_TRIANGLES);//////////////////////////// glColor3f(1,0,0); glVertex3f(0,1,0); glColor3f(0,1,0); glVertex3f(-1,-1,0); glColor3f(0,0,1); glVertex3f(1,-1,0); glEnd();/////////////////////////////// glLoadIdentity();//重置当前模型观察矩阵 glTranslatef(1.5,0,-6); glRotatef(rquad,1,0,0); glColor3f(0.5,0.5,1); glBegin(GL_QUADS);///////////////////// glVertex3f(-1,1,0); glVertex3f(1,1,0); glVertex3f(1,-1,0); glVertex3f(-1,-1,0); glEnd();/////////////////////////////// glFlush(); } void ReSizeFunc(int width,int height) { glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45,width/height,0.1,100); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void KeyBoardFunc(unsigned char key, int x, int y) { if(0x1B == key) { exit(0); } rtri+=20; rquad-=5; cout<<rtri<<endl; MainDisplay(); } int Init() { glShadeModel(GL_SMOOTH); glClearColor(0,0,0,0); glClearDepth(1); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); return 1; } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100, 100); glutInitWindowSize(500, 500); glutCreateWindow("第一个OpenGL程序"); glutReshapeFunc(ReSizeFunc); glutDisplayFunc(MainDisplay); glutKeyboardFunc(KeyBoardFunc); Init(); glutMainLoop(); return 0; } |