/* matrix4.c * This program draws three axes and three planes * with matrix push and pop operations. * This program animates a "fly-around" * of the camera. */ #include #include #include #define DELTA_RAD 0.01 #define TWO_PI 2*M_PI static double rad=0.0, x=0.0, y=0.0, z=0.0; static int hunter=0; void fly_around() { rad += DELTA_RAD; if(rad >= TWO_PI) rad=0.0; z = cos(rad); if(hunter==1) { x = sin(rad); y = 2.0; } else { y=sin(rad); x=2.0; } glutPostRedisplay(); } void axes (double r, double g, double b, GLubyte ax[]) { GLfloat axes_vertices[] = {0.0, 0.0, 0.0, 0.8, 0.0, 0.0, 0.0, 0.8, 0.0, 0.0, 0.0, 0.8, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.2, 0.0, 0.0, 0.0, 1.2, 0.0, 0.0, 0.0, 1.2}; glColor3f(r,g,b); glVertexPointer(3, GL_FLOAT, 0, axes_vertices); glDrawElements(GL_LINES, 6, GL_UNSIGNED_BYTE, ax); } void plane(double r, double g, double b) { GLfloat plane_vertices[] = {-0.5, -0.5, -0.01, 0.5, -0.5, -0.01, 0.5, 0.5, -0.01, -0.5, 0.5, -0.01}; GLubyte plane_indices[] = {0, 1, 2, 3}; glColor3f(r,g,b); glVertexPointer(3, GL_FLOAT, 0, plane_vertices); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, plane_indices); } void init(void) { glClearColor (0.6, 0.6, 0.6, 0.6); glShadeModel(GL_FLAT); glEnable(GL_DEPTH_TEST); glEnableClientState(GL_VERTEX_ARRAY); glMatrixMode (GL_PROJECTION); glLoadIdentity(); gluPerspective(35.0, 1.0, 1.0, 100.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity(); gluLookAt (0.0, 2.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); hunter=1; /* gluLookAt (5.0, 2.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); */ } void draw_scene() { GLubyte first_indices[] = {0,1,0,2,0,3}; GLubyte second_indices[] = {0,4,0,5,0,6}; GLubyte third_indices[] = {0,7,0,8,0,9}; glLoadIdentity(); if(hunter==1) gluLookAt (x*5.0, 2.0, z*5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); else gluLookAt (2.0, y*5.0, z*5.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0); /* gluLookAt (5.0, 2.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); */ glPushMatrix(); glTranslatef(0.0, 0.0, 0.6); axes(1.0, 1.0, 0.0, first_indices); plane(1.0,0.0,0.0); glPopMatrix(); glPushMatrix(); glRotatef(90.0, 0.0, 1.0, 0.0); glTranslatef(0.0, 0.0, 0.6); axes(1.0, 0.0, 1.0, second_indices); plane(0.0,1.0,0.0); glPopMatrix(); glPushMatrix(); glRotatef(180.0, 1.0, 0.0, 0.0); glTranslatef(0.0, 0.0, 0.6); axes(0.0, 1.0, 1.0, third_indices); plane(0.0,0.0,1.0); glPopMatrix(); glFlush(); } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw_scene(); glFlush(); glutSwapBuffers(); } void reshape (int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); } /* ARGSUSED1 */ void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; case '1': hunter=1; break; case '2': hunter=2; break; } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (340, 340); glutInitWindowPosition (0, 0); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutIdleFunc(fly_around); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }