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
|
float fTranslate; float fRotate; float fScale = 1.0f;
void (GLfloat x, GLfloat y , GLfloat z,GLfloat width_, GLfloat depth_, GLfloat height_) { GLfloat width = width_ * 0.5; GLfloat depth = depth_ * 大专栏 OpenGL的矩阵使用——绘制桌子pan class="number">0.5; GLfloat height = height_; GLfloat point[24][3] = { { x + width,y + depth,z },{ x + width,y - depth,z },{ x - width,y - depth,z },{ x - width,y + depth,z }, { x - width,y + depth,z },{ x - width,y - depth,z },{ x - width,y - depth,z - height },{ x - width,y + depth,z - height }, { x - width,y + depth,z - height },{ x - width,y - depth,z - height },{ x + width,y - depth,z - height },{ x + width,y + depth,z - height }, { x + width,y + depth,z - height },{ x + width,y - depth,z - height },{ x + width,y - depth,z },{ x + width,y + depth,z }, { x + width,y + depth,z },{ x - width,y + depth,z },{ x - width,y + depth,z - height },{ x + width,y + depth,z - height }, { x + width,y - depth,z },{ x - width,y - depth,z },{ x - width,y - depth,z - height },{ x + width,y - depth,z - height } };
glBegin(GL_QUADS); for (int i = 0; i < 24; i++) glVertex3f(point[i][0], point[i][1],point[i][2]); glEnd(); }
void Draw_Dest() { Draw_Cube(0, 0, 0, 1, 0.8, 0.2); Draw_Cube(0.3, 0.2, -0.2, 0.2, 0.2, 0.6); Draw_Cube(-0.3, 0.2, -0.2, 0.2, 0.2, 0.6); Draw_Cube(0.3, -0.2, -0.2, 0.2, 0.2, 0.6); Draw_Cube(-0.3, -0.2, -0.2, 0.2, 0.2, 0.6); }
void reshape(int width, int height) { if (height==0) { height=1; }
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION); glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }
void idle() { glutPostRedisplay(); }
void redraw() {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glPushMatrix(); glTranslatef(-2.0f, 0.0f,-6.0f); glTranslatef(0.0f, fTranslate, 0.0f); Draw_Dest(); glPopMatrix();
glPushMatrix(); glTranslatef(0.0f, 0.0f,-6.0f); glRotatef(fRotate, 0, 3.0f, 0); Draw_Dest(); glPopMatrix();
glPushMatrix(); glTranslatef(2.0f, 0.0f, -6.0f); glScalef(fScale, fScale, fScale); Draw_Dest(); glPopMatrix();
fScale -= 0.005f; fTranslate += 0.005f; fRotate += 0.3f;
if (fTranslate > 0.5f) fTranslate = 0.0f; if (fScale < 0.5f) fScale = 1.0f;
glutSwapBuffers(); }
void processNormalKeys(unsigned char key, int x, int y) {
if (key == 27) exit(0); }
int main (int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInitWindowSize(640,480); glutCreateWindow("Exercise2");
glutDisplayFunc(redraw); glutReshapeFunc(reshape); glutIdleFunc(idle);
glutKeyboardFunc(processNormalKeys);
glutMainLoop();
return 0; }
|