/* matrix4.c * * An example showing matrix maniuplation in OpenGL. * Ray S. Babcock, September 23, 1996, Montana State University * revised September 22, 1998 for Vertex Arrays * * OpenGL(TM) is a trademark of Silicon Graphics, Inc. * * 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.005 #define TWO_PI 2*M_PI static double rad=0.0, x=0.0, z=0.0; void fly_around() { rad += DELTA_RAD; if(rad >= TWO_PI) rad=0.0; z = cos(rad); x = sin(rad); glutPostRedisplay(); } void axes (double r, double g, double b, GLubyte ax[]) { GLfloat axes_vertices[] = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}; GLfloat max=1.0; glColor3f(r,g,b); glVertexPointer(3, GL_FLOAT, 0, axes_vertices); glDrawElements(GL_LINES, 6, GL_UNSIGNED_BYTE, ax); /* draw a letter X */ glBegin(GL_LINES); glVertex3f(max, 0.05, 0.0); glVertex3f(max+0.05, -0.05, 0.0); glVertex3f(max+0.05, 0.05, 0.0); glVertex3f(max, -0.05, 0.0); glEnd(); /* draw a letter Y */ glBegin(GL_LINES); glVertex3f(-0.025, max+0.05, 0.0); glVertex3f(0.0, max+0.025, 0.0); glVertex3f(0.0, max+0.025, 0.0); glVertex3f(0.025, max+0.05, 0.0); glVertex3f(0.0, max+0.025, 0.0); glVertex3f(0.0, max+0.005, 0.0); glEnd(); /* draw a letter Z */ glBegin(GL_LINES); glVertex3f(-0.025, 0.025, max+.005); glVertex3f(0.025, 0.025, max+0.005); glVertex3f(0.025, 0.025, max+0.005); glVertex3f(-0.025, -0.025, max+0.005); glVertex3f(-0.025, -0.025, max+0.005); glVertex3f(0.025, -0.025, max+0.005); glEnd(); } 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(40.0, 1.0, 1.0, 100.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity(); gluLookAt (0.0, 2.0, 5.0, 0.0, 0.0, 0.5, 0.0, 1.0, 0.0); } void draw_scene() { GLubyte axes_indices[] = {0,1,0,2,0,3}; glLoadIdentity(); gluLookAt (x*5.0, 2.0, z*5.0, 0.0, 0.5, 0.0, 0.0, 1.0, 0.0); axes(1.0, 1.0, 1.0, axes_indices); glPushMatrix(); glTranslatef(0.0, 1.0, 0.6); axes(1.0, 1.0, 0.0, axes_indices); plane(1.0,0.0,0.0); glPopMatrix(); glPushMatrix(); glRotatef(90.0, 0.0, 1.0, 0.0); glTranslatef(0.0, 1.0, 0.6); axes(1.0, 0.0, 1.0, axes_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, axes_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; } } 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; }