/* matrix2.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 * commented out! */ #include #include void axes (double r, double g, double b) { 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}; GLubyte axes_indices[] = {0,1,0,2,0,3}; glColor3f(r,g,b); glVertexPointer(3, GL_FLOAT, 0, axes_vertices); glDrawElements(GL_LINES, 6, GL_UNSIGNED_BYTE, axes_indices); } void plane(double r, double g, double b) { GLfloat plane_vertices[] = {-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.5, 0.5, 0.0, -0.5, 0.5, 0.0}; 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.8, 0.8, 0.8, 0.0); glShadeModel(GL_FLAT); glEnable(GL_DEPTH_TEST); glEnableClientState(GL_VERTEX_ARRAY); } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode (GL_PROJECTION); glLoadIdentity(); gluPerspective(15.0, 1.0, 1.0, 100.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity(); gluLookAt (4.0, 5.0, 5.0, 0.5, 0.5, 0.0, 0.0, 1.0, 0.0); /* glPushMatrix(); */ glTranslatef(0.0, 0.0, -0.5); axes(1.0, 1.0, 0.0); plane(1.0,0.0,0.0); /* glPopMatrix(); */ /* glPushMatrix();*/ glRotatef(90.0, 0.0, 1.0, 0.0); glTranslatef(0.0, 0.0, 0.5); axes(1.0, 0.0, 1.0); plane(0.0,1.0,0.0); /* glPopMatrix(); */ /* glPushMatrix();*/ glRotatef(90.0, 1.0, 0.0, 0.0); glTranslatef(0.0, 0.0, 0.5); axes(0.0, 1.0, 1.0); plane(0.0,0.0,1.0); glFlush(); } 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_SINGLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc (keyboard); glutMainLoop(); return 0; }