/* CS425 * This program demonstrates 3D object drawn with * glVertex3f calls. This program DOES NOT use a vertex array. * A cube is drawn in a 3-D projection. */ #include #include #define STYLE GL_LINE_LOOP /* use this for solid sides #define STYLE GL_POLYGON */ void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); } void axes(int max) { /* set color to cyan */ glColor3f(0.0, 1.0, 1.0); glBegin(GL_LINES); /* draw X-axis */ glVertex3f(-1.0, 0.0, 0.0); glVertex3f(max, 0.0, 0.0); /* draw a letter X */ glVertex3f(max, 0.5, 0.0); glVertex3f(max+0.5, -0.5, 0.0); glVertex3f(max+0.5, 0.5, 0.0); glVertex3f(max, -0.5, 0.0); /* draw Y-axis */ glVertex3f(0.0, -1.0, 0.0); glVertex3f(0.0, max, 0.0); /* draw a letter Y */ glVertex3f(-0.25, max+0.5, 0.0); glVertex3f(0.0, max+0.25, 0.0); glVertex3f(0.0, max+0.25, 0.0); glVertex3f(0.25, max+0.5, 0.0); glVertex3f(0.0, max+0.25, 0.0); glVertex3f(0.0, max+0.05, 0.0); /* draw Z-axis */ glVertex3f(0.0, 0.0, -1.0); glVertex3f(0.0, 0.0, max); /* draw a letter Z */ glVertex3f(-0.25, 0.25, max+.05); glVertex3f(0.25, 0.25, max+0.05); glVertex3f(0.25, 0.25, max+0.05); glVertex3f(-0.25, -0.25, max+0.05); glVertex3f(-0.25, -0.25, max+0.05); glVertex3f(0.25, -0.25, max+0.05); } void cube(void) { /* set color to white */ glColor3f(1.0, 1.0, 1.0); /* draw bottom */ glBegin (STYLE); glVertex3f(5.0, 0.0, 2.0); glVertex3f(13.0, 0.0, 2.0); glVertex3f(13.0, 0.0, 10.0); glVertex3f(5.0, 0.0, 10.0); glEnd(); /* draw front */ glBegin (STYLE); glVertex3f(5.0, 0.0, 10.0); glVertex3f(13.0, 0.0, 10.0); glVertex3f(13.0, 8.0, 10.0); glVertex3f(5.0, 8.0, 10.0); glEnd(); /* draw left */ glBegin (STYLE); glVertex3f(5.0, 0.0, 2.0); glVertex3f(5.0, 0.0, 10.0); glVertex3f(5.0, 8.0, 10.0); glVertex3f(5.0, 8.0, 2.0); glEnd(); /* draw right */ glBegin (STYLE); glVertex3f(13.0, 0.0, 10.0); glVertex3f(13.0, 0.0, 2.0); glVertex3f(13.0, 8.0, 2.0); glVertex3f(13.0, 8.0, 10.0); glEnd(); /* draw back */ glBegin (STYLE); glVertex3f(13.0, 0.0, 2.0); glVertex3f(13.0, 8.0, 2.0); glVertex3f(5.0, 8.0, 2.0); glVertex3f(5.0, 0.0, 2.0); glEnd(); /* draw top */ glBegin (STYLE); glVertex3f(5.0, 8.0, 10.0); glVertex3f(13.0, 8.0, 10.0); glVertex3f(13.0, 8.0, 2.0); glVertex3f(5.0, 8.0, 2.0); glEnd(); } void display(void) { glMatrixMode (GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, 1.0, 1.0, 100.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity(); gluLookAt (15.0, 13.0, 40.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); axes(20); cube (); 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: /* escape key */ 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; }