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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
#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,-10); glRotatef(rtri,0,1,0); glBegin(GL_TRIANGLES);//////////////////////////// glColor3f(1,0,0); glVertex3f(0,1,0); glColor3f(0,1,0); glVertex3f(-1,-1,1); glColor3f(0,0,1); glVertex3f(1,-1,1); glColor3f(1,0,0); glVertex3f(0,1,0); glColor3f(0,0,1); glVertex3f(1,-1,1); glColor3f(0,1,0); glVertex3f(1,-1,-1); glColor3f(1,0,0); glVertex3f(0,1,0); glColor3f(0,1,0); glVertex3f(1,-1,-1); glColor3f(0,0,1); glVertex3f(-1,-1,-1); glColor3f(1,0,0); glVertex3f(0,1,0); glColor3f(0,0,0); glVertex3f(-1,-1,-1); glColor3f(0,1,0); glVertex3f(-1,-1,1); glEnd();/////////////////////////////// glLoadIdentity();//重置当前模型观察矩阵 glTranslatef(1.5,0,-10); glRotatef(rquad,1,1,1); 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();/////////////////////////////// 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(600, 600); glutCreateWindow("第二个OpenGL程序"); glutReshapeFunc(ReSizeFunc); glutDisplayFunc(MainDisplay); glutKeyboardFunc(KeyBoardFunc); Init(); glutMainLoop(); return 0; } |