/* CS425 * cubevarray.c * * This program demonstrates 3D object drawn without * glVertex3fv calls. This program DOES use a vertex array. * A cube is drawn in a 3-D projection. */ #include #include #define MY_STYLE GL_LINE_LOOP /* use this for solid sides #define MY_STYLE GL_POLYGON */ void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); glEnableClientState(GL_VERTEX_ARRAY); } void axes(int max) { /* Leave axes function without vertex arrays. */ /* 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) { /* Define vertices for the cube */ GLfloat cube_vertices[][3] = { /* 0 */ { 5.0, 0.0, 10.0}, /* 1 */ { 13.0, 0.0, 10.0}, /* 2 */ { 13.0, 8.0, 10.0}, /* 3 */ { 5.0, 8.0, 10.0}, /* 4 */ { 5.0, 8.0, 2.0}, /* 5 */ { 13.0, 8.0, 2.0}, /* 6 */ { 13.0, 0.0, 2.0}, /* 7 */ { 5.0, 0.0, 2.0}, }; /* Define faces of the cube using indices */ GLubyte bottom[] = {7, 6, 1, 0}; GLubyte front[] = {0, 1, 2, 3}; GLubyte left[] = {7, 0, 3, 4}; GLubyte right[] = {1, 6, 5, 2}; GLubyte back[] = {6, 7, 4, 5}; GLubyte top[] = {3, 2, 5, 4}; /* initialize pointer to vertex array */ glVertexPointer(3, GL_FLOAT, 0, cube_vertices); /* set color to white */ glColor3f(1.0, 1.0, 1.0); /* draw bottom */ glDrawElements(MY_STYLE, 4, GL_UNSIGNED_BYTE, bottom); /* draw front */ glDrawElements(MY_STYLE, 4, GL_UNSIGNED_BYTE, front); /* draw left */ glDrawElements(MY_STYLE, 4, GL_UNSIGNED_BYTE, left); /* draw right */ glDrawElements(MY_STYLE, 4, GL_UNSIGNED_BYTE, right); /* draw back */ glDrawElements(MY_STYLE, 4, GL_UNSIGNED_BYTE, back); /* draw top */ glDrawElements(MY_STYLE, 4, GL_UNSIGNED_BYTE, top); } 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; }